-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (38 loc) · 1.47 KB
/
Copy pathmain.py
File metadata and controls
54 lines (38 loc) · 1.47 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
import cv2
import cvzone
import math
from ultralytics import YOLO
cap = cv2.VideoCapture('fall.mp4')
model = YOLO('yolov8s.pt')
classnames = []
with open('classes.txt', 'r') as f:
classnames = f.read().splitlines()
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, (980,740))
results = model(frame)
for info in results:
parameters = info.boxes
for box in parameters:
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
confidence = box.conf[0]
class_detect = box.cls[0]
class_detect = int(class_detect)
class_detect = classnames[class_detect]
conf = math.ceil(confidence * 100)
# implement fall detection using the coordinates x1,y1,x2
height = y2 - y1
width = x2 - x1
threshold = height - width
if conf > 80 and class_detect == 'person':
cvzone.cornerRect(frame, [x1, y1, width, height], l=30, rt=6)
cvzone.putTextRect(frame, f'{class_detect}', [x1 + 8, y1 - 12], thickness=2, scale=2)
if threshold < 0:
cvzone.putTextRect(frame, 'Fall Detected', [height, width], thickness=2, scale=2)
else:pass
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('t'):
break
cap.release()
cv2.destroyAllWindows()