|
@@ -1,15 +1,26 @@
|
|
-from time import localtime, strftime
|
|
|
|
-from random import randint, seed
|
|
|
|
import cv2
|
|
import cv2
|
|
-from paddleocr.tools.infer.utility import draw_box_txt_fine
|
|
|
|
import numpy as np
|
|
import numpy as np
|
|
from flask import jsonify
|
|
from flask import jsonify
|
|
|
|
+from paddleocr import PaddleOCR
|
|
|
|
+from random import randint, seed
|
|
|
|
+from time import localtime, strftime
|
|
|
|
+from paddleocr.tools.infer.utility import draw_box_txt_fine
|
|
|
|
|
|
-__all__ = ["Args", "Response", "rand_str", "current_time", "get_ext_name", "is_image_ext", "draw_img"]
|
|
|
|
|
|
+__all__ = [
|
|
|
|
+ "Args", "Response", "rand_str", "current_time", "get_ext_name", "is_image_ext", "recognize", "draw_img"
|
|
|
|
+]
|
|
|
|
|
|
-StrBase = "qwertyuioplkjhgfdsazxcvbnm1234567890ZXCVBNMLKJHGFDSAQWERTYUIOP"
|
|
|
|
-StrBaseLen = len(StrBase) - 1
|
|
|
|
-AcceptExtNames = ["jpg", "jpeg", "bmp", "png", "rgb", "tif", "tiff", "gif", "pdf"]
|
|
|
|
|
|
+__StrBase = "qwertyuioplkjhgfdsazxcvbnm1234567890ZXCVBNMLKJHGFDSAQWERTYUIOP"
|
|
|
|
+__StrBaseLen = len(__StrBase) - 1
|
|
|
|
+__AcceptExtNames = ["jpg", "jpeg", "bmp", "png", "rgb", "tif", "tiff", "gif", "pdf"]
|
|
|
|
+__OcrEngine = PaddleOCR(
|
|
|
|
+ use_gpu=True,
|
|
|
|
+ det_model_dir="models/det/",
|
|
|
|
+ rec_model_dir="models/rec/",
|
|
|
|
+ cls_model_dir="models/cls/",
|
|
|
|
+ use_angle_cls=True,
|
|
|
|
+ use_space_char=True
|
|
|
|
+)
|
|
|
|
|
|
|
|
|
|
class Args:
|
|
class Args:
|
|
@@ -25,7 +36,7 @@ class Args:
|
|
det_pse_scale=1, scales=[8, 16, 32], alpha=1.0, beta=1.0, fourier_degree=5,
|
|
det_pse_scale=1, scales=[8, 16, 32], alpha=1.0, beta=1.0, fourier_degree=5,
|
|
rec_algorithm="SVTR_LCNet", rec_model_dir="models/rec/", rec_image_inverse=True,
|
|
rec_algorithm="SVTR_LCNet", rec_model_dir="models/rec/", rec_image_inverse=True,
|
|
rec_image_shape="3, 48, 320", rec_batch_num=6, max_text_length=25,
|
|
rec_image_shape="3, 48, 320", rec_batch_num=6, max_text_length=25,
|
|
- rec_char_dict_path="E:/Project/Python/PaddleOCR/venv/lib/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt",
|
|
|
|
|
|
+ rec_char_dict_path="venv/lib/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt",
|
|
use_space_char=True, vis_font_path="static/simfang.ttf", drop_score=0.5,
|
|
use_space_char=True, vis_font_path="static/simfang.ttf", drop_score=0.5,
|
|
e2e_algorithm="PGNet", e2e_model_dir=None, e2e_limit_side_len=768, e2e_limit_type="max",
|
|
e2e_algorithm="PGNet", e2e_model_dir=None, e2e_limit_side_len=768, e2e_limit_type="max",
|
|
e2e_pgnet_score_thresh=0.5, e2e_char_dict_path="./ppocr/utils/ic15_dict.txt",
|
|
e2e_pgnet_score_thresh=0.5, e2e_char_dict_path="./ppocr/utils/ic15_dict.txt",
|
|
@@ -60,7 +71,7 @@ class Args:
|
|
|
|
|
|
|
|
|
|
def rand_str(size: "int" = 8) -> "str":
|
|
def rand_str(size: "int" = 8) -> "str":
|
|
- return "".join([StrBase[randint(0, StrBaseLen)] for _ in range(size)])
|
|
|
|
|
|
+ return "".join([__StrBase[randint(0, __StrBaseLen)] for _ in range(size)])
|
|
|
|
|
|
|
|
|
|
def current_time() -> "str":
|
|
def current_time() -> "str":
|
|
@@ -72,7 +83,7 @@ def get_ext_name(name: "str") -> "str":
|
|
|
|
|
|
|
|
|
|
def is_image_ext(ext: "str") -> bool:
|
|
def is_image_ext(ext: "str") -> bool:
|
|
- return ext in AcceptExtNames
|
|
|
|
|
|
+ return ext in __AcceptExtNames
|
|
|
|
|
|
|
|
|
|
def Response(message: "str" = None, data=None):
|
|
def Response(message: "str" = None, data=None):
|
|
@@ -81,7 +92,13 @@ def Response(message: "str" = None, data=None):
|
|
return jsonify(success=False, message=message, data=data)
|
|
return jsonify(success=False, message=message, data=data)
|
|
|
|
|
|
|
|
|
|
-def draw_img(shape: "tuple", data: "list[dict]", drop: "float" = 0.5):
|
|
|
|
|
|
+def recognize(content: "str") -> "tuple[list, tuple]":
|
|
|
|
+ img = cv2.imdecode(np.fromstring(content, np.uint8), 1) # noqa
|
|
|
|
+
|
|
|
|
+ return __OcrEngine.ocr(img)[0], img.shape
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def draw_img(shape: "tuple", data: "list[dict]", path: "str", drop: "float" = 0.5):
|
|
img = np.ones(shape, dtype=np.uint8) * 255
|
|
img = np.ones(shape, dtype=np.uint8) * 255
|
|
seed(0)
|
|
seed(0)
|
|
|
|
|
|
@@ -94,4 +111,4 @@ def draw_img(shape: "tuple", data: "list[dict]", drop: "float" = 0.5):
|
|
cv2.polylines(text, [pts], True, color, 1) # noqa
|
|
cv2.polylines(text, [pts], True, color, 1) # noqa
|
|
img = cv2.bitwise_and(img, text) # noqa
|
|
img = cv2.bitwise_and(img, text) # noqa
|
|
|
|
|
|
- return np.array(img)
|
|
|
|
|
|
+ cv2.imwrite(path, np.array(img)) # noqa
|