Can't show image with opencv when importing av

Question:

When importing the PyAv module, I am unable to show an image with opencv using imshow()

Code without the PyAv module (works as expected)

import cv2

img = cv2.imread("test_image.jpeg")
cv2.imshow('image', img)
cv2.waitKey(0)

Code with the import (doesn’t work, just hangs)

import cv2
import av

img = cv2.imread("test_image.jpeg")
cv2.imshow('image', img)
cv2.waitKey(0)

OS: Linux arch 5.18.3-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 09 Jun 2022 16:14:10 +0000 x86_64 GNU/Linux

Am I doing something wrong or is this a (un-)known issue?

Asked By: Flojomojo

||

Answers:

So it is a known issue, mentioned here, but it is possible to use the workaround mentioned in the post, here is a quick rundown of how I did it:

NOTE: Do this in a python virtualenv or you might run into problems in the long run

  1. Find which version of ffmpeg PyAv uses (currently it’s ‘ffmpeg-4.4.1-5’) and download the exact ffmpeg version here
  2. Clone the python-opencv github repo like this git clone --recursive https://github.com/opencv/opencv-python.git and run this script (If this fails, because the path ‘/tmp/vendor/include doesn’t exists, just create it like this mkdir -p /tmp/vendor/include)
  3. After this is done you can check in the output if the correct ffmpeg version is selected
  4. If everything looks correct, you can build it with this command make -j4 and let it build
  5. Finally cd into the python_loader folder and run python3 setup.py develop and you are done installing 🙂
Answered By: Flojomojo

Instead of having to compile from source, you can instead uninstall pyav and reinstall it with:

pip install av --no-binary av

This installs PyAV without FFmpeg and defaults to using your system’s already available FFmpeg (likely OpenCV’s). This answer was suggested by the people at PyAV reference. This is assuming that you already have FFmpeg (likely from OpenCV).

Answered By: Eduardo Davalos
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.