π Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
Optim code
SWHL752864dc46bb
diff --git a/python/config.yaml b/python/config.yamlindex 992f3ed..99a3ad8 100644--- a/python/config.yaml+++ b/python/config.yaml@@ -37,7 +37,7 @@ Det:max_candidates: 1000unclip_ratio: 1.6use_dilation: true- score_mode: "fast"+ score_mode: fastCls:module_name: ch_ppocr_v2_clsdiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.pyindex 18f3a56..6a662df 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.py@@ -405,7 +405,7 @@ class DBPostProcess(object):box[:, 1] = box[:, 1] - ymincv2.fillPoly(mask, box.reshape(1, -1, 2).astype(np.int32), 1)return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]-+def box_score_slow(self, bitmap, contour):'''box_score_slow: use polyon mean score as the mean scorediff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.pyindex a0dbf8d..7f60530 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.py@@ -112,5 +112,5 @@ class CTCLabelDecode(object):else:conf_list.append(1)text = ''.join(char_list)- result_list.append((text, np.mean(conf_list)))+ result_list.append((text, np.mean(conf_list + [1e-10] )))return result_listdiff --git a/python/rapidocr_openvino/ch_ppocr_v2_det/utils.py b/python/rapidocr_openvino/ch_ppocr_v2_det/utils.pyindex 57fc11d..577c5e8 100644--- a/python/rapidocr_openvino/ch_ppocr_v2_det/utils.py+++ b/python/rapidocr_openvino/ch_ppocr_v2_det/utils.py@@ -274,12 +274,14 @@ class DBPostProcess(object):box_thresh=0.7,max_candidates=1000,unclip_ratio=2.0,+ score_mode="fast",use_dilation=False):self.thresh = threshself.box_thresh = box_threshself.max_candidates = max_candidatesself.unclip_ratio = unclip_ratioself.min_size = 3+ self.score_mode = score_modeif use_dilation:self.dilation_kernel = np.array([[1, 1], [1, 1]])@@ -312,7 +314,10 @@ class DBPostProcess(object):if sside < self.min_size:continuepoints = np.array(points)- score = self.box_score_fast(pred, points.reshape(-1, 2))+ if self.score_mode == "fast":+ score = self.box_score_fast(pred, points.reshape(-1, 2))+ else:+ score = self.box_score_slow(pred, contour)if self.box_thresh > score:continue@@ -376,6 +381,27 @@ class DBPostProcess(object):cv2.fillPoly(mask, box.reshape(1, -1, 2).astype(np.int32), 1)return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]+ def box_score_slow(self, bitmap, contour):+ '''+ box_score_slow: use polyon mean score as the mean score+ '''+ h, w = bitmap.shape[:2]+ contour = contour.copy()+ contour = np.reshape(contour, (-1, 2))++ xmin = np.clip(np.min(contour[:, 0]), 0, w - 1)+ xmax = np.clip(np.max(contour[:, 0]), 0, w - 1)+ ymin = np.clip(np.min(contour[:, 1]), 0, h - 1)+ ymax = np.clip(np.max(contour[:, 1]), 0, h - 1)++ mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8)++ contour[:, 0] = contour[:, 0] - xmin+ contour[:, 1] = contour[:, 1] - ymin++ cv2.fillPoly(mask, contour.reshape(1, -1, 2).astype(np.int32), 1)+ return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]+def __call__(self, pred, shape_list):pred = pred[:, 0, :, :]segmentation = pred > self.threshdiff --git a/python/rapidocr_openvino/ch_ppocr_v2_rec/utils.py b/python/rapidocr_openvino/ch_ppocr_v2_rec/utils.pyindex eeda4e1..e9f7719 100644--- a/python/rapidocr_openvino/ch_ppocr_v2_rec/utils.py+++ b/python/rapidocr_openvino/ch_ppocr_v2_rec/utils.py@@ -83,6 +83,5 @@ class CTCLabelDecode(object):else:conf_list.append(1)text = ''.join(char_list)- result_list.append((text, np.mean(conf_list)))+ result_list.append((text, np.mean(conf_list + [1e-10] )))return result_list-diff --git a/python/requirements.txt b/python/requirements.txtindex ac08210..134aa08 100644--- a/python/requirements.txt+++ b/python/requirements.txt@@ -2,7 +2,7 @@ pyclipper>=1.2.0Shapely>=1.7.1opencv_python==4.5.1.48six>=1.15.0-numpy>=1.19.2+numpy>=1.19.5Pillow==8.3.0PyYAMLpytest\ No newline at end of file