当前位置:网站首页>Face detection based on Dlib

Face detection based on Dlib

2022-07-08 02:21:00 So come on

1、dlib Basic concepts of

1. Dlib Is a deep learning open source tool , be based on C++ Development , Also support Python Development interface .

2. because Dlib It has good support for facial feature extraction , There are many trained face feature extraction models for developers to use , therefore Dlib Face recognition development is very suitable for face project development .

3. HOG Direction gradient histogram (Histogram of Oriented Gradient)

1HOG It's a feature descriptor , It is usually used to extract features from image data . It is widely used in object detection of computer vision tasks .

2 The role of feature descriptors : It is a simplified representation of an image , Only the most important information about the image .

2、dlib Method of library installation

1. open pycharm
2.File—>settings—>Project Interpreter
Choose your installation python3.6.1 Version as interpreter
3. Click the right side. + Number , Enter dlib, And then in the lower right corner Specify version Check the box in front , Select the version after checking 19.6.1( It is the same as the version just installed

4. Click on the bottom left corner Install Package Installation , If it doesn't work once, install it several times
Once again remind : The version you just installed must be selected

3、dlib The use of

1、 Recognize images

# 1  Import library 
import cv2
import dlib
import numpy as np
import matplotlib.pyplot as plt

# 2  Method : display picture 
def show_image(image, title):
    img_RGB = image[:, :, ::-1] # BGR to RGB
    plt.title(title)
    plt.imshow(img_RGB)
    plt.axis("off")

# 3  Method : Draw a face rectangle 
def plot_rectangle(image, faces):
    for face in faces:
        cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (255,0,0), 4)
        #  Draw a rectangular , Two coordinates 
    return image

def main():
    # 4  Read a picture 
    img = cv2.imread("21.jpg")

    # 5  Gray scale conversion 
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 6  call dlib Detectors in the library 
    detector = dlib.get_frontal_face_detector()
    dets_result = detector(gray, 1) # 1 : Represents doubling the image 
    # 7  Draw a rectangular box for the detected face 
    img_result = plot_rectangle(img.copy(), dets_result)

    # 8  Create a canvas 
    plt.figure(figsize=(9, 6))
    plt.suptitle("face detection with dlib", fontsize=14, fontweight="bold")

    # 9  Show the final detection effect 
    show_image(img_result, "face detection")

    plt.show()

if __name__ == '__main__':
    main()

 2、 Computer camera recognition

# 1  Import library 
import cv2
import dlib

# 2  Method : Draw a face rectangle 
def plot_rectangle(image, faces):
    for face in faces:
        cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (255,0,0), 4)
    return image

def main():
    # 3  Turn on the camera , Read video 
    capture = cv2.VideoCapture(0)
    # 4  Judge whether the camera works normally 
    if capture.isOpened() is False:
        print("Camera Error !")
    # 5  The camera turns on normally : Cycle through every frame 
    while True:
        ret, frame = capture.read()
        if ret:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # BGR to GRAY

            # 6  call dlib Detectors in the library 
            detector = dlib.get_frontal_face_detector()
            det_result = detector(gray, 1)  #  Detect the picture , Return a result 
            # 7  Draw test results 
            dets_image = plot_rectangle(frame, det_result)  #   Original pictures and test results    Draw the result on the picture .

            # 8  Display the final detection effect in real time 
            cv2.imshow("face detection with dlib", dets_image)

            # 9  Key "ESC", sign out , Turn off camera 
            if cv2.waitKey(1) == 27:
                break

    # 10  Release all resources 
    capture.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

原网站

版权声明
本文为[So come on]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130539412284.html