Computer Vision Security System
This is a cool project that uses computer vision techniques and a webcam to act as a security system. I will demonstrate how to detect if a door is opened then have my computer yell at the opener. For this all you need is a webcam, python, and the modules opencv and pyttsx.A brief overview:- Use background subtraction to see if the door gets moved- Then have the pyttsx module make your computer say whatever you want at the person who opened itThe opencv background subtractors weren't working for me so I made my own.https://spaces.aperture.science/p/?n=computer-vision-security-system&editto the top of the code around the other imports. Then switch out theline with(or whatever else you want it to say inside the quotes). The talked variable makes it so it only talks once each time the door opens and doesn't keep repeating. And if you're wondering where the proxy._driver came from, the original pyttsx API wasn't working properly so I had to do some reverse engineering on it.
Door Movement
Opencv has a nice tutorial on their background subtractor, but I'll be using the one I made. Grabbing a frame from the webcam, putting it into the background subtractor, then displaying it looks something like thisimport background_subtractor
import cv2
import numpy as np
import video
cap=video.create_capture(0) #get your webcam
#####wait until a good frame comes in####
ret=False
while not ret:
ret, frame=cap.read()
#########################################
frame=cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
bs=background_subtractor.BackgroundSubtractor(frame)
while True:
#get a grayscale frame from webcam
frame=cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
foreground_mask=bs.apply(frame)
cv2.imshow("frame", frame)
cv2.imshow("mask", foreground_mask)
k=cv2.waitKey(10) #delay 10 miliseconds and get key press
if k==1048690: #the 'r' key; will probably be 114 for you
bs.update_reference_frame()
if k==1048603: #the 'esc' key; will probably be 27 for you
cv2.destroyAllWindows()
quit()
Make It Talk!
All we have to do now is use the text to speech module. To do so, addimport pyttsx
engine=pyttsx.init()
talked=False
print "DOOR IS OPEN"
if not talked:
engine.proxy._driver.say("Go away!")
talked=True
else:
if talked:
talked=False