-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
218 lines (178 loc) · 6.14 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
#
# Project: Video Streaming with Flask
# Author: Log0 <im [dot] ckieric [at] gmail [dot] com>
# Date: 2014/12/21
# Website: http://www.chioka.in/
# Description:
# Modified to support streaming out with webcams, and not just raw JPEGs.
# Most of the code credits to Miguel Grinberg, except that I made a small tweak. Thanks!
# Credits: http://blog.miguelgrinberg.com/post/video-streaming-with-flask
#
# Usage:
# 1. Install Python dependencies: cv2, flask. (wish that pip install works like a charm)
# 2. Run "python main.py".
# 3. Navigate the browser to the local webpage.
from flask import Flask, render_template, Response, send_from_directory, request, make_response
from camera import VideoCamera
from tf_pose_estimation.run_v3 import PoseEstimator
import threading, queue
import cv2
import json
from serial_connect import measurementSerialThread
from tape_tracker.tape_tracker import TapeTrackerThread
app = Flask(__name__, static_url_path='')
dataQ = queue.Queue()
errQ = queue.Queue()
isDetInRightPlace = {"bool": "false"}
estimator = PoseEstimator()
estimator.configure()
objd_in_q = queue.Queue()
objd_out_q = queue.Queue()
measurements_consol = {}
tracker = TapeTrackerThread(objd_in_q, objd_out_q)
tracker.daemon = True
tracker.start()
print("Tracker is initialised")
@app.before_first_request
def configure():
#estimator.configure()
ser = measurementSerialThread(dataQ, errQ)
ser.daemon = True
ser.start()
print("Measurement thread started")
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('js', path)
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory('css', path)
@app.route('/images/<path:path>')
def send_img(path):
return send_from_directory('images', path)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/check_reference', methods=['POST'])
def check_reference():
resp = make_response(json.dumps(isDetInRightPlace))
resp.status_code = 200
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/get_readings' , methods=['GET','POST'])
def get_readings():
#data = request.form['keyword']
data = get_bluno_data()
if data != "No reading yet":
measurements_consol["data"] = "{0:.2f}".format(float(data))
resp = make_response(json.dumps(measurements_consol))
resp.status_code = 200
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/next', methods=['POST'])
def next():
estimator.state_next()
print(measurements_consol[0])
resp = make_response(estimator.state)
resp.status_code = 200
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/reset', methods=['POST'])
def reset():
estimator.state_reset()
isDetInRightPlace["bool"] ="false"
resp = make_response("Success")
resp.status_code = 200
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/set_mode', methods=['POST'])
def set_mode():
data = request.form
mode = json.loads(data["mode"])
print(mode)
estimator.set_mode(mode)
resp = make_response("Success")
resp.status_code = 200
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/configure_tracker', methods=['POST'])
def configure_tracker():
data = request.form
bounding_box = json.loads(data["bb"])
estimator.configure_tracker(bounding_box)
resp = make_response("Success")
resp.status_code = 200
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera().start()),
mimetype='multipart/x-mixed-replace; boundary=frame')
def gen(camera):
count = 0
det_rect = None
while True:
if camera.stopped:
break
#frame = camera.get_frame(estimator)
#get_bluno_data()
curr_state = estimator.state
curr_mode = estimator.mode
img = camera.read()
# estimate
if img is not None:
img_est, measurements = estimator.predict(img)
measurements_consol[0] = measurements
# obj det
objd_in_q.put(img)
(img_det, bb_coord) = objd_out_q.get()
if len(bb_coord) != 0 and count > 10:
estimator.configure_tracker(bb_coord[0])
count = 0
det_rect = bb_coord[0]
#print(bb_coord[0])
count = count + 1
if curr_state is not "reference":
byte_frame = img_to_bytes(img_est)
elif curr_mode is "quiz":
byte_frame = img_to_bytes(img)
else:
rect = draw_ref_box(img)
isDetInPlace = check_if_in_rect(det_rect, rect)
isDetInRightPlace["bool"] = isDetInPlace
byte_frame = img_to_bytes(img_det)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + byte_frame + b'\r\n\r\n')
def img_to_bytes(img):
ret, jpeg = cv2.imencode('.jpg', img)
return jpeg.tobytes()
def draw_ref_box(img):
x = 210
y = 110
cv2.rectangle(img, (x, y), (x + 50, y + 50),
(0, 0, 0), 2)
return [(x, y), (x+50, y+50)]
def check_if_in_rect(det_rect, ref_rect):
isDetInRightPlace = False
if det_rect is None or ref_rect is None:
return isDetInRightPlace
top_left_rect = ref_rect[0]
bot_right_rect = ref_rect[1]
top_left_tracker = (det_rect[0], det_rect[1])
bot_right_tracker = (det_rect[0]+det_rect[2], det_rect[1]+det_rect[3])
tracker_centre = ((top_left_tracker[0]+bot_right_tracker[0])*0.5, (top_left_tracker[1]+bot_right_tracker[1])*0.5)
if top_left_rect[0] < tracker_centre[0] \
and top_left_rect[1] < tracker_centre[1] \
and bot_right_rect[0] > tracker_centre[0] and bot_right_rect[1] > tracker_centre[1]:
isDetInRightPlace = True
return isDetInRightPlace
def get_bluno_data():
measurement = read_measurement_from_serial()
return measurement
def read_measurement_from_serial():
reading = "No reading yet"
if not dataQ.empty():
reading = dataQ.get_nowait()
return reading
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, threaded=True, ssl_context='adhoc')