-2

I'm working on real-time object detection using TensorFlow. So the first step is to collect images for some signs.

import cv2
import os
import time
import uuid
cv2.__version__

labels = ['hi']
number_imgs = 5

IMAGES_PATH = os.path.join('Tensorflow', 'workspace', 'images', 'collectedimages')

if not os.path.exists(IMAGES_PATH):
    if os.name == 'posix':
        !mkdir -p {IMAGES_PATH}
    if os.name == 'nt':
         !mkdir {IMAGES_PATH}
for label in labels:
    path = os.path.join(IMAGES_PATH, label)
    if not os.path.exists(path):
        !mkdir {path}

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()

This code is successfully running on Jupyter notebook but not in Google Colab getting this error

Collecting images for hi Collecting image 0
--------------------------------------------------------------------------- error                                     Traceback (most recent call last) <ipython-input-23-d176419936b4> in <module>()
      7         ret, frame = cap.read()
      8         imgname = os.path.join(IMAGES_PATH,label,label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
----> 9         cv2.imwrite(imgname, frame)
     10         cv2.imshow('frame', frame)
     11         time.sleep(2)

error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
M.K. Mandawar
  • 69
  • 1
  • 12

1 Answers1

1

Google colab is browser based, so there's no way to access local hardware such as webcams without and API. There is an example code for accessing your computer's webcam below. Please note that imshow is also not available in Google Colab, you need to execute from google.colab.patches import cv2_imshow and call cv2_imshow(frame) to show your images.

https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi

Ege Yıldırım
  • 430
  • 3
  • 14
  • Are you able to understand my code, what it exactly do? I tried your solution but got the same error. Once try to run this code in colab at your side please. Why we can't access the webcam using colab. – M.K. Mandawar Mar 28 '22 at 14:42
  • Read my answer carefully. You can access webcam using colab, just not without a communication API. VideoCapture(0) command is running on another device when you call it. (Google's Servers). You cannot expect it to magically use your own PC's webcam. Through JS codes you can establish a connection between Google Colab and your own PC, then capture the frames. if you have another question about your code, post another question. – Ege Yıldırım Mar 28 '22 at 15:16