-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleyfiveyolo.py
66 lines (50 loc) · 1.88 KB
/
simpleyfiveyolo.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
61
62
63
64
65
66
import torch
import cv2
import csv
import time
# Load the YOLOv5 model
weights = "yolov5s.pt"
model = torch.hub.load('ultralytics/yolov5', 'custom', path=weights)
# Set device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device).eval()
# OpenCV setup for video capture
cap = cv2.VideoCapture(0) # Use webcam (change the index if you have multiple cameras)
# CSV setup for saving detection results
csv_file = 'detection_results.csv'
csv_fields = ['timestamp', 'class', 'confidence']
# Initialize CSV file
with open(csv_file, 'w', newline='') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(csv_fields)
# Object detection loop
while True:
ret, frame = cap.read()
if not ret:
break
# Perform object detection
results = model(frame)
# Get detection information
detections = results.pandas().xyxy[0]
# Get the timestamp
timestamp = time.time()
# Save detection results in CSV
with open(csv_file, 'a', newline='') as f:
csv_writer = csv.writer(f)
for _, detection in detections.iterrows():
class_label = detection['name']
confidence = detection['confidence']
# Write to CSV
csv_writer.writerow([timestamp, class_label, confidence])
# Draw bounding boxes
bbox = detection[['xmin', 'ymin', 'xmax', 'ymax']].values.astype(int)
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
cv2.putText(frame, f'{class_label}: {confidence:.2f}', (bbox[0], bbox[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Display the frame with bounding boxes and labels
cv2.imshow('Object Detection', frame)
if cv2.waitKey(1) == 27: # Press Esc to exit
break
# Release resources
cap.release()
cv2.destroyAllWindows()