forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuideAppForBlindPeople
74 lines (47 loc) · 1.79 KB
/
GuideAppForBlindPeople
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
import cv2
import pyttsx3
import numpy as np
cap = cv2.VideoCapture(0)
engine = pyttsx3.init()
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
with open("coco.names", "r") as f:
classes = f.read().strip().split("\n")
while True:
ret, frame = cap.read()
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
layer_names = net.getUnconnectedOutLayersNames()
outs = net.forward(layer_names)
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * frame.shape[1])
center_y = int(detection[1] * frame.shape[0])
width = int(detection[2] * frame.shape[1])
height = int(detection[3] * frame.shape[0])
x = int(center_x - width / 2)
y = int(center_y - height / 2)
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, width, height])
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
for i in indices:
i = i[0]
label = str(classes[class_ids[i]])
confidence = confidences[i]
x, y, w, h = boxes[i]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
object_label = f"I see a {label} with {int(confidence * 100)}% confidence"
engine.say(object_label)
engine.runAndWait()
cv2.imshow('Object Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()