如何使用 python(Open cv) 识别头部(面部)运动

How to recognize head(face) movement using python(Open cv)

美好的一天,我发现很难使用 openCV 识别人的头部运动,我已经完成了一个使用 haarcascade 分类器检测面部和眼睛的项目,但无法跟踪头部运动,比如头部向左移动, 向右,向上或向下运动。

这是我的代码

if __name__=='__main__':

#initialize the webcam
webcam =cv2.VideoCapture(0)
#capture frame by frame
ret,frame = webcam.read()

#convert image from BGR(OpenCV) to RGB(face_recognition)
frameRGB = frame[:, :, ::-1]
#frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#array co-ordinate of faces
box = face_recognition.face_locations(frameRGB)

cx_ = (box[0][3] + box[0][1])/2
cy_ = (box[0][3] + box[0][1])/2

cx = cx_
cy = cy_
MIN_MOVE = 10

while True:
    ret,frame = webcam.read()
    
    frameRGB = frame[:, :, ::-1]
    #frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    box = face_recognition.face_locations(frameRGB)
    
    
    if (box != []):
        
        #if the box is not empty do the following
        cx = (box[0][3] + box[0][1])/2
        cy = (box[0][0] + box[0][2])/2
        cv2.rectangle(frame, (box[0][3],box[0][2]), (box[0][1],box[0][0]), (0,0,255), 2)
        
        if abs(cx-cx_) > abs(cy-cy_):
            
            if cx - cx_ > MIN_MOVE:
                print("LEFT")
            elif cx - cx_ < -MIN_MOVE:
                print("RIGHT")
                
        else:
            
            if cy - cy_ > MIN_MOVE:
                print("DOWN")
            elif cy - cy_ < -MIN_MOVE:
                print("UP")
                
    cv2.imshow('Unlock Face', frame)
    key = cv2.waitKey(30)
    cx_ = cx
    cy_ = cy
    
    if key == 27: #press Esc key to exit
        break

您可以使用 dlib 人脸检测。它使用 Deep CNN,非常好。首先通过以下方式安装 face_recogniton 模块:

pip install face-recognition

那么你可以使用下面的代码

import cv2
import numpy as np
import face_recognition

if __name__ =='__main__':

    Camera = cv2.VideoCapture(0)
    _,frame = Camera.read()
            
    frameRGB = cv2.cvtColor ( frame , cv2.COLOR_BGR2RGB )
    box = face_recognition.face_locations(frameRGB)          

    cx_ = (box[0][3] + box[0][1])/2
    cy_ = (box[0][3] + box[0][1])/2
    cx = cx_
    cy = cy_

    MIN_MOVE=10
    while True:
            _,frame = Camera.read()
            
            frameRGB = cv2.cvtColor ( frame , cv2.COLOR_BGR2RGB )
            box = face_recognition.face_locations(frameRGB)          


            if ( box!= [] ):
                    cx = (box[0][3] + box[0][1])/2
                    cy = (box[0][0] + box[0][2])/2
                    cv2.rectangle ( frame ,(box[0][3],box[0][2]) , (box[0][1],box[0][0]) , (0,0,255) , 2 )

                    if abs(cx-cx_) > abs(cy-cy_):
                    #print(cx,cx_, cy, cy_)
                            if cx - cx_ > MIN_MOVE:
                                    print('LEFT')
                            elif cx - cx_ < -MIN_MOVE:
                                    print('RIHT')
                    else:

                            if cy - cy_ > MIN_MOVE:
                                    print('DOWN')
                            elif cy - cy_ < -MIN_MOVE:
                                    print('UP')


            cv2.imshow ( "unlock Face" ,frame )
            key = cv2.waitKey (30)
            cx_ = cx
            cy_ = cy
            if key == 27:
                    break