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