0

I am using https://github.com/theAIGuysCode/yolov4-custom-functions this repository. I want to crop and save pictures while running on webcam. I'm using Colab. But when I run the following code:

!python detect_video.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --video 0 --output ./detections/results2.avi --crop

then it says:

[ WARN:0@5.489] global /io/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Video has ended or failed, try a different video format!

I know the problem is with opening the camera in Colab. But I couldn't find how to solve it.

isinsu
  • 53
  • 1
  • 7
  • Colab runs your code in a remote server, you cannot use your local webcam because it is literally not connected to the remote server. – Dr. Snoopy Aug 18 '22 at 12:19
  • I saw a Colab notebook like this: https://colab.research.google.com/drive/1xdjyBiY75MAVRSjgmiqI7pbRLn58VrbE?usp=sharing#scrollTo=UfaHiGn461oG But I have no idea if this is adaptable. – isinsu Aug 18 '22 at 12:33

1 Answers1

0

If you're somehow able to use the webcam through Colab, make sure that the format of the video stream from your camera is supported by cv2. I had a quick look and apparently only .avi is supported. So make sure that your input is in avi format.

Having a look at the detect_video.py file that you linked, the error raises in this piece of code:

# begin video capture
try:
    vid = cv2.VideoCapture(int(video_path))
except:
    vid = cv2.VideoCapture(video_path)

...

while True:
    return_value, frame = vid.read()
    if return_value:
        ...
    else:
        print('Video has ended or failed, try a different video format!')
        break

This means that cv2.VideoCapture() is not able to read your input.

ClaudiaR
  • 3,108
  • 2
  • 13
  • 27