-
Notifications
You must be signed in to change notification settings - Fork 1
/
motion_detection.py
63 lines (59 loc) · 2.54 KB
/
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
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
"""
@file: motion_detection.py
@author: Nish Gowda
The purpose of this program is to compare each frame by
computing the difference between their rgb values for every pixel.
This is later applied to video_splice.py to connect each frame to
a full video.
"""
import cv2
import numpy as np
from PIL import Image
import collections
from tqdm import tqdm
import time
class MotionDetection:
def __init__(self):
self.total_diff = 0.0
self.frames = {}
# computes the rgb value for each pixel in each frame
def compare_frames(self, videopath):
vid = cv2.VideoCapture(videopath)
video_length = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
old_frame = None
for i in tqdm(range(video_length)):
ret, frame = vid.read()
if not ret:
break
pilimg = Image.fromarray(frame)
prev_frame = cv2.cvtColor(frame, cv2.COLOR_HSV2RGB)
pilimg2 = Image.fromarray(prev_frame)
img = np.array(pilimg)
prev_img = np.array(pilimg2)
img_width = img.shape[0]
img_height = img.shape[1]
num_pixels = img.size
# compare the rgb value of each pixel between the current and previous frames
diff_r, diff_g, diff_b = 0.0, 0.0, 0.0
if old_frame is not None:
# .split grabs the rgb (in order of grb) of the img
colors_b1, colors_g1, colors_r1 = cv2.split(img)
colors_b2, colors_g2, colors_r2 = cv2.split(prev_img)
# grab the sums of each channels difference -- matrix or np array and divide by 255
diff_r += np.sum(colors_r1 - colors_r2) / 255.0
diff_g += np.sum(colors_g1 - colors_g2) / 255.0
diff_b += np.sum(colors_b1 - colors_b2) / 255.0
# divide each difference by num pixels to get the avg difference per chanel
diff_r /= num_pixels
diff_g /= num_pixels
diff_b /= num_pixels
# get the avg of all three channels
self.total_diff = (diff_r + diff_g + diff_b) / 3.0
self.total_diff = round(self.total_diff * 100)
# update our dictionary to grab the difference of the current and previous frame as well as the current frame
self.frames.update({self.total_diff : frame})
old_frame = prev_frame
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
cv2.destroyAllWindows()