|
@@ -1,6 +1,8 @@
|
|
from dataclasses import dataclass
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
+import cv2
|
|
import numpy as np
|
|
import numpy as np
|
|
|
|
+import math
|
|
from paddleocr import PaddleOCR, draw_ocr
|
|
from paddleocr import PaddleOCR, draw_ocr
|
|
|
|
|
|
from core.direction import *
|
|
from core.direction import *
|
|
@@ -20,7 +22,6 @@ class BankOcr:
|
|
print(f'---------- detect angle: {angle} 角度 --------')
|
|
print(f'---------- detect angle: {angle} 角度 --------')
|
|
# 这里使用自己训练的检测识别模型,在此之前,理想情况下,所有的银行卡的角度都已经是0,(正向)
|
|
# 这里使用自己训练的检测识别模型,在此之前,理想情况下,所有的银行卡的角度都已经是0,(正向)
|
|
_, _, result = self._ocr(image)
|
|
_, _, result = self._ocr(image)
|
|
-
|
|
|
|
# self.imshow(image, result) # 将检测图片保存
|
|
# self.imshow(image, result) # 将检测图片保存
|
|
return self._post_process(result, angle)
|
|
return self._post_process(result, angle)
|
|
|
|
|
|
@@ -38,13 +39,28 @@ class BankOcr:
|
|
|
|
|
|
if angle == 1:
|
|
if angle == 1:
|
|
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
|
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
|
- # print("检测出来的角度:", angle) # 逆时针
|
|
|
|
if angle == 2:
|
|
if angle == 2:
|
|
image = cv2.rotate(image, cv2.ROTATE_180)
|
|
image = cv2.rotate(image, cv2.ROTATE_180)
|
|
if angle == 3:
|
|
if angle == 3:
|
|
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
|
|
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
|
|
|
|
|
|
- return image, angle, result
|
|
|
|
|
|
+ # if -60 <= rotate_angle <= -20 or 20 <= rotate_angle <= 60:
|
|
|
|
+ # print("需要旋转角度")
|
|
|
|
+ # image = imutils.rotate(image, rotate_angle)
|
|
|
|
+
|
|
|
|
+ # 因为有些img像素过大,导致检测框效果不好,识别就会出问题
|
|
|
|
+ h, w, _ = image.shape
|
|
|
|
+ h_ratio = 1 if h <= 1000 else h / 1000
|
|
|
|
+ w_ratio = 1 if w <= 1000 else w / 1000
|
|
|
|
+
|
|
|
|
+ if h_ratio == 1 and w_ratio == 1:
|
|
|
|
+ return image, angle, result
|
|
|
|
+ elif h_ratio != 1 or w_ratio != 1:
|
|
|
|
+ ratio = h_ratio if h_ratio > w_ratio else w_ratio
|
|
|
|
+ image = cv2.resize(image, (w // math.ceil(ratio), h // math.ceil(ratio)))
|
|
|
|
+ print(image.shape)
|
|
|
|
+
|
|
|
|
+ return image, angle, result
|
|
|
|
|
|
def _ocr(self, image):
|
|
def _ocr(self, image):
|
|
# 获取模型检测结果,因为是正的照片了,所以不需要方向分类器
|
|
# 获取模型检测结果,因为是正的照片了,所以不需要方向分类器
|
|
@@ -69,29 +85,64 @@ class BankOcr:
|
|
if result:
|
|
if result:
|
|
confs = [line[1][1] for line in result]
|
|
confs = [line[1][1] for line in result]
|
|
print("自己的检测模型得到的conf:", confs)
|
|
print("自己的检测模型得到的conf:", confs)
|
|
- if len(result) == 2 and all(map(lambda x: x > 0.975, confs)):
|
|
|
|
- l_box, r_box = [], []
|
|
|
|
- l_box.extend(result[0][0])
|
|
|
|
- r_box.extend(result[1][0])
|
|
|
|
-
|
|
|
|
- l_max, _ = np.max(l_box, 0)
|
|
|
|
- r_min, _ = np.min(r_box, 0)
|
|
|
|
|
|
|
|
- if l_max > r_min:
|
|
|
|
- print("说明自己的检测模型不好")
|
|
|
|
- result = self.angle_detector.origin_detect(image)
|
|
|
|
- else:
|
|
|
|
- # 一般情况下,len=1
|
|
|
|
- flag = 0
|
|
|
|
- if map(lambda x: x >= 0.975, confs):
|
|
|
|
- flag = 1
|
|
|
|
- # for conf in confs:
|
|
|
|
- # if conf >= 0.975:
|
|
|
|
- # flag = 1
|
|
|
|
- # break
|
|
|
|
- if flag == 0:
|
|
|
|
- print("需要再次进行官方的检测代码。。。。。。。。。。。。")
|
|
|
|
|
|
+ # 根绝len(result)分规则判断
|
|
|
|
+ if len(result) == 1:
|
|
|
|
+ if confs[0] > 0.987:
|
|
|
|
+ txts = [line[1][0] for line in result]
|
|
|
|
+ return txts, confs, result
|
|
|
|
+ else:
|
|
|
|
+ print("len(result)=1时,再次用官方代码检测。。。。。。")
|
|
result = self.angle_detector.origin_detect(image)
|
|
result = self.angle_detector.origin_detect(image)
|
|
|
|
+ elif len(result) == 2:
|
|
|
|
+ # 1.判断两个检测框在不在一行
|
|
|
|
+ is_oneline = self.angle_detector.det_oneline(result)
|
|
|
|
+ # 2.如果不在一行
|
|
|
|
+ if not is_oneline:
|
|
|
|
+ txts = [line[1][0] for line in result]
|
|
|
|
+ if not (any(map(lambda x: x > 0.987, confs)) and len(re.findall('\d{16,20}', txts)) > 0):
|
|
|
|
+ print("len(result)=2,但是不在一行。。。。。。")
|
|
|
|
+ result = self.angle_detector.origin_detect(image)
|
|
|
|
+ # 3. 如果在一行
|
|
|
|
+ elif is_oneline:
|
|
|
|
+ if all(map(lambda x: x > 0.987, confs)):
|
|
|
|
+ l_box, r_box = [], []
|
|
|
|
+ l_box.extend(result[0][0])
|
|
|
|
+ r_box.extend(result[1][0])
|
|
|
|
+
|
|
|
|
+ l_max, _ = np.max(l_box, 0)
|
|
|
|
+ r_min, _ = np.min(r_box, 0)
|
|
|
|
+
|
|
|
|
+ if l_max > r_min:
|
|
|
|
+ print("len(result)=2,在一行,但有重叠。。。。。。")
|
|
|
|
+ result = self.angle_detector.origin_detect(image)
|
|
|
|
+ else:
|
|
|
|
+ print("len(result)=2,在一行,但有一个检测不行。。。。。。")
|
|
|
|
+ result = self.angle_detector.origin_detect(image)
|
|
|
|
+ elif len(result) > 2:
|
|
|
|
+ print("len(result)=3,直接换官方检测。。。。。。")
|
|
|
|
+ result = self.angle_detector.origin_detect(image)
|
|
|
|
+
|
|
|
|
+ # elif len(result) == 2 and all(map(lambda x: x > 0.975, confs)):
|
|
|
|
+ # l_box, r_box = [], []
|
|
|
|
+ # l_box.extend(result[0][0])
|
|
|
|
+ # r_box.extend(result[1][0])
|
|
|
|
+ #
|
|
|
|
+ # l_max, _ = np.max(l_box, 0)
|
|
|
|
+ # r_min, _ = np.min(r_box, 0)
|
|
|
|
+ #
|
|
|
|
+ # if l_max > r_min:
|
|
|
|
+ # print("说明自己的检测模型不好")
|
|
|
|
+ # result = self.angle_detector.origin_detect(image)
|
|
|
|
+ # else:
|
|
|
|
+ # # 一般情况下,len=1
|
|
|
|
+ # flag = 0
|
|
|
|
+ # if all(map(lambda x: x >= 0.975, confs)):
|
|
|
|
+ # flag = 1
|
|
|
|
+ #
|
|
|
|
+ # if flag == 0:
|
|
|
|
+ # print("需要再次进行官方的检测代码。。。。。。。。。。。。")
|
|
|
|
+ # result = self.angle_detector.origin_detect(image)
|
|
|
|
|
|
# 如果还是空,那就检测不出来
|
|
# 如果还是空,那就检测不出来
|
|
if not result:
|
|
if not result:
|