-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_detection.py
34 lines (31 loc) · 824 Bytes
/
motion_detection.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
import time
import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
print('Imports complete')
cap = cv2.VideoCapture(0)
prev = []
run = True
frames = []
fps_list = []
while run:
bef = time.time()
ret, current = cap.read()
grey = np.asarray(cv2.cvtColor(current, cv2.COLOR_BGR2GRAY))
if prev == []:
pass
else:
modified = 255-(grey - prev)
modified[modified > 200] = 0
cv2.imshow('Motion', modified)
time.sleep(0.022)
prev = grey
aft = time.time()
fps_list.append(1/(aft-bef))
if cv2.waitKey(1) & 0xFF == ord('q'):
run = False
x = [i+1 for i in range(len(fps_list))]
plt.plot(x, fps_list, color = 'orange', linewidth = 0.8)
plt.title(f'{round(np.average(fps_list),1)}')
plt.show()