forked from mindee/doctr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c73cdf
commit 61d0f1c
Showing
7 changed files
with
163 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (C) 2021-2022, Mindee. | ||
|
||
# This program is licensed under the Apache License 2.0. | ||
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. | ||
|
||
import numpy as np | ||
import torch | ||
|
||
from doctr.models import ocr_predictor | ||
from doctr.models.predictor import OCRPredictor | ||
|
||
DET_ARCHS = ["db_resnet50", "db_mobilenet_v3_large", "linknet_resnet50_rotation"] | ||
RECO_ARCHS = ["crnn_vgg16_bn", "crnn_mobilenet_v3_small", "master", "sar_resnet31"] | ||
|
||
|
||
def load_predictor(det_arch: str, reco_arch: str, device) -> OCRPredictor: | ||
""" | ||
Args: | ||
device is torch.device | ||
""" | ||
predictor = ocr_predictor( | ||
det_arch, reco_arch, pretrained=True, assume_straight_pages=("rotation" not in det_arch) | ||
).to(device) | ||
return predictor | ||
|
||
|
||
def forward_image(predictor: OCRPredictor, image: np.ndarray, device) -> np.ndarray: | ||
""" | ||
Args: | ||
device is torch.device | ||
""" | ||
with torch.no_grad(): | ||
processed_batches = predictor.det_predictor.pre_processor([image]) | ||
out = predictor.det_predictor.model(processed_batches[0].to(device), return_model_output=True) | ||
seg_map = out["out_map"].to("cpu").numpy() | ||
|
||
return seg_map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright (C) 2021-2022, Mindee. | ||
|
||
# This program is licensed under the Apache License 2.0. | ||
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. | ||
|
||
import numpy as np | ||
import tensorflow as tf | ||
|
||
from doctr.models import ocr_predictor | ||
from doctr.models.predictor import OCRPredictor | ||
|
||
DET_ARCHS = ["db_resnet50", "db_mobilenet_v3_large", "linknet_resnet18_rotation"] | ||
RECO_ARCHS = ["crnn_vgg16_bn", "crnn_mobilenet_v3_small", "master", "sar_resnet31"] | ||
|
||
|
||
def load_predictor(det_arch: str, reco_arch: str, device) -> OCRPredictor: | ||
""" | ||
Args: | ||
device is tf.device | ||
""" | ||
with device: | ||
predictor = ocr_predictor( | ||
det_arch, reco_arch, pretrained=True, assume_straight_pages=("rotation" not in det_arch) | ||
) | ||
return predictor | ||
|
||
|
||
def forward_image(predictor: OCRPredictor, image: np.ndarray, device) -> np.ndarray: | ||
""" | ||
Args: | ||
device is tf.device | ||
""" | ||
with device: | ||
processed_batches = predictor.det_predictor.pre_processor([image]) | ||
out = predictor.det_predictor.model(processed_batches[0], return_model_output=True) | ||
seg_map = out["out_map"] | ||
|
||
with tf.device("/cpu:0"): | ||
seg_map = tf.identity(seg_map).numpy() | ||
|
||
return seg_map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-e git+https://github.com/mindee/doctr.git#egg=python-doctr[torch] | ||
streamlit>=1.0.0 |
File renamed without changes.