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

问题描述 投票:0回答:1

美好的一天,我发现很难使用 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
python opencv tracking face-detection
1个回答
0
投票

您可以使用 dlib 人脸检测。它使用 Deep CNN,非常好。首先安装face_recogniton madule:

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
    
© www.soinside.com 2019 - 2024. All rights reserved.