python - 如何在使用 google colab 时修复“RuntimeError:无法打开 shape_predictor_68_face_landmarks.dat”?
问题描述
我正在 Google Colaboratory中编写以下 Python 代码并收到错误消息:
代码:
import cv2
import dlib
cap = cv2.VideoCapture(0)
hog_face_detector = dlib.get_frontal_face_detector()
dlib_facelandmark = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
while True: _,
frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = hog_face_detector(gray)
for face in faces:
face_landmarks = dlib_facelandmark(gray, face)
for n in range(0, 16):
x = face_landmarks.part(n).x
y = face_landmarks.part(n).y
cv2.circle(frame, (x, y), 1, (0, 255, 255), 1)
cv2.imshow("Face Landmarks", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
错误:
() 6 hog_face_detector = dlib.get_frontal_face_detector() 7 ----> 8 dlib_facelandmark = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") 9 10 中的 RuntimeError Traceback (最近一次调用最后一次) 为真:
RuntimeError:无法打开 shape_predictor_68_face_landmarks.dat
解决方案
您的笔记本无法打开该文件shape_predictor_68_face_landmarks.dat
。这可能是因为文件没有上传到您的笔记本上,或者您指定的路径dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
错误。
以下是您的代码的编辑,它会自动下载bzip2
文件、提取文件并将其设置为您的形状预测器。.dat
您可以通过更改链接来使用不同的文件!wget
。
单元格 1:
!wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 # DOWNLOAD LINK
!bunzip2 /content/shape_predictor_68_face_landmarks.dat.bz2
datFile = "/content/shape_predictor_68_face_landmarks.dat"
单元格 2:
import cv2
import dlib
cap = cv2.VideoCapture(0)
hog_face_detector = dlib.get_frontal_face_detector()
dlib_facelandmark = dlib.shape_predictor(datFile)
while True: _,
frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = hog_face_detector(gray)
for face in faces:
face_landmarks = dlib_facelandmark(gray, face)
for n in range(0, 16):
x = face_landmarks.part(n).x
y = face_landmarks.part(n).y
cv2.circle(frame, (x, y), 1, (0, 255, 255), 1)
cv2.imshow("Face Landmarks", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
此外,请注意,如果您cv2.VideoCapture(0)
在 Jupyter 笔记本上使用打开相机,它将无法工作,因为代码运行在某个远程服务器上,而不是在您的计算机上。查看此处的代码片段,了解如何在 Colab 中访问本地网络摄像头的示例。
推荐阅读
- php - 父术语和子术语分类显示在多个选择复选框下拉菜单中 Wordpress
- javascript - 如何在 ReactJS 中向 Iframe 添加标头参数
- django - AttributeError: 'NoneType' object has no attribute 'startswith' 尝试在 django 中迁移旧数据库时出现错误
- javascript - Vue -v 在选择标签中建模对象
- c - 释放结构中定义的数组的正确方法
- unicode - FastText 显示中文 (WindowsOS?)
- mysql - Nextcloud (docker) 无法连接到 Mysql (docker) SQLSTATE[HY000] [2002]
- typescript - tsc 忽略 .yaml 文件,即使它设置为包含在 tsconfig.json 中
- android - SetForegroundAsync 在 Workmanager 的 ListenableWorker 中创建前台服务不起作用
- php - 我想为安装在一个 WordPress 中的 2 个自定义主题创建 ACF