-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject_detection.py
More file actions
103 lines (81 loc) · 3.57 KB
/
Object_detection.py
File metadata and controls
103 lines (81 loc) · 3.57 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import cv2
import sys
from ultralytics import YOLO
class VideoPersonDetector:
def __init__(self, input_video="Sample_Video.mp4", output_video="Sample_Video_Detected.mp4", model_path="yolov8n.pt"):
# --- Paths ---
self.base_dir = os.path.dirname(os.path.abspath(__file__))
self.input_path = os.path.join(self.base_dir, input_video)
self.output_path = os.path.join(self.base_dir, output_video)
# --- Load YOLO model ---
self.model = YOLO(model_path)
# --- Video Capture ---
self.cap = cv2.VideoCapture(self.input_path)
if not self.cap.isOpened():
raise FileNotFoundError(f"Could not open video file: {self.input_path}")
# --- Video Properties ---
self.total_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.fps = int(self.cap.get(cv2.CAP_PROP_FPS))
self.width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.fourcc = cv2.VideoWriter_fourcc(*"mp4v")
# --- Output Writer ---
self.out = cv2.VideoWriter(self.output_path, self.fourcc, self.fps, (self.width, self.height))
def print_video_info(self):
print("Video Information:")
print(f" - Resolution : {self.width}x{self.height}")
print(f" - FPS : {self.fps}")
print(f" - Total Frames : {self.total_frames}")
print(" - Detecting : person\n")
def detect_persons(self):
"""Run YOLOv8 on the video and save annotated output."""
self.print_video_info()
frame_count = 0
print("Starting detection...\n")
while True:
ret, frame = self.cap.read()
if not ret:
break
frame_count += 1
# Run YOLO detection silently
results = self.model.predict(source=frame, verbose=False, stream=True)
for r in results:
for box in r.boxes:
cls = int(box.cls[0])
label_name = self.model.names[cls]
# Only detect "person" class
if label_name != "person":
continue
x1, y1, x2, y2 = map(int, box.xyxy[0])
conf = float(box.conf[0])
label = f"Person {conf:.2f}"
# Draw detection box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 1)
cv2.putText(frame, label, (x1, max(20, y1 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
# Write to output
self.out.write(frame)
# Show live window
cv2.imshow("Person Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
print("\nDetection stopped by user")
break
# Show progress
progress = (frame_count / self.total_frames) * 100
sys.stdout.write(f"\rProcessing frame {frame_count}/{self.total_frames} ({progress:.1f}% done)")
sys.stdout.flush()
self.cleanup()
def cleanup(self):
"""Release all resources."""
self.cap.release()
self.out.release()
cv2.destroyAllWindows()
print("\nHuman detection completed successfully!")
print(f"Output video saved at: {self.output_path}")
if __name__ == "__main__":
detector = VideoPersonDetector(
input_video="Sample_Video.mp4",
output_video="Sample_Video_Detected.mp4",
model_path="yolov8n.pt"
)
detector.detect_persons()