-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_script.py
153 lines (65 loc) · 2.46 KB
/
main_script.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import cv2
import time
# start_time = time.time()
# # The thing to time. Using sleep as an example
# time.sleep(10)
# end_time = time.time()
# elapsed_time = end_time - start_time
# print("Hello there")
# Function to detect motion in the video frames
def detect_motion(frame1, frame2):
# Convert frames to grayscale
gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
# Calculate absolute difference between frames
frame_diff = cv2.absdiff(gray1, gray2)
# Apply threshold to get binary image
_, thresh = cv2.threshold(frame_diff, 30, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Check if motion is detected
motion_detected = False
for contour in contours:
if cv2.contourArea(contour) > 1000: # Adjust this threshold as needed
motion_detected = True
break
return motion_detected
# Function to detect falls
def detect_fall():
cap = cv2.VideoCapture(0)
# Read the first frame
ret, frame1 = cap.read()
if not ret:
print("Error: Couldn't read video source")
return
while True:
# Read the next frame
ret, frame2 = cap.read()
if not ret:
break
# Check for motion
motion_detected = detect_motion(frame1, frame2)
if motion_detected:
print("Motion detected!")
# Here you can implement additional logic to analyze the motion and detect falls
else:
print("No")
# start_time = time.time()
# # The thing to time. Using sleep as an example
# time.sleep(10)
# end_time = time.time()
# elapsed_time = end_time - start_time
# print("Hello there")
# Update frame1 to next frame
frame1 = frame2
# Display the frames
cv2.imshow('Video', frame2)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Main function
if __name__ == "__main__":
# Call the function to detect falls
detect_fall()