-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_cap_detection.py
60 lines (45 loc) · 1.83 KB
/
python_cap_detection.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
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('best.pt') # Adjust path if needed
# Initialize video capture from the webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
confidence_threshold = 0.5 # Adjust this value as needed
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
# Perform cap detection on the frame
results = model(frame)
# Flag to determine if cap is detected
cap_detected = False
# Process detection results
for result in results:
for box in result.boxes:
confidence = box.conf[0].item()
if confidence > confidence_threshold:
x1, y1, x2, y2 = box.xyxy[0].tolist() # Extract bounding box coordinates
class_id = int(box.cls[0].item()) # Get the class ID
if class_id == 0: # Assuming class ID 0 corresponds to 'cap'
# Set cap_detected to True
cap_detected = True
# Draw bounding box
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, f'Cap {confidence:.2f}', (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display text based on detection
if cap_detected:
cv2.putText(frame, 'Cap Detected', (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else:
cv2.putText(frame, 'Cap Not Detected', (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Show the frame with bounding boxes and text
cv2.imshow('Cap Detection', frame)
# Exit on 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()