def train_ticket(filepath): scale, maxScale = IMGSIZE[0], 2048 from run.text.keras_detect import text_detect from run.main import TextOcrModel angle_detect = None model = TextOcrModel(ocr, text_detect, angle_detect) img = cv.imread(filepath) result, angle = model.model( img, scale=scale, maxScale=maxScale, detectAngle=False, ##是否进行文字方向检测 MAX_HORIZONTAL_GAP=100, ##字符之间的最大间隔 MIN_V_OVERLAPS=0.6, MIN_SIZE_SIM=0.6, TEXT_PROPOSALS_MIN_SCORE=0.1, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH=0.99, ##iou值 LINE_MIN_SCORE=0.1, leftAdjustAlph=0.01, ## rightAdjustAlph=0.01, ## ) res = trainTicket.trainTicket(result) res = res.res res = [{'text': res[key], 'name': key, 'box': {}} for key in res] return res
def ocr_train_ticket(path, detect_angle=False): img = cv2.imread(path) # GBR H, W = img.shape[:2] time_take = time.time() _, result, angle = model.model( img, detectAngle=detect_angle, # 是否进行文字方向检测,通过web传参控制 config=dict( MAX_HORIZONTAL_GAP=50, # 字符之间的最大间隔,用于文本行的合并 MIN_V_OVERLAPS=0.6, MIN_SIZE_SIM=0.6, TEXT_PROPOSALS_MIN_SCORE=0.1, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH=0.7), # 文本行之间测iou值 leftAdjust=True, # 对检测的文本行进行向左延伸 rightAdjust=True, # 对检测的文本行进行向右延伸 alpha=0.01) # 对检测的文本行进行向右、左延伸的倍数 # print('[ocr_train_ticket] result', result) # print('[ocr_train_ticket] angle', angle) res = trainTicket.trainTicket(result) res = res.res res = [{'text': res[key], 'name': key, 'box': {}} for key in res] print('[ocr_train_ticket] res', res) time_take = time.time() - time_take print('[ocr_train_ticket] time_take', time_take)
def POST(self): data = web.data() data = json.loads(data) billModel = data.get('billModel', '') imgString = data['imgString'].encode().split(b';base64,')[-1] imgString = base64.b64decode(imgString) jobid = uuid.uuid1().__str__() path = '/tmp/{}.jpg'.format(jobid) with open(path, 'wb') as f: f.write(imgString) img = Image.open(path).convert("RGB") W, H = img.size timeTake = time.time() _, result, angle = model.model( img, detectAngle=DETECTANGLE, ##是否进行文字方向检测 config=dict( MAX_HORIZONTAL_GAP=100, ##字符之间的最大间隔,用于文本行的合并 MIN_V_OVERLAPS=0.7, MIN_SIZE_SIM=0.7, TEXT_PROPOSALS_MIN_SCORE=0.1, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH=0.99, ##文本行之间测iou值 MIN_RATIO=1.0, LINE_MIN_SCORE=0.2, TEXT_PROPOSALS_WIDTH=0, MIN_NUM_PROPOSALS=0, ), leftAdjust=True, ##对检测的文本行进行向左延伸 rightAdjust=True, ##对检测的文本行进行向右延伸 alph=0.2, ##对检测的文本行进行向右、左延伸的倍数 ifadjustDegree=False ##是否先小角度调整文字倾斜角度 ) if billModel == '' or billModel == '通用OCR': result = union_rbox(result, 0.2) res = [{ 'text': x['text'], 'name': str(i) } for i, x in enumerate(result)] elif billModel == '火车票': res = trainTicket.trainTicket(result) res = res.res res = [{'text': res[key], 'name': key} for key in res] elif billModel == '身份证': res = idcard.idcard(result) res = res.res res = [{'text': res[key], 'name': key} for key in res] timeTake = time.time() - timeTake os.remove(path) return json.dumps({ 'res': res, 'timeTake': round(timeTake, 4) }, ensure_ascii=False)
def format_text(self, textbox, img, angle, billModel='general_OCR', CommandID= ''): """ 格式化各种图片提取的文本 :param textbox: 提取的文本框(包括坐标和文本内容) :param img: 原图 :param angle: 原图需要旋转的角度 :param billModel: 图片类型,方便格式化 :param CommandID: 判断来自网页的请求(文本展示),还是返回给服务器的请求 :return: res: json格式的格式化结果 """ if billModel == '' or billModel == 'general_OCR': result = union_rbox(textbox, 0.2) res = [{'text': x['text'], 'name': str(i), 'box': {'cx': x['cx'], 'cy': x['cy'], 'w': x['w'], 'h': x['h'], 'angle': x['degree'] } } for i, x in enumerate(result)] res = adjust_box_to_origin(img, angle, res) ##修正box elif billModel == 'trainticket': res = trainTicket.trainTicket(textbox) res = res.res if CommandID != '': res = {key: res[key] for key in res} else: res = [{'text': res[key], 'name': key, 'box': {}} for key in res] elif billModel == 'idcard': res = idcard.idcard(textbox) res = res.res if CommandID != '': res = {key: res[key] for key in res} else: res = [{'text': res[key], 'name': key, 'box': {}} for key in res] elif billModel == 'invoice': res = invoice.invoice(textbox) res = res.res if CommandID != '': res = {key: res[key] for key in res} else: res = [{'text': res[key], 'name': key, 'box': {}} for key in res] elif billModel == 'bankcard': res = bankcard.bankcard(textbox) res = res.res if CommandID != '': res = {key: res[key] for key in res} else: res = [{'text': res[key], 'name': key, 'box': {}} for key in res] elif billModel == 'licenseplate': if CommandID != '': res = {'carNo': list(textbox), 'picUrl': '', 'picName': ''} else: res = [{'text': text, 'name': 'carNo', 'box': {}} for text in list(textbox)] return res
def result(img): back = {} img = Image.open(BytesIO(img)).convert('RGB') img = np.array(img) result = text_predict(img) back['文本'] = list(map(lambda x: x['text'], result)) res = trainTicket.trainTicket(result) back['火车票'] = str(res) res = idcard.idcard(result) back['身份证'] = str(res) return back
def trainTicketOCR(): if 'image' not in request.files: return {"error": "'image' not found"} img = Image.open(request.files["image"]).convert("RGB") img = np.array(img) result = text_predict(img) res = trainTicket.trainTicket(result) res = res.res res = [{'text': res[key], 'name':key} for key in res] return {"result": res}
def api_root(): app.logger.info(PROJECT_HOME) if request.method == 'POST' and request.files['image']: starttime = time.time() img = request.files['image'] img_cv = cv2.imdecode(np.fromstring(img.read(), np.uint8), cv2.IMREAD_COLOR) boxes, rec_res = text_sys(img_cv) for box in boxes: xy_sum = np.sum(box, axis=0) / 4.0 cx = xy_sum[0] cy = xy_sum[1] degree = np.arcsin( (box[1][1] - box[0][1]) / (box[1][0] - box[0][0])) w = abs(box[0][0] - box[1][0]) h = abs(box[0][1] - box[3][1]) x1, y1, x2, y2, x3, y3, x4, y4 = xy_rotate_box( cx, cy, w, h, degree / 180 * np.pi) box[0][0] = x1 box[0][1] = y1 box[1][0] = x2 box[1][1] = y2 box[2][0] = x3 box[2][1] = y3 box[3][0] = x4 box[3][1] = y4 assorted_results = [{ 'box': boxes[i], 'txt': rec_res[i][0] } for i in range(len(rec_res))] res = trainTicket.trainTicket(assorted_results, img=img_cv) res = res.res elapse = time.time() - starttime app.logger.info("Predict time : %.3fs" % elapse) return json.dumps(res, ensure_ascii=False) else: return "Where is the image?"
def main(): billModel = '通用OCR' path = 'test/t3.jpg' img = Image.open(path).convert("RGB") W,H = img.size timeTake = time.time() _,result,angle= model.model(img, detectAngle=DETECTANGLE,##是否进行文字方向检测 config=dict(MAX_HORIZONTAL_GAP=100,##字符之间的最大间隔,用于文本行的合并 MIN_V_OVERLAPS=0.7, MIN_SIZE_SIM=0.7, TEXT_PROPOSALS_MIN_SCORE=0.1, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH = 0.99,##文本行之间测iou值 MIN_RATIO=1.0, LINE_MIN_SCORE=0.2, TEXT_PROPOSALS_WIDTH=0, MIN_NUM_PROPOSALS=0, ), leftAdjust=True,##对检测的文本行进行向左延伸 rightAdjust=True,##对检测的文本行进行向右延伸 alph=0.2,##对检测的文本行进行向右、左延伸的倍数 ifadjustDegree=False##是否先小角度调整文字倾斜角度 ) if billModel=='' or billModel=='通用OCR': result = union_rbox(result,0.2) res = [{'text':x['text'],'name':str(i)} for i,x in enumerate(result)] elif billModel=='火车票': res = trainTicket.trainTicket(result) res = res.res res =[ {'text':res[key],'name':key} for key in res] elif billModel=='身份证': res = idcard.idcard(result) res = res.res res =[ {'text':res[key],'name':key} for key in res] print(result)
def POST(self): t = time.time() data = web.data() uidJob = uuid.uuid1().__str__() data = json.loads(data) # print(data) billModel = data.get('billModel','') # textAngle = data.get('textAngle',False)##文字检测 textLine = data.get('textLine',False)##只进行单行识别 imgString = data['imgString'].encode().split(b';base64,')[-1] img = base64_to_PIL(imgString) if img is not None: img = np.array(img) H,W = img.shape[:2] while time.time()-t<=TIMEOUT: if os.path.exists(filelock): continue else: with open(filelock,'w') as f: f.write(uidJob) if textLine: ##单行识别 partImg = Image.fromarray(img) text = crnn_handle.predict(partImg) res =[ {'text':text,'name':'0','box':[0,0,W,0,W,H,0,H]} ] os.remove(filelock) break else: # detectAngle = textAngle result= text_predict(img) if billModel=='' or billModel=='通用OCR' : # result = union_rbox(result,0.2) res = [{'text':x['text'], 'name':str(i), 'box':{'cx':x['cx'], 'cy':x['cy'], 'w':x['w'], 'h':x['h'], 'angle':x['degree'] } } for i,x in enumerate(result)] # res = adjust_box_to_origin(img,angle, res)##修正box elif billModel=='火车票': res = trainTicket.trainTicket(result) res = res.res res =[ {'text':res[key],'name':key,'box':{}} for key in res] elif billModel=='身份证': res = idcard.idcard(result) res = res.res res =[ {'text':res[key],'name':key,'box':{}} for key in res] os.remove(filelock) break timeTake = time.time()-t return json.dumps({'res':res,'timeTake':round(timeTake,4)},ensure_ascii=False)
def result(img): back = {} img = Image.open(BytesIO(img)).convert('RGB') img = np.array(img) result = text_predict(img) back['文本'] = list(map(lambda x: x['text'], result)) res = trainTicket.trainTicket(result) back['火车票'] = str(res) res = idcard.idcard(result) back['身份证'] = str(res) return back if __name__ == '__main__': img = './test/idcard-demo.jpg' img = Image.open(img).convert('RGB') # img.show() img = np.array(img) text = text_predict(img) print('文本预测:', list(map(lambda x: x['text'], text))) # 火车票 res = trainTicket.trainTicket(text) print('火车票预测:', res) # 身份证 res = idcard.idcard(text) print('身份证预测:', res)
def POST(self): t = time.time() data = web.data() uidJob = uuid.uuid1().__str__() data = json.loads(data) billModel = data.get('billModel', '') textAngle = data.get('textAngle', False) ##文字检测 textLine = data.get('textLine', False) ##只进行单行识别 imgString = data['imgString'].encode().split(b';base64,')[-1] img = base64_to_PIL(imgString) if img is not None: img = np.array(img) H, W = img.shape[:2] while time.time() - t <= TIMEOUT: if os.path.exists(filelock): continue else: with open(filelock, 'w') as f: f.write(uidJob) if textLine: ##单行识别 partImg = Image.fromarray(img) text = crnn.predict(partImg.convert('L')) res = [{ 'text': text, 'name': '0', 'box': [0, 0, W, 0, W, H, 0, H] }] os.remove(filelock) break else: detectAngle = textAngle result, angle = model.model( img, scale=scale, maxScale=maxScale, detectAngle=detectAngle, ##是否进行文字方向检测,通过web传参控制 MAX_HORIZONTAL_GAP=100, ##字符之间的最大间隔,用于文本行的合并 MIN_V_OVERLAPS=0.6, MIN_SIZE_SIM=0.6, TEXT_PROPOSALS_MIN_SCORE=0.1, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH=0.99, ##文本行之间测iou值 LINE_MIN_SCORE=0.1, leftAdjustAlph=0.01, ##对检测的文本行进行向左延伸 rightAdjustAlph=0.01, ##对检测的文本行进行向右延伸 ) if billModel == '' or billModel == '通用OCR': result = union_rbox(result, 0.2) res = [{ 'text': x['text'], 'name': str(i), 'box': { 'cx': x['cx'], 'cy': x['cy'], 'w': x['w'], 'h': x['h'], 'angle': x['degree'] } } for i, x in enumerate(result)] res = adjust_box_to_origin(img, angle, res) ##修正box elif billModel == '火车票': res = trainTicket.trainTicket(result) res = res.res res = [{ 'text': res[key], 'name': key, 'box': {} } for key in res] elif billModel == '身份证': res = idcard.idcard(result) res = res.res res = [{ 'text': res[key], 'name': key, 'box': {} } for key in res] os.remove(filelock) break timeTake = time.time() - t return json.dumps({ 'res': res, 'timeTake': round(timeTake, 4) }, ensure_ascii=False)
def POST(self): t = time.time() data = web.data() uidJob = uuid.uuid1().__str__() data = json.loads(data) # print(data) billModel = data.get("billModel", "") # textAngle = data.get('textAngle',False)##文字检测 textLine = data.get("textLine", False) ##只进行单行识别 imgString = data["imgString"].encode().split(b";base64,")[-1] img = base64_to_PIL(imgString) if img is not None: img = np.array(img) H, W = img.shape[:2] while time.time() - t <= TIMEOUT: if os.path.exists(filelock): continue else: with open(filelock, "w") as f: f.write(uidJob) if textLine: ##单行识别 partImg = Image.fromarray(img) text = crnn_handle.predict(partImg) res = [{ "text": text, "name": "0", "box": [0, 0, W, 0, W, H, 0, H] }] os.remove(filelock) break else: # detectAngle = textAngle result = text_predict(img) if billModel == "" or billModel == "通用OCR": # result = union_rbox(result,0.2) res = [{ "text": x["text"], "name": str(i), "box": { "cx": x["cx"], "cy": x["cy"], "w": x["w"], "h": x["h"], "angle": x["degree"], }, } for i, x in enumerate(result)] # res = adjust_box_to_origin(img,angle, res)##修正box elif billModel == "火车票": res = trainTicket.trainTicket(result) res = res.res res = [{ "text": res[key], "name": key, "box": {} } for key in res] elif billModel == "身份证": res = idcard.idcard(result) res = res.res res = [{ "text": res[key], "name": key, "box": {} } for key in res] os.remove(filelock) break timeTake = time.time() - t return json.dumps({ "res": res, "timeTake": round(timeTake, 4) }, ensure_ascii=False)
def POST(self): data = web.data() data = json.loads(data) billModel = data.get('billModel', '') textAngle = data.get('textAngle', False) # 文字检测 textLine = data.get('textLine', False) # 只进行单行识别 imgString = data['imgString'].encode().split(b';base64,')[-1] imgString = base64.b64decode(imgString) jobid = uuid.uuid1().__str__() path = 'test/{}.jpg'.format(jobid) with open(path, 'wb') as f: f.write(imgString) img = cv2.imread(path) # GBR H, W = img.shape[:2] timeTake = time.time() if textLine: # 单行识别 partImg = Image.fromarray(img) text = model.crnnOcr(partImg.convert('L')) res = [{ 'text': text, 'name': '0', 'box': [0, 0, W, 0, W, H, 0, H] }] else: detectAngle = textAngle # 是否进行文字方向检测 _, result, angle = model.model( img, file_name="test.jpg", detectAngle=detectAngle, # 是否进行文字方向检测,通过web传参控制 config=dict( MAX_HORIZONTAL_GAP=50, # 字符之间的最大间隔,用于文本行的合并 MIN_V_OVERLAPS=0.6, MIN_SIZE_SIM=0.6, TEXT_PROPOSALS_MIN_SCORE=0.1, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH=0.7), # 文本行之间测iou值 leftAdjust=True, # 对检测的文本行进行向左延伸 rightAdjust=True, # 对检测的文本行进行向右延伸 alpha=0.01) # 对检测的文本行进行向右、左延伸的倍数 print('[POST] result', result) print('[POST] angle', angle) if billModel == '' or billModel == '通用OCR': result = union_rbox(result, 0.2) res = [{ 'text': x['text'], 'name': str(i), 'box': { 'cx': x['cx'], 'cy': x['cy'], 'w': x['w'], 'h': x['h'], 'angle': x['degree'] } } for i, x in enumerate(result)] res = adjust_box_to_origin(img, angle, res) # 修正box elif billModel == '火车票': res = trainTicket.trainTicket(result) res = res.res res = [{ 'text': res[key], 'name': key, 'box': {} } for key in res] elif billModel == '身份证': res = idcard.idcard(result) res = res.res res = [{ 'text': res[key], 'name': key, 'box': {} } for key in res] timeTake = time.time() - timeTake os.remove(path) return json.dumps({ 'res': res, 'timeTake': round(timeTake, 4) }, ensure_ascii=False)
def ocr(): if request.method == "POST": request_start_time = time.time() uid_job = uuid.uuid1().__str__() data = request.json # 模型参数 bill_model = data.get("billModel", "") # 文字检测 # text_angle = data.get('textAngle', False) # 只进行单行识别 text_line = data.get("textLine", False) img_str = data["imgString"].encode().split(b";base64,")[-1] img = base64_to_PIL(img_str) if img is None: response_time = time.time() - request_start_time return jsonify({"res": [], "timeTake": round(response_time, 4)}) else: img = np.array(img) h, w = img.shape[:2] final_result: list = [] while time.time() - request_start_time <= TIMEOUT: if os.path.exists(file_lock): continue else: with open(file_lock, "w") as f: f.write(uid_job) if text_line: # 单行识别 part_img = Image.fromarray(img) text = crnn_handle.predict(part_img) final_result = [{ "text": text, "name": "0", "box": [0, 0, w, 0, w, h, 0, h] }] os.remove(file_lock) break else: result = text_predict(img) if bill_model == "" or bill_model == "通用OCR": final_result = [{ "text": x["text"], "name": str(i), "box": { "cx": x["cx"], "cy": x["cy"], "w": x["w"], "h": x["h"], "angle": x["degree"], }, } for i, x in enumerate(result)] elif bill_model == "火车票": train_ticket_result = trainTicket.trainTicket( result) result = train_ticket_result.res final_result = [{ "text": result[key], "name": key, "box": {} } for key in result] elif bill_model == "身份证": id_card_result = idcard.idcard(result) result = id_card_result.res final_result = [{ "text": result[key], "name": key, "box": {} } for key in result] os.remove(file_lock) break response_time = time.time() - request_start_time return jsonify({ "res": final_result, "timeTake": round(response_time, 4) })
def upload(): t = time.time() parser = reqparse.RequestParser() parser.add_argument('file') args = parser.parse_args() # data = request.json uidJob = uuid.uuid1().__str__() file = args['file'] # data = json.loads(data) billModel = '通用OCR' textAngle = True # 文字检测 imgString = file.encode().split(b';base64,')[-1] img = base64_to_PIL(imgString) if img is not None: img = np.array(img) else: return 0 H, W = img.shape[:2] while time.time() - t <= TIMEOUT: if os.path.exists(filelock): continue else: with open(filelock, 'w') as f: f.write(uidJob) detectAngle = textAngle result, angle = model.model(img, scale=scale, maxScale=maxScale, detectAngle=detectAngle, # 是否进行文字方向检测,通过web传参控制 MAX_HORIZONTAL_GAP=100, # 字符之间的最大间隔,用于文本行的合并 MIN_V_OVERLAPS=0.6, MIN_SIZE_SIM=0.6, TEXT_PROPOSALS_MIN_SCORE=0.1, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH=0.99, # 文本行之间测iou值 LINE_MIN_SCORE=0.1, leftAdjustAlph=0.01, # 对检测的文本行进行向左延伸 rightAdjustAlph=0.01, # 对检测的文本行进行向右延伸 ) if billModel == '' or billModel == '通用OCR': result = union_rbox(result, 0.2) res = {'name': '', 'position': '', 'company': '', 'local': '', 'email': '', 'phone': '', 'other': ''} # 对公司进行提取 companyKeyword = ['公司', 'company', '银行', 'Bank', '集团', 'Group', '商行', 'Factory', 'CO', 'LTD'] for i, x in enumerate(result): for ck in companyKeyword: if ck in x['text']: res['company'] = x['text'] result.pop(i) break if res['company']: break name = '' name_height = 0 pop_data = 0 # 对姓名进行处理 for i, x in enumerate(result): if 'name' in x['text'] or '姓名' in x['text']: res['name'] = x['text'].replace('name', '') res['name'] = res['name'].replace('姓名', '') res['name'] = res['name'].replace(':', '') result.pop(i) break else: # 正则取中文,进行分别进行中文名和英文名的判断 text_list = re.findall(r'[\u4e00-\u9fa5]', x['text']) if text_list: if name_height < x['h'] and 1 < len(text_list) < 5: name_height = x['h'] name = x['text'] pop_data = i else: strUpper = x['text'].upper() if name_height < x['h'] and strUpper != x['text']: name_height = x['h'] name = x['text'] pop_data = i res['name'] = name result.pop(pop_data) # 对职位进行提取 pos = ['GM', 'VP', 'HRD', 'OD', 'MD', 'OM', 'PM', 'BM', 'DM', 'RM', 'AAD', 'ACD', 'AD', 'AE', 'AP', 'ASM', 'VC', 'CWO', 'COO', 'CXO', 'CYO', 'CZO', 'PS', 'Manager', 'Engineer'] posNum = "".join(filter(str.isdigit, x['text'])) for i, x in enumerate(result[pop_data:pop_data + 3]): if '职位' in x['text'] or 'position' in x['text']: res['position'] = x['text'].replace('职位', '') res['position'] = res['position'].replace('position', '') res['position'] = res['position'].replace(':', '') result.pop(i) break if not res['position']: for i, x in enumerate(result[pop_data:pop_data + 3]): for ch in x['text']: if u'\u4e00' <= ch <= u'\u9fff' and len(posNum) == 0: res['position'] = x['text'] result.pop(i) break if res['position']: break for p in pos: if p in x['text']: res['position'] = x['text'] result.pop(i) break if res['position']: break # 对邮箱进行提取 for i, x in enumerate(result): if '@' in x['text'] or '邮箱' in x['text'] or 'email' in x['text']: res['email'] = x['text'].replace('邮箱', '') res['email'] = res['email'].replace('email', '') res['email'] = res['email'].replace(':', '') result.pop(i) break # 对地址进行提取 localKeyword = ['市', '省', '区', '号', '路', '岛', '地址', 'sheng', 'shi', 'qu', 'hao', 'lu'] for i, x in enumerate(result): for k in localKeyword: if k in x['text'] or 'Add' in x['text'] or 'add' in x['text']: res['local'] = x['text'].replace('地址', '') res['local'] = res['local'].replace('Add', '') res['local'] = res['local'].replace(':', '') result.pop(i) break if res['local']: break # 对手机号码进行提取 for i, x in enumerate(result): if '-' not in x['text'] and '-' not in x['text']: telephone = "".join(filter(str.isdigit, x['text'])) if 11 <= len(telephone) <= 13: res['phone'] += x['text'].replace('电话', '') + ';' res['phone'] = res['phone'].replace('Tel', '') res['phone'] = res['phone'].replace('手机', '') res['phone'] = res['phone'].replace(':', '') result.pop(i) # 其他文本提取 for i, x in enumerate(result): res['other'] += x['text'] + ';' elif billModel == '火车票': res = trainTicket.trainTicket(result) res = res.res res = [{'text': res[key], 'name': key, 'box': {}} for key in res] elif billModel == '身份证': res = idcard.idcard(result) res = res.res res = [{'text': res[key], 'name': key, 'box': {}} for key in res] os.remove(filelock) break timeTake = time.time() - t return jsonify({ "code": "0000", "msg": "成功", "data": res })
import os os.environ['KMP_DUPLICATE_LIB_OK']='True' import easyocr from application import trainTicket reader_ch = easyocr.Reader(['en', 'ch_tra']) # need to run only once to load model into memory reader_en = easyocr.Reader(['en']) result_en = reader_en.readtext('D:/dataset/invoice/7.jpg') result_ch = reader_ch.readtext('D:/dataset/invoice/7.jpg') assorted_results_ch = [{'box': bbox, 'txt': txt} for bbox, txt, _ in result_ch] assorted_results_en = [{'box': bbox, 'txt': txt} for bbox, txt, _ in result_en] res1 = trainTicket.trainTicket(assorted_results_ch) res2 = trainTicket.trainTicket(assorted_results_en) print(res1) print(res2)
def run_ocr(img, billModel, textLine): t = time.time() H, W = img.shape[:2] res = '' uidJob = uuid.uuid1().__str__() while time.time() - t <= TIMEOUT: if os.path.exists(filelock): continue else: with open(filelock, 'w') as f: f.write(uidJob) if textLine: ##单行识别 partImg = Image.fromarray(img) text = crnn_handle.predict(partImg) res = [{ 'text': text, 'name': '0', 'box': [0, 0, W, 0, W, H, 0, H] }] os.remove(filelock) break else: # detectAngle = textAngle result = text_predict(img) if billModel == '' or billModel == '通用OCR': # result = union_rbox(result,0.2) res = [{ 'text': x['text'], 'name': str(i), 'box': { 'cx': x['cx'], 'cy': x['cy'], 'w': x['w'], 'h': x['h'], 'angle': x['degree'] } } for i, x in enumerate(result)] # res = adjust_box_to_origin(img,angle, res)##修正box elif billModel == '火车票': res = trainTicket.trainTicket(result) res = res.res res = [{ 'text': res[key], 'name': key, 'box': {} } for key in res] elif billModel == '身份证': res = idcard.idcard(result) res = res.res res = [{ 'text': res[key], 'name': key, 'box': {} } for key in res] os.remove(filelock) break return res