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 OpenCv, pandas,numpy
code be written in python,
cap=cv2.VideoCapture("vtest.avi")
ret,frame1=cap.read()
ret,frame2=cap.read()
while cap.isOpened():
diff=cv2.absdiff(frame1,frame2)
gray=cv2.cvtColor(diff,cv2.COLOR_BGR2GRAY)
blur=cv2.GaussianBlur(gray,(5,5),0)
_,thresh=cv2.threshold(blur,20,255,cv2.THRESH_BINARY)
dilated=cv2.dilate(thresh,None,iterations=3)
_,contours,_=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(frame1,contours,-1,(0,0,255),4)
for contour in contours:
(x,y,w,h)=cv2.boundingRect(contour)
if cv2.contourArea(contour)<700:
continue
else:
cv2.rectangle(frame1,(x,y),(x+w,y+h),(0,255,0),3)
cv2.putText(frame1,"Status :
Movement",(10,500),cv2.FONT_HERSHEY_SIMPLEX,2,(255,0,0),5)
cv2.imshow("video",frame1)
cv2.imshow("difference",diff)
cv2.imshow("difference gray",gray)
cv2.imshow("difference gray blurred",blur)
cv2.imshow("difference gray blurred thresholded",thresh)
cv2.imshow("difference gray blurred thresholded
dilated",dilated)
frame1=frame2
ret,frame2=cap.read()
if cv2.waitKey(60)==27:
break
cv2.destroyAllWindows()
cap.release()
## The code needs cv2 , numpy as np imports .
## The code detects motion in the avi.test video and draws rectangular boxes on the the contours with area >700
#The video for this code is vtest.avi which can be found online (open cv motion detection video)