captcha_rotate_server.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from flask import request, Flask, jsonify,logging
  2. import time
  3. import logging
  4. app = Flask(__name__)
  5. from RotateMatch.rotateInterface import *
  6. @app.route("/tailor", methods=["POST"])
  7. def tailor():
  8. start_time = time.time()
  9. """receive image and predict """
  10. base64pic = request.json.get('base64pic')
  11. height = request.json.get("height")
  12. # print(request.data)
  13. data = {'success':False}
  14. if base64pic is None:
  15. data["errorinfo"] = "captcha tailor needs two params:base64pic and height,please check"
  16. return jsonify(data),404
  17. try:
  18. img_b64 = base64pic.split(',')[-1]
  19. width = getMoveLength(img_b64,height)
  20. data["destination"] = width
  21. data["success"] = True
  22. data["cost"] = time.time()-start_time
  23. logging.info("tailor takes %.2f"%(time.time()-start_time))
  24. return jsonify(data),200
  25. except Exception as e:
  26. data["errorinfo"] = "exception with %s"%(str(e))
  27. return jsonify(data),500
  28. @app.route("/rotate", methods=["POST"])
  29. def rotate():
  30. start_time = time.time()
  31. """receive image and predict """
  32. base64pic = request.json.get('base64pic')
  33. radius = request.json.get('radius')
  34. # print(request.data)
  35. data = {'success':False}
  36. if base64pic is None :
  37. data["errorinfo"] = "captcha tailor needs one params:base64pic,please check"
  38. return jsonify(data),404
  39. try:
  40. img_b64 = base64pic.split(',')[-1]
  41. angle = getRotateAngle(img_b64,radius)
  42. data["angle"] = angle
  43. data["success"] = True
  44. data["cost"] = time.time()-start_time
  45. logging.info("tailor takes %.2f"%(time.time()-start_time))
  46. return jsonify(data),200
  47. except Exception as e:
  48. data["errorinfo"] = "exception with %s"%(str(e))
  49. traceback.print_exc()
  50. return jsonify(data),500
  51. if __name__ == '__main__':
  52. handler = logging.FileHandler('flask.log', encoding='UTF-8')
  53. app.logger.setLevel("INFO")
  54. logging_format = logging.Formatter(
  55. '%(asctime)s - %(levelname)s - %(filename)s -%(lineno)s - %(message)s'
  56. )
  57. handler.setFormatter(logging_format)
  58. app.logger.addHandler(handler)
  59. app.run("0.0.0.0", port=17053, debug=False) # 2.177 本地IP