Reader small image

You're reading from  Qt 5 and OpenCV 4 Computer Vision Projects

Product typeBook
Published inJun 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789532586
Edition1st Edition
Languages
Right arrow
Author (1)
Zhuo Qingliang
Zhuo Qingliang
author image
Zhuo Qingliang

Zhuo Qingliang (a.k.a. KDr2 online) is presently working at Beijing Paoding Technology Co. LTD., a start-up Fintech company in China that is dedicated to improving the financial industry by using artificial intelligence technologies. He has over 10 years experience in Linux, C, C++, Python, Perl, and Java development. He is interested in programming, doing consulting work, participating in and contributing to the open source community (of course, includes the Julia community).
Read more about Zhuo Qingliang

Right arrow

Home Security Applications

In Chapter 2, Editing Images Like a Pro, we learned about the plugin mechanism of the Qt library and many image filters and transformations from the OpenCV library by building our own image editor application. In this chapter, we will move on from working with images to working with videos. We will build a new application with which we can do many things with the webcam of a PC, such as play video that's been captured from it in real time, record portions of video from its video feed, calculate its frames per second (FPS), detect motion by doing motion analysis on its video feed in real time, and more.

The following topics will be covered in this chapter:

  • Designing and creating the user interface (UI)
  • Handling cameras and videos
  • Recording videos
  • Calculating the FPS in real time
  • Motion analysis and movement detection
  • Sending notifications to a mobile...

Technical requirements

As we saw in the previous chapters, you are required to have Qt version 5 (at least) installed and have basic knowledge of C++ and Qt programming. Also, the latest version of OpenCV (4.0) should be correctly installed. Besides the core and imgproc modules, the video and videoio modules of OpenCV will also be used in this chapter. Following the previous chapters, these requirements must be already met.

In this chapter, we will show you how to handle cameras, so you need to have a webcam, either a built-in one or an external one, which can be accessed from your computer.

A basic knowledge of multi-threading is also a requirement of this chapter.

All the code for this chapter can be found in this book's GitHub repository at https://github.com/PacktPublishing/Qt-5-and-OpenCV-4-Computer-Vision-Projects/tree/master/Chapter-03.

Check out the following video...

The Gazer application

In order to delve into camera handling, video processing, and motion analysis, we will develop a brand new application. Besides learning about these topics, we will also get an application that has many pragmatic features: being able to record video through a webcam, monitor for our home security, and notify us on our mobile if a suspicious motion is detected. Let's clarify its features, which are as follows:

  • Open a webcam and play the video that's been captured from it in real time
  • Record video from the webcam by clicking on a start/stop button
  • Show a list of saved videos
  • Detect motion, save video, and send notifications to our mobile phone if suspicious motion is detected
  • Show some information about the cameras and the application's status

After these features have been clarified, we can design the UI. Again, we will use the open source...

Accessing cameras

In the preceding section, we set up the UI of our application. In this section, we will play the video feed that's provided by a camera from a personal computer.

Before accessing a camera, we should find out some information about it—if we use OpenCV, we need the index of the camera that we want to capture a video from; if we use Qt, we need the device name of it. For a typical laptop, it usually has one default built-in webcam whose index is 0, but its name is platform or environment dependent. If we have multiple webcams for a computer, usually both of their indices and names are platform or environment dependent. To determine this information, we can turn to the QCameraInfo class from the Qt library.

Listing cameras with Qt

...

Saving videos

In the preceding section, we learned how to access the cameras attached to our computer, as well as getting the information of all the cameras, playing the video that was captured from a camera in real time, and calculating the frame rate of a camera. In this section, we will learn how to record videos from the camera.

The principle of recording videos is simple: while we are capturing frames from a camera, we compress each frame in a certain way and write it to a video file. The VideoWriter class in the videoio module of the OpenCV library provides a convenient way to do this, and we will use it to record videos in this section.

Before we start with the work of recording videos, we should do some preparation work for our application, for example, where to save the video and how to name each video file. To resolve these prerequisites, we will create an assistant...

Motion analysis with OpenCV

In the previous sections, we built a complete application for playing and saving videos with our cameras. But for a home security application, that's not enough. We must know the situation at our home while something is happening there. This will be done by using the motion detection feature that's provided by OpenCV.

Motion detection with OpenCV

Usually, motion detection is done by the segmentation of background and foreground content in images. Because of that, while detecting motion, we usually assume that the background part of the given scene that appears in our camera is static and will not change over consecutive frames of the video. By analyzing these consecutive frames, we can...

Summary

In this chapter, we created a new desktop application, the Gazer, for capturing, playing, and saving videos from our cameras. For home security purposes, we also added a feature for motion detection. We built the UI with Qt and developed the video processing features with OpenCV. These two parts were integrated into our application organically. In the development of this application, we learned about how to use the Qt layout system to arrange the widgets on the UI, how to use multithreading technology to do slow work in a thread that differs from the main UI thread, how to detect motion using OpenCV, and how to send notifications to our mobile phone via IFTTT by firing a HTTP request.

In the next chapter, we will learn how to recognize faces in images or videos in real time, and we will build an interesting application so that we can put funny masks on the detected faces...

Questions

Try these questions to test your knowledge of this chapter:

  1. Can we detect motions from a video file instead of a camera? How can we do that?
  2. Can we do the motion detecting work in a thread that differs from the video capturing thread? Why or why not?
  3. IFTTT allows you to include images in the notifications it sends—How could we send an image with the motion we detected while sending notifications to your mobile phone via this feature of IFTTT?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Qt 5 and OpenCV 4 Computer Vision Projects
Published in: Jun 2019Publisher: PacktISBN-13: 9781789532586
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Zhuo Qingliang

Zhuo Qingliang (a.k.a. KDr2 online) is presently working at Beijing Paoding Technology Co. LTD., a start-up Fintech company in China that is dedicated to improving the financial industry by using artificial intelligence technologies. He has over 10 years experience in Linux, C, C++, Python, Perl, and Java development. He is interested in programming, doing consulting work, participating in and contributing to the open source community (of course, includes the Julia community).
Read more about Zhuo Qingliang