如何使用python(Opencv)识别头部(面部)运动
Posted
技术标签:
【中文标题】如何使用python(Opencv)识别头部(面部)运动【英文标题】:How to recognize head(face) movement using python(Open cv) 【发布时间】:2021-02-18 10:33:02 【问题描述】:美好的一天,我发现使用 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
【问题讨论】:
我收到此错误 IndexError box = face_recognition.face_locations(frameRGB) cx_ = (box[0][3] + box[0][1])/2 cy_ = (box[0] [3] + box[0][1])/2 IndexError: list index out of range @amirhossein_mlkz 【参考方案1】:您可以使用 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
【讨论】:
我收到此错误 IndexError box = face_recognition.face_locations(frameRGB) cx_ = (box[0][3] + box[0][1])/2 cy_ = (box[0] [3] + box[0][1])/2 IndexError: list index out of range以上是关于如何使用python(Opencv)识别头部(面部)运动的主要内容,如果未能解决你的问题,请参考以下文章
人脸识别实战:使用Python OpenCV 和深度学习进行人脸识别