Download fix_char_rectangle.py from dlxjj/imradv3: direct link, hf CLI and curl.
- Browser
- Download file 42.3 kB
-
https://huggingface.co/datasets/dlxjj/imradv3/resolve/main/fix_char_rectangle.py
- Command line
-
hf download hf://datasets/dlxjj/imradv3/fix_char_rectangle.py
-
curl -L -o fix_char_rectangle.py https://huggingface.co/datasets/dlxjj/imradv3/resolve/main/fix_char_rectangle.py
42.3 kB
| # see github\project\flask_auto_selection.py | |
| # 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 这个有问题 | |
| """ | |
| 根据字数对比平均宽度、腐蚀膨胀后填充区域对象, 等方法改进单个字符的坐标准确度 | |
| """ | |
| # huggingface\imradv3\src\WeChatOcrCpp\WeChatOcrResult.cpp | |
| # std::string OcrResult::ToJson(std::string& pth_img) const { | |
| # cpp 加工原始 wechatocr 结果,处理多字框(现在是让它们有相同坐标)。todo: 改成一个字一个框 | |
| 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) | |
| def rec_wechatocr(img_base64_str): | |
| import requests | |
| url = 'http://127.0.0.1:7788/wechatocr' | |
| headers = { | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "img": img_base64_str | |
| } | |
| response = requests.post(url, headers=headers, json=data) | |
| # 检查请求是否成功 | |
| if response.status_code == 200: | |
| data = response.text | |
| else: | |
| print(f"请求失败: {response.status_code} - {response.text}") | |
| return None | |
| return data | |
| if __name__ == '__main__': | |
| import base64 | |
| with open('data/0022.jpg', "rb") as f: | |
| img_bytes = f.read() | |
| img_buffer_numpy = np.frombuffer(img_bytes, dtype=np.uint8) | |
| img = cv2.imdecode(img_buffer_numpy, 1) | |
| base64_bytes = base64.b64encode(img) | |
| base64_str = base64_bytes.decode('ascii') | |
| jsn = rec_wechatocr(base64_str) | |
| if jsn is not None: | |
| jsn = jsonparse(jsn) | |
| else: | |
| raise Exception('something wrong.') | |
| # 连续相同的 x, y, w, h 的单字符坐标,给它修正为准确的单字坐标 | |
| 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) | |