import base64 import json import logging import os import sys import time import traceback from glob import glob import cv2 import numpy as np os.environ["CUDA_VISIBLE_DEVICES"] = "-1" import tensorflow as tf from flask import Flask, request sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../") from chinese_equation_recognize.inference_equation import recognize from chinese_equation_recognize.model import crnn_ctc_equation_large, crnn_ctc_equation_loss from utils import pil_resize, np2bytes, request_post, bytes2np, base64_decode, image_to_str, str_to_image logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') tf.compat.v1.disable_eager_execution() sess = tf.compat.v1.Session(graph=tf.Graph()) package_dir = os.path.abspath(os.path.dirname(__file__)) model_path = package_dir + "/models/e55-loss0.14-equation.h5" image_shape = (32, 192, 1) # 接口配置 app = Flask(__name__) @app.route('/cer', methods=['POST']) def cer(): start_time = time.time() logging.info("into cer_interface cer") try: # 接收网络数据 if not request.form: logging.info("cer no data!") return json.dumps({"data": "", "success": 0}) data = request.form.get("data") logging.info("cer_interface get data time" + str(time.time()-start_time)) # 加载模型 cer_model = globals().get("global_cer_model") if cer_model is None: print("=========== init cer model ===========") cer_model = CerModels().get_model() globals().update({"global_cer_model": cer_model}) # 数据转换 data = base64_decode(data) image_np = bytes2np(data) # 预测 result = recognize(image_np, cer_model, sess) if result is None: return json.dumps({"data": "", "success": 0}) return json.dumps({"data": result, "success": 1}) except: traceback.print_exc() return json.dumps({"data": "", "success": 0}) finally: logging.info("cer interface finish time " + str(time.time()-start_time)) class CerModels: def __init__(self): with sess.as_default(): with sess.graph.as_default(): self.model = crnn_ctc_equation_loss(input_shape=image_shape, class_num=35+2, is_train=False) self.model.load_weights(model_path) def get_model(self): return self.model def test_cer_model(from_remote=True): paths = glob("D:/Project/captcha/data/test/FileInfo1021/1d419189-5116-11ed-851c-b4b5b67760ae_7.jpg") paths = glob(r'C:\Users\Administrator\Downloads\default.jfif') for file_path in paths: img_np = cv2.imread(file_path) h, w = img_np.shape[:2] file_bytes = np2bytes(img_np) file_base64 = base64.b64encode(file_bytes) if from_remote: file_json = {"data": file_base64} # _url = "http://192.168.2.102:17061/cer" _url = "http://127.0.0.1:17061/cer" result = json.loads(request_post(_url, file_json)) if result.get("success"): result = int(result.get("data")) cv2.imshow("img_np", img_np) print("equation result", result) cv2.waitKey(0) else: print("failed!") if __name__ == "__main__": # app.run(host='127.0.0.1', port=17061, debug=False) test_cer_model()