-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (46 loc) · 1.67 KB
/
Copy pathmain.py
File metadata and controls
65 lines (46 loc) · 1.67 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
import cv2
import time
from ultralytics import YOLO
# Load YOLO model
model = YOLO("yolov8n.pt")
# Start webcam
cap = cv2.VideoCapture(0)
# FPS calculation
prev_time = 0
while True:
ret, frame = cap.read()
if not ret:
break
# Run detection
results = model(frame)
for r in results:
for box in r.boxes:
cls = int(box.cls[0])
label = model.names[cls]
if label == "person":
x1, y1, x2, y2 = map(int, box.xyxy[0])
# Draw bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0,255,0), 2)
# Worker label
cv2.putText(frame, "Worker", (x1, y1-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
# 🔴 RED ALERT BOX (Professional UI)
cv2.rectangle(frame, (x1, y2+10), (x2, y2+40), (0,0,255), -1)
cv2.putText(frame, "NO HELMET", (x1+5, y2+30),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,255,255), 2)
# ALERT text (top)
cv2.putText(frame, "ALERT!", (50,50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 3)
print("⚠️ Safety Violation Detected!")
# 🟡 FPS DISPLAY
curr_time = time.time()
fps = 1 / (curr_time - prev_time) if prev_time != 0 else 0
prev_time = curr_time
cv2.putText(frame, f"FPS: {int(fps)}", (10,30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,0), 2)
# Window title
cv2.imshow("AI Worker Safety Monitoring System", frame)
if cv2.waitKey(1) == 27: # press ESC to exit
break
cap.release()
cv2.destroyAllWindows()