From 4e076d6ab17e94fec121458d22b591bda5342116 Mon Sep 17 00:00:00 2001 From: liuyebo <1515783401@qq.com> Date: Thu, 18 Jul 2024 09:51:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=A4=E4=B8=AA=E6=B6=82?= =?UTF-8?q?=E6=8A=B9=E5=86=85=E5=AE=B9=E9=9D=A0=E5=A4=AA=E8=BF=91=E6=97=B6?= =?UTF-8?q?=E5=AE=B9=E6=98=93=E5=9C=A8=E6=8F=90=E5=8D=87=E7=B2=BE=E5=BA=A6?= =?UTF-8?q?=E6=97=B6=E5=B0=86=E5=8F=A6=E4=B8=80=E4=B8=AA=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- photo_mask/photo_mask.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/photo_mask/photo_mask.py b/photo_mask/photo_mask.py index a97fdeb..94b0bbd 100644 --- a/photo_mask/photo_mask.py +++ b/photo_mask/photo_mask.py @@ -13,7 +13,7 @@ from ucloud import BUCKET, ucloud from util import image_util, util -def find_box(content, layout, offset=0, length=None, improve=False, image_path=None): +def find_boxes(content, layout, offset=0, length=None, improve=False, image_path=None): full_box = layout[0] x_len = full_box[2] - full_box[0] y_len = full_box[3] - full_box[1] @@ -49,6 +49,7 @@ def find_box(content, layout, offset=0, length=None, improve=False, image_path=N full_box[1] + (index + offset + length) * char_len, ] + boxes = [] if improve: # 再次识别,提高精度 image = cv2.imread(image_path) @@ -68,17 +69,19 @@ def find_box(content, layout, offset=0, length=None, improve=False, image_path=N raise e for layout in layouts: if content in layout[1]: - temp_box = find_box(content, layout) + temp_box = find_boxes(content, layout)[0] if temp_box: - box = [ + boxes.append([ temp_box[0] + capture_box[0] - offset_x, temp_box[1] + capture_box[1] - offset_y, temp_box[2] + capture_box[0] - offset_x, temp_box[3] + capture_box[1] - offset_y, - ] - break + ]) util.delete_temp_file(temp_file.name) - return box + + if not boxes: + boxes.append(box) + return boxes def get_mask_layout(image, name, id_card_num): @@ -106,10 +109,10 @@ def get_mask_layout(image, name, id_card_num): find_name_by_key = True find_id_card_num_by_key = True if name in layout[1]: - result.append(find_box(name, layout, improve=True, image_path=temp_file.name)) + result += find_boxes(name, layout, improve=True, image_path=temp_file.name) find_name_by_key = False if id_card_num in layout[1]: - result.append(find_box(id_card_num, layout, improve=True, image_path=temp_file.name)) + result += find_boxes(id_card_num, layout, improve=True, image_path=temp_file.name) find_id_card_num_by_key = False keys = [] @@ -119,10 +122,11 @@ def get_mask_layout(image, name, id_card_num): keys += ID_CARD_NUM_KEYS for key in keys: if key["key"] in layout[1]: - result.append(find_box(key["key"], layout, offset=len(key["key"]), length=key["length"])) + result += find_boxes(key["key"], layout, offset=len(key["key"]), length=key["length"]) return result except Exception as e: logging.error("涂抹时出错!", exc_info=e) + return result finally: util.delete_temp_file(temp_file.name)