-3

i just try this code in 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() and i got this erreur

meriem za
  • 1
  • 1
  • 1
  • 2
    Which camera do you expect to capture in colab? img.empty() means there was no image successfully captured, better test whether ret is true. – Micka Dec 11 '21 at 16:45

1 Answers1

3

There are no webcams on Google Colab servers.

Code on Google Colab is running on google servers, not in your browser.

You can't open VideoCapture(0) when you run code on Google Colab.

You need to have error checking in your code for VideoCapture.

Since you don't check for errors, and opening the VideoCapture failed, frame will be None. You can't imwrite None because that's not a valid array/image.

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

while True:
    ret, frame = cap.read()
    if not ret: break # AND THIS
...
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36