当前位置:网站首页>Face key point detection Dlib

Face key point detection Dlib

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

1、 Image recognition

 

 

Be careful :

1. dlib.get_frontal_face_detector( ) Get face detector

2. dlib.shape_predictor( ) Predict face key points

Face key point model , Download address :

# 1  Add to Library 
import cv2
import matplotlib.pyplot as plt
import dlib

# 2  Read a picture 
image = cv2.imread("Tom.jpeg")

# 3  Call the face detector 
detector = dlib.get_frontal_face_detector()

# 4  Load prediction key model (68 A key point )
#  Face key point model , Download address :
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

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

# 6  Face detection 
faces = detector(gray, 1)

# 7  loop , Traverse every face , Draw a rectangle and key points for the face 
for face in faces: #(x, y, w, h)
    # 8  Draw rectangle 
    cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (0,255,0), 2)

    # 9  Predict the key points 
    shape = predictor(image, face)

    # 10  Get the key coordinates 
    for pt in shape.parts():
        #  Get the abscissa and ordinate 
        pt_position = (pt.x, pt.y)
        # 11  Draw key coordinates 
        cv2.circle(image, pt_position, 1, (255, 0, 0), -1)# -1 fill ,2 Represent size 

# 12  Show the whole rendering 
plt.imshow(image)
plt.axis("off")
plt.show()



2、 Computer camera recognition

# 1  Add to Library 
import cv2
import dlib

# 2  Turn on the camera 
capture = cv2.VideoCapture(0)

# 3  Get face detector 
detector = dlib.get_frontal_face_detector()

# 4  Get the face key detection model 
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

while True:
    # 5  Read the video stream 
    ret, frame = capture.read()
    # 6  Gray scale conversion 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # 7  Face detection 
    faces = detector(gray, 1)
    # 8  Draw rectangular boxes and keys for each face 
    for face in faces:
        # 8.1  Draw rectangle 
        cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0,255,0), 3)
        # 8.2  Key detected 
        shape = predictor(gray, face)  #68 A key point 
        # 8.3  Get the coordinates of the key points 
        for pt in shape.parts():
            #  The coordinates of each point 
            pt_position = (pt.x, pt.y)
            # 8.4  Draw key points 
            cv2.circle(frame, pt_position, 3, (255,0,0), -1)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    # 9  According to the effect 
    cv2.imshow("face detection landmark", frame)
capture.release()
cv2.destroyAllWindows()

原网站

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