首页 > 解决方案 > 使用 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()

标签: pythonraspberry-picameracv2

解决方案


推荐阅读