Home > Net >  !_img.empty() in function 'imwrite'
!_img.empty() in function 'imwrite'

Time:12-12

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

CodePudding user response:

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
...

CodePudding user response:

colab doesn't use imshow directly

I suggest you look at this part.

Link

  • Related