下面的代码在Google Colab上运行,它显示“您的会话被粉碎。自动重启”。我是初学者,我需要使用相机捕捉面部表情。
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(-1)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
1条答案
按热度按时间zsohkypk1#
Colab环境中没有本地视频设备;它运行在云中的某个地方。我们需要的是浏览器中的一些JavaScript,以便访问用户附近的摄像头。Colab提供了代码片段示例,其中一个是用于视频捕获的,我在下面介绍了这些示例。
先运行此单元格。
运行此单元时,您必须允许视频访问。