Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
66 lines (48 loc) · 1.48 KB
/
Copy pathtest.py
File metadata and controls
66 lines (48 loc) · 1.48 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
import cv2
from ultralytics import YOLO
# Load your trained model
model = YOLO("detect_best.pt") # path to your trained weights
# Function for video detection
def detect_from_video(video_path):
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Run YOLO inference
results = model(frame)
# Visualize results on frame
annotated_frame = results[0].plot()
# Display
cv2.imshow("Trash Detection - Video", annotated_frame)
# Press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Function for webcam detection
def detect_from_webcam():
cap = cv2.VideoCapture(0) # 0 = default webcam
while True:
ret, frame = cap.read()
if not ret:
break
# Run YOLO inference
results = model(frame)
# Visualize results
annotated_frame = results[0].plot()
# Show
cv2.imshow("Trash Detection - Webcam", annotated_frame)
# Press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
mode = input("Enter 'v' for video or 'w' for webcam: ").strip().lower()
if mode == 'v':
detect_from_video("Video2.mp4")
elif mode == 'w':
detect_from_webcam()
else:
print("Invalid option.")