pythonopencvffmpegpyav

Can't show image with opencv when importing av


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?


Solution

  • 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).