Skip to content

Commit 7fbf2f5

Browse files
main file
1 parent db81f0f commit 7fbf2f5

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

main.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import pyttsx3
2+
import cv2
3+
import numpy as np
4+
5+
# TTS
6+
engine = pyttsx3.init()
7+
8+
# TTS Functionality
9+
def speak(text):
10+
engine.say(text)
11+
engine.runAndWait()
12+
13+
# Load your model here, u can use yolov3 v9 v8 v7 doesn't matter, in my case, I used v3!
14+
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
15+
layer_names = net.getLayerNames() # Get the layer names
16+
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()] # output layers
17+
18+
# Classnames for our model ( in this case YOLOv3 ofc )
19+
with open("coco.names", "r") as f:
20+
classes = [line.strip() for line in f.readlines()]
21+
22+
# Initialize the webcam, in my opinion my webcam device is 1
23+
# you can check it out in device manager to see how many webcams you've got.
24+
# example : VideoCapture(0) - VideoCapture(3) - etc...
25+
cap = cv2.VideoCapture(1)
26+
27+
while True:
28+
ret, frame = cap.read()
29+
if not ret:
30+
print("Failed to grab frame")
31+
break
32+
33+
height, width, channels = frame.shape
34+
35+
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
36+
net.setInput(blob)
37+
outs = net.forward(output_layers)
38+
39+
class_ids = []
40+
confidences = []
41+
boxes = []
42+
43+
for out in outs:
44+
for detection in out:
45+
scores = detection[5:]
46+
class_id = np.argmax(scores)
47+
confidence = scores[class_id]
48+
if confidence > 0.5: # Confidence threshold (You can increase it if you want), by confidence you can tell how much u sure about ur object detection guess, something like that...
49+
center_x = int(detection[0] * width)
50+
center_y = int(detection[1] * height)
51+
w = int(detection[2] * width)
52+
h = int(detection[3] * height)
53+
x = int(center_x - w / 2)
54+
y = int(center_y - h / 2)
55+
56+
boxes.append([x, y, w, h])
57+
confidences.append(float(confidence))
58+
class_ids.append(class_id)
59+
60+
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
61+
62+
if len(indexes) > 0:
63+
for i in indexes.flatten():
64+
x, y, w, h = boxes[i]
65+
label = str(classes[class_ids[i]])
66+
confidence = str(round(confidences[i], 2))
67+
68+
# Drawing frames around detected objects
69+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
70+
cv2.putText(frame, label + " " + confidence, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
71+
72+
73+
# Calling tts to tell me what Computer watching (like before, u can delete this if u want)
74+
speak(f"You are showing me {label}")
75+
76+
# Window name
77+
cv2.imshow("Object Detection by AlirezaPlus", frame)
78+
79+
# Break on 'q' key ( to quit the instance just press Q (WAITKEY 1 stands for 1 ms thats mean u have to hold ur Q for 1ms which is instant is this case))
80+
if cv2.waitKey(1) & 0xFF == ord('q'):
81+
break
82+
83+
cap.release()
84+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)