【问题标题】:!_img.empty() in function 'imwrite'!_img.empty() 在函数“imwrite”中
【发布时间】:2022-01-15 21:08:29
【问题描述】:

我只是在 colab google 中尝试此代码

for label in labels:
cap = cv2.VideoCapture(0)
print('Collecting images for {}'.format(label))
time.sleep(5)
for imgnum in range(number_imgs):
    print('Collecting image {}'.format(imgnum))
    ret, frame = cap.read()
    imgname = os.path.join(IMAGES_PATH,label,label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
    cv2.imwrite(imgname, frame)
    cv2.imshow('frame', frame)
    time.sleep(2)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release() cv2.destroyAllWindows() 我得到了这个错误

【问题讨论】:

  • 您希望在 colab 中拍摄哪个相机? img.empty() 表示没有成功捕获图像,最好测试一下ret是否为真。

标签: opencv google-colaboratory


【解决方案1】:

Google Colab 服务器上没有网络摄像头。

Google Colab 上的代码在 Google 服务器上运行,而不是在您的浏览器中。

在 Google Colab 上运行代码时无法打开 VideoCapture(0)

您需要对 VideoCapture 的代码进行错误检查。

由于您不检查错误,并且打开 VideoCapture 失败,frame 将是 None。你不能 imwrite None 因为那不是一个有效的数组/图像。

...
cap = cv2.VideoCapture(0)
assert cap.isOpened() # THIS

while True:
    ret, frame = cap.read()
    if not ret: break # AND THIS
...

【讨论】:

  • 一个关于colab的问题,这个代码不起作用。
  • stackoverflow.com/a/57651882/10850556这个链接会帮助你
  • 我试了一下还是一样的错误
  • 我再说一遍,frame 是空的,你不能 imwrite 是一个空框架。它是空的,因为 google colab 上没有网络摄像头。我不能更清楚地说明这一点。 -- 忽略阿里和他的链接。他没有意识到问题出在哪里。
猜你喜欢
  • 2021-02-15
  • 1970-01-01
  • 2020-06-07
  • 2021-04-14
  • 2020-04-22
  • 2021-05-24
  • 1970-01-01
  • 2019-02-15
  • 2019-09-01
相关资源
最近更新 更多