'OpenCV camera Issue, Empty image

to learn more about machine learning I am trying to teach my "App" to recognise the difference between multiple soda brands. there for I want to use my camera in my laptop to capture some pictures, but I am getting this error message :

error: OpenCV(4.5.5) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgcodecs/src/loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

The camera opens up and works, but it gets stuck after a 2 seconds. (working on a MacBook Pro )

import cv2
import os
import time
import uuid


IMAGES_PATH = 'TensorFlow/workspace/images/collectedimages'

labels = ['Sprite','Coca-Cola','Fanta','Pepsi','Redbull']
number_imgs = 15

for label in labels:
 !mkdir { 'Tensorflow\workspace\images\collectedimages\\'+ label}
 cap = cv2.VideoCapture(0)
 assert cap.isOpened()
 print('collecting images for {}'.format(label))
 time.sleep(5)

 for imgnum in range(number_imgs):
     ret, frame = cap.read()
     imagename = os.path.join(IMAGES_PATH, label, label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
    cv2.imwrite(imagename, frame)
    cv2.imshow('frame', frame)
    time.sleep(2)

    if cv2.waitKey(1) and 0xFF == ord('q'):
        break
    cap.release()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source