123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #!/usr/bin/python3
- # -*- coding: utf-8 -*-
- # @Author : bidikeji
- # @Time : 2019/11/21 0021 15:55
- from predict_model import *
- from flask import request, Flask, jsonify
- from PIL import Image
- from io import BytesIO
- import base64
- import time
- total_num = 0
- neg_num = 0
- app = Flask(__name__)
- @app.route("/getlog", methods=["POST"])
- def get_acc():
- global total_num
- global neg_num
- data = {'total_num':total_num, 'neg_numative':neg_num}
- clear = request.form.get('clear_log', 'no')
- if clear == 'yes':
- with open('upload_num_log.txt', 'a', encoding='utf=8') as f:
- f.write('total_number:%d,\t error_number:%d\n'%(total_num, neg_num))
- total_num = 0
- neg_num = 0
- return jsonify(data)
- @app.route("/errorlog", methods=["POST"])
- def save_error():
- """receive not success image and save """
- global total_num
- global neg_num
- code_type = request.form.get('code', 'unknow')
- base64pic = request.form.get('base64pic')
- file_obj = request.files.get("pic")
- data = {'save_success':False}
- if base64pic is not None:
- try:
- src = base64.b64decode(base64pic.split(',')[-1])
- img = Image.open(BytesIO(src))
- time_tr = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
- img.save('pic/'+str(code_type)+'_'+time_tr+'.jpg')
- data['save_success'] = True
- neg_num += 1
- return jsonify(data)
- except:
- return jsonify(data)
- if file_obj is not None:
- try:
- img = Image.open(file_obj)
- time_tr = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
- img.save('pic/'+str(code_type)+'_'+time_tr+'.jpg')
- data['save_success'] = True
- neg_num += 1
- return jsonify(data)
- except:
- return jsonify(data)
- @app.route("/upload", methods=["POST"])
- def upload():
- global total_num
- global neg_num
- """receive image and predict """
- code_type = request.form.get('code')
- base64pic = request.form.get('base64pic')
- file_obj = request.files.get("pic")
- data = {'success':False}
- print(type(code_type))
- if code_type is None or str(code_type) not in ['shuzi', 'suanshu','yingwen','hanzi']:
- data = {'errorinfo':'please check you param:code, code must be in shuzi/suanshu/yingwen/hanzi'}
- return jsonify(data)
- if base64pic is not None:
- try:
- src = base64.b64decode(base64pic.split(',')[-1])
- img = Image.open(BytesIO(src))
- if img.mode != "RGB":
- img = img.convert("RGB")
- if code_type == 'shuzi':
- pre = predict_digit(img)
- elif code_type == 'suanshu':
- pre = predict_arith(img)
- pre = str(eval(pre))
- data['predict'] = pre
- data['success'] = True
- total_num += 1
- return jsonify(data)
- except:
- return jsonify(data)
- if file_obj is not None:
- try:
- img = Image.open(file_obj)
- if img.mode != "RGB":
- img = img.convert("RGB")
- if code_type == 'shuzi':
- pre = predict_digit(img)
- elif code_type == 'suanshu':
- pre = predict_arith(img)
- print(pre)
- pre = str(eval(pre))
- data['success'] = True
- data['predict'] = pre
- total_num += 1
- return jsonify(data)
- except:
- return jsonify(data)
- return 'please check you post '
- if __name__ == '__main__':
- app.run("192.168.2.101", port=5025, debug=False)
|