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?
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
- Find which version of ffmpeg PyAv uses (currently it’s ‘ffmpeg-4.4.1-5’) and download the exact ffmpeg version here
- 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
)
- After this is done you can check in the output if the correct ffmpeg version is selected
- If everything looks correct, you can build it with this command
make -j4
and let it build
- Finally cd into the
python_loader
folder and run python3 setup.py develop
and you are done installing 🙂
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).
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?
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
- Find which version of ffmpeg PyAv uses (currently it’s ‘ffmpeg-4.4.1-5’) and download the exact ffmpeg version here
- 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 thismkdir -p /tmp/vendor/include
) - After this is done you can check in the output if the correct ffmpeg version is selected
- If everything looks correct, you can build it with this command
make -j4
and let it build - Finally cd into the
python_loader
folder and runpython3 setup.py develop
and you are done installing 🙂
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).