def post(self, **kwargs): parser = reqparse.RequestParser() parser.add_argument('ImgString', type=str, required=True, location='form') args = parser.parse_args() rst = None imgString = args['ImgString'].encode().split(b';base64,')[-1] try: b64_image = base64_to_PIL(imgString) if b64_image is not None: b64_image = np.array(b64_image) result = text_predict(b64_image) text = ' '.join([i['text'] for i in result]) rst = { 'code': "success", 'text': text } except ValueError: rst = { "code": "FAILURE", "errMsg": "ocr访问出错", "id": "1501" } return rst
def post(self, **kwargs): parser = reqparse.RequestParser() parser.add_argument('imgString', type=str, location='form') args = parser.parse_args() result = None t = time.time() uidJob = uuid.uuid1().__str__() imgString = args['imgString'].encode().split(b';base64,')[-1] b64_image = base64_to_PIL(imgString) if b64_image: b64_image = np.array(b64_image) # 避免识别过程中重复上传识别 while time.time() - t <= TIMEOUT: if os.path.exists(filelock): continue else: with open(filelock, 'w') as f: f.write(uidJob) # 图片识别 result = text_predict(b64_image) os.remove(filelock) break return jsonify({'text': [i['text'] for i in result]})
def ocr_post(): data = request.json 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) res = run_ocr(img, billModel, textLine) return json.dumps({'res': res}, ensure_ascii=False)
def get_job(self, callback): ##获取队列中的图像 ind = self.get_list(jobListTable) if ind is not None: value = self.get_dict(imgStringTable, [ind]) value = value[0] if value is not None: value = base64_to_PIL(value) if value is not None: value = value.convert("L") value = callback(value) else: value = "" print(ind, value) self.set_dict(resSetTable, ind, value) ##存储识别结果到set
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): 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 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 })
def POST(self): data = web.data() data = json.loads(data) CommandID = data.get('commandID', '') BusinessID = data.get('businessID','') SessionID = data.get('sessionID','') # 下面三行兼容原有的web app demo billModel = data.get('billModel','') ## 确定具体使用哪种模式识别 textAngle = data.get('textAngle', False) ## 文字方向检测 textLine = data.get('textLine', False) ## 只进行单行识别 # 处理传递参数 if CommandID != '': # self.logger.info('post request from JiuTian IP= %s ,CommandID=%s' % (web.ctx.get('ip'), CommandID)) if CommandID == '100001': billModel = 'invoice' elif CommandID == '200001': billModel = 'idcard' elif CommandID == '300001': billModel = 'bankcard' elif CommandID == '400001': billModel = 'licenseplate' else: ## 返回请求参数错误 return json.dumps( {'sessionID': SessionID, 'commandID': CommandID, 'businessID': BusinessID, 'timeStamp': time.strftime('%Y%m%d%H%M%S', time.localtime()), 'execStatus': {"statusCode": 0x800003, "statusDescription": "请求参数错误"}, 'resultInfo': {}}, ensure_ascii=False ) picName = data.get('picName', 'new.jpg') picpath = 'http://172.31.201.35:18081' + data.get('picUrl', '') + picName response = requests.get(picpath, stream=True) ## 处理可能出现的视频(只可能出现在‘licenseplate’中) if picName.endswith(('.jpg', '.png', '.jpeg', '.JPG','.JPEG','.PNG')): img = Image.open(BytesIO(response.content)).convert('RGB') elif picName.endswith(('.mp4','.MP4','.avi','.AVI')) and billModel == 'licenseplate': with open(picName, 'wb+') as f: f.write(response.content) saveName = picName.split('.')[0]+'_new.mp4' result = model_lp.model_video(picName,saveName) res = {'carNo': list(result), 'picUrl': '', 'picName': ''} upload_url = 'http://172.31.201.35:18081' + '/cmcc-ocr-webapi-1.0/service/remoteUploadPic/' files = {'image': (saveName, open(saveName, 'rb'), 'image/jpeg', {})} reply = requests.post(upload_url, files=files) # get the picUrl and picName reply = reply.json() # print(reply) res['picUrl'] = reply['picUrl'] res['picName'] = reply['picName'] # delete tmp files # os.remove(picName) #os.remove(saveName) return json.dumps({'sessionID': SessionID, 'commandID': CommandID, 'businessID': BusinessID, 'timeStamp': time.strftime('%Y%m%d%H%M%S', time.localtime()), 'execStatus': {"statusCode": 0x000000, "statusDescription": "成功"}, 'resultInfo': res}, ensure_ascii=False) else: ## 返回请求参数错误 return json.dumps( {'sessionID': SessionID, 'commandID': CommandID, 'businessID': BusinessID, 'timeStamp': time.strftime('%Y%m%d%H%M%S', time.localtime()), 'execStatus': {"statusCode": 0x800004, "statusDescription": "内部数据错误"}, 'resultInfo': {}}, ensure_ascii=False ) else: ## 兼容原有的web app demo 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] timeTake = time.time() if textLine: ##单行识别 partImg = Image.fromarray(img) # text = model.crnnOcr(partImg.convert('L')) text = crnn.predict(partImg.convert('L')) res = [{'text': text, 'name': '0', 'box': [0, 0, W, 0, W, H, 0, H]}] else: if billModel == 'licenseplate': img = Image.fromarray(img) img, result = model_lp.model_MTCNN(img) res = self.format_text(result, img, 0, billModel, CommandID) else: detectAngle = textAngle result, angle = model.model(img, scale=scale, maxScale=maxScale, detectAngle=detectAngle, ##是否进行文字方向检测,通过web传参控制 MAX_HORIZONTAL_GAP=30, ##字符之间的最大间隔,用于文本行的合并 MIN_V_OVERLAPS=0.6, MIN_SIZE_SIM=0.6, TEXT_PROPOSALS_MIN_SCORE=0.07, TEXT_PROPOSALS_NMS_THRESH=0.3, TEXT_LINE_NMS_THRESH=0.99, ##文本行之间测iou值 LINE_MIN_SCORE=0.07, leftAdjustAlph=0.02, ##对检测的文本行进行向左延伸 rightAdjustAlph=0.02, ##对检测的文本行进行向右延伸 ) res = self.format_text(result, img, angle, billModel, CommandID) timeTake = time.time() - timeTake ## 输出,同样区分是否是原有的web app demo接口 if CommandID == '': # os.remove(path) # print(res) # outpic = self.plot_boxes(img, angle, result, color=(0, 0, 0)) # outpic.save('new.jpg') return json.dumps({'res': res, 'timeTake': round(timeTake, 4)}, ensure_ascii=False) else: if timeTake > 15: return json.dumps( {'sessionID': SessionID, 'commandID': CommandID, 'businessID': BusinessID, 'timeStamp': time.strftime('%Y%m%d%H%M%S', time.localtime()), 'execStatus': {"statusCode": 0x800001, "statusDescription": "响应超时"}, 'resultInfo': {}}, ensure_ascii=False ) # save and upload the box pic if billModel == 'licenseplate': outpic = Image.fromarray(img) else: outpic = self.plot_boxes(img, angle, result, color=(0, 0, 0)) outpic.save(picName) upload_url = 'http://172.31.201.35:18081' + '/cmcc-ocr-webapi-1.0/service/remoteUploadPic/' files = {'image': (picName, open(picName, 'rb'), 'image/jpeg', {})} reply = requests.post(upload_url, files=files) # get the picUrl and picName reply = reply.json() # print(reply) res['picUrl'] = reply['picUrl'] res['picName'] = reply['picName'] # delete tmp files os.remove(picName) return json.dumps({'sessionID': SessionID, 'commandID': CommandID, 'businessID': BusinessID, 'timeStamp': time.strftime('%Y%m%d%H%M%S', time.localtime()), 'execStatus': {"statusCode": 0x000000, "statusDescription": "成功"}, 'resultInfo': res}, ensure_ascii=False)