-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.py
More file actions
82 lines (69 loc) · 3.22 KB
/
motion.py
File metadata and controls
82 lines (69 loc) · 3.22 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
from ultralytics import YOLO
import cv2
import time
import threading
import pygame
# Load YOLO models
model_yawn = YOLO(r"C:\Users\mihar\OneDrive\Documents\Deep Leaning Project\ultralytics\motion.pt")
model_eyes = YOLO(r"D:\Users\mihar\Downloads\best (7).pt")
# Audio control variables
close_alert_playing = False
audio_lock = threading.Lock()
def play_close_audio_once():
global close_alert_playing
with audio_lock: # Lock to ensure one audio at a time
pygame.mixer.init()
pygame.mixer.music.load(r"C:\Users\mihar\OneDrive\Documents\Deep Leaning Project\ultralytics\voice1.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
close_alert_playing = False # Reset only after audio is finished
def detect_with_model(model, frame, conf=0.25, label_color=(0, 255, 0)):
detections = []
results = model(frame, conf=conf, imgsz=640, device='cpu') # imgsz=320 to improve speed
for result in results:
if result.boxes is not None:
boxes = result.boxes.xyxy.cpu().numpy()
scores = result.boxes.conf.cpu().numpy()
class_ids = result.boxes.cls.cpu().numpy().astype(int)
for i, box in enumerate(boxes):
detections.append({
'box': box,
'score': scores[i],
'class_id': class_ids[i],
'class_name': model.names[class_ids[i]],
'color': label_color
})
return detections
def video_detectionn(frame, conf=0.25):
global close_alert_playing
start_time = time.time()
output_frame = frame.copy()
# Run both models
detections_yawn = detect_with_model(model_yawn, frame, conf, (255, 0, 0))
detections_eyes = detect_with_model(model_eyes, frame, conf, (0, 255, 0))
detected_class_names = []
total_detections = 0
close_confident_count = 0 # Count 'close' with confidence > 0.50
# Process detections
for det in detections_yawn + detections_eyes:
x1, y1, x2, y2 = map(int, det['box'])
label = f"{det['class_name']} {det['score']:.2f}"
cv2.rectangle(output_frame, (x1, y1), (x2, y2), det['color'], 2)
cv2.putText(output_frame, label, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, det['color'], 2)
detected_class_names.append(det['class_name'])
total_detections += 1
if det['class_name'].lower() == 'close' and det['score'] > 0.5:
close_confident_count += 1
# Trigger audio if two or more 'close' objects detected with score > 0.50
if close_confident_count >= 2 and not close_alert_playing:
close_alert_playing = True
threading.Thread(target=play_close_audio_once).start()
# FPS calculation
fps_x = int(1 / (time.time() - start_time + 1e-5))
cv2.putText(output_frame, f'Total Detected: {total_detections}', (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(output_frame, f'FPS: {fps_x}', (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
return [(output_frame, fps_x, output_frame.shape, total_detections)]