a379fac9 by 周伟奇

fix key search

1 parent c0cf9254
...@@ -23,6 +23,7 @@ print(res) ...@@ -23,6 +23,7 @@ print(res)
23 # {'words_result': 23 # {'words_result':
24 # {'姓名': {'words': 'xx', 'score': 1, 'location': {'left': 105, 'top': 277, 'width': 60, 'height': 28}}, 24 # {'姓名': {'words': 'xx', 'score': 1, 'location': {'left': 105, 'top': 277, 'width': 60, 'height': 28}},
25 # '个人身份证件号码': {'words': 'xx', 'score': 1, 'location': {'left': 320, 'top': 278, 'widtght': 24}}, 25 # '个人身份证件号码': {'words': 'xx', 'score': 1, 'location': {'left': 320, 'top': 278, 'widtght': 24}},
26 # '经销商名称': {'words': 'xx', 'score': 1, 'location': {'left': 320, 'top': 278, 'widtght': 24}},
26 # '签字': {'words': '有', 'score': 1, 'location': {'left': 540, 'top': 1293, 'width': 143, 'height': 91}}} 27 # '签字': {'words': '有', 'score': 1, 'location': {'left': 540, 'top': 1293, 'width': 143, 'height': 91}}}
27 # } 28 # }
28 29
......
...@@ -31,11 +31,15 @@ class Retriever: ...@@ -31,11 +31,15 @@ class Retriever:
31 coordinates_list.sort(key=lambda x: x[1]) 31 coordinates_list.sort(key=lambda x: x[1])
32 return coordinates_list[0] 32 return coordinates_list[0]
33 33
34 @staticmethod 34 def key_right(self, coordinates_list, key_coordinates, top_padding, bottom_padding, scope):
35 def key_right(coordinates_list, key_coordinates, top_padding, bottom_padding, scope):
36 # 关键词查找方向:右侧 35 # 关键词查找方向:右侧
37 if len(coordinates_list) == 1: 36 if len(coordinates_list) == 1:
38 return coordinates_list[0] 37 return coordinates_list[0]
38
39 # 没有上一层关键词的坐标时,返回最上面的坐标
40 if key_coordinates is None:
41 return self.key_top1(coordinates_list, key_coordinates)
42
39 height = key_coordinates[-1] - key_coordinates[1] 43 height = key_coordinates[-1] - key_coordinates[1]
40 y_min = key_coordinates[1] - (top_padding * height) 44 y_min = key_coordinates[1] - (top_padding * height)
41 y_max = key_coordinates[-1] + (bottom_padding * height) 45 y_max = key_coordinates[-1] + (bottom_padding * height)
...@@ -45,15 +49,19 @@ class Retriever: ...@@ -45,15 +49,19 @@ class Retriever:
45 x_max = key_coordinates[2] + (width * scope) 49 x_max = key_coordinates[2] + (width * scope)
46 50
47 x_min_find = None 51 x_min_find = None
48 key_coordinates = None 52 find_key_coordinates = None
49 for x0, y0, x1, y1 in coordinates_list: 53 for x0, y0, x1, y1 in coordinates_list:
50 cent_x = x0 + ((x1 - x0) / 2) 54 cent_x = x0 + ((x1 - x0) / 2)
51 cent_y = y0 + ((y1 - y0) / 2) 55 cent_y = y0 + ((y1 - y0) / 2)
52 if x_min < cent_x < x_max and y_min < cent_y < y_max: 56 if x_min < cent_x < x_max and y_min < cent_y < y_max:
53 if x_min_find is None or x0 < x_min_find: 57 if x_min_find is None or x0 < x_min_find:
54 x_min_find = x0 58 x_min_find = x0
55 key_coordinates = (x0, y0, x1, y1) 59 find_key_coordinates = (x0, y0, x1, y1)
56 return key_coordinates 60
61 if find_key_coordinates is None:
62 return self.key_top1(coordinates_list, key_coordinates)
63 else:
64 return find_key_coordinates
57 65
58 def value_right(self, go_res, key_coordinates, top_padding, bottom_padding, scope, value_type=None): 66 def value_right(self, go_res, key_coordinates, top_padding, bottom_padding, scope, value_type=None):
59 # 字段值查找方向:右侧 67 # 字段值查找方向:右侧
...@@ -147,19 +155,17 @@ class Retriever: ...@@ -147,19 +155,17 @@ class Retriever:
147 # 搜索关键词 155 # 搜索关键词
148 key_coordinates_info = dict() 156 key_coordinates_info = dict()
149 for field, key_text_list in self.target_fields[self.keys_str].items(): 157 for field, key_text_list in self.target_fields[self.keys_str].items():
150 pre_key_coordinates = None 158 last_key_coordinates = None
151 for key_text, _, direction, kwargs in key_text_list: 159 for key_text, _, direction, kwargs in key_text_list:
152 if key_text not in key_text_info: 160 if key_text not in key_text_info:
153 break 161 last_key_coordinates = None
154 key_coordinates = getattr(self, 'key_{0}'.format(direction))( 162 continue
163 last_key_coordinates = getattr(self, 'key_{0}'.format(direction))(
155 key_text_info[key_text], 164 key_text_info[key_text],
156 pre_key_coordinates, 165 last_key_coordinates,
157 **kwargs) 166 **kwargs)
158 if not isinstance(key_coordinates, tuple): 167
159 break 168 key_coordinates_info[field] = last_key_coordinates
160 pre_key_coordinates = key_coordinates
161 else:
162 key_coordinates_info[field] = pre_key_coordinates
163 169
164 # 搜索字段值 170 # 搜索字段值
165 value_res = dict() 171 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!