|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
def jsonparse(s):
|
|
|
return json.loads(s, strict=False)
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
|
print( f'Warnnig: no image {pth_img}' )
|
|
|
return []
|
|
|
|
|
|
if not os.path.exists(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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(img.shape) != 3:
|
|
|
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
|
|
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
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'] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
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'] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if match := re.compile('(^[\S]+)').search(word):
|
|
|
g1 = match.group(1)
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
|
print( f'Warnnig: no image {pth_img}' )
|
|
|
return []
|
|
|
|
|
|
if not os.path.exists(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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(img.shape) != 3:
|
|
|
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
|
|
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
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'] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(word) >= 10 and ( match := re.compile('(^\d+)\.[\S]').search(word) ):
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
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'] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
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'] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if match := re.compile('(^[\S]+)').search(word):
|
|
|
g1 = match.group(1)
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
"""
|
|
|
左右分栏
|
|
|
"""
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return transcriptions
|
|
|
|
|
|
|
|
|
@app.route('/img/autoselection', methods=['post'])
|
|
|
def autoselection():
|
|
|
global dic_cache
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|