pythonopencvtrackingface-detection

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


Good day, I am finding it difficult to recognize the head movement of a person using openCV, I have done a project that detects the face and eyes using haarcascade classifier, but unable to track the head movement say head moves left, right, up or down movement.

here is my code

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

Solution

  • You can use dlib Face Detection. it uses Deep CNN and it is very good. First install face_recogniton madule by:

    pip install face-recognition
    

    then you can use below code

    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