Sunday, September 5, 2021

Fixing Failure to Load Video in OpenCV-Python on Linux

The failure to load video in OpenCV-Python usually happened with older Linux distributions. In my case, it happened in Ubuntu 18.04. The failure in video loading can be detected in python code similar to this:

import cv2

..

cap = cv2.VideoCapture(video_file_path)

if cap.isOpened():

    print("Video file loaded successfully")

else:

    print("Failed to load video!") 

    return

..

The failure to load the video very possibly is caused by erratic OpenCV-Python installation. In my case it was caused by very old pip version. Old pip version will trigger unexpected problems, in my case the old pip version (pip version 9.x) forces OpenCV recompilation when installing OpenCV-python. The old pip version installed the result of the recompilation at the end of pip's execution. Due to erratic pip behaviour, the resulting opencv-python is not working properly. Anyway, as a sidenote: I'm using python 3 venv virtual environment when I encountered this problem. 

THE FIX:

The fix is quite simple:

  1. Uninstall your current (unworkable) OpenCV-python. In my case, simply invoking "pip unistall opencv-python" works. I carried-out this command inside the virtual environment that has the erratic OpenCV-python. 
  2. Upgrade your pip version into more up-to-date version with "pip install --upgrade pip" command. Don't be afraid to carry-out this step if you're using python virtual environment because it won't affect your machine in any way. 
  3. Re-install OpenCV-python with pip with the following command: "pip install opencv-python" or if you want to specify the specific OpenCV-python version you can do that as well, as in (installing opencv-python version 4.3.0.38): "pip install opencv-python==4.3.0.38" 
This isssue is explained in more detail here: https://pypi.org/project/opencv-python/ (point 2 in the Installation section). It took me an hour to figure out the problem because I've never encountered the issue before. Hopefully this helps you out if you have similar problem.

Post a Comment

No comments: