0

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

python detect_video.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --video ./data/video/bobinmi.mp4 --output ./detections/results.avi --crop

I can just detect the first frame of the video. Then it gives error and not detect. What should I do?

I'm using CUDA 10.1, Python 3.7 and Tensorflow 2.3. The error is same in CPU and GPU working environments.

The error message is:

FPS: 1.50
qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/usr/local/lib/python3.7/dist-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

Fatal Python error: Aborted

Current thread 0x00007fbeed09b780 (most recent call first):
  File "detect_video.py", line 165 in main
  File "/usr/local/lib/python3.7/dist-packages/absl/app.py", line 254 in _run_main
  File "/usr/local/lib/python3.7/dist-packages/absl/app.py", line 308 in run
  File "detect_video.py", line 178 in <module>

Second problem is when I use this command:

!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
  • I tried it both on my machine and colab. The result is same both of them. It detects only the first frame and then nothing, it does not give an error in my local machine. But it detects only the first frame and then gives error in colab. Yes it has a display. – isinsu Aug 16 '22 at 06:04

1 Answers1

3

The error message is only caused in Colab at line 165, that is with the following instruction:

cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)

From the documentation this function is used to create a window. The reason is that Colab does not have a screen and some cv2 functions need a screen to perform the operations. Creating a windows is definitely one of them.

To fix the error in Colab should be enough to install a virtual screen. The code (from this stack answer):

!apt-get install -y xvfb # Install X Virtual Frame Buffer
import os
os.system('Xvfb :1 -screen 0 1600x1200x16  &')    # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
os.environ['DISPLAY']=':1.0'    # tell X clients to use our virtual DISPLAY :1.0
ClaudiaR
  • 3,108
  • 2
  • 13
  • 27