打开cv错误:(-215)函数cvtColor:Google Colab中的scn==3||scn==4



当我在colab中尝试以下代码时,我没有得到任何错误,也没有得到任何输出。此外,我还尝试使用javascript访问网络摄像头。

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

如本文所述,ret值很可能返回false。因此,程序无法获取帧。

  • 选项#1:确保您以管理员权限运行代码,因为程序可能无法访问相机。

  • 选项2:在将帧转换为灰度之前,可以使用if ret==True:语句。

    例如:

    while(True):
    ret, frame = cap.read()
    if ret:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Rest of the code ..
    

最新更新