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 search_photo(): nowTime = datetime.datetime.now() file = request.files['file'] filename = file.filename dst = os.path.dirname(__file__) + '/tmp/' + str(nowTime) + '.jpg' file.save(dst) content = text_predict(dst) os.remove(dst) return content
def idCardOCR(): 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 = idcard.idcard(result) res = res.res res = [{'text': res[key], 'name':key} for key in res] return {"result": res}
def ocr(): 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 = [ { '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) ] return {"result": res}
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) # 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 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