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