修正两个涂抹内容靠太近时容易在提升精度时将另一个忽略的问题

This commit is contained in:
2024-07-18 09:51:07 +08:00
parent 33cf259212
commit 4e076d6ab1

View File

@@ -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)