current position:Home>Face recognition practice: face recognition using Python opencv and deep learning

Face recognition practice: face recognition using Python opencv and deep learning

2022-02-01 06:07:58 AI Hao

This is my participation 11 The fourth of the yuegengwen challenge 9 God , Check out the activity details :2021 One last more challenge

In this tutorial , You will learn how to use OpenCV、Python And deep learning to perform face recognition .

We will first briefly discuss the working principle of face recognition based on deep learning , Include “ Depth measurement learning ” The concept of . From there, , I'll help you install the libraries needed to actually perform face recognition . Last , We will implement face recognition for still images and video streams .

Understand deep learning face recognition embedding

that , Deep learning + How does face recognition work ?

The secret is a technique called depth measurement learning . If you have any deep learning experience before , You will know that we usually train the network to : Accept a single input image And output the classification of the image / label However , Depth measurement learning is different .

contrary , We're not trying to output a single label ( Even the coordinates of the object in the image / Bounding box ), Instead, we output real valued eigenvectors .

about dlib Face recognition network , The output eigenvector is 128-d( namely 128 A list of real values ), Used to quantify human faces . The training network is completed using triples :

image-20211119131551638

Here we provide three pictures to the network : Two of the pictures are sample faces of the same person . The third picture is a random face in our dataset , Not the same person as the other two pictures .

As an example , Let's consider the graph again 1, Among them, we provide three images : a sheet Chad Smith And two Will Ferrell. Our network quantifies faces , Build... For everyone 128 Dimension embedding ( quantitative ).

From there , The general idea is that we will adjust the weight of our neural network , So that two Will Ferrel Of 128 Dimensional measurements will be closer to each other , And with Chad Smith The measurements are further apart .

Our network architecture for face recognition is based on He Etc. Deep Residual Learning for Image Recognition In the paper ResNet-34, But fewer layers , The number of filters has been reduced by half .

The network itself consists of Davis King stay ≈300 Training on a data set of 10000 images . Mark faces in the wild (LFW) On dataset , The network is compared with other state-of-the-art methods , Accuracy reached 99.38%. Davis King(dlib The creator of the ) and Adam Geitgey( We will soon use face_recognition The author of the module ) Have written detailed articles on how face recognition based on deep learning works :

I strongly recommend that you read the above article , To learn more about how deep learning facial embedding works .

Install face recognition library

In order to use Python and OpenCV Perform face recognition , We need to install two additional libraries :

from Davis King Maintenance of dlib The library contains our “ Depth measurement learning ” Realization , It is used to construct a face embedding algorithm for the actual recognition process .

from Adam Geitgey Created face_recognition The library contains dlib Face recognition function , Make it easier to use .

install dlib

pip install dlib
 Copy code 

Or you can compile from source code :

git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DUSE_AVX_INSTRUCTIONS=1
cmake --build .
cd ..
python setup.py install --yes USE_AVX_INSTRUCTIONS
 Copy code 

If the installation fails, you can try :

pip install dlib-bin
 Copy code 

The effect is the same , But doing so can only install CPU edition , The disadvantage is slow .

Installation support GPU Of dlib( Optional )

If you have a compatible CUDA Of GPU, You can install support GPU Of dlib, Make face recognition faster 、 More efficient . So , I recommend installing... From source code dlib, Because you have better control over the build :

$ git clone https://github.com/davisking/dlib.git
$ cd dlib
$ mkdir build
$ cd build
$ cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1
$ cmake --build .
$ cd ..
$ python setup.py install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA
 Copy code 

Suggest using GPU edition , If the installation fails, refer to this article :

blog.csdn.net/hhhhhhhhhhw…

install face_recognition package

face_recognition Modules can be implemented through a simple pip Command to install :

pip install face_recognition
 Copy code 

install imutils

You also need my convenience pack imutils. You can go through pip Install it on your Python In a virtual environment :

pip install imutils
 Copy code 

Face recognition dataset

image-20211119182347794

Because Jurassic Park (1993) It's my favorite movie ever , To commemorate the Jurassic world, which opened in the United States on Friday : The fallen Kingdom (2018), We will perform facial recognition on the character samples in the film : Alan Grant, paleontologist (22 A picture ) Claire Dearing, Park operations manager (53 A picture ) Ellie Sattler, Paleobotanists (31 A picture ) Ian Malcolm , Mathematician (41 A picture ) John Hammond , The merchant / Jurassic Park owner (36 A picture ) Owen Grady, Dinosaur researcher (35 A picture )

Get this image dataset , We will :

  • Create... For each face in the dataset 128 Dimension embedding
  • These embeddings are used to recognize the faces of people in images and video streams

Method of making data set :

  • download 《 Jurassic Park 》 The movie , New folder , Copy the movie .
  • Use ffmpeg Convert video to pictures . If you don't know how to use ffmeg, Reference resources :wanghao.blog.csdn.net/article/det…
  • Choose the right picture , Put it in the corresponding folder .

Face recognition project structure

myface
├── dataset
│   ├── alan_grant [22 entries]
│   ├── claire_dearing [53 entries]
│   ├── ellie_sattler [31 entries]
│   ├── ian_malcolm [41 entries]
│   ├── john_hammond [36 entries]
│   └── owen_grady [35 entries]
├── examples
│   ├── example_01.png
│   ├── example_02.png
│   └── example_03.png
├── output
│   └── lunch_scene_output.avi
├── videos
│   └── lunch_scene.mp4
├── encode_faces.py
├── recognize_faces_image.py
├── recognize_faces_video.py
├── recognize_faces_video_file.py
└── encodings.pickle

 Copy code 

Our projects include 4 A top-level Directory :

  • dataset/ : Facial image containing six characters , Organize them into subdirectories according to their respective names .
  • examples/ : There are three facial images for testing that are not in the dataset .
  • output/ : This is where you can store processed face recognition videos . I'll leave one of mine in the folder —— Classic in the original Jurassic Park movie “ Lunch scene ”.
  • videos/ : The input video should be stored in this folder . The folder also contains “ Lunch scene ” video , But it has not been through our face recognition system .

We still have... In the root directory 6 File :

  • encode_faces.py : Face coding (128 Dimension vector ) It was built with this script .
  • identify_faces_image.py : Recognize faces in a single image ( Coding based on data set ).
  • identify_faces_video.py : Recognize the face in the real-time video stream from the webcam and output the video .
  • identify_faces_video_file.py : Recognize the face in the video file residing on the disk, and output the processed video to the disk . I won't discuss this document today , Because the skeleton comes from the same skeleton as the video stream file .
  • encodings.pickle : The facial recognition code passes encode_faces.py Generate... From your dataset , Then serialize to disk .

After creating the image dataset ( Use search_bing_api.py ), We will run encode_faces.py To build embedded systems . then , We will run the recognition script to actually recognize the face .

Use OpenCV And deep learning to encode face

img

Before recognizing faces in images and videos , We first need to quantify the faces in the training set . please remember , We're not actually training the network here —— The network has been trained in about 300 Create a data set of 10000 images 128 Dimension embedding .

Of course we can train the network from scratch , You can even fine tune the weights of existing models , But for many projects, this is likely to be overkill .

Besides , You will need a lot of images to train the network from scratch . contrary , Use the pre training network and then use it for the... In our dataset 218 Each of the faces is constructed 128 Dimension embedding is easier .

then , In the process of classification , We can use a simple k-NN Model + Vote for the final face classification . Other traditional machine learning models can also be used here . To build our face embedding ,

Please the new encode_faces.py:

# import the necessary packages
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os
 Copy code 

First , We need to import the required package . Again, pay attention to , This script needs to be installed imutils 、 face_recognition and OpenCV . Scroll up to “ Install your face recognition library ” To ensure that you are ready to use these libraries on your system . Let's deal with using... At run time argparse Command line arguments for processing :

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--dataset", required=True,
	help="path to input directory of faces + images")
ap.add_argument("-e", "--encodings", required=True,
	help="path to serialized db of facial encodings")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
	help="face detection model to use: either `hog` or `cnn`")
args = vars(ap.parse_args())
 Copy code 

Let's list the parameter flags and discuss them :

  • --dataset : The path to the dataset ( We used the method of last week's blog post #2 Described in the search_bing_api.py Created a dataset ).
  • --encodings : Our face code is written into the file pointed to by this parameter .
  • --detection-method : Before we encode the face in the image , We need to test them first . Or two face detection methods include hog or cnn . These two signs are the only ones that apply to --detection-method The logo of .

Now we have defined our parameters , Let's get the path to the file in the dataset ( And perform two initializations ):

# grab the paths to the input images in our dataset
print("[INFO] quantifying faces...")
imagePaths = list(paths.list_images(args["dataset"]))
# initialize the list of known encodings and known names
knownEncodings = []
knownNames = []
 Copy code 

Enter the path to the dataset directory to build a list of all image paths contained in it .

We also need to initialize the two lists before the loop knownEncodings and knownNames . These two lists will contain the face code and corresponding name of each person in the dataset . It's time to start cycling our character faces in Jurassic Park !

# loop over the image paths
for (i, imagePath) in enumerate(imagePaths):
	# extract the person name from the image path
	print("[INFO] processing image {}/{}".format(i + 1,
		len(imagePaths)))
	name = imagePath.split(os.path.sep)[-2]
	# load the input image and convert it from BGR (OpenCV ordering)
	# to dlib ordering (RGB)
	image = cv2.imread(imagePath)
	rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 Copy code 

This loop will loop 218 Time , Corresponds to our in the dataset 218 Face images .

Traverse the path of each image . From there, , We will start from imagePath( Because our subdirectories are properly named ) Extract people's names from . Then let's load the image , At the same time imagePath Pass to cv2.imread. OpenCV stay BGR Order color channel in , but dlib Actually expect RGB. face_recognition Module USES dlib , So before we go on , Let's be at 37 Line swap color space , Name the new image rgb . Next , Let's locate the face and calculate the code :

# detect the (x, y)-coordinates of the bounding boxes
	# corresponding to each face in the input image
	boxes = face_recognition.face_locations(rgb,
		model=args["detection_method"])
	# compute the facial embedding for the face
	encodings = face_recognition.face_encodings(rgb, boxes)
	# loop over the encodings
	for encoding in encodings:
		# add each encoding + name to our set of known names and
		# encodings
		knownEncodings.append(encoding)
		knownNames.append(name)
 Copy code 

This is the interesting part of the play !

For each iteration of the loop , We'll test a face ( Or maybe multiple faces , And assume that it is the same person in multiple positions of the image —— This assumption may hold in your own image , It may not be true , So be careful here ). for example , hypothesis rgb Include a ( Or more )Ellie Sattler A picture of the face . lookup / Positioned her face , This generates a face box list . We pass two parameters to face_recognition.face_locations Method :

  • rgb : our RGB Images .

  • model:cnn or hog( The value is contained in the and “detection_method” The command line parameter dictionary associated with the key ). CNN The method is more accurate but slower . HOG Faster , But the accuracy is low .

then , We will Ellie Sattler The bounding box of the face is converted to 128 A list of numbers . This is called encoding faces into vectors , and face_recognition.face_encodings Method will handle it for us . From there we just need to Ellie Sattler Attach the code and name to the appropriate list (knownEncodings and knownNames). We will continue to work on all in the dataset 218 Perform this operation on an image . Unless we can use coding in another script that handles recognition , Otherwise, what's the point of encoding the image ? Now let's solve this problem :

# dump the facial encodings + names to disk
print("[INFO] serializing encodings...")
data = {"encodings": knownEncodings, "names": knownNames}
f = open(args["encodings"], "wb")
f.write(pickle.dumps(data))
f.close()
 Copy code 

Constructed a dictionary with two keys —— "encodings" and "names" .

Dump the name and encoding to disk for future calls . How should I run in the terminal encode_faces.py Script ? To create our face embedding , Please open a terminal and execute the following command :

python encode_faces.py --dataset dataset --encodings encodings.pickle
[INFO] quantifying faces...
[INFO] processing image 1/218
[INFO] processing image 2/218
[INFO] processing image 3/218
...
[INFO] processing image 216/218
[INFO] processing image 217/218
[INFO] processing image 218/218
[INFO] serializing encodings...
$ ls -lh encodings*
[email protected] 1 adrian  staff   234K May 29 13:03 encodings.pickle
 Copy code 

As you can see from our output , We now have one called encodings.pickle The file of —— This file contains the of each face in our dataset 128 Dimension face embedding .

In my Titan X GPU On , It takes a little more than a minute to process the entire dataset , But if you use CPU, Please be ready to wait for this script to complete !

In my Macbook Pro( nothing GPU) On , code 218 An image needs 21 branch 20 second .

If you have one GPU And compiled dlib Support GPU, You should expect faster .

Recognize faces in images

img

Now we have created... For each image in the dataset 128 Dimension face embedding , Now we can use it OpenCV、Python And deep learning to recognize the face in the image . open recognize_faces_image.py And insert the following code :

# import the necessary packages
import face_recognition
import argparse
import pickle
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
	help="path to serialized db of facial encodings")
ap.add_argument("-i", "--image", required=True,
	help="path to input image")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
	help="face detection model to use: either `hog` or `cnn`")
args = vars(ap.parse_args())
 Copy code 

Parse three command line arguments :

--encodings : Including our facial code pickle Path to file .

--image : This is the image of face recognition .

--detection-method : You should be familiar with this by now —— According to the capabilities of your system , We either use hog Method , Or use cnn Method . For speed , choice hog , For accuracy , choice cnn .

then , Let's load the precomputed code + Face name , Then build... For the input image 128 Dimensional face coding :

# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args["encodings"], "rb").read())
# load the input image and convert it from BGR to RGB
image = cv2.imread(args["image"])
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# detect the (x, y)-coordinates of the bounding boxes corresponding
# to each face in the input image, then compute the facial embeddings
# for each face
print("[INFO] recognizing faces...")
boxes = face_recognition.face_locations(rgb,
	model=args["detection_method"])
encodings = face_recognition.face_encodings(rgb, boxes)
# initialize the list of names for each face detected
names = []
 Copy code 

Load our pickled code and face name from disk . We will need this data later in the actual face recognition step .

then , We load the input image and convert it to rgb Color channel sorting ( It's like we're here encode_faces.py As in the script ).

Then we continue to detect all faces in the input image , And calculate their 128 Dimensional coding ( These lines should also look familiar ).

Now is a good time to initialize the name list for each face detected —— The list will be populated in the next step .

Next , Let's go through the facial code :

# loop over the facial embeddings
for encoding in encodings:
	# attempt to match each face in the input image to our known
	# encodings
	matches = face_recognition.compare_faces(data["encodings"],
		encoding)
	name = "Unknown"
 Copy code 

We begin to traverse the face code calculated from the input image . Then facial recognition magic happened ! We try to use face_recognition.compare_faces The input image ( code ) Each face in is associated with our known encoded data set ( Save in data["encodings"] in ) Match .

This function returns True / False List of values , Each image in the dataset corresponds to a value . For our Jurassic Park example , There are 218 Images , So the returned list will have 218 A Boolean value .

In the internal ,compare_faces The function is calculating the Euclidean distance between the candidate embedding and all faces in our dataset :

  • If the distance is below a certain tolerance ( The smaller the tolerance , The more stringent our facial recognition system will be ), So let's go back to True , Indicates a facial match .
  • otherwise , If the distance is above the tolerance threshold , We will return False, Because the faces don't match .

Essentially , We are using “ More fancy ” Of k-NN Model classification . Please be sure to refer to compare_faces Implementation for more details . name The variable will eventually hold the person's name string —— Now? , We keep it as “Unknown”, In case there is no “ vote ”.

Given our matching list , We can calculate the of each name “ vote ” Count ( Associated with each name True The amount of value ), Count the votes , And choose the name of the person with the largest number of votes :

# check to see if we have found a match
	if True in matches:
		# find the indexes of all matched faces then initialize a
		# dictionary to count the total number of times each face
		# was matched
		matchedIdxs = [i for (i, b) in enumerate(matches) if b]
		counts = {}
		# loop over the matched indexes and maintain a count for
		# each recognized face face
		for i in matchedIdxs:
			name = data["names"][i]
			counts[name] = counts.get(name, 0) + 1
		# determine the recognized face with the largest number of
		# votes (note: in the event of an unlikely tie Python will
		# select first entry in the dictionary)
		name = max(counts, key=counts.get)
	
	# update the list of names
	names.append(name)
 Copy code 

If there is any in the match True vote , We need to determine these True Index of the position of the value in the match . That's what we did , There we construct a simple match Idxs list , about example_01.png It might look like this :

(Pdb) matchedIdxs

[35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75]
 Copy code 

Then we initialize a program named counts Dictionary , It will take the character name as the key , Take the number of votes as the value . From there, , Let's loop the matching Idxs And set the value associated with each name , At the same time, as needed in counts Add it in . about Ian Malcolm High number of votes ,counts The dictionary might look like this :

(Pdb) counts
{'ian_malcolm': 40}
 Copy code 

Think about it , We have only... In the dataset 41 Zhang Ian Photos of the , So without voting for anyone else ,40 The score is very high . from counts The name that gets the most votes , In this case 'ian_malcolm' . The second cycle of the main face coding cycle ( Because our example image has two faces ) by counts Generate the following :

(Pdb) counts
{'alan_grant': 5}
 Copy code 

This is definitely a smaller voting score , But there is still only one name in the dictionary , So we may have found Alan Grant .

Here's the picture 5 Shown ,Ian Malcolm and Alan Grant Have been correctly identified , So this part of the script works well . Let's continue and traverse everyone's bounding box and tag name , And draw them on our output image for visualization :

# loop over the recognized faces
for ((top, right, bottom, left), name) in zip(boxes, names):
	# draw the predicted face name on the image
	cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
	y = top - 15 if top - 15 > 15 else top + 15
	cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
		0.75, (0, 255, 0), 2)
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)
 Copy code 

We begin to traverse the detected face bounding box and the predicted name .

To create an iteratable object , So that we can easily traverse these values , We call zip(boxes, names) Generate tuples , We can extract the frame coordinates and names .

We use box coordinates to draw a green rectangle . We also use coordinates to calculate where the person name text should be drawn , Then the name text is actually placed on the image . If the face bounding box is at the top of the image , We need to move the text below the top of the box , Otherwise, the text will be truncated . Then we continue to display the image , Until you press a key . How should you run facial recognition Python Script ? Use your terminal , use first workon The command ensures that you are in their respective Python In the right virtual environment ( Of course , If you are using a virtual environment ).

Then run the script , Provide at least two command line parameters at the same time . If you choose to use HoG Method , Please make sure you pass --detection-method hog( Otherwise, it will default to deep learning detector ).

Come on! ! To use OpenCV and Python Face recognition , Please open your terminal and execute our script :

python recognize_faces_image.py --encodings encodings.pickle \
	--image examples/example_01.png
 Copy code 

img

The second example of human face recognition is as follows :

img

Recognize faces in video

img

Now we have applied face recognition to image recognition , Let's also apply face recognition to video ( real time ).

Important performance notes : Only in use GPU Can be used in real time CNN Facial recognizer ( You can combine it with CPU Use it together , But it is expected to be lower than 0.5 FPS, This will cause the video to be intermittent ).

perhaps ( You are using CPU), You should use HoG Method ( Even in future blog posts OpenCV Haar cascade ) And expect enough speed . The following script is the same as the previous identify_faces_image.py Scripts have many similarities .

therefore , I'll skip what we've already introduced , View only video components , So that you can understand what's going on .

newly build identify_faces_video.py And insert the following code :

# import the necessary packages
from imutils.video import VideoStream
import face_recognition
import argparse
import imutils
import pickle
import time
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
	help="path to serialized db of facial encodings")
ap.add_argument("-o", "--output", type=str,
	help="path to output video")
ap.add_argument("-y", "--display", type=int, default=1,
	help="whether or not to display output frame to screen")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
	help="face detection model to use: either `hog` or `cnn`")
args = vars(ap.parse_args())
 Copy code 

Import package , Then continue to parse our command line parameters . We have four command line arguments , Two of them you should identify from above (--encodings and --detection-method). The other two parameters are :

--output : Path to output video .

--display : Flag indicating that the script displays the frame on the screen . The value is 1 Time display , The value is 0 The output frame will not be displayed on our screen .

Load our code and start our VideoStream :

# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args["encodings"], "rb").read())
# initialize the video stream and pointer to output video file, then
# allow the camera sensor to warm up
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
writer = None
time.sleep(2.0)
 Copy code 

To access our camera , We used imutils Of VideoStream class .

Start the flow . If you have multiple cameras on your system ( For example, built-in webcam and external USB camera ), You can use src=0 Change to src=1 wait . Later, we will selectively write the processed video frames to disk , So we will writer Initialize to None. Sleep 2 Seconds can warm up our camera . From there we will start a while Loop and start grabbing and processing frames :

# loop over frames from the video file stream
while True:
	# grab the frame from the threaded video stream
	frame = vs.read()
	
	# convert the input frame from BGR to RGB then resize it to have
	# a width of 750px (to speedup processing)
	rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
	rgb = imutils.resize(frame, width=750)
	r = frame.shape[1] / float(rgb.shape[1])
	# detect the (x, y)-coordinates of the bounding boxes
	# corresponding to each face in the input frame, then compute
	# the facial embeddings for each face
	boxes = face_recognition.face_locations(rgb,
		model=args["detection_method"])
	encodings = face_recognition.face_encodings(rgb, boxes)
	names = []
 Copy code 

The first step we take is to grab a frame from the video stream . The above code block is almost the same as the line in the previous script , The difference is that this is a video frame, not a still image . Essentially , We read frames , Preprocessing , Then detect the face bounding box + Calculate the coding of each bounding box .

Next , Let's go through the face codes related to the face we just found :

# loop over the facial embeddings
	for encoding in encodings:
		# attempt to match each face in the input image to our known
		# encodings
		matches = face_recognition.compare_faces(data["encodings"],
			encoding)
		name = "Unknown"
		# check to see if we have found a match
		if True in matches:
			# find the indexes of all matched faces then initialize a
			# dictionary to count the total number of times each face
			# was matched
			matchedIdxs = [i for (i, b) in enumerate(matches) if b]
			counts = {}
			# loop over the matched indexes and maintain a count for
			# each recognized face face
			for i in matchedIdxs:
				name = data["names"][i]
				counts[name] = counts.get(name, 0) + 1
			# determine the recognized face with the largest number
			# of votes (note: in the event of an unlikely tie Python
			# will select first entry in the dictionary)
			name = max(counts, key=counts.get)
		
		# update the list of names
		names.append(name)
 Copy code 

In this code block , We iterate through each code and try to match the face . If I find a match , We will calculate the number of votes for each name in the dataset . Then we extract the highest number of votes , That is, the name related to the face . These lines are the same as the previous script we looked at , So let's continue .

In the next block , We traverse the recognized face and continue to draw a box around the face , And display the person's name above the face :

# loop over the recognized faces
	for ((top, right, bottom, left), name) in zip(boxes, names):
		# rescale the face coordinates
		top = int(top * r)
		right = int(right * r)
		bottom = int(bottom * r)
		left = int(left * r)
		# draw the predicted face name on the image
		cv2.rectangle(frame, (left, top), (right, bottom),
			(0, 255, 0), 2)
		y = top - 15 if top - 15 > 15 else top + 15
		cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
			0.75, (0, 255, 0), 2)
 Copy code 

These lines are the same , So let's focus on the video related code . perhaps , We write frames to disk , Let's see how to use OpenCV Write video to disk :

# if the video writer is None *AND* we are supposed to write
	# the output video to disk initialize the writer
	if writer is None and args["output"] is not None:
		fourcc = cv2.VideoWriter_fourcc(*"MJPG")
		writer = cv2.VideoWriter(args["output"], fourcc, 20,
			(frame.shape[1], frame.shape[0]), True)
	# if the writer is not None, write the frame with recognized
	# faces to disk
	if writer is not None:
		writer.write(frame)
 Copy code 

Suppose we provide an output file path in the command line arguments , And we haven't initialized the video writer yet , Let's continue to initialize it . We initialize VideoWriter_fourcc . FourCC It's a 4 A character code , In our case , We will use “MJPG” 4 A character code . From there, , We put this object together with our output file path 、 The number of frames per second and frame size are passed to VideoWriter. Last , If writer There is , We can continue to write frames to disk .

Let's deal with whether face recognition video frames should be displayed on the screen :

# check to see if we are supposed to display the output frame to
	# the screen
	if args["display"] > 0:
		cv2.imshow("Frame", frame)
		key = cv2.waitKey(1) & 0xFF
		# if the `q` key was pressed, break from the loop
		if key == ord("q"):
			break
 Copy code 

If set display Command line arguments , We continue to display the frame and check whether the exit key is pressed (“q”), At this point we will jump out loop . Last , Let's perform our housekeeping duties :

# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()
# check to see if the video writer point needs to be released
if writer is not None:
	writer.release()
 Copy code 

We clean up and release the display 、 Video stream and video writer . Are you ready to view the running script ?

To demonstrate the use of OpenCV and Python Real time face recognition , Please open the terminal and execute the following command :

python recognize_faces_video.py --encodings encodings.pickle \
	--output output/webcam_face_recognition_output.avi --display 1
[INFO] loading encodings...
[INFO] starting video stream...
 Copy code 

Face recognition in video files

As I am “ Face recognition project structure ” Mentioned in Section , This blog post is “ download ” Contains an additional script ——recognize_faces_video_file.py.

This file is basically the same as the webcam file we just viewed , But if you like , It takes the input video file and generates the output video file .

I applied our facial recognition code to the popular... In primitive Jurassic Park movies “ Lunch scene ”, The actors sit around the table and share their concerns with the park :

python recognize_faces_video_file.py --encodings encodings.pickle \
	--input videos/lunch_scene.mp4 --output output/lunch_scene_output.avi \
	--display 0
 Copy code 

summary

In this tutorial , You learned how to use OpenCV、Python And deep learning to perform face recognition . Besides , We used Davis King Of dlib Kuhe Adam Geitgey Of face_recognition modular , The module revolves around dlib Depth measurement learning , Make face recognition easier . As we found ,

Our face recognition implementation is :

  • accurate
  • Can pass GPU Real time execution

I hope you like today's blog about face recognition !

Complete code and data set : download.csdn.net/download/hh…

copyright notice
author[AI Hao],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010607559869.html

Random recommended