【发布时间】: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
标签: python opencv tracking face