Python learning-using dlib and opencv for Face Detection and tagging, dlibopencv
Reference self-https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/
There are some modifications on the basis of the original. If you want to study in depth, you can follow this article by 'Mr. wuk.
This article does not cover the face detection Training Section (although I will send relevant essays later as I learn more), it is just a simple use of the wheel.
Today we will use dlib and opencv for Face Detection and tagging.
First install opencv and dlib
pip install dlibpip install opencv-python
In this program, imutils is used for resize images. The installation method is as follows:
pip install imutils
Dlib provides us with a file for Face Detection tagging training.Http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2download (ifThis file is also included in the example downloaded from the reference page)
Trained files can recognize 68 key points of a human face and mark it (the fewer key points, the more likely it will lead to recognition errors)
Program running method: If. py and shape_predictor_68_face_landmarks.dat are in the same directory, enter
python my_facial_landmarks.py -p shape_predictor_68_face_landmarks.dat -i guanhai.jpg
Or, input
python my_facial_landmarks.py -p shape_predictor_68_face_landmarks.dat
Click q in the image box to take a photo.
The marked photo will be displayed later.
For example, input the image to run
Take a photo and then identify it. You can try it on your own.
The Code is as follows: my_facial_landmarks.py
From imutils import face_utils
Import argparse
Import imutils
Import dlib
Import cv2
Def takephoto ():
Cap = cv2.VideoCapture (0)
While (1 ):
# Get a frame
Ret, frame = cap. read ()
# Show a frame
Cv2.imshow ("capture", frame)
If cv2.waitKey (1) & 0xFF = ord ('q'): # press the q key to complete photography.
# Cv2.imwrite ("./test0.jpg", frame) Save the photo, but we do not need
Return frame # return Image
Cap. release ()
Cv2.destroyAllWindows ()
Def main ():
# Construct the argument parser and parse the arguments use argparse to set the input parameters
Ap = argparse. ArgumentParser ()
Ap. add_argument ("-p", "-- shape-predictor", required = True, # detected files
Help = "path to facial landmark predictor ")
Ap. add_argument ("-I", "-- image", required = False, # image
Help = "path to input image ")
Args = vars (ap. parse_args ())
# Initialize dlib's face detector (HOG-based) and then create
# The facial landmark predictor
# Initialize dlib face detection (based on HOG), and then create a facial flag Estimator
Detector = dlib. get_frontal_face_detector ()
Predictor = dlib. shape_predictor (args ["shape_predictor"])
# Load the input image, resize it, and convert it to grayscale
If args ['image']! = '0 ':
Image = cv2.imread (args ['image']) # input image parameters to read the image
Else:
Image = takephoto () # photography if not input
Image = imutils. resize (image, width = 500) # adjust the image width to 500
Gray = cv2.cvtColor (image, cv2.COLOR _ BGR2GRAY) # adjust the image to gray.
# Detect faces in the grayscale image to detect faces in grayscale images
Rects = detector (gray, 1)
# Loop over the face detections loop for face Detection
For (I, rect) in enumerate (rects ):
# Determine the facial landmarks for the face region, then
# Convert the facial landmark (x, y)-coordinates to a NumPy
# Array
# Determine the facial signs in the face area, and convert the facial signs (x, y) into NumPy arrays.
Shape = predictor (gray, rect)
Shape = face_utils.shape_to_np (shape)
# Convert dlib's rectangle to a OpenCV-style bounding box
# [I. e., (x, y, w, h)], then draw the face bounding box
# Convert the dlib rectangle to the border box [(x, y, w, h)] of the OpenCV style, and then draw the border box
(X, y, w, h) = face_utils.rect_to_bb (rect)
Cv2.rectangle (image, (x, y), (x + w, y + h), (0,255, 0), 2)
# Show the mark of the face number (multiple numbers can be recognized)
Cv2.putText (image, "Face # {}". format (I + 1), (x-10, y-10 ),
Cv2.FONT _ HERSHEY_SIMPLEX, 0.5, (0,255, 0), 2)
# Loop over the (x, y)-coordinates for the facial landmarks
# And draw them on the image
# Loop the coordinates of the facial landmark (x, y) and draw them on the Image
For (x, y) in shape:
Cv2.circle (image, (x, y), 1, (0, 0,255),-1)
# Show the output image with the face detections + facial landmarks
# Display output images with face detection + facial marks
Cv2.imshow ("Output", image)
Cv2.waitKey (0)
If _ name _ = '_ main __':
Main ()