Face Recognition in a Video Stream
In this tutorial you'll learn how to recognize faces in a video stream. For recognition, use a ready-made database of faces from Face SDK distribution package. The database includes the images of several famous people. Recognized faces are highlighted with a green rectangle. The name and image of a recognized person are displayed next to the person's face in a video stream. This tutorial is based on Face Detection and Tracking in a Video Stream and the corresponding project.
You can find the tutorial project in Face SDK: examples/tutorials/face_recognition_with_video_worker
#
Set Up the Project- In Face Detection and Tracking in a Video Stream, we set only two parameters of Face SDK (a path to Face SDK and a configuration file name for the
VideoWorker
object). However, in this tutorial we need to set several more parameters: we'll add a path to the database, a configuration file name with a recognition method, and FAR. For convenience, we'll modify several files. Specify all the parameters in theFaceSdkParameters
structure. Infacesdkparameters.h
specify the path to thevideo_worker_lbf.xml
configuration file.
facesdkparameters.h
- Pass the
face_sdk_parameters
structure to the constructor of theWorker
object.
viewwindow.h
viewwindow.cpp
worker.h
worker.cpp
- In this project we focus only on face detection in a video stream (creating a bounding rectangle) and face recognition. Note that in the first project (
detection_and_tracking_with_video_worker
), which you can use as a reference for this project, we also displayed anthropometric points and angles. If you don't want to display this information, you can just remove unnecessary visualization from the first project.
#
Create the Database of FacesFirst of all, create a database of faces. To check face recognition, you can use the ready-made database from Face SDK. This database includes images of three famous people (Elon Musk, Emilia Clarke, Lionel Messi). To check recognition, copy the database to the project root folder (next to a .pro file), run the project, open an image from the database, and point a camera at the screen. You can also add your picture to the database. To do this, create a new folder in the database, specify your name in a folder name, and copy your picture to the folder (in the same way as other folders in the database).
Create a new
Database
class to work with the database: Add New > C++ > C++ Class > Choose... > Class name โ Database > Next > Project Management (default settings) > Finish. Indatabase.h
include theQImage
andQString
headers to work with images and strings andlibfacerec.h
to integrate Face SDK.
database.h
- In
database.cpp
include thedatabase.h
andvideoframe.h
headers (implementation of theIRawImage
interface, used byVideoWorker
to receive the frames). Also, include necessary headers for working with the file system, debugging, exception handling, and working with files.
database.cpp
- In
database.h
add a constructor and set the path to the database. Specify theRecognizer
object to create templates, and specify theCapturer
object to detect faces andfar
. FAR is frequency that the system makes false accepts. False accept means that a system claims a pair of pictures is a match, when these are actually the pictures of different individuals. Thevw_elements
vector contains the elements of theVideoWorker
database. Thethumbnails
andnames
vectors contain the previews of images and names of people from the database.
database.h
- In
database.cpp
implement theDatabase
constructor, declared in the previous subsection. Thedistance_threshold
value means the recognition distance. Since this distance is different for different recognition methods, we get it based on theFAR
value using thegetROCCurvePointByFAR
method.
database.cpp
- Specify the path to the database with faces in the
database_dir
variable. If this path doesn't exist, you'll see the"database directory doesn't exist"
exception. Create a newperson_id
variable to store the id of a person from the database (folder name in the database) and theelement_id
variable to store the id of an element in the database (a person's image from the database). In thedirs
list, create a list of all subdirectories of the specified directory with the database.
database.cpp
Note: See more information about FAR and TAR values for different recognition methods in Identification Performance.
- In the
for(const auto &dir: dirs)
loop, process each subdirectory (data about each person). A folder name corresponds to a person's name. Create a list of images inperson_files
.
database.cpp
- In the
for(const auto &person_file: person_files)
nested loop, process each image. If an image doesn't exist, the"Can't read image"
warning is displayed.
database.cpp
- Detect a face in an image using the
Capturer
object. If an image cannot be read, a face cannot be found in an image or more than one face is detected, the warning is displayed and this image is ignored.
database.cpp
- Using the
recognizer->processing
method, create a face template used for recognition.
database.cpp
- In the
pbio::VideoWorker::DatabaseElement vw_element
structure, specify all the information about the database element that will be passed for processing to theVideoWorker
object (element id, person id, face template, recognition threshold). Using thepush_back
method, add an element to the end of the list.
database.cpp
- In
database.h
, add themakeThumbnail
method to create a preview of a picture from the database.
database.cpp
- In
database.cpp
, implement the method usingmakeThumbnail
to create a preview of a picture from the database, which will be displayed next to the face of a recognized person. Set the preview size (120 pixels) and scale it (keep the ratio if the image is resized).
database.cpp
- In the .pro file, set the path to the database.
face_recognition_with_video_worker.pro
- In
facesdkparameters.h
, set the path to the database and the FAR value.
facesdkparameters.h
#
Search a Face in the Database and Display the Result- In
facesdkparameters.h
set the path to the configuration file with the recognition method. In this project we use the method 6.7 because it is suitable for video stream processing and provides optimal recognition speed and good quality. You can learn more about recommended recognition methods in Face Identification.
facesdkparameters.h
Note: If you want to recognize faces in a video stream using low-performance devices, you can use the method 9.30. In this case recognition speed is higher but recognition quality is lower compared to the method 6.7.
- In
worker.h
add thematch_database_index
variable to theFaceData
structure. This variable will store the database element, if a person is recognized, or"-1"
if a person isn't recognized. AddDatabase
and a callback indicating that a person is recognized (MatchFoundCallback
).
worker.h
- In
worker.cpp
, override the parameter value in the configuration file so thatMatchFoundCallback
is received for non-recognized faces too. Set the parameters of theVideoWorker
object: in the first tutorial we didn't recognize faces, that's why we set only the value ofstreams_count
. Since in this project we'll recognize faces in a video stream, we need to specify in the constructor the path to the configuration file with the recognition method, and also the values ofprocessing_threads_count
(number of threads to create templates) andmatching_threads_count
(number of threads to compare the templates). In this project we use only one stream (a webcam connected to our PC). Connect the database: pass the path to the database, createCapturer
to detect faces andRecognizer
to create templates, and also specify theFAR
coefficient. Using thesetDatabase
method, set the database forVideoWorker
. Using theaddMatchFoundCallback
method, add theMatchFound
recognition event handler.
worker.cpp
- In the
Worker::~Worker()
destructor, removeMatchFoundCallback
.
worker.cpp
- In
MatchFoundCallback
, the result is received in the form of theMatchFoundCallbackData
structure that stores the information about recognized and unrecognized faces.
worker.cpp
- When a template for a tracked person is created, this template is compared with each template from the database. If the distance to the closest element is less than
distance_threshold
specified in this element, then it is a match. If a face in a video stream is not recognized, then you'll see the message"Match not found"
. If a face is recognized, you'll see the message"Match found with..."
and the name of the matched person.
worker.cpp
- Save the data about the recognized face to display a preview.
worker.cpp
- Run the project. The recognition results will be displayed in the console. If a face is recognized, you'll see the face id and name of a recognized person from the database. If a face isn't recognized, you'll see the message
"Match not found"
.
#
Display the Preview of the Recognized Face from the Database- Display the image and name of a person from the database next to the face in a video stream. In
drawfunction.h
, add a reference to the database, because we will need it when rendering the recognition results.
drawfunction.h
- In
drawfunction.cpp
modify theDrawFunction::Draw
function by passing the database to it.
drawfunction.cpp
- Save the bounding rectangle in the
pbio::RawSample::Rectangle
structure. Pass its parameters (x, y, width, height) to theQRect rect
object.
drawfunction.cpp
- Create a boolean variable
recognized
that indicates whether a face is recognized or unrecognized. If a face is recognized, the bounding rectangle is green, otherwise it is red.
drawfunction.cpp
- Get a relevant image from the database for a preview by
face.match_database_index
. Calculate the position of a preview in the frame.
drawfunction.cpp
- Draw an image from the database in the preview. Create the
QImage face_preview
object that is higher thanthumbnail
ontext_bar_height
. The original preview image is drawn in the (0, 0) position. As a result, we get a preview with a black rectangle at the bottom with the name of a person. Set the font parameters, calculate the position of a text and display the text in the preview.
drawfunction.cpp
- Draw
face_preview
in the frame using thedrawPixmap
method.
drawfunction.cpp
- In
worker.h
, add a method that returns the reference to the database.
worker.h
- Modify the call to
DrawFunction::Draw
by passing the database to it.
viewwindow.cpp
- Run the project. If a face is recognized, it will be highlighted with a green rectangle, and you'll see a preview of an image from the database and a person's name. Unrecognized faces will be highlighted with a red rectangle.