In: Computer Science
Use your webcam and record a movie, write a motion detection code and print the message "Motion Detected" on the original frame. Try to make a better visualization by using different masking.
---using Numpy and opencv
import numpy as np
import cv2 import time
sthresh = 12
fonts = cv2.font_Hershey_sim
# for facial detection, where f1 and f2 are frames
def distmp(f1, f2):"distance between two frames f1 and f2 using
Pythagorean theorem"
f1_32 = np.float32(f1)
f2_32 = np.float32(f2)
d32 = f1_32 - f2_32
n32 = np.sqrt(d32[:,:,0]**2 + d32[:,:,1]**2 +
d32[:,:,2]**2)/np.sqrt(255**2 + 255**2 + 255**2)
distance = np.uint8(n32*255)
return distance
cv2.named_window('frames')
cv2.named_window('distance')
#capturing video stream from web camera . 1 refers to first web
camera, 2 refers second web camera
capt = cv2.video_capture(1)
_, f1 = capt.read()
_, f2 = capt.read()
face_count = 0
while(1):
_, f3 = capt.read()
rows, cols, _ = np.shape(f3)
cv2.im_show('distance', f3)
distance = distmp(f1, f3)
f1 = f2
f2 = f3
# Using Gaussian smoothing approach
mod1 = cv2.GaussBlur(distance, (9,9), 0)
# for Thresholding purpose
_, thresh1 = cv2.thhold(mod1, 100, 255, 0)
# find test stdev
_, stdv = cv2.meanstdv(mod1)
cv2.im_show('distance', mod1)
cv2.put_txt(f2, "Std deviation - {}".format(round(stdv[0][0],0)),
(70, 70), font, 1, (255, 0, 255), 1, cv2.Line1_AA)
if stdv > sthresh:
print(""Motion Detected on the original frame");
# For face_detection second
cv2.im_show('frames', f2)
if cv2.wait_key(1) & 0xFF == 27:
break
capt.release()
cv2.destroy_all_windows()