Master Python through building real-world applications (Part 8)

We have to use the cv2.

imshow method and iterate just like we did in batch image resizing.

Here, waitKey(1) means it is taking frames every 1 millisecond.

And when you’ll type q it will break the loop and stop capturing the video.

Here is how I look while the webcam is on.

I know, I know, I look funny *facepalm* You can laugh but also make sure you see how the frames change, rapidly.

Now that we know how to capture video, let’s learn how to detect objects in real-time, including faces.

Step 5— Motion DetectorLet us first understand theoretically how it works and then we will move towards the code.

If you don’t understand something, keep one reading, once you are at the end of the step, everything will make sense.

Why a motion detector?To be honest, it is easiest to start with.

Also, it’s useful in some scenarios.

For example, if you want to monitor your baby or your dog (whichever is more important to you.

Kidding, it’s the dog of course) while you are away.

It has two steps in that.

First is detect objects in real time and record time when the object is entering and exiting the frame.

First — Object DetectionCapture a frame where the background is a static/empty frame and use this image as a base image so that we compare it with other images and detect if there’s a change between the first frame and the next several frames.

The base image is set.

Now, as soon as an object comes in the frame we’ll gray it out.

You’ll notice in the delta frame that first there was a static background (black) and now there’s a change (gray).

See the 2nd delta frame where I used a static frame which included me and then moved a little in the second frame, that’s how it knows that the object is moved.

3.

And then there is a more enhanced version of the delta frame which is the threshold frame.

If the difference between the previous frame and this frame is more than, let’s say, 100 intensity, convert those pixels to white.

As you can see, my face is in white pixels while the background is black.

4.

Basically, that’s how we detect if an object is moving or not.

We find the contours (points which moved) of the white image and the area of the contour is more than 500 pixels, then we will consider this as a moving object.

And mark those objects with a rectangle.

Let’s look at the code and comments and see how it works.

Second — Recording Motion TimeWe’ll assume we are monitoring a dog.

We have got a camera setup where he usually sits.

Now, we will record the time to know how many times he was out of his spot to mess up with the house.

To do that, we first have to define points when the object is entering into the frame and exiting the frame.

We will create an object called status and we will set it equal to 0 and 1 (line 20 and 47, respectively).

Now we will store those values into a list (line 52).

Which will look something like shown below.

First two are none because it is a static frame.

Then series of zeros because there’s nothing in the frame and then series of ones because there’s an object and so on.

So many zeros and ones look beautiful.

But we don’t need all of them.

We only want to record when the object enters and exits from the frame.

So what we will do is, we will compare last and second last element every time to see if it’s changed from 0 to 1 or 1 to 0.

If it is changed, then only we record the time (line 54–57).

And finally, we append the time to the data frame and save it to our local machine.

Mine looks something like this.

You can format it according to your needs.

End NotesComputer Vision is the field where a lot of advances take place every day.

I am sure you are aware of some of them.

In addition to that, you now know how to manipulate images, how to recognize a face in the images, how to detect objects in real-time (real-world application: self-driving cars) and how to record time of the object entering and exiting the frame.

Basically, you are now a step ahead than you were yesterday, transforming your theoretical/conceptual knowledge to practical knowledge.

Pat yourself on the back.

However; if you have any doubts/feedback regarding this, feel free to reach out to me via Email, Twitter or Linkedin.

All the files including images are available on my Github repository.

Read my other post of this series from the links below.

Happy Learning.

__Part 1 Building an Interactive DictionaryPart 2 Creating Web Maps using FoliumPart 3 Building a Website BlockerPart 4 Build and Deploy a Website using Flask and Heroku AppPart 5 Twitter Sentiment Analysis in 3 MinutesPart 6 Scraping data from FIFA.

com using BeautifulSoupPart 7 Data Collector Web Application using PostgreSQL and FlaskPart 8 Face and Motion Detector using OpenCV.

. More details

Leave a Reply