-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
268 lines (199 loc) · 7.71 KB
/
app.py
File metadata and controls
268 lines (199 loc) · 7.71 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import heapq
import cv2
import pyttsx3
import asyncio
from threading import Thread
import os
from flask import Flask, render_template, Response
import cv2
KNOWN_DISTANCE = 130
PERSON_WIDTH = 16
MOBILE_WIDTH = 3.0
CONFIDENCE_THRESHOLD = 0.4
NMS_THRESHOLD = 0.3
COLORS = [
(255, 0, 0),
(255, 0, 255),
(0, 255, 255),
(255, 255, 0),
(0, 255, 0),
(255, 0, 0),
]
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
FONTS = cv2.FONT_HERSHEY_COMPLEX
class_names = []
with open("classes.txt", "r") as f:
class_names = [cname.strip() for cname in f.readlines()]
yoloNet = cv2.dnn.readNet("yolov4-tiny.weights", "yolov4-tiny.cfg")
yoloNet.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
yoloNet.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16)
model = cv2.dnn_DetectionModel(yoloNet)
model.setInputParams(size=(416, 416), scale=1 / 255, swapRB=True)
def object_detector(image):
classes, scores, boxes = model.detect(image, CONFIDENCE_THRESHOLD, NMS_THRESHOLD)
data_list = []
for classid, score, box in zip(classes, scores, boxes):
color = COLORS[int(classid) % len(COLORS)]
label = "%s : %f" % (class_names[classid], score)
cv2.rectangle(image, box, color, 2)
cv2.putText(image, label, (box[0], box[1] - 14), FONTS, 0.5, color, 2)
if classid == 0:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 67:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 2:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 3:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 7:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 1:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 5:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 10:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 16:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 39:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
elif classid == 56:
data_list.append([class_names[classid], box[2], (box[0], box[1] - 2)])
else:
data_list.append(["Unknown", box[2], (box[0], box[1] - 2)])
return data_list
def focal_length_finder(measured_distance, real_width, width_in_rf):
focal_length = (width_in_rf * measured_distance) / real_width
return focal_length
def distance_finder(focal_length, real_object_width, width_in_frmae):
distance = (real_object_width * focal_length) / width_in_frmae
return distance
ref_person = cv2.imread("ReferenceImages/image14.jpeg")
ref_mobile = cv2.imread("ReferenceImages/image4.jpeg")
mobile_data = object_detector(ref_mobile)
mobile_width_in_rf = mobile_data[1][1]
person_data = object_detector(ref_person)
person_width_in_rf = person_data[0][1]
print(
f"Person width in pixels : {person_width_in_rf} mobile width in pixel: {mobile_width_in_rf}"
)
focal_person = focal_length_finder(KNOWN_DISTANCE, PERSON_WIDTH, person_width_in_rf)
focal_mobile = focal_length_finder(KNOWN_DISTANCE, MOBILE_WIDTH, mobile_width_in_rf)
cap = cv2.VideoCapture(0)
async def read_annotations_aloud(annotations, distance=0):
engine = pyttsx3.init()
engine.setProperty("rate", 150)
# make volume increase if the distance i near
if distance < 10:
engine.setProperty("volume", 1)
elif 10 < distance < 40:
engine.setProperty("volume", 0.8)
elif 40 < distance < 70:
engine.setProperty("volume", 0.7)
else:
engine.setProperty("volume", 0.5)
engine.setProperty("volume", 0.5)
for annotation in annotations:
annotation = heapq.heappop(annotations)
class_name = annotation[1]
distance = annotation[0]
bbox = annotation[2]
x1, y1, x2, y2 = bbox
text = f"{class_name}, distance: {distance:.2f} inches."
if text is None:
continue
engine.say(text)
try:
engine.runAndWait()
except RuntimeError:
pass
def process_anote(annotations, d, distance=0):
x, y = d[2]
heapq.heappush(
annotations,
(
distance,
d[0],
(x, y, x + 150, y + 23),
),
)
def gen_frames():
while True:
ret, frame = cap.read()
cv2.waitKey(100)
data = object_detector(frame)
annotations = []
for d in data:
if d[0] == "person":
distance = distance_finder(focal_person, PERSON_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
elif d[0] == "cell phone":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
elif d[0] == "bicycle":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
elif d[0] == "car":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
elif d[0] == "motorbike":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
elif d[0] == "bus":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
elif d[0] == "dog":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
elif d[0] == "truck":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
if d[0] == "chair" or "sofa":
distance = distance_finder(focal_mobile, MOBILE_WIDTH, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
else:
distance = distance_finder(100, 10, d[1])
x, y = d[2]
process_anote(annotations, d, distance)
# Draw bounding box and display distance
cv2.rectangle(frame, (x, y - 3), (x + 150, y + 23), BLACK, -1)
cv2.putText(
frame,
f"Dis: {round(distance, 2)} inch",
(x + 5, y + 13),
FONTS,
0.48,
GREEN,
2,
)
# Read out annotations aloud
try:
Thread(
target=asyncio.run,
args=(read_annotations_aloud(annotations, distance=distance),),
).start()
except RuntimeError:
pass
# cv2.imshow('frame', frame)
ret, buffer = cv2.imencode(".jpg", frame)
frame = buffer.tobytes()
yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n")
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/video_feed")
def video_feed():
return Response(gen_frames(), mimetype="multipart/x-mixed-replace; boundary=frame")
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True, port=5000)