flash_server.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # @Author : bidikeji
  4. # @Time : 2019/11/21 0021 15:55
  5. from predict_model import *
  6. from flask import request, Flask, jsonify
  7. from PIL import Image
  8. from io import BytesIO
  9. import base64
  10. import time
  11. total_num = 0
  12. neg_num = 0
  13. app = Flask(__name__)
  14. @app.route("/getlog", methods=["POST"])
  15. def get_acc():
  16. global total_num
  17. global neg_num
  18. data = {'total_num':total_num, 'neg_numative':neg_num}
  19. clear = request.form.get('clear_log', 'no')
  20. if clear == 'yes':
  21. with open('upload_num_log.txt', 'a', encoding='utf=8') as f:
  22. f.write('total_number:%d,\t error_number:%d\n'%(total_num, neg_num))
  23. total_num = 0
  24. neg_num = 0
  25. return jsonify(data)
  26. @app.route("/errorlog", methods=["POST"])
  27. def save_error():
  28. """receive not success image and save """
  29. global total_num
  30. global neg_num
  31. code_type = request.form.get('code', 'unknow')
  32. base64pic = request.form.get('base64pic')
  33. file_obj = request.files.get("pic")
  34. data = {'save_success':False}
  35. if base64pic is not None:
  36. try:
  37. src = base64.b64decode(base64pic.split(',')[-1])
  38. img = Image.open(BytesIO(src))
  39. time_tr = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
  40. img.save('pic/'+str(code_type)+'_'+time_tr+'.jpg')
  41. data['save_success'] = True
  42. neg_num += 1
  43. return jsonify(data)
  44. except:
  45. return jsonify(data)
  46. if file_obj is not None:
  47. try:
  48. img = Image.open(file_obj)
  49. time_tr = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
  50. img.save('pic/'+str(code_type)+'_'+time_tr+'.jpg')
  51. data['save_success'] = True
  52. neg_num += 1
  53. return jsonify(data)
  54. except:
  55. return jsonify(data)
  56. @app.route("/upload", methods=["POST"])
  57. def upload():
  58. global total_num
  59. global neg_num
  60. """receive image and predict """
  61. code_type = request.form.get('code')
  62. base64pic = request.form.get('base64pic')
  63. file_obj = request.files.get("pic")
  64. data = {'success':False}
  65. print(type(code_type))
  66. if code_type is None or str(code_type) not in ['shuzi', 'suanshu','yingwen','hanzi']:
  67. data = {'errorinfo':'please check you param:code, code must be in shuzi/suanshu/yingwen/hanzi'}
  68. return jsonify(data)
  69. if base64pic is not None:
  70. try:
  71. src = base64.b64decode(base64pic.split(',')[-1])
  72. img = Image.open(BytesIO(src))
  73. if img.mode != "RGB":
  74. img = img.convert("RGB")
  75. if code_type == 'shuzi':
  76. pre = predict_digit(img)
  77. elif code_type == 'suanshu':
  78. pre = predict_arith(img)
  79. pre = str(eval(pre))
  80. data['predict'] = pre
  81. data['success'] = True
  82. total_num += 1
  83. return jsonify(data)
  84. except:
  85. return jsonify(data)
  86. if file_obj is not None:
  87. try:
  88. img = Image.open(file_obj)
  89. if img.mode != "RGB":
  90. img = img.convert("RGB")
  91. if code_type == 'shuzi':
  92. pre = predict_digit(img)
  93. elif code_type == 'suanshu':
  94. pre = predict_arith(img)
  95. print(pre)
  96. pre = str(eval(pre))
  97. data['success'] = True
  98. data['predict'] = pre
  99. total_num += 1
  100. return jsonify(data)
  101. except:
  102. return jsonify(data)
  103. return 'please check you post '
  104. if __name__ == '__main__':
  105. app.run("192.168.2.101", port=5025, debug=False)