python - 头部姿势估计
问题描述
OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find start number (in the name文件):函数 'cv::icvExtractPattern' 中的 head_pose_poc.webm
[错误 - 系统]无法从源读取:head_pose_poc.webm
我正在尝试使用 opencv 和 dlib 运行头部姿势估计,但出现错误。我正在使用 Windows 并在我的命令提示符中收到此错误。我不知道这有什么问题。这是代码:
#!/usr/bin/env python3
import os
import cv2
import sys
import dlib
import argparse
import numpy as np
# helper modules
from drawFace import draw
import reference_world as world
PREDICTOR_PATH = os.path.join("models", "shape_predictor_68_face_landmarks.dat")
if not os.path.isfile(PREDICTOR_PATH):
print("[ERROR] USE models/downloader.sh to download the predictor")
sys.exit()
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--focal",
type=float,
help="Callibrated Focal Length of the camera")
args = parser.parse_args()
def main(source=0):
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(PREDICTOR_PATH)
cap = cv2.VideoCapture(source)
while True:
GAZE = "Face Not Found"
ret, img = cap.read()
if not ret:
print(f"[ERROR - System]Cannot read from source: {source}")
break
faces = detector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), 0)
face3Dmodel = world.ref3DModel()
for face in faces:
shape = predictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
draw(img, shape)
refImgPts = world.ref2dImagePoints(shape)
height, width, channels = img.shape
focalLength = (args.focal * width)
cameraMatrix = world.cameraMatrix(focalLength, (height / 2, width / 2))
mdists = np.zeros((4, 1), dtype=np.float64)
# calculate rotation and translation vector using solvePnP
success, rotationVector, translationVector = cv2.solvePnP(
face3Dmodel, refImgPts, cameraMatrix, mdists)
noseEndPoints3D = np.array([[0, 0, 1000.0]], dtype=np.float64)
noseEndPoint2D, jacobian = cv2.projectPoints(
noseEndPoints3D, rotationVector, translationVector, cameraMatrix, mdists)
# draw nose line
p1 = (int(refImgPts[0, 0]), int(refImgPts[0, 1]))
p2 = (int(noseEndPoint2D[0, 0, 0]), int(noseEndPoint2D[0, 0, 1]))
cv2.line(img, p1, p2, (110, 220, 0),
thickness=2, lineType=cv2.LINE_AA)
# calculating euler angles
rmat, jac = cv2.Rodrigues(rotationVector)
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)
if angles[1] < -15:
GAZE = "Looking: Left"
elif angles[1] > 15:
GAZE = "Looking: Right"
else:
GAZE = "Forward"
cv2.putText(img, GAZE, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 80), 2)
cv2.imshow("Head Pose", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main("head_pose_poc.webm")
解决方案
将其更改
if __name__ == "__main__": main("head_pose_poc.webm")
为:
if __name__ == "__main__": main()
推荐阅读
- python-3.x - 在 PySimpleGUI 窗口中自动更新 Matplotlib 图
- python - 在 PyCharm 上使用 Optuna 运行研究优化时获得 SIGKILL
- javascript - JSON 对象 - 在值内搜索关键字
- r - reprex 显示正确的输出,但 rstudio 控制台显示错误
- npm - 源文本中无法识别的标记
- scala - Scala:foreachPartition 将 Object 类型而不是 Iterator [Row] 传递给方法
- matplotlib - 如何绘制连接seaborn中多个小提琴分布的中位数的线?
- javascript - Seaside & jQuery UI:可排序列表不起作用?
- twitter-bootstrap - 为什么列不在 Bootstrap 5 中居中?
- azure - 天蓝色应用服务身份验证/授权不可用