In this post I will show you an amazing usecase of opencv module with the help of which we will play alarm sound when there is a movement infront of your web cam. FROM SCRATCH using python programming language.
To build your own you need to have a little knowledge of opencv module which is a Python library helps to build Machine learning and Artificial Intelligence projects. It can process images and videos to identify objects, faces, or even the handwriting of a human. Here in this project it will help to detect the motion.
And another module that we will be using is playsound. Which will help us to play alarm sound when there is any moving thing detected.
Coding our motion detector using python
Let me first show you that it only requires 10-12 lines of code to make a very basic motion detector. And 2 lines to play sound. Then step step I will try to make you understand what each line does.
You need to know about few functions and understand what they does then you too will feel comfortable with it.
Installation of required modules and libraries
In this project we are only dealing with opencv and playsound. You need to open your terminal (linux) or comand prompt (windows) and type
#for installing opencv pip install opencv-python #for installing playsound pip install playsound
If You Face Errors in running this code. Here are some solutions to them.
Opening Camera to record
You can use you laptop web camera or any other camera to detect motion. But it must be connected to your system. You can connect a cctv camera too and take it to a next level.
#If you use web camera (default) - 0 cam = cv2.VideoCapture(0) #If you use any other camera - 1 cam = cv2.VideoCapture(1)
Intuition behind motion detector
To detect moving object we will doing nothing but see the differences in the frame. As you know if you follow my other opencv and Ml projects A video is of multiple images(frame) played together in a row. So here we will see if there if any difference in frame, if yes then there must be some moving thing going on.
for infinite loop we will use while loop. Untill our camera is opened (which will pass True) our loop will run continiously.
Using read() function we will have first frame and second frame in frame1 and frame2.
ret, frame1 = cam.read() ret, frame2 = cam.read()
Now once we have two frames we need to see whether there is any motion or not. So using absdiff() which discribes itself that it gives absoute difference in frames. And cvtColor() convert image into other channels(here coloured to black and white)
And for GaussianBlur() I think it don’t need any explaination.
diff = cv2.absdiff(frame1, frame2) gray = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY) blur = cv2.GaussianBlur(gray,(5,5),0)
The basic Thresholding technique is Binary Thresholding. For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value.
_,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_SIMPLE) for c in contours: if cv2.contourArea(c) < 5000: continue
cv2.boundingRect() function of OpenCV is used to draw an approximate rectangle around the image.
x,y,w,h = cv2.boundingRect(c) cv2.rectangle(frame1, (x,y),(x+w,y+h),(0,255,0),2)
that’s it !
CONGRATULATE yourself on your first detection project.
Now just tell the world playsound that you are successful!
download a sound that you want to play and put it inside playsound()
playsound('paste the absolute path to your sound file here')
You can tweek in this project and make it up to your fit. You can make a security camera using this. When there is any abnormality detected in camera, alarm will start or something like that. You can make a student checting detector or car moving in road and there are many possibities using this.