📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
chore: update files
SWHLa86fb7381190
diff --git a/python/rapidocr/cal_rec_boxes/main.py b/python/rapidocr/cal_rec_boxes/main.pyindex 43f53c2..8d6b9e3 100644--- a/python/rapidocr/cal_rec_boxes/main.py+++ b/python/rapidocr/cal_rec_boxes/main.py@@ -12,7 +12,8 @@ from ..ch_ppocr_rec.utils import TextRecOutputclass CalRecBoxes:- """计算识别文字的汉字单字和英文单词的坐标框。代码借鉴自PaddlePaddle/PaddleOCR和fanqie03/char-detection"""+ """计算识别文字的汉字单字和英文单词的坐标框。+ 代码借鉴自PaddlePaddle/PaddleOCR和fanqie03/char-detection"""def __init__(self):pass@@ -43,7 +44,7 @@ class CalRecBoxes:list(zip(word_box_content_list, conf_list, word_box_list)))- rec_res.word_results = tuple(list(v) for v in word_results)+ rec_res.word_results = tuple(word_results)return rec_res@staticmethoddiff --git a/python/rapidocr/ch_ppocr_det/__init__.py b/python/rapidocr/ch_ppocr_det/__init__.pyindex 502f4a3..7fa4067 100644--- a/python/rapidocr/ch_ppocr_det/__init__.py+++ b/python/rapidocr/ch_ppocr_det/__init__.py@@ -1,4 +1,5 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]-from .text_detect import TextDetector+from .main import TextDetector+from .utils import TextDetOutputdiff --git a/python/rapidocr/ch_ppocr_det/text_detect.py b/python/rapidocr/ch_ppocr_det/main.pysimilarity index 51%rename from python/rapidocr/ch_ppocr_det/text_detect.pyrename to python/rapidocr/ch_ppocr_det/main.pyindex c0c578e..a6cce33 100644--- a/python/rapidocr/ch_ppocr_det/text_detect.py+++ b/python/rapidocr/ch_ppocr_det/main.py@@ -15,13 +15,13 @@# @Author: SWHL# @Contact: [email protected]import time-from typing import Any, Dict, Optional, Tuple+from typing import Any, Dict, Listimport numpy as npfrom rapidocr.utils import OrtInferSession-from .utils import DBPostProcess, DetPreProcess+from .utils import DBPostProcess, DetPreProcess, TextDetOutputclass TextDetector:@@ -44,7 +44,7 @@ class TextDetector:self.infer = OrtInferSession(config)- def __call__(self, img: np.ndarray) -> Tuple[Optional[np.ndarray], float]:+ def __call__(self, img: np.ndarray) -> TextDetOutput:start_time = time.perf_counter()if img is None:@@ -54,15 +54,18 @@ class TextDetector:self.preprocess_op = self.get_preprocess(max(img.shape[0], img.shape[1]))prepro_img = self.preprocess_op(img)if prepro_img is None:- return None, 0+ return TextDetOutput()preds = self.infer(prepro_img)[0]- dt_boxes, dt_boxes_scores = self.postprocess_op(preds, ori_img_shape)- dt_boxes = self.filter_tag_det_res(dt_boxes, ori_img_shape)+ boxes, scores = self.postprocess_op(preds, ori_img_shape)+ if len(boxes) < 1:+ return TextDetOutput()++ boxes = self.sorted_boxes(boxes)elapse = time.perf_counter() - start_time- return dt_boxes, elapse+ return TextDetOutput(boxes, scores, elapse=elapse)- def get_preprocess(self, max_wh):+ def get_preprocess(self, max_wh: int) -> DetPreProcess:if self.limit_type == "min":limit_side_len = self.limit_side_lenelif max_wh < 960:@@ -73,52 +76,28 @@ class TextDetector:limit_side_len = 2000return DetPreProcess(limit_side_len, self.limit_type, self.mean, self.std)- def filter_tag_det_res(- self, dt_boxes: np.ndarray, image_shape: Tuple[int, int]- ) -> np.ndarray:- img_height, img_width = image_shape- dt_boxes_new = []- for box in dt_boxes:- box = self.order_points_clockwise(box)- box = self.clip_det_res(box, img_height, img_width)-- rect_width = int(np.linalg.norm(box[0] - box[1]))- rect_height = int(np.linalg.norm(box[0] - box[3]))- if rect_width <= 3 or rect_height <= 3:- continue-- dt_boxes_new.append(box)- return np.array(dt_boxes_new)-- def order_points_clockwise(self, pts: np.ndarray) -> np.ndarray:+ @staticmethod+ def sorted_boxes(dt_boxes: np.ndarray) -> List[np.ndarray]:"""- reference from:- https://github.com/jrosebr1/imutils/blob/master/imutils/perspective.py- sort the points based on their x-coordinates+ Sort text boxes in order from top to bottom, left to right+ args:+ dt_boxes(array):detected text boxes with shape [4, 2]+ return:+ sorted boxes(array) with shape [4, 2]"""- xSorted = pts[np.argsort(pts[:, 0]), :]-- # grab the left-most and right-most points from the sorted- # x-roodinate points- leftMost = xSorted[:2, :]- rightMost = xSorted[2:, :]-- # now, sort the left-most coordinates according to their- # y-coordinates so we can grab the top-left and bottom-left- # points, respectively- leftMost = leftMost[np.argsort(leftMost[:, 1]), :]- (tl, bl) = leftMost-- rightMost = rightMost[np.argsort(rightMost[:, 1]), :]- (tr, br) = rightMost-- rect = np.array([tl, tr, br, bl], dtype="float32")- return rect-- def clip_det_res(- self, points: np.ndarray, img_height: int, img_width: int- ) -> np.ndarray:- for pno in range(points.shape[0]):- points[pno, 0] = int(min(max(points[pno, 0], 0), img_width - 1))- points[pno, 1] = int(min(max(points[pno, 1], 0), img_height - 1))- return points+ num_boxes = dt_boxes.shape[0]+ sorted_boxes = sorted(dt_boxes, key=lambda x: (x[0][1], x[0][0]))+ _boxes = list(sorted_boxes)++ for i in range(num_boxes - 1):+ for j in range(i, -1, -1):+ if (+ abs(_boxes[j + 1][0][1] - _boxes[j][0][1]) < 10+ and _boxes[j + 1][0][0] < _boxes[j][0][0]+ ):+ tmp = _boxes[j]+ _boxes[j] = _boxes[j + 1]+ _boxes[j + 1] = tmp+ else:+ break+ return _boxesdiff --git a/python/rapidocr/ch_ppocr_det/utils.py b/python/rapidocr/ch_ppocr_det/utils.pyindex ef41063..377e9f2 100644--- a/python/rapidocr/ch_ppocr_det/utils.py+++ b/python/rapidocr/ch_ppocr_det/utils.py@@ -1,6 +1,7 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]+from dataclasses import dataclassfrom typing import List, Optional, Tupleimport cv2@@ -9,6 +10,13 @@ import pyclipperfrom shapely.geometry import Polygon+@dataclass+class TextDetOutput:+ boxes: Optional[np.ndarray] = None+ scores: Optional[Tuple[float]] = None+ elapse: float = 0.0++class DetPreProcess:def __init__(self, limit_side_len: int = 736, limit_type: str = "min", mean=None, std=None@@ -119,6 +127,7 @@ class DBPostProcess:np.array(segmentation[0]).astype(np.uint8), self.dilation_kernel)boxes, scores = self.boxes_from_bitmap(pred[0], mask, src_w, src_h)+ boxes, scores = self.filter_det_res(boxes, scores, src_h, src_w)return boxes, scoresdef boxes_from_bitmap(@@ -235,3 +244,53 @@ class DBPostProcess:offset.AddPath(box, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)expanded = np.array(offset.Execute(distance)).reshape((-1, 1, 2))return expanded++ def filter_det_res(+ self, dt_boxes: np.ndarray, scores: List[float], img_height: int, img_width: int+ ) -> Tuple[np.ndarray, List[float]]:+ dt_boxes_new, new_scores = [], []+ for box, score in zip(dt_boxes, scores):+ box = self.order_points_clockwise(box)+ box = self.clip_det_res(box, img_height, img_width)++ rect_width = int(np.linalg.norm(box[0] - box[1]))+ rect_height = int(np.linalg.norm(box[0] - box[3]))+ if rect_width <= 3 or rect_height <= 3:+ continue++ dt_boxes_new.append(box)+ new_scores.append(score)+ return np.array(dt_boxes_new), new_scores++ def order_points_clockwise(self, pts: np.ndarray) -> np.ndarray:+ """+ reference from:+ https://github.com/jrosebr1/imutils/blob/master/imutils/perspective.py+ sort the points based on their x-coordinates+ """+ xSorted = pts[np.argsort(pts[:, 0]), :]++ # grab the left-most and right-most points from the sorted+ # x-roodinate points+ leftMost = xSorted[:2, :]+ rightMost = xSorted[2:, :]++ # now, sort the left-most coordinates according to their+ # y-coordinates so we can grab the top-left and bottom-left+ # points, respectively+ leftMost = leftMost[np.argsort(leftMost[:, 1]), :]+ (tl, bl) = leftMost++ rightMost = rightMost[np.argsort(rightMost[:, 1]), :]+ (tr, br) = rightMost++ rect = np.array([tl, tr, br, bl], dtype="float32")+ return rect++ def clip_det_res(+ self, points: np.ndarray, img_height: int, img_width: int+ ) -> np.ndarray:+ for pno in range(points.shape[0]):+ points[pno, 0] = int(min(max(points[pno, 0], 0), img_width - 1))+ points[pno, 1] = int(min(max(points[pno, 1], 0), img_height - 1))+ return pointsdiff --git a/python/rapidocr/ch_ppocr_rec/main.py b/python/rapidocr/ch_ppocr_rec/main.pyindex 25d1bee..5926bc5 100644--- a/python/rapidocr/ch_ppocr_rec/main.py+++ b/python/rapidocr/ch_ppocr_rec/main.py@@ -11,7 +11,6 @@# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.-import argparseimport mathimport timefrom typing import Any, Dict@@ -19,7 +18,7 @@ from typing import Any, Dictimport cv2import numpy as np-from rapidocr.utils import OrtInferSession, read_yaml+from rapidocr.utils import OrtInferSessionfrom .utils import CTCLabelDecode, TextRecArguments, TextRecOutput@@ -73,7 +72,7 @@ class TextRecognizer:norm_img_batch.append(norm_img[np.newaxis, :])norm_img_batch = np.concatenate(norm_img_batch).astype(np.float32)- starttime = time.time()+ start_time = time.perf_counter()preds = self.session(norm_img_batch)[0]line_results, word_results = self.postprocess_op(preds,@@ -88,7 +87,7 @@ class TextRecognizer:continuerec_res[indices[beg_img_no + rno]] = (one_res, None)- elapse += time.time() - starttime+ elapse += time.perf_counter() - start_timeall_line_results, all_word_results = list(zip(*rec_res))return TextRecOutput(all_line_results, all_word_results, elapse)@@ -115,17 +114,3 @@ class TextRecognizer:padding_im = np.zeros((img_channel, img_height, img_width), dtype=np.float32)padding_im[:, :, 0:resized_w] = resized_imagereturn padding_im---if __name__ == "__main__":- parser = argparse.ArgumentParser()- parser.add_argument("--image_path", type=str, help="image_dir|image_path")- parser.add_argument("--config_path", type=str, default="config.yaml")- args = parser.parse_args()-- config = read_yaml(args.config_path)- text_recognizer = TextRecognizer(config)-- img = cv2.imread(args.image_path)- rec_res, predict_time = text_recognizer(img)- print(f"rec result: {rec_res}\t cost: {predict_time}s")diff --git a/python/rapidocr/ch_ppocr_rec/utils.py b/python/rapidocr/ch_ppocr_rec/utils.pyindex 7644201..6130f59 100644--- a/python/rapidocr/ch_ppocr_rec/utils.py+++ b/python/rapidocr/ch_ppocr_rec/utils.py@@ -29,8 +29,10 @@ class TextRecArguments:@dataclassclass TextRecOutput:- line_results: Tuple[List[Any]] = ([""],)- word_results: Tuple[List[Any]] = ([""],)+ line_results: Tuple[Tuple[str, float]] = (("", 1.0),)+ word_results: Tuple[Tuple[str, float, Optional[List[List[int]]]]] = (+ ("", 1.0, None),+ )elapse: Optional[float] = Nonediff --git a/python/rapidocr/main.py b/python/rapidocr/main.pyindex 0172735..efe9c74 100644--- a/python/rapidocr/main.py+++ b/python/rapidocr/main.py@@ -14,6 +14,7 @@ from .ch_ppocr_det import TextDetectorfrom .ch_ppocr_rec import TextRecArguments, TextRecognizer, TextRecOutputfrom .utils import (LoadImage,+ RapidOCROutput,UpdateParameters,VisRes,add_round_letterbox,@@ -93,14 +94,14 @@ class RapidOCR:img, ratio_h, ratio_w = self.preprocess(img)op_record["preprocess"] = {"ratio_h": ratio_h, "ratio_w": ratio_w}- dt_boxes, cls_res, rec_res = None, None, None- det_elapse, cls_elapse, rec_elapse = 0.0, 0.0, 0.0+ dt_boxes, cls_res, rec_res = None, None, TextRecOutput()+ det_elapse, cls_elapse = 0.0, 0.0if use_det:img, op_record = self.maybe_add_letterbox(img, op_record)- dt_boxes, det_elapse = self.auto_text_det(img)- if dt_boxes is None:- return None, None+ det_res = self.text_det(img)+ if det_res.boxes is None:+ return RapidOCROutput()img = self.get_crop_img_list(img, dt_boxes)@@ -117,13 +118,18 @@ class RapidOCR:and all(v for v in rec_res.word_results)):rec_res = self.cal_rec_boxes(img, dt_boxes, rec_res)- for rec_res_i in rec_res.word_results:- if rec_res_i[2]:- rec_res_i[2] = (- self._get_origin_points([rec_res_i[2]], op_record, raw_h, raw_w)- .astype(np.int32)- .tolist()- )+ origin_words = []+ for one_word in rec_res.word_results:+ one_word_points = one_word[2]+ if one_word_points is None:+ continue++ origin_words_points = self._get_origin_points(+ [one_word_points], op_record, raw_h, raw_w+ )+ origin_words_points = origin_words_points.astype(np.int32).tolist()[0]+ origin_words.append((one_word[0], one_word[1], origin_words_points))+ rec_res.word_results = tuple(origin_words)if dt_boxes is not None:dt_boxes = self._get_origin_points(dt_boxes, op_record, raw_h, raw_w)diff --git a/python/rapidocr/utils/__init__.py b/python/rapidocr/utils/__init__.pyindex 150da63..0dde27c 100644--- a/python/rapidocr/utils/__init__.py+++ b/python/rapidocr/utils/__init__.py@@ -11,6 +11,7 @@ from .load_image import LoadImage, LoadImageErrorfrom .logger import get_loggerfrom .parse_parameters import UpdateParameters, init_args, update_model_pathfrom .process_img import add_round_letterbox, increase_min_side, reduce_max_side+from .typings import RapidOCROutputfrom .vis_res import VisResdiff --git a/python/rapidocr/utils/typings.py b/python/rapidocr/utils/typings.pynew file mode 100644index 0000000..b3e2907--- /dev/null+++ b/python/rapidocr/utils/typings.py@@ -0,0 +1,9 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+from dataclasses import dataclass+++@dataclass+class RapidOCROutput:+ pass