python - 使用 cv2 训练相机时出错(Python)
问题描述
我正在尝试使用 cv2 库训练我的相机。我正在使用连接到 Raspberry Pi3 的相机。它工作正常,因为我测试了相机。但是,每当我稍后尝试使用相机进行人脸识别时,我都会不断收到此错误:
return compile(source, filename, mode, PyCF_ONLY_AST)
IndentationError: expected an indented block
这是下面的代码:
import cv2
name = 'Mohammed' #replace with your name
cam = cv2.VideoCapture(0)
cv2.namedWindow("press space to take a photo", cv2.WINDOW_NORMAL)
cv2.resizeWindow("press space to take a photo", 500, 300)
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("press space to take a photo", frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing…")
break
elif k%256 == 32:
# SPACE pressed
img_name = "dataset/"+ name +"/image_{}.jpg".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
cam.release()
cv2.destroyAllWindows()
解决方案
推荐阅读
- jquery - 一种排除特定数组的方法
- debugging - 调试器盒子里面有什么以及它是如何工作的
- ajax - Laravel - 获取复选框值并更新数据库
- php - 如何使用 php curl 进行双向/双向身份验证?
- java - 映射到 POJO 的 ObjectMapper 返回 Null
- r - 按功能汇总信息
- typescript - ts-node 和 ts-node-dev 不适用于 `cannot read property of undefined`,它可能不是循环导入
- sql - 基于相同的 Id 导出不同的名称
- javascript - 如何从菜单项将图像加载到场景中?
- c - 接受多个客户的正确方法?