fasdfsa commited on
Commit
f70c124
·
1 Parent(s): d354a1c

add fix_char_rectangle

Browse files
Files changed (1) hide show
  1. fix_char_rectangle.py +1263 -0
fix_char_rectangle.py ADDED
@@ -0,0 +1,1263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # see github\project\flask_auto_selection.py
3
+
4
+ # pip install numpy==1.24.4 opencv-python==4.10.0.84 flask==3.1.0 -i https://mirrors.aliyun.com/pypi/simple/
5
+ # paddlex 3.0.0b2 requires numpy==1.24.4
6
+ # numpy==1.23.5
7
+
8
+ # .88 see /root/tools/flask_auto_selection.py
9
+ # 44c08650fa2e54c6be381530a8dd8b5e 这个有问题
10
+
11
+ """
12
+ 根据字数对比平均宽度、腐蚀膨胀后填充区域对象, 等方法改进单个字符的坐标准确度
13
+ """
14
+
15
+
16
+ is_debug = False
17
+
18
+ dic_cache = {}
19
+
20
+ # from flask import Flask, request, jsonify
21
+
22
+ # app = Flask(__name__)
23
+
24
+ import numpy as np
25
+ import cv2
26
+ import json
27
+ import base64
28
+ import os, math, re
29
+
30
+ def save_json(filename, dics):
31
+ with open(filename, 'w', encoding='utf-8') as fp:
32
+ json.dump(dics, fp, indent=4, ensure_ascii=False)
33
+ fp.close()
34
+
35
+
36
+ def load_json(filename):
37
+ with open(filename, encoding='utf-8') as fp:
38
+ js = json.load(fp)
39
+ fp.close()
40
+ return js
41
+
42
+ # convert string to json
43
+ def jsonparse(s):
44
+ return json.loads(s, strict=False)
45
+
46
+ # convert dict to string
47
+ def jsonstring(d):
48
+ return json.dumps(d, ensure_ascii=False)
49
+
50
+ def show_img(image, target_width=400):
51
+ # 获取原始图片的宽度和高度
52
+ original_height, original_width = image.shape[:2]
53
+
54
+ # 计算缩放比例和目标高度
55
+ scale = target_width / original_width
56
+ target_height = int(original_height * scale)
57
+
58
+ # 等比例缩放图片
59
+ resized_image = cv2.resize(image, (target_width, target_height), interpolation=cv2.INTER_AREA)
60
+ cv2.imshow("green", resized_image)
61
+ cv2.waitKey(0)
62
+ return resized_image
63
+
64
+ def get_selections(dir, m5):
65
+
66
+ dir_pplabel = 'pplabel'
67
+ if not os.path.exists(dir_pplabel):
68
+ os.mkdir( dir_pplabel )
69
+
70
+ pth_Label = os.path.join(dir_pplabel, 'Label.txt')
71
+ Label_txt = ''
72
+
73
+ Label_txt += f'''{dir_pplabel}/{m5}.jpg\t'''
74
+
75
+ pth_img = os.path.join(dir, 'img/{}.txt'.format(m5))
76
+ pth_json = os.path.join(dir, 'json/{}.json'.format(m5))
77
+
78
+ if not os.path.exists(pth_img): # 没有相应的图片,可能被删除了
79
+ # raise Exception(f'Warnnig: no image {pth_img}')
80
+ print( f'Warnnig: no image {pth_img}' )
81
+ return []
82
+
83
+ if not os.path.exists(pth_json):
84
+ # raise Exception(f'Warnnig: no image {pth_json}')
85
+ return []
86
+
87
+ if is_debug:
88
+ with open(pth_img, "r", encoding="utf-8") as fp:
89
+ imgdata = fp.read()
90
+ imgdata = base64.b64decode(imgdata)
91
+ imgdata = np.frombuffer(imgdata, np.uint8)
92
+ img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
93
+
94
+ # cv2.imshow('img', img)
95
+ # cv2.waitKey(0)
96
+
97
+ if len(img.shape) != 3: # 转彩图
98
+ img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
99
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # DBNet 原版只能处理彩图,这里转一下
100
+
101
+ else:
102
+ img_color = img.copy()
103
+
104
+ cv2.imwrite(os.path.join(dir_pplabel, f'{m5}.jpg'), img_color)
105
+
106
+
107
+ jsn = load_json(pth_json)
108
+
109
+ orgHeight = jsn['orgHeight']
110
+ orgWidth = jsn['orgWidth']
111
+
112
+
113
+
114
+
115
+ def left_right(jsn, img_color, Label_txt, pth_Label):
116
+ """
117
+ 左右分栏模板
118
+ """
119
+
120
+ array = []
121
+ array2 = []
122
+
123
+ wordsInfo = jsn['prism_wordsInfo']
124
+ for j in range(len(wordsInfo)):
125
+ jo = wordsInfo[j]
126
+ word = jo["word"]
127
+
128
+ # prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
129
+ angle = jo['angle']
130
+
131
+ word_x = jo['x']
132
+ word_y = jo['y']
133
+ word_width = jo['width']
134
+ word_height = jo['height']
135
+
136
+ if abs(angle) == 90 or abs(angle) == 270:
137
+ word_width = jo['height']
138
+ word_height = jo['width']
139
+
140
+ pos = jo['pos']
141
+
142
+ # 四个角的位置 左上、右上、右下、左下 顺时针方向
143
+ lu = [pos[0]['x'], pos[0]['y']]
144
+ ru = [pos[1]['x'], pos[1]['y']]
145
+ rd = [pos[2]['x'], pos[2]['y']]
146
+ ld = [pos[3]['x'], pos[3]['y']]
147
+
148
+ x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
149
+ x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
150
+
151
+ y1 = min( pos[0]['y'], pos[1]['y'] ) # 当前行的极上
152
+ y2 = max( pos[2]['y'], pos[3]['y'] ) # 当前行的极下
153
+
154
+ #img_color = cv2.rectangle(img_color, (x1, y1), (x2, y2), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
155
+ #img_color = cv2.rectangle(img_color, (lu[0], lu[1]), (rd[0], rd[1]), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
156
+
157
+ # cv2.imshow("green", img_color)
158
+ # cv2.waitKey(0)
159
+
160
+ if match := re.compile('(^\d+)\.').search(word):
161
+ nth = int( match.group(1) )
162
+ print( nth, word )
163
+
164
+ array.append( { "nth":nth, "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
165
+ else:
166
+ array2.append( { "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
167
+
168
+ pass
169
+
170
+
171
+
172
+ transcriptions = []
173
+ last_x2 = 99999
174
+ x_right = -1
175
+ left = []
176
+ right = []
177
+ for idx, item in enumerate(array):
178
+ leftup = item['leftup']
179
+ rightdown = item['rightdown']
180
+ x1 = leftup[0]
181
+ x2 = rightdown[0]
182
+
183
+ if x_right == -1 and x1 > last_x2:
184
+ # 第一次超过上一行的右边
185
+ x_right = x1
186
+
187
+ if x_right != -1:
188
+ right.append( item )
189
+ else:
190
+ left.append( item )
191
+
192
+ last_x2 = x2
193
+
194
+
195
+ if len(left) > 0 and len(right) > 0:
196
+
197
+ min_x1_left = 99999
198
+ min_y1_left = 99999
199
+
200
+ max_x2_left = -1
201
+ max_y2_left = -1
202
+
203
+ for item in left:
204
+ leftup = item['leftup']
205
+ rightdown = item['rightdown']
206
+ x1 = leftup[0]
207
+ x2 = rightdown[0]
208
+
209
+ y1 = leftup[1]
210
+ y2 = rightdown[1]
211
+
212
+ if x1 < min_x1_left:
213
+ min_x1_left = x1
214
+
215
+ if y1 < min_y1_left:
216
+ min_y1_left = y1
217
+
218
+ if x2 > max_x2_left:
219
+ max_x2_left = x2
220
+
221
+ if y2 > max_y2_left:
222
+ max_y2_left = y2
223
+
224
+ if len(array2)> 0:
225
+ for item in array2:
226
+ if item['leftup'][1] < min_y1_left:
227
+ min_y1_left = item['leftup'][1]
228
+
229
+ if item['rightdown'][1] > max_y2_left:
230
+ max_y2_left = item['rightdown'][1]
231
+
232
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
233
+
234
+ # cv2.imshow("green", img_color)
235
+ # cv2.waitKey(0)
236
+
237
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
238
+
239
+
240
+ if len(left) > 0 and len(right) > 0:
241
+
242
+ min_x1_left = 99999
243
+ min_y1_left = 99999
244
+
245
+ max_x2_left = -1
246
+ max_y2_left = -1
247
+
248
+ for item in right:
249
+ leftup = item['leftup']
250
+ rightdown = item['rightdown']
251
+ x1 = leftup[0]
252
+ x2 = rightdown[0]
253
+
254
+ y1 = leftup[1]
255
+ y2 = rightdown[1]
256
+
257
+ if x1 < min_x1_left:
258
+ min_x1_left = x1
259
+
260
+ if y1 < min_y1_left:
261
+ min_y1_left = y1
262
+
263
+ if x2 > max_x2_left:
264
+ max_x2_left = x2
265
+
266
+ if y2 > max_y2_left:
267
+ max_y2_left = y2
268
+
269
+
270
+ if len(array2)> 0:
271
+ for item in array2:
272
+ if item['leftup'][1] < min_y1_left:
273
+ min_y1_left = item['leftup'][1]
274
+
275
+ if item['rightdown'][1] > max_y2_left:
276
+ max_y2_left = item['rightdown'][1]
277
+
278
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
279
+
280
+ # cv2.imshow("green", img_color)
281
+ # cv2.waitKey(0)
282
+
283
+
284
+
285
+
286
+ # [{"transcription":"","points":[[160,167],[1123,167],[1123,2970],[160,2970]]},{"transcription":"","points":[[1134,160],[2078,160],[2078,2933],[1134,2933]]}]
287
+
288
+ # points 是框选的矩形四个角坐标: 左上 右上 右下 左下
289
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
290
+
291
+
292
+ if len(transcriptions) > 0:
293
+ Label_txt += jsonstring( transcriptions )
294
+ Label_txt += '\n'
295
+
296
+ with open(pth_Label, 'w', encoding='utf-8') as f:
297
+ f.write(Label_txt)
298
+ # huggingface/PPOCRLabel/PPOCRLabel.py 用这个打开目录
299
+ # ok 标注框是对的
300
+
301
+ cv2.imwrite(os.path.join(dir_pplabel, f'{m5}_auto_select.jpg'), img_color)
302
+
303
+ return transcriptions
304
+
305
+ def left_noright(jsn, img_color, Label_txt, pth_Label):
306
+ """
307
+ 左右分栏模板
308
+ """
309
+
310
+ array = []
311
+ array2 = []
312
+
313
+ wordsInfo = jsn['prism_wordsInfo']
314
+ for j in range(len(wordsInfo)):
315
+ jo = wordsInfo[j]
316
+ word = jo["word"]
317
+
318
+ # prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
319
+ angle = jo['angle']
320
+
321
+ word_x = jo['x']
322
+ word_y = jo['y']
323
+ word_width = jo['width']
324
+ word_height = jo['height']
325
+
326
+ if abs(angle) == 90 or abs(angle) == 270:
327
+ word_width = jo['height']
328
+ word_height = jo['width']
329
+
330
+ pos = jo['pos']
331
+
332
+ # 四个角的位置 左上、右上、右下、左下 顺时针方向
333
+ lu = [pos[0]['x'], pos[0]['y']]
334
+ ru = [pos[1]['x'], pos[1]['y']]
335
+ rd = [pos[2]['x'], pos[2]['y']]
336
+ ld = [pos[3]['x'], pos[3]['y']]
337
+
338
+ x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
339
+ x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
340
+
341
+ y1 = min( pos[0]['y'], pos[1]['y'] ) # 当前行的极上
342
+ y2 = max( pos[2]['y'], pos[3]['y'] ) # 当前行的极下
343
+
344
+ #img_color = cv2.rectangle(img_color, (x1, y1), (x2, y2), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
345
+ #img_color = cv2.rectangle(img_color, (lu[0], lu[1]), (rd[0], rd[1]), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
346
+
347
+ # cv2.imshow("green", img_color)
348
+ # cv2.waitKey(0)
349
+
350
+ if match := re.compile('(^[\S]+)').search(word):
351
+ g1 = match.group(1)
352
+ # print( g1 )
353
+
354
+ array.append( { "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
355
+ array2.append( { "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
356
+
357
+ pass
358
+
359
+
360
+
361
+ transcriptions = []
362
+ last_x2 = 99999
363
+ x_right = -1
364
+ left = []
365
+ right = []
366
+ for idx, item in enumerate(array):
367
+ leftup = item['leftup']
368
+ rightdown = item['rightdown']
369
+ x1 = leftup[0]
370
+ x2 = rightdown[0]
371
+
372
+ if x_right == -1 and x1 > last_x2:
373
+ # 第一次超过上一行的右边
374
+ x_right = x1
375
+
376
+ if x_right != -1:
377
+ right.append( item )
378
+ else:
379
+ left.append( item )
380
+
381
+ last_x2 = x2
382
+
383
+
384
+ if len(left) > 5:
385
+
386
+ min_x1_left = 99999
387
+ min_y1_left = 99999
388
+
389
+ max_x2_left = -1
390
+ max_y2_left = -1
391
+
392
+ for item in left:
393
+ leftup = item['leftup']
394
+ rightdown = item['rightdown']
395
+ x1 = leftup[0]
396
+ x2 = rightdown[0]
397
+
398
+ y1 = leftup[1]
399
+ y2 = rightdown[1]
400
+
401
+ if x1 < min_x1_left:
402
+ min_x1_left = x1
403
+
404
+ if y1 < min_y1_left:
405
+ min_y1_left = y1
406
+
407
+ if x2 > max_x2_left:
408
+ max_x2_left = x2
409
+
410
+ if y2 > max_y2_left:
411
+ max_y2_left = y2
412
+
413
+ if len(array2)> 0:
414
+ for item in array2:
415
+ if item['leftup'][1] < min_y1_left:
416
+ min_y1_left = item['leftup'][1]
417
+
418
+ if item['rightdown'][1] > max_y2_left:
419
+ max_y2_left = item['rightdown'][1]
420
+
421
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
422
+
423
+ # cv2.imshow("green", img_color)
424
+ # cv2.waitKey(0)
425
+
426
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
427
+
428
+
429
+ if len(right) > 5:
430
+
431
+ min_x1_left = 99999
432
+ min_y1_left = 99999
433
+
434
+ max_x2_left = -1
435
+ max_y2_left = -1
436
+
437
+ for item in right:
438
+ leftup = item['leftup']
439
+ rightdown = item['rightdown']
440
+ x1 = leftup[0]
441
+ x2 = rightdown[0]
442
+
443
+ y1 = leftup[1]
444
+ y2 = rightdown[1]
445
+
446
+ if x1 < min_x1_left:
447
+ min_x1_left = x1
448
+
449
+ if y1 < min_y1_left:
450
+ min_y1_left = y1
451
+
452
+ if x2 > max_x2_left:
453
+ max_x2_left = x2
454
+
455
+ if y2 > max_y2_left:
456
+ max_y2_left = y2
457
+
458
+
459
+ if len(array2)> 0:
460
+ for item in array2:
461
+ if item['leftup'][1] < min_y1_left:
462
+ min_y1_left = item['leftup'][1]
463
+
464
+ if item['rightdown'][1] > max_y2_left:
465
+ max_y2_left = item['rightdown'][1]
466
+
467
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
468
+
469
+ # cv2.imshow("green", img_color)
470
+ # cv2.waitKey(0)
471
+
472
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
473
+
474
+
475
+ if len(transcriptions) > 0:
476
+ Label_txt += jsonstring( transcriptions )
477
+ Label_txt += '\n'
478
+
479
+ with open(pth_Label, 'w', encoding='utf-8') as f:
480
+ f.write(Label_txt)
481
+ # huggingface/PPOCRLabel/PPOCRLabel.py 用这个打开目录
482
+ # ok 标注框是对的
483
+
484
+ cv2.imwrite(os.path.join(dir_pplabel, f'{m5}_auto_select.jpg'), img_color)
485
+
486
+ return transcriptions
487
+
488
+
489
+ transcriptions = left_right(jsn, img_color, Label_txt, pth_Label)
490
+ if len(transcriptions) > 0:
491
+ return transcriptions
492
+
493
+ transcriptions = left_noright(jsn, img_color, Label_txt, pth_Label)
494
+ if len(transcriptions) > 0:
495
+ return transcriptions
496
+
497
+ return []
498
+
499
+ def get_selectionsv2(dir, m5):
500
+
501
+ transcriptions = []
502
+
503
+ dir_pplabel = 'pplabel'
504
+ if not os.path.exists(dir_pplabel):
505
+ os.mkdir( dir_pplabel )
506
+
507
+ pth_Label = os.path.join(dir_pplabel, 'Label.txt')
508
+ Label_txt = ''
509
+
510
+ Label_txt += f'''{dir_pplabel}/{m5}.jpg\t'''
511
+
512
+ pth_img = os.path.join(dir, 'img/{}.txt'.format(m5))
513
+ pth_json = os.path.join(dir, 'json/{}.json'.format(m5))
514
+ if not os.path.exists(pth_img): # 没有相应的图片,可能被删除了
515
+ # raise Exception(f'Warnnig: no image {pth_img}')
516
+ print( f'Warnnig: no image {pth_img}' )
517
+ return []
518
+
519
+ if not os.path.exists(pth_json):
520
+ # raise Exception(f'Warnnig: no image {pth_json}')
521
+ print( f'Warnnig: no json {pth_json}' )
522
+ return []
523
+
524
+ img_color = None
525
+
526
+ if is_debug:
527
+
528
+ with open(pth_img, "r", encoding="utf-8") as fp:
529
+ imgdata = fp.read()
530
+ imgdata = base64.b64decode(imgdata)
531
+ imgdata = np.frombuffer(imgdata, np.uint8)
532
+ img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
533
+
534
+ # cv2.imshow('img', img)
535
+ # cv2.waitKey(0)
536
+
537
+ if len(img.shape) != 3: # 转彩图
538
+ img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
539
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # DBNet 原版只能处理彩图,这里转一下
540
+
541
+ else:
542
+ img_color = img.copy()
543
+
544
+ cv2.imwrite(os.path.join(dir_pplabel, f'{m5}.jpg'), img_color)
545
+
546
+
547
+ jsn = load_json(pth_json)
548
+
549
+ orgHeight = jsn['orgHeight']
550
+ orgWidth = jsn['orgWidth']
551
+
552
+ def pos_info(jsn, img_color):
553
+
554
+ array_digitNum = []
555
+ array_digitZH = []
556
+ array_ZH = []
557
+ array_other = []
558
+
559
+ wordsInfo = jsn['prism_wordsInfo']
560
+ for j in range(len(wordsInfo)):
561
+ jo = wordsInfo[j]
562
+ word = jo["word"]
563
+
564
+ # prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
565
+ angle = jo['angle']
566
+
567
+ word_x = jo['x']
568
+ word_y = jo['y']
569
+ word_width = jo['width']
570
+ word_height = jo['height']
571
+
572
+ if abs(angle) == 90 or abs(angle) == 270:
573
+ word_width = jo['height']
574
+ word_height = jo['width']
575
+
576
+ pos = jo['pos']
577
+
578
+ # 四个角的位置 左上、右上、右下、左下 顺时针方向
579
+ lu = [pos[0]['x'], pos[0]['y']]
580
+ ru = [pos[1]['x'], pos[1]['y']]
581
+ rd = [pos[2]['x'], pos[2]['y']]
582
+ ld = [pos[3]['x'], pos[3]['y']]
583
+
584
+ x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
585
+ x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
586
+
587
+ y1 = min( pos[0]['y'], pos[1]['y'] ) # 当前行的极上
588
+ y2 = max( pos[2]['y'], pos[3]['y'] ) # 当前行的极下
589
+
590
+ #img_color = cv2.rectangle(img_color, (x1, y1), (x2, y2), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
591
+ #img_color = cv2.rectangle(img_color, (lu[0], lu[1]), (rd[0], rd[1]), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
592
+
593
+ # cv2.imshow("green", img_color)
594
+ # cv2.waitKey(0)
595
+
596
+ if len(word) >= 10 and ( match := re.compile('(^\d+)\.[\S]').search(word) ):
597
+ # if len(word) >= 10:
598
+ nth = int( match.group(1) )
599
+ print('digitNum: ', nth)
600
+ array_digitNum.append( { "nth":nth, "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
601
+
602
+ elif match := re.compile('(^[一二三四五六七八九十]+、)').search(word):
603
+ nth = match.group(1)
604
+ print('digitZH: ', nth)
605
+ array_digitZH.append( { "nth":nth, "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
606
+ elif match := re.compile('(^[\u4e00-\u9fa5]+)').search(word):
607
+ zh = match.group(1)
608
+ print('ZH: ', zh)
609
+ array_ZH.append( { "zh":zh, "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
610
+ elif match := re.compile('(^[\S])').search(word):
611
+ other = match.group(1)
612
+ print('other: ', other)
613
+ array_other.append( { "other":other, "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
614
+
615
+
616
+
617
+
618
+ return array_digitNum, array_digitZH, array_ZH, array_other
619
+
620
+ # 是否左右分栏
621
+ def is_left_right(array, orgWidth):
622
+
623
+ def minmax(ar):
624
+ """
625
+ 极左极右
626
+ """
627
+ min_x = 99999
628
+ max_x = -1
629
+ for item in ar:
630
+ leftup = item['leftup']
631
+ rightdown = item['rightdown']
632
+ x1 = leftup[0]
633
+ x2 = rightdown[0]
634
+ if x1 < min_x:
635
+ min_x = x1
636
+ if x2 > max_x:
637
+ max_x = x2
638
+
639
+ return min_x, max_x
640
+
641
+ def relatedWidth(min_x, max_x):
642
+ """
643
+ 相对宽度
644
+ """
645
+ w = max_x - min_x
646
+ return w
647
+
648
+ min_x, max_x = minmax(array)
649
+ rltWidth = relatedWidth( min_x, max_x )
650
+
651
+ last_x2 = 99999
652
+ x_right = -1
653
+ left = []
654
+ right = []
655
+ flag = False
656
+ for idx, item in enumerate(array):
657
+ leftup = item['leftup']
658
+ rightdown = item['rightdown']
659
+ x1 = leftup[0]
660
+ x2 = rightdown[0]
661
+
662
+ if not flag and x1 > rltWidth / 2:
663
+ right.append( item )
664
+ continue
665
+ elif x1 <= rltWidth / 2:
666
+ flag = True
667
+
668
+
669
+ if x_right == -1 and x1 > last_x2:
670
+ # 第一次超过上一行的右边
671
+ x_right = x1
672
+
673
+ if x_right != -1 and x1 > last_x2:
674
+ right.append( item )
675
+ else:
676
+ left.append( item )
677
+
678
+ if x_right == -1:
679
+ last_x2 = x2
680
+
681
+ # if len(left) > 0 and len(right) >0:
682
+ # return True
683
+ # return False
684
+ return left, right
685
+
686
+ def middle_left_right(left, right):
687
+ """
688
+ 找出左右分栏中间区域的极左和极右
689
+ """
690
+
691
+ # 左边找极右
692
+ max_right = -1
693
+ for idx, item in enumerate(left):
694
+ leftup = item['leftup']
695
+ rightdown = item['rightdown']
696
+ x1 = leftup[0]
697
+ x2 = rightdown[0]
698
+ if x2 > max_right:
699
+ max_right = x2
700
+
701
+ # 右边找极左
702
+ min_left = 99999
703
+ for idx, item in enumerate(right):
704
+ leftup = item['leftup']
705
+ rightdown = item['rightdown']
706
+ x1 = leftup[0]
707
+ x2 = rightdown[0]
708
+ if x1 < min_left:
709
+ min_left = x1
710
+
711
+ return min_left, max_right
712
+
713
+ def left_or_right( min_left, max_right, array ):
714
+ """
715
+ max_right: 左边极右(左右分栏的左栏)
716
+ min_left: 右边极左(左右分栏的右栏)
717
+ """
718
+
719
+ left = []
720
+ right = []
721
+ for idx, item in enumerate(array):
722
+ leftup = item['leftup']
723
+ rightdown = item['rightdown']
724
+ x1 = leftup[0]
725
+ x2 = rightdown[0]
726
+ if x1 < min_left and x2 < min_left:
727
+ # 它的左边小于的右边极左,并且它的右边也小于的右边极左
728
+ left.append( item )
729
+ elif x1 > max_right:
730
+ # 它的左边大于中间区域的右边
731
+ right.append( item )
732
+ else:
733
+ print(f'Waring: not left AND not right. {item}')
734
+
735
+ return left, right
736
+
737
+
738
+ def get_selection(array, img_color):
739
+ transcriptions = []
740
+ if len(array) > 0:
741
+
742
+ min_x1_left = 99999
743
+ min_y1_left = 99999
744
+
745
+ max_x2_left = -1
746
+ max_y2_left = -1
747
+
748
+ for item in array:
749
+ leftup = item['leftup']
750
+ rightdown = item['rightdown']
751
+ x1 = leftup[0]
752
+ x2 = rightdown[0]
753
+
754
+ y1 = leftup[1]
755
+ y2 = rightdown[1]
756
+
757
+ if x1 < min_x1_left:
758
+ min_x1_left = x1
759
+
760
+ if y1 < min_y1_left:
761
+ min_y1_left = y1
762
+
763
+ if x2 > max_x2_left:
764
+ max_x2_left = x2
765
+
766
+ if y2 > max_y2_left:
767
+ max_y2_left = y2
768
+
769
+ if is_debug:
770
+
771
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
772
+
773
+ resized_image = show_img(img_color)
774
+ cv2.imwrite(os.path.join(dir_pplabel, f'{m5}_select.jpg'), resized_image)
775
+
776
+ # cv2.imshow("green", img_color)
777
+ # cv2.waitKey(0)
778
+
779
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
780
+
781
+ return transcriptions
782
+
783
+ def left_right(jsn, img_color, Label_txt, pth_Label):
784
+ """
785
+ 左右分栏模板
786
+ """
787
+
788
+ array = []
789
+ array2 = []
790
+
791
+ wordsInfo = jsn['prism_wordsInfo']
792
+ for j in range(len(wordsInfo)):
793
+ jo = wordsInfo[j]
794
+ word = jo["word"]
795
+
796
+ # prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
797
+ angle = jo['angle']
798
+
799
+ word_x = jo['x']
800
+ word_y = jo['y']
801
+ word_width = jo['width']
802
+ word_height = jo['height']
803
+
804
+ if abs(angle) == 90 or abs(angle) == 270:
805
+ word_width = jo['height']
806
+ word_height = jo['width']
807
+
808
+ pos = jo['pos']
809
+
810
+ # 四个角的位置 左上、右上、右下、左下 顺时针方向
811
+ lu = [pos[0]['x'], pos[0]['y']]
812
+ ru = [pos[1]['x'], pos[1]['y']]
813
+ rd = [pos[2]['x'], pos[2]['y']]
814
+ ld = [pos[3]['x'], pos[3]['y']]
815
+
816
+ x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
817
+ x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
818
+
819
+ y1 = min( pos[0]['y'], pos[1]['y'] ) # 当前行的极上
820
+ y2 = max( pos[2]['y'], pos[3]['y'] ) # 当前行的极下
821
+
822
+ #img_color = cv2.rectangle(img_color, (x1, y1), (x2, y2), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
823
+ #img_color = cv2.rectangle(img_color, (lu[0], lu[1]), (rd[0], rd[1]), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
824
+
825
+ # cv2.imshow("green", img_color)
826
+ # cv2.waitKey(0)
827
+
828
+ if match := re.compile('(^\d+)\.').search(word):
829
+ nth = int( match.group(1) )
830
+ print( nth, word )
831
+
832
+ array.append( { "nth":nth, "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
833
+ else:
834
+ array2.append( { "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
835
+
836
+ pass
837
+
838
+
839
+
840
+ transcriptions = []
841
+ last_x2 = 99999
842
+ x_right = -1
843
+ left = []
844
+ right = []
845
+ for idx, item in enumerate(array):
846
+ leftup = item['leftup']
847
+ rightdown = item['rightdown']
848
+ x1 = leftup[0]
849
+ x2 = rightdown[0]
850
+
851
+ if x_right == -1 and x1 > last_x2:
852
+ # 第一次超过上一行的右边
853
+ x_right = x1
854
+
855
+ if x_right != -1:
856
+ right.append( item )
857
+ else:
858
+ left.append( item )
859
+
860
+ last_x2 = x2
861
+
862
+
863
+ if len(left) > 0 and len(right) > 0:
864
+
865
+ min_x1_left = 99999
866
+ min_y1_left = 99999
867
+
868
+ max_x2_left = -1
869
+ max_y2_left = -1
870
+
871
+ for item in left:
872
+ leftup = item['leftup']
873
+ rightdown = item['rightdown']
874
+ x1 = leftup[0]
875
+ x2 = rightdown[0]
876
+
877
+ y1 = leftup[1]
878
+ y2 = rightdown[1]
879
+
880
+ if x1 < min_x1_left:
881
+ min_x1_left = x1
882
+
883
+ if y1 < min_y1_left:
884
+ min_y1_left = y1
885
+
886
+ if x2 > max_x2_left:
887
+ max_x2_left = x2
888
+
889
+ if y2 > max_y2_left:
890
+ max_y2_left = y2
891
+
892
+ if len(array2)> 0:
893
+ for item in array2:
894
+ if item['leftup'][1] < min_y1_left:
895
+ min_y1_left = item['leftup'][1]
896
+
897
+ if item['rightdown'][1] > max_y2_left:
898
+ max_y2_left = item['rightdown'][1]
899
+
900
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
901
+
902
+ # cv2.imshow("green", img_color)
903
+ # cv2.waitKey(0)
904
+
905
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
906
+
907
+
908
+ if len(left) > 0 and len(right) > 0:
909
+
910
+ min_x1_left = 99999
911
+ min_y1_left = 99999
912
+
913
+ max_x2_left = -1
914
+ max_y2_left = -1
915
+
916
+ for item in right:
917
+ leftup = item['leftup']
918
+ rightdown = item['rightdown']
919
+ x1 = leftup[0]
920
+ x2 = rightdown[0]
921
+
922
+ y1 = leftup[1]
923
+ y2 = rightdown[1]
924
+
925
+ if x1 < min_x1_left:
926
+ min_x1_left = x1
927
+
928
+ if y1 < min_y1_left:
929
+ min_y1_left = y1
930
+
931
+ if x2 > max_x2_left:
932
+ max_x2_left = x2
933
+
934
+ if y2 > max_y2_left:
935
+ max_y2_left = y2
936
+
937
+
938
+ if len(array2)> 0:
939
+ for item in array2:
940
+ if item['leftup'][1] < min_y1_left:
941
+ min_y1_left = item['leftup'][1]
942
+
943
+ if item['rightdown'][1] > max_y2_left:
944
+ max_y2_left = item['rightdown'][1]
945
+
946
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
947
+
948
+ # cv2.imshow("green", img_color)
949
+ # cv2.waitKey(0)
950
+
951
+
952
+
953
+
954
+ # [{"transcription":"","points":[[160,167],[1123,167],[1123,2970],[160,2970]]},{"transcription":"","points":[[1134,160],[2078,160],[2078,2933],[1134,2933]]}]
955
+
956
+ # points 是框选的矩形四个角坐标: 左上 右上 右下 左下
957
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
958
+
959
+
960
+ if len(transcriptions) > 0:
961
+ Label_txt += jsonstring( transcriptions )
962
+ Label_txt += '\n'
963
+
964
+ with open(pth_Label, 'w', encoding='utf-8') as f:
965
+ f.write(Label_txt)
966
+ # huggingface/PPOCRLabel/PPOCRLabel.py 用这个打开目录
967
+ # ok 标注框是对的
968
+
969
+ cv2.imwrite(os.path.join(dir_pplabel, f'{m5}_auto_select.jpg'), img_color)
970
+
971
+ return transcriptions
972
+
973
+ def left_noright(jsn, img_color, Label_txt, pth_Label):
974
+ """
975
+ 左右分栏模板
976
+ """
977
+
978
+ array = []
979
+ array2 = []
980
+
981
+ wordsInfo = jsn['prism_wordsInfo']
982
+ for j in range(len(wordsInfo)):
983
+ jo = wordsInfo[j]
984
+ word = jo["word"]
985
+
986
+ # prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
987
+ angle = jo['angle']
988
+
989
+ word_x = jo['x']
990
+ word_y = jo['y']
991
+ word_width = jo['width']
992
+ word_height = jo['height']
993
+
994
+ if abs(angle) == 90 or abs(angle) == 270:
995
+ word_width = jo['height']
996
+ word_height = jo['width']
997
+
998
+ pos = jo['pos']
999
+
1000
+ # 四个角的位置 左上、右上、右下、左下 顺时针方向
1001
+ lu = [pos[0]['x'], pos[0]['y']]
1002
+ ru = [pos[1]['x'], pos[1]['y']]
1003
+ rd = [pos[2]['x'], pos[2]['y']]
1004
+ ld = [pos[3]['x'], pos[3]['y']]
1005
+
1006
+ x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
1007
+ x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
1008
+
1009
+ y1 = min( pos[0]['y'], pos[1]['y'] ) # 当前行的极上
1010
+ y2 = max( pos[2]['y'], pos[3]['y'] ) # 当前行的极下
1011
+
1012
+ #img_color = cv2.rectangle(img_color, (x1, y1), (x2, y2), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
1013
+ #img_color = cv2.rectangle(img_color, (lu[0], lu[1]), (rd[0], rd[1]), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
1014
+
1015
+ # cv2.imshow("green", img_color)
1016
+ # cv2.waitKey(0)
1017
+
1018
+ if match := re.compile('(^[\S]+)').search(word):
1019
+ g1 = match.group(1)
1020
+ # print( g1 )
1021
+
1022
+ array.append( { "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
1023
+ array2.append( { "line":word, "leftup":(x1, y1), "rightdown":(x2, y2) } )
1024
+
1025
+ pass
1026
+
1027
+
1028
+
1029
+ transcriptions = []
1030
+ last_x2 = 99999
1031
+ x_right = -1
1032
+ left = []
1033
+ right = []
1034
+ for idx, item in enumerate(array):
1035
+ leftup = item['leftup']
1036
+ rightdown = item['rightdown']
1037
+ x1 = leftup[0]
1038
+ x2 = rightdown[0]
1039
+
1040
+ if x_right == -1 and x1 > last_x2:
1041
+ # 第一次超过上一行的右边
1042
+ x_right = x1
1043
+
1044
+ if x_right != -1:
1045
+ right.append( item )
1046
+ else:
1047
+ left.append( item )
1048
+
1049
+ last_x2 = x2
1050
+
1051
+
1052
+ if len(left) > 5:
1053
+
1054
+ min_x1_left = 99999
1055
+ min_y1_left = 99999
1056
+
1057
+ max_x2_left = -1
1058
+ max_y2_left = -1
1059
+
1060
+ for item in left:
1061
+ leftup = item['leftup']
1062
+ rightdown = item['rightdown']
1063
+ x1 = leftup[0]
1064
+ x2 = rightdown[0]
1065
+
1066
+ y1 = leftup[1]
1067
+ y2 = rightdown[1]
1068
+
1069
+ if x1 < min_x1_left:
1070
+ min_x1_left = x1
1071
+
1072
+ if y1 < min_y1_left:
1073
+ min_y1_left = y1
1074
+
1075
+ if x2 > max_x2_left:
1076
+ max_x2_left = x2
1077
+
1078
+ if y2 > max_y2_left:
1079
+ max_y2_left = y2
1080
+
1081
+ if len(array2)> 0:
1082
+ for item in array2:
1083
+ if item['leftup'][1] < min_y1_left:
1084
+ min_y1_left = item['leftup'][1]
1085
+
1086
+ if item['rightdown'][1] > max_y2_left:
1087
+ max_y2_left = item['rightdown'][1]
1088
+
1089
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
1090
+
1091
+ # cv2.imshow("green", img_color)
1092
+ # cv2.waitKey(0)
1093
+
1094
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
1095
+
1096
+
1097
+ if len(right) > 5:
1098
+
1099
+ min_x1_left = 99999
1100
+ min_y1_left = 99999
1101
+
1102
+ max_x2_left = -1
1103
+ max_y2_left = -1
1104
+
1105
+ for item in right:
1106
+ leftup = item['leftup']
1107
+ rightdown = item['rightdown']
1108
+ x1 = leftup[0]
1109
+ x2 = rightdown[0]
1110
+
1111
+ y1 = leftup[1]
1112
+ y2 = rightdown[1]
1113
+
1114
+ if x1 < min_x1_left:
1115
+ min_x1_left = x1
1116
+
1117
+ if y1 < min_y1_left:
1118
+ min_y1_left = y1
1119
+
1120
+ if x2 > max_x2_left:
1121
+ max_x2_left = x2
1122
+
1123
+ if y2 > max_y2_left:
1124
+ max_y2_left = y2
1125
+
1126
+
1127
+ if len(array2)> 0:
1128
+ for item in array2:
1129
+ if item['leftup'][1] < min_y1_left:
1130
+ min_y1_left = item['leftup'][1]
1131
+
1132
+ if item['rightdown'][1] > max_y2_left:
1133
+ max_y2_left = item['rightdown'][1]
1134
+
1135
+ img_color = cv2.rectangle(img_color, (min_x1_left, min_y1_left), (max_x2_left, max_y2_left), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
1136
+
1137
+ # cv2.imshow("green", img_color)
1138
+ # cv2.waitKey(0)
1139
+
1140
+ transcriptions.append( {"transcription": "", "points":[ [min_x1_left, min_y1_left], [max_x2_left, min_y1_left], [max_x2_left, max_y2_left], [min_x1_left, max_y2_left] ]} )
1141
+
1142
+
1143
+ if len(transcriptions) > 0:
1144
+ Label_txt += jsonstring( transcriptions )
1145
+ Label_txt += '\n'
1146
+
1147
+ with open(pth_Label, 'w', encoding='utf-8') as f:
1148
+ f.write(Label_txt)
1149
+ # huggingface/PPOCRLabel/PPOCRLabel.py 用这个打开目录
1150
+ # ok 标注框是对的
1151
+
1152
+ cv2.imwrite(os.path.join(dir_pplabel, f'{m5}_auto_select.jpg'), img_color)
1153
+
1154
+ return transcriptions
1155
+
1156
+
1157
+ array_digitNum, array_digitZH, array_ZH, array_other = pos_info(jsn, img_color)
1158
+
1159
+ left_digitNum, right_digitNum = is_left_right(array_digitNum, orgWidth)
1160
+ left_digitZH, right_digitZH = is_left_right(array_digitZH, orgWidth)
1161
+
1162
+ if len(left_digitNum) > 0 and len(right_digitNum) > 0:
1163
+ """
1164
+ 左右分栏
1165
+ """
1166
+ # 找中间区域的左右 x 坐标 我
1167
+ min_left, max_right = middle_left_right(left_digitNum, right_digitNum)
1168
+ # 乘下的东西全部分成左右两边
1169
+ left, right = left_or_right( min_left, max_right, array_digitZH + array_ZH + array_other )
1170
+
1171
+ left_filted = [ item for item in left if len(item['line']) > 1 ]
1172
+ right_filted = [ item for item in right if len(item['line']) > 1 ]
1173
+
1174
+ transcriptions_left = get_selection(left_digitNum + left_filted, img_color)
1175
+ transcriptions_right = get_selection(right_digitNum + right_filted, img_color)
1176
+
1177
+ transcriptions = transcriptions_left + transcriptions_right
1178
+
1179
+ pass
1180
+ elif len(left_digitZH) > 0 and len(right_digitZH) > 0:
1181
+ # 左右分栏
1182
+ pass
1183
+ else:
1184
+ # 单栏
1185
+ transcriptions = get_selection(array_digitNum + array_digitZH + array_ZH + array_other, img_color)
1186
+
1187
+ # transcriptions = left_right(jsn, img_color, Label_txt, pth_Label)
1188
+ # if len(transcriptions) > 0:
1189
+ # return transcriptions
1190
+
1191
+ # transcriptions = left_noright(jsn, img_color, Label_txt, pth_Label)
1192
+ # if len(transcriptions) > 0:
1193
+ # return transcriptions
1194
+
1195
+ return transcriptions
1196
+
1197
+
1198
+ # @app.route('/img/autoselection', methods=['post'])
1199
+ # def autoselection():
1200
+ # global dic_cache
1201
+
1202
+ # # request.json 只能够接受方法为POST、Body为raw,header 内容为 application/json类型的数据
1203
+ # print(request.json, type(request.json))
1204
+
1205
+
1206
+ # dir = '/yingedu/project/ocr_server_test/data/'
1207
+
1208
+ # import sys
1209
+ # if sys.platform.startswith('win'):
1210
+ # dir = 'D:/workcode/nodejs/ocr/tools/data'
1211
+
1212
+ # m5 = request.json['m5']
1213
+
1214
+ # if m5 in dic_cache:
1215
+ # transcriptions = dic_cache[m5]
1216
+ # print( '########## hint cache.' )
1217
+ # else:
1218
+ # transcriptions = get_selectionsv2(dir, m5)
1219
+ # dic_cache[m5] = transcriptions
1220
+
1221
+ # # print( transcriptions )
1222
+
1223
+ # # ret = { "status": 200, "data":transcriptions }
1224
+
1225
+
1226
+ # return jsonify(transcriptions)
1227
+
1228
+ if __name__ == '__main__':
1229
+
1230
+
1231
+ dir = '/yingedu/project/ocr_server_test/data/'
1232
+ m5 = 'b0cd8663d03d596eab297a8250bf116c'
1233
+
1234
+ mds = [
1235
+ 'b0cd8663d03d596eab297a8250bf116c',
1236
+ '002a5d44e1771320fdcddb5df4e3cdb1',
1237
+ '3ce51feeb9a548e373fe72eb28df4a6b',
1238
+ '4b890c40319f8857be7a95705d8e61a3',
1239
+ 'b4ea47ca3af4c2862b75299fdaf103a8',
1240
+ 'd153c1c67a9502584c579ea7dd8f54e5',
1241
+ '9c80bd010a27cc410e77eea76170c829',
1242
+ 'ab35bfe44d130564efce70bf5f1152cd',
1243
+ '794629e25384df1b8b49936a7159166d',
1244
+ 'b89548a9c03b2de2d19c10e9173d9624',
1245
+ '756819dd53a98691859de016f7138d71',
1246
+ '6f63f59e6b826fa8a3284282cb503574',
1247
+ '5f232626c6d37267733c4383bea0550f',
1248
+ '5269bc6508598a8ee5dbd753ea353a3c',
1249
+ ]
1250
+
1251
+ # mds = [
1252
+ # '020bbac5534add85eb9d5bce4afedfb3',
1253
+ # ]
1254
+
1255
+ # if is_debug:
1256
+ # dir = 'D:/workcode/nodejs/ocr/tools/data'
1257
+
1258
+ # for m in mds:
1259
+
1260
+ # transcriptions = get_selectionsv2(dir, m)
1261
+ # print( transcriptions )
1262
+ # else:
1263
+ # app.run(host="0.0.0.0", port=2393, debug=True)