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
nclass, 256, leakyRelu=False, lstmFlag=LSTMFLAG, GPU=GPU, alphabet=alphabet) if os.path.exists(ocrModel): crnn.load_weights(ocrModel) else: print("download model or tranform model with tools!") ocr = crnn.predict_job from main import TextOcrModel model = TextOcrModel(ocr, text_detect, angle_detect) billList = ['通用OCR', '火车票', '身份证'] class OCR: """通用OCR识别""" def GET(self): post = {} post['postName'] = 'ocr' ##请求地址 post['height'] = 1000 post['H'] = 1000 post['width'] = 600 post['W'] = 600 post['billList'] = billList return render.ocr(post)
def currency_invoice(img, arg, arg2, model_flg): from main import TextOcrModel angle_detect = None model = TextOcrModel(ocr, text_detect, angle_detect) # print(arg[12],'---------------------',type(arg[12])) if model_flg == True: model = TextOcrModel(ocr, text_detect, angle_detect) rotate_img = color_filter(img, color_thre=255, mode='less') img = color_filter(rotate_img, color_thre=50, mode='more') if lab == '-1': result, angle, im_show = model.model_CRAFT( img, scale=arg[0], detectAngle=False, ##是否进行文字方向检测 MAX_HORIZONTAL_GAP=arg[2], ##字符之间的最大间隔 #30 MIN_V_OVERLAPS=arg[3], #0.5 TEXT_PROPOSALS_MIN_SCORE=arg[4], #[0.5, 0.2, 0.7], leftAdjustAlph=arg[6], ## rightAdjustAlph=arg[7], ## Adjustbox=arg[5], ## pixel_filter=arg[8], ## batch_by_1=arg[9], scoremap_enhance_pixel=arg[10], ) elif lab == '0': result, angle, im_show = model.model_CRAFT( img, scale=arg[0], detectAngle=False, ##是否进行文字方向检测 MAX_HORIZONTAL_GAP=arg[2], ##字符之间的最大间隔 #30 MIN_V_OVERLAPS=arg[3], #0.5 TEXT_PROPOSALS_MIN_SCORE=arg[4], #[0.5, 0.2, 0.7], leftAdjustAlph=arg[6], ## rightAdjustAlph=arg[7], ## Adjustbox=arg[5], ## pixel_filter=arg[8], ## 参数一是过滤宽,第二个参数是过滤高 batch_by_1=arg[9], scoremap_enhance_pixel=arg[10], ) # ti2 = time.asctime(time.localtime(time.time())) # print("本地时间为 :", ti2) else: model = TextOcrModel(ocr, text_detect_p, angle_detect) rotate_img = color_filter(img, color_thre=255, mode='less') img = color_filter(rotate_img, color_thre=50, mode='more') # ------------------------变换--------------------------- gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) edges = cv.Canny(gray, 50, 150, apertureSize=3) # 霍夫变换 lines = cv.HoughLines(edges, 1, np.pi / 180, 0) for rho, theta in lines[0]: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 1000 * (-b)) y1 = int(y0 + 1000 * (a)) x2 = int(x0 - 1000 * (-b)) y2 = int(y0 - 1000 * (a)) # print(x1, '---------', x2, '---------', x2, '---------', y2) if x1 == x2 or y1 == y2 or y2 == -1000: rotate_img = img else: t = float(y2 - y1) / (x2 - x1) rotate_angle = math.degrees(math.atan(t)) if rotate_angle > 45: rotate_angle = -90 + rotate_angle elif rotate_angle < -45: rotate_angle = 90 + rotate_angle rotate_img = ndimage.rotate(img, rotate_angle) result, angle, im_show = model.model_PSENET( rotate_img, scale=arg2[0], maxScale=arg2[1], detectAngle=arg2[2], ##是否进行文字方向检测 MAX_HORIZONTAL_GAP=arg2[3], ##字符之间的最大间隔 MIN_V_OVERLAPS=arg2[4], TEXT_PROPOSALS_MIN_SCORE=arg2[5], leftAdjustAlph=arg2[6], ## rightAdjustAlph=arg2[7], ## Adjustbox=arg2[8], ## ) # 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 return res, im_show