-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
153 lines (129 loc) · 5.07 KB
/
app.py
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
import os
import cv2
import json
from flask import Flask, redirect, url_for, request, render_template, Response, jsonify, redirect
from common.utils import base64_to_pil,np_to_base64,str_to_bool,gen_web
from common.camera import CameraParams,Camera
# Declare a flask app
app = Flask(__name__,static_folder="./web/static",template_folder="./web/templates",)
IMAGE_SUFFIX = ['jpg','jpeg','png']
VIDEO_SUFFIX = ['mp4','avi']
@app.route('/')
def upload_file():
return render_template('index1.html')
# @app.route('/')
# def upload_index():
# return render_template('index0.html')
# API that returns image with detections on it
@app.route('/images', methods= ['POST'])
def get_image():
image = request.files["images"]
image_name = image.filename
with open('./caches/result.txt', 'r') as f:
im_na = f.read()
try:
os.remove(im_na)
except:
pass
if image_name.split('.')[-1] in VIDEO_SUFFIX:
with open('./caches/result.txt', 'w') as f:
f.write(image_name)
image.save(os.path.join(os.getcwd(), image_name))
if image_name.split(".")[-1] in IMAGE_SUFFIX:
img = cv2.imread(image_name)
h,w,_ = img.shape
if h > 2000 or w > 2000:
h = h // 2
w = w // 2
img = cv2.resize(img,(int(w),int(h)))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# bbox, cls_conf, cls_ids = yolo(img)
# from vizer.draw import draw_boxes as db
# if bbox is not None:
# img = db(img, bbox, cls_ids, cls_conf, class_name_map=class_names)
img = img[:, :, (2, 1, 0)]
_, img_encoded = cv2.imencode('.jpg', img)
response = img_encoded.tobytes()
os.remove(image_name)
try:
return Response(response=response, status=200, mimetype='image/jpg')
except:
return render_template('index1.html')
# return render_template('index0.html')
else:
return render_template('video1.html')
# return render_template('video0.html')
@app.route('/cameraParams', methods=['GET', 'POST'])
def cameraParams():
if request.method == 'GET':
data = {
'gray': CameraParams.gray,
'gaussian': CameraParams.gaussian,
'sobel': CameraParams.sobel,
'canny': CameraParams.canny,
}
return app.response_class(response=json.dumps(data),
status=200,
mimetype='application/json')
elif request.method == 'POST':
try:
data = request.form.to_dict()
CameraParams.gray = str_to_bool(data['gray'])
CameraParams.gaussian = str_to_bool(data['gaussian'])
CameraParams.sobel = str_to_bool(data['sobel'])
CameraParams.canny = str_to_bool(data['canny'])
message = {'message': 'Success'}
response = app.response_class(response=json.dumps(message),
status=200,
mimetype='application/json')
return response
except Exception as e:
print(e)
response = app.response_class(response=json.dumps(e),
status=400,
mimetype='application/json')
return response
else:
data = { "error": "Method not allowed. Please GET or POST request!" }
return app.response_class(response=json.dumps(data),
status=400,
mimetype='application/json')
@app.route('/realtime')
def realtime():
# return render_template('video0.html')
return render_template('video1.html')
########get path
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen_web(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/cutter', methods=['GET', 'POST'])
def web_cutter():
if request.method == 'POST':
# Get the image from post request
img = base64_to_pil(request.json)
# Save the image to ./uploads
# img.save("./uploads/image.png")
# Make prediction
preds = model_predict(img, model)
# Process your result for human
pred_proba = "{:.3f}".format(np.amax(preds)) # Max probability
pred_class = decode_predictions(preds, top=1) # ImageNet Decode
result = str(pred_class[0][0][1]) # Convert to string
result = result.replace('_', ' ').capitalize()
# Serialize the result, you can add additional fields
return jsonify(result=result, probability=pred_proba)
return None
@app.route('/upload_video', methods=['GET'])
def upload_video():
return render_template('video.html')
def web_transcriber():
return
def web_daemoner():
return
if __name__=="__main__":
# Run locally
app.run(debug=True,port=5002,threaded=False)
# Run on the server
# app.run(debug=True, host = '0.0.0.0', port=5000)