a379fac9 by 周伟奇

fix key search

1 parent c0cf9254
......@@ -23,6 +23,7 @@ print(res)
# {'words_result':
# {'姓名': {'words': 'xx', 'score': 1, 'location': {'left': 105, 'top': 277, 'width': 60, 'height': 28}},
# '个人身份证件号码': {'words': 'xx', 'score': 1, 'location': {'left': 320, 'top': 278, 'widtght': 24}},
# '经销商名称': {'words': 'xx', 'score': 1, 'location': {'left': 320, 'top': 278, 'widtght': 24}},
# '签字': {'words': '有', 'score': 1, 'location': {'left': 540, 'top': 1293, 'width': 143, 'height': 91}}}
# }
......
......@@ -31,11 +31,15 @@ class Retriever:
coordinates_list.sort(key=lambda x: x[1])
return coordinates_list[0]
@staticmethod
def key_right(coordinates_list, key_coordinates, top_padding, bottom_padding, scope):
def key_right(self, coordinates_list, key_coordinates, top_padding, bottom_padding, scope):
# 关键词查找方向:右侧
if len(coordinates_list) == 1:
return coordinates_list[0]
# 没有上一层关键词的坐标时,返回最上面的坐标
if key_coordinates is None:
return self.key_top1(coordinates_list, key_coordinates)
height = key_coordinates[-1] - key_coordinates[1]
y_min = key_coordinates[1] - (top_padding * height)
y_max = key_coordinates[-1] + (bottom_padding * height)
......@@ -45,15 +49,19 @@ class Retriever:
x_max = key_coordinates[2] + (width * scope)
x_min_find = None
key_coordinates = None
find_key_coordinates = None
for x0, y0, x1, y1 in coordinates_list:
cent_x = x0 + ((x1 - x0) / 2)
cent_y = y0 + ((y1 - y0) / 2)
if x_min < cent_x < x_max and y_min < cent_y < y_max:
if x_min_find is None or x0 < x_min_find:
x_min_find = x0
key_coordinates = (x0, y0, x1, y1)
return key_coordinates
find_key_coordinates = (x0, y0, x1, y1)
if find_key_coordinates is None:
return self.key_top1(coordinates_list, key_coordinates)
else:
return find_key_coordinates
def value_right(self, go_res, key_coordinates, top_padding, bottom_padding, scope, value_type=None):
# 字段值查找方向:右侧
......@@ -147,19 +155,17 @@ class Retriever:
# 搜索关键词
key_coordinates_info = dict()
for field, key_text_list in self.target_fields[self.keys_str].items():
pre_key_coordinates = None
last_key_coordinates = None
for key_text, _, direction, kwargs in key_text_list:
if key_text not in key_text_info:
break
key_coordinates = getattr(self, 'key_{0}'.format(direction))(
last_key_coordinates = None
continue
last_key_coordinates = getattr(self, 'key_{0}'.format(direction))(
key_text_info[key_text],
pre_key_coordinates,
last_key_coordinates,
**kwargs)
if not isinstance(key_coordinates, tuple):
break
pre_key_coordinates = key_coordinates
else:
key_coordinates_info[field] = pre_key_coordinates
key_coordinates_info[field] = last_key_coordinates
# 搜索字段值
value_res = dict()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!