|
| 1 | +from flask import Flask, request, render_template, jsonify |
| 2 | +from io import BytesIO |
| 3 | +from captchaModel.model import load_model, inference |
| 4 | +# from waitress import serve |
| 5 | +import numpy as np |
| 6 | +import base64 |
| 7 | +from PIL import Image |
| 8 | +from waitress import serve |
| 9 | +from paste.translogger import TransLogger |
| 10 | +import time |
| 11 | +import math |
| 12 | + |
| 13 | +sess, dectection_graph = load_model() |
| 14 | +app = Flask(__name__) |
| 15 | + |
| 16 | +# app.config['SECRET_KEY'] = 'secret key here' |
| 17 | +# app.config['HOST'] = '0.0.0.0' |
| 18 | +# app.config['DEBUG'] = True |
| 19 | +# app.config["CACHE_TYPE"] = "null" |
| 20 | + |
| 21 | +# change to "redis" and restart to cache again |
| 22 | + |
| 23 | +# some time later |
| 24 | +# cache.init_app(app) |
| 25 | + |
| 26 | + |
| 27 | +# @app.route('/') |
| 28 | +# def home(): |
| 29 | +# return "Hello World" |
| 30 | + |
| 31 | +# def shutdown_server(): |
| 32 | +# func = request.environ.get('werkzeug.server.shutdown') |
| 33 | +# if func is None: |
| 34 | +# raise RuntimeError('Not running with the Werkzeug Server') |
| 35 | +# func() |
| 36 | + |
| 37 | +# @app.route('/shutdown', methods=['POST']) |
| 38 | +# def shutdown(): |
| 39 | +# shutdown_server() |
| 40 | +# return 'Server shutting down...' |
| 41 | + |
| 42 | + |
| 43 | +@app.route('/api/predict/', methods=['GET', 'POST','DELETE', 'PATCH']) |
| 44 | +def api_predict(): |
| 45 | + # print(request.is_json) |
| 46 | + # image_base64 = request.form['image'] |
| 47 | + # image = base64.b64decode(image_base64) |
| 48 | + # image = Image.open(BytesIO(image)) |
| 49 | + # if image.mode != "RGB": |
| 50 | + # image.convert("RGB") |
| 51 | + |
| 52 | + # image_arr = np.array(image, dtype=np.uint8) |
| 53 | + # # print(image_arr) |
| 54 | + # start_date = time.time() |
| 55 | + # res = inference(sess, dectection_graph, image_arr) |
| 56 | + # end_date = time.time() |
| 57 | + # result_date = start_date - end_date |
| 58 | + # # print(res) |
| 59 | + # return jsonify(answer=res, status=True, captcha=res, time=format(math.floor(result_date)).replace("-","") + "s") |
| 60 | + # return { |
| 61 | + # "captcha": res, |
| 62 | + # "time": format(math.floor(result_date)).replace("-","") + "s"} |
| 63 | + |
| 64 | + # return jsonify(answer=res, status=True) |
| 65 | + apiKeys = [] |
| 66 | + with open('key_api.txt', 'r') as listApiKeys: |
| 67 | + apiKeys = [listApiKey.rstrip() for listApiKey in listApiKeys.readlines()] |
| 68 | + # print(apiKey) |
| 69 | + # apiKey = 'Pong11299' |
| 70 | + |
| 71 | + |
| 72 | + if request.is_json: |
| 73 | + data_json = request.get_json() |
| 74 | + if data_json['api_key'] in apiKeys: |
| 75 | + print('found') |
| 76 | + try: |
| 77 | + if data_json: |
| 78 | + image_base64 = data_json['image'] |
| 79 | + image = base64.b64decode(image_base64) |
| 80 | + image = Image.open(BytesIO(image)) |
| 81 | + if image.mode != "RGB": |
| 82 | + image.convert("RGB") |
| 83 | + |
| 84 | + image_arr = np.array(image, dtype=np.uint8) |
| 85 | + # print(image_arr) |
| 86 | + res = inference(sess, dectection_graph, image_arr) |
| 87 | + |
| 88 | + return jsonify(answer=res, status=True) |
| 89 | + except Exception as e: |
| 90 | + return jsonify(answer="", status=False) |
| 91 | + else: |
| 92 | + return jsonify(answer="NoApiKey", status=False, text='Please Enter API KEY') |
| 93 | + else: |
| 94 | + image_base64 = request.form['image'] |
| 95 | + image = base64.b64decode(image_base64) |
| 96 | + image = Image.open(BytesIO(image)) |
| 97 | + if image.mode != "RGB": |
| 98 | + image.convert("RGB") |
| 99 | + |
| 100 | + image_arr = np.array(image, dtype=np.uint8) |
| 101 | + # print(image_arr) |
| 102 | + start_date = time.time() |
| 103 | + res = inference(sess, dectection_graph, image_arr) |
| 104 | + end_date = time.time() |
| 105 | + result_date = start_date - end_date |
| 106 | + # print(res) |
| 107 | + return { |
| 108 | + "captcha": res, |
| 109 | + "time": format(math.floor(result_date)).replace("-","") + "s"} |
| 110 | + |
| 111 | + |
| 112 | + # base64 = request.form['base64'] |
| 113 | + # start_date = time.time() |
| 114 | + # captcha = Captcha_detection(base64) |
| 115 | + # end_date = time.time() |
| 116 | + # result_date = start_date - end_date |
| 117 | + # return { |
| 118 | + # "captcha": captcha, |
| 119 | + # "time": format(math.floor(result_date)).replace("-","") + "s" |
| 120 | + |
| 121 | + |
| 122 | +@app.route('/' , methods=['GET', 'POST','DELETE', 'PATCH']) |
| 123 | +def home(): |
| 124 | + base64few = str(request.form['base64']) |
| 125 | + image = base64.b64decode(base64few) |
| 126 | + image = Image.open(BytesIO(image)) |
| 127 | + if image.mode != "RGB": |
| 128 | + image.convert("RGB") |
| 129 | + image_arr = np.array(image, dtype=np.uint8) |
| 130 | + start_date = time.time() |
| 131 | + #captcha = Captcha_detection(base64) |
| 132 | + res = inference(sess, dectection_graph, image_arr) |
| 133 | + end_date = time.time() |
| 134 | + result_date = start_date - end_date |
| 135 | + |
| 136 | + #print('I have money {:,.2f} baht'.format(result_date)) |
| 137 | + |
| 138 | + return { |
| 139 | + "captcha": res, |
| 140 | + "time": '{:,.2f}s'.format(result_date).replace("-","") |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | +@app.route('/api/images/', methods=['POST']) |
| 145 | +def api_images(): |
| 146 | + if request.is_json: |
| 147 | + try: |
| 148 | + data_json = request.get_json() |
| 149 | + if data_json: |
| 150 | + image_base64 = data_json['image'] |
| 151 | + image = base64.b64decode(image_base64) |
| 152 | + image = Image.open(BytesIO(image)) |
| 153 | + if image.mode != "RGB": |
| 154 | + image.convert("RGB") |
| 155 | + |
| 156 | + image_arr = np.array(image, dtype=np.uint8) |
| 157 | + # print(image_arr) |
| 158 | + res = inference(sess, dectection_graph, image_arr) |
| 159 | + |
| 160 | + return jsonify(answer=res, status=True) |
| 161 | + except Exception as e: |
| 162 | + return jsonify(answer="", status=False) |
| 163 | + return jsonify(answer="", status=False) |
| 164 | + |
| 165 | +@app.route('/api/imgArr/', methods=['POST']) |
| 166 | +def api_imgArr(): |
| 167 | + print(request.get_json()) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + # app.run(debug=True) |
| 172 | + app.debug = False |
| 173 | + # app.config["CACHE_TYPE"] = "null" |
| 174 | + app.run(host = '0.0.0.0',port=8080) |
| 175 | + # serve(TransLogger(app, setup_console_handler=False), threads=20, host = '0.0.0.0',port=5000) |
| 176 | + # serve(TransLogger(app, setup_console_handler=False), threads=40, host = '0.0.0.0',port=5000) |
| 177 | + |
0 commit comments