captcha_flask_server.py_bak 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if code_type is None or str(code_type) not in ['shuzi', 'suanshu','yingwen','hanzi']:
  66. data = {'errorinfo':'please check you param:code, code must be in shuzi/suanshu/yingwen/hanzi'}
  67. return jsonify(data)
  68. if base64pic is not None:
  69. try:
  70. src = base64.b64decode(base64pic.split(',')[-1])
  71. img = Image.open(BytesIO(src))
  72. if img.mode != "RGB":
  73. img = img.convert("RGB")
  74. if code_type == 'shuzi':
  75. pre = predict_digit(img)
  76. elif code_type == 'suanshu':
  77. pre = predict_arith(img)
  78. pre = str(eval(pre))
  79. elif code_type == 'hanzi':
  80. pre = predict_chinese(img)
  81. data['predict'] = pre
  82. data['success'] = True
  83. total_num += 1
  84. return jsonify(data)
  85. except:
  86. return jsonify(data)
  87. if file_obj is not None:
  88. try:
  89. img = Image.open(file_obj)
  90. if img.mode != "RGB":
  91. img = img.convert("RGB")
  92. if code_type == 'shuzi':
  93. pre = predict_digit(img)
  94. elif code_type == 'suanshu':
  95. pre = predict_arith(img)
  96. pre = str(eval(pre))
  97. elif code_type == 'hanzi':
  98. pre = predict_chinese(img)
  99. data['success'] = True
  100. data['predict'] = pre
  101. total_num += 1
  102. return jsonify(data)
  103. except:
  104. return jsonify(data)
  105. return 'please check you post '
  106. if __name__ == '__main__':
  107. app.run("0.0.0.0", port=17052, debug=False)