Face Detection and Tracking in a Video Stream
In this tutorial, you'll learn how to detect and track faces in a video stream from your camera using the VideoWorker
object from Face SDK API. Tracked faces are highlighted with a green rectangle.
Besides Face SDK and Qt, you'll need a camera connected to your PC (for example, a webcam). You can build and run this project either on Windows or on Ubuntu (v16.04 or higher).
You can find the tutorial project in Face SDK: examples/tutorials/detection_and_tracking_with_video_worker
#
Creating a Qt Project- Run Qt and create a new project: File > New File or Project > Application > Qt Widgets Application > Choose...
- Name it, for example, 1_detection_and_tracking_with_video_worker and choose the path. Click Next and choose the necessary platform for you project in the Kit Selection section, for example, Desktop. Click Details and select the Release build configuration (we don't need Debug in this project).
- In the Class Information window, leave settings as default and click Next. Then, leave settings as default in the Project Management window and click Finish.
- Let's title the main window of our app: in the project tree, double-click the file Forms > mainwindow.ui. Specify the name of the window in the Properties tab (the right part of the editor): windowTitle > Face SDK Tracking.
- To lay out widgets in a grid, drag-and-drop the Grid Layout object to the MainWindow widget. Call context menu of MainWindow by right-clicking and select Layout > Lay Out in a Grid. The Grid Layout object will be stretched to the size of the MainWindow widget. Rename the Layout: layoutName > viewLayout.
- To run the project, click Run (Ctrl+R). You'll see an empty window with the title Face SDK Tracking.
#
Displaying the Image from Camera- In order to use a camera in our project, we have to add Qt multimedia widgets. To do this, add the following line to the .pro file:
detection_and_tracking_with_video_worker.pro
- Create a new class
QCameraCapture
to receive the image from a camera: Add New > C++ > C++ Class > Choose… > Class name – QCameraCapture > Base class – QObject > Next > Project Management (default settings) > Finish. Create a new classCameraSurface
in the fileqcameracapture.h
, which will provide the frames from camera via thepresent
callback.
qcameracapture.h
- Describe the implementation of this class in the file
qcameracapture.cpp
. Designate theCameraSurface::CameraSurface
constructor and thesupportedPixelFormats
method. InCameraSurface::supportedPixelFormats
, list all the image formats supported by Face SDK (RGB24, BGR24, NV12, NV21). With some cameras, the image is received in the RGB32 format, so we add this format to the list. This format isn't supported by Face SDK, so we'll convert the image from RGB32 to RGB24.
qcameracapture.cpp
- In the
CameraSurface::start
method, check the image format. Start the camera, if the format is supported, otherwise handle the exception.
qcameracapture.cpp
- In the
CameraSurface::present
method, process a new frame. If the frame is successfully verified, send the signalframeUpdatedSignal
to update the frame. Next, we'll connect this signal to the slotframeUpdatedSlot
, where the frame will be processed.
qcameracapture.cpp
- The
QCameraCapture
constructor takes the pointer to a parent widget (parent
), camera id and image resolution (width and height), which will be stored in the relevant class fields.
cameracapture.h
- Add the camera objects
m_camera
andm_surface
to theQCameraCapture
class.
qcameracapture.h
- Include the
stdexcept
header file toqcameracapture.cpp
to throw exceptions. Save the pointer to a parent widget, camera id and image resolution in the initializer list of the constructorQCameraCapture::QCameraCapture
. In the constructor body, get the list of available cameras. The list of cameras should contain at least one camera, otherwise, theruntime_error
exception will be thrown. Check that the camera with the requested id is in the list. Create a camera and connect the camera signals to the slots processing the object. When the camera status changes, the camera sends thestatusChanged
signal. Create theCameraSurface
object to display the frames from the camera. Connect the signalCameraSurface::frameUpdatedSignal
to the slotQCameraCapture::frameUpdatedSlot
.
qcameracapture.cpp
- Stop the camera in the destructor
QCameraCapture
.
qcameracapture.h
qcameracapture.cpp
- Add the method
QCameraCapture::frameUpdatedSlot
, which processes the signalCameraSurface::frameUpdatedSignal
. In this method, we convert theQVideoFrame
object toQImage
and send a signal that a new frame is available. Create a pointer to the imageFramePtr
. If the image is received in the RGB32 format, convert it to RGB888.
qcameracapture.h
qcameracapture.cpp
- Add the methods to start and stop the camera to
QCameraCapture
.
qcameracapture.h
qcameracapture.cpp
- In the
QCameraCapture::onStatusChanged
method, process the change of the camera status toLoadedStatus
. Check if the camera supports the requested resolution. Set the requested resolution, if it's supported by the camera, otherwise set the default resolution (640 x 480), specified by the static fieldsdefault_res_width
,default_res_height
.
qcameracapture.h
qcameracapture.cpp
- In the
cameraError
method, display the camera error messages if they occur.
qcameracapture.h
qcameracapture.cpp
- Create a new class
Worker
: Add New > C++ > C++ Class > Choose… > Class name - Worker > Next > Finish. Through theaddFrame
method, theWorker
class will save the last frame from the camera and pass this frame through thegetDataToDraw
method.
worker.h
worker.cpp
- Frames will be displayed in the
ViewWindow
class. Create a widget ViewWindow: Add > New > Qt > Designer Form Class > Choose... > Template > Widget (default settings) > Next > Name – ViewWindow > Project Management (default settings) > Finish. - In the editor (Design), drag-and-drop the Grid Layout object to the widget. To do this, call context menu of ViewWindow by right-clicking and select Layout > Lay Out in a Grid. The Grid Layout object allows you to place widgets in a grid and is stretched to the size of the ViewWindow widget. Then, add the Label object to gridLayout and name it frame: QObject > objectName > frame.
- Delete the default text in QLabel > text.
- Add the camera
_qCamera
to theViewWindow
class and initialize it in the constructor. Using the static fieldscamera_image_width
andcamera_image_height
, set the required image resolution to 1280x720. The_running
flag stores the status of the camera:true
means that the camera is running,false
- the camera is stopped.
viewwindow.h
viewwindow.cpp
- Add the
Worker
object to theViewWindow
class and initialize it in the constructor.
viewwindow.h
viewwindow.cpp
- Frames will be passed to
Worker
fromQCameraCapture
. Modify theQCameraCapture
andViewWindow
classes.
qcameracapture.h
qcameracapture.cpp
viewwindow.cpp
- The
QCameraCapture::newFrameAvailable
signal is processed in theViewWindow::draw
slot, which displays the camera image on the frame widget.
viewwindow.h
viewwindow.cpp
- Start the camera in the
runProcessing
method and stop it instopProcessing
.
viewwindow.h
viewwindow.cpp
- Stop the camera in the desctructor
~ViewWindow
.
viewwindow.cpp
- Connect the camera widget to the main application window: create a view window and start processing in the
MainWindow
constructor. Stop the processing in the~MainWindow
destructor.
mainwindow.h
mainwindow.cpp
- Modify the
main
function to catch possible exceptions.
main.cpp
- Run the project. You should see a window with the image from your camera.
Note: On Windows, the image from some cameras can be flipped or mirrored, which happens due to some peculiarities of the image processing by Qt. In this case, you'll need to process the image, for example, using QImage::mirrored().
#
Detecting and Tracking Faces in Video Stream- Download and extract the Face SDK distribution as described in the section Getting Started. The root folder of the distribution should contain the bin and lib folders, depending on your platform.
- To detect and track faces on the image from your camera, you have to integrate Face SDK into your project. In the .pro file, specify the path to the Face SDK root folder in the variable
FACE_SDK_PATH
, which includes necessary headers. Also, specify the path to theinclude
folder (from Face SDK). If the paths are not specified, the exception “Empty path to Face SDK” is thrown.
detection_and_tracking_with_video_worker.pro
Note: When specifying the path to Face SDK, please use a slash ("/").
- [Linux only] To build the project with Face SDK, add the following option to the .pro file:
detection_and_tracking_with_video_worker.pro
- Besides, we have to specify the path to the
facerec
library and configuration files. Create theFaceSdkParameters
class, which will store the configuration (Add New > C++ > C++ Header File > FaceSdkParameters) and use it inMainWindow
.
facesdkparameters.h
mainwindow.h
- Integrate Face SDK: add necessary headers to
mainwindow.h
and theinitFaceSdkService
method to initialize the Face SDK services. Create aFacerecService
object, which is a component used to create the Face SDK modules, by calling theFacerecService::createService
static method. Pass the path to the library and path to the folder with the configuration files in atry-catch
block in order to catch possible exceptions. If the initialization was successful, theinitFaceSdkService
function will returntrue
, otherwise, it'll returnfalse
and you'll see a window with an exception.
mainwindow.h
mainwindow.cpp
- In the
MainWindow::MainWindow
constructor, add a service initialization call. In case of an error, throw thestd::runtime_error
exception.
mainwindow.cpp
- Pass
FacerecService
and Face SDK parameters to theViewWindow
constructor, where they'll be used to create theVideoWorker
tracking module. Save the service and parameters to the class fields.
mainwindow.cpp
viewwindow.h
viewwindow.cpp
- Modify the
Worker
class for interaction with Face SDK. TheWorker
class takes theFacerecService
pointer and name of the configuration file of the tracking module. TheWorker
class creates theVideoWorker
component from Face SDK, which is responsible for face tracking, passes the frames to it and processes the callbacks, which contain the tracking results. Imlement the constructor – create the objectVideoWorker
, specifying the configuration file, recognizer method (in our case, it's empty because we don't recognize faces in this project), number of video streams (it's 1 in our case because we use only one camera).
worker.h
worker.cpp
Note: In addition to the face detection and tracking, VideoWorker
can be used for face recognition on several video streams. In this case, you have to specify the recognizer method and the streams processing_threads_count
and matching_threads_count
.
- Subscribe to the callbacks from the
VideoWorker
class –TrackingCallback
(a face is detected and tracked),TrackingLostCallback
(a face was lost). Delete them in the destructor.
worker.h
worker.cpp
- Include the
cassert
header to handle exceptions. InTrackingCallback
, the result is received in the form of theTrackingCallbackData
structure, which stores data about all faces, which are being tracked. The preview output is synchronized with the result output. We cannot immediately display the frame, which is passed toVideoWorker
, because it'll processed a little later. Therefore, frames are stored in a queue. When we get a result, we can find a frame that matches this result. Some frames may be skipped byVideoWorker
under heavy load, which means that sometimes there's no matching result for some frames. In the algorithm below, the image corresponding to the last received frame is extracted from the queue. Save the detected faces for each frame so that we can use them later for visualization. To synchronize the changes of shared data inTrackingCallback
andTrackingLostCallback
, we usestd::mutex
.
worker.h
worker.cpp
- Implement
TrackingLostCallback
, in which we mark that the tracked face left the frame.
worker.cpp
VideoWorker
receives the frames via thepbio::IRawImage
interface. Create theVideoFrame
header file: Add New > C++ > C++ Header File > VideoFrame. Include it to the filevideoframe.h
and implement thepbio::IRawImage
interface for theQImage
class. Thepbio::IRawImage
interface allows to get the pointer to image data, its format, width and height.
videoframe.h
- In the
addFrame
method, pass the frames toVideoWorker
. If there are any exceptions during the callback processing, they're thrown again in thecheckExceptions
method. Create the_frames
queue to store the frames. This queue will contain the frame id and the corresponding image, so that we can find the frame, which matches the processing result inTrackingCallback
. To synchronize the changes in shared data, we usestd::mutex
.
worker.h
worker.cpp
- Modify the
getDataToDraw
method - we won't draw the faces, for whichTrackingLostCallback
was called.
worker.cpp
- Modify the
QCameraCapture
class to catch the exceptions, which may be thrown inWorker::addFrame
.
qcameracapture.cpp
- Create the
DrawFunction
class, which will contain a method to draw the tracking results in the image: Add New > C++ > C++ Class > Choose… > Class name – DrawFunction.
drawfunction.h
drawfunction.cpp
- In the
ViewWindow
constructor, pass theFacerecService
pointer and the name of the configuration file of the tracking module when creatingWorker
. In theDraw
method, draw the tracking result in the image by callingDrawFunction::Draw
.
viewwindow.cpp
- Run the project. Now you should see that faces in the image are detected and tracked (they're highlighted with a green rectangle). You can find more info about using the
VideoWorker
object in the section Video Stream Processing.