Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - OpenCV VideoCapture = False (Windows)

I have a simple piece of code, written in Python (version 2.7.11) designed to do things to a video file as follows:

import cv2

cap = cv2.VideoCapture('MyVideo.mov')
print(cap)
print(cap.isOpened())

while(cap.isOpened()):
    #Do some stuff

The result of print(cap) is a 8 digit hex number, so I don't know if that means that the video has been found.

However, the print statement for cap.isOpened() returns False. I have tried several fixes, but none of them worked. Any help or insight would be very helpful.

Things to note/things I have tried

  • I am running Windows 8.1, Python 2.7.11 and OpenCV 3.1.0
  • The location of the video file is in the same directory as the Python script
  • I have the following directories appended to my PATH variable:

    C:\Users\MyName\OpenCV3\opencv\build\x64\vc14\bin; 
    C:\Users\MyName\OpenCV3\opencv\sources\3rdparty\ffmpeg;
    C:\Python27\;
    C:\Python27\Scripts
    
  • I have checked that I have opencv_ffmpeg.dll in the OpenCV vc14 bin directory

  • I have checked that said dll file is titled opencv_ffmpeg310_64.dll

  • I have tried redownloading said dll file, and renaming it to include the version of OpenCV and the fact that my system is a 64-bit one

  • I have tried placing the dll file in the Python27 directory

  • The code above works on Mac, but not on Windows (tried the code on 2 different Macs and it worked, tried it on 2 different Windows machines and it returned false both times)

like image 387
Whitehawk Avatar asked Feb 13 '26 02:02

Whitehawk


1 Answers

It is most likely that if you are using windows, files are in a \Users or other \<something> directory. The \ is seen as a unicode escape by the python interpreter and whatever follows it is probably an invalid escape. try typing r'<file path>' to cause the path to be read as raw text and have the unicode escapes ignored.

Try adding:

if(not cap.isOpened()):
    cap.open(r'<file_path>')

And if the problem is the file path, it will probably cause an error. Alternatively, you could just use a loop like this:

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    #if frame can't be read
    if ret==False:
        print('end of input or incompatible video file')
        break
    cv2.imshow('frame',frame)
    #if esc key pressed
    if cv2.waitKey(1) & 0xFF == 27:
        break


cv2.destroyAllWindows()
cap.release()
like image 169
ThisGuyCantEven Avatar answered Feb 15 '26 14:02

ThisGuyCantEven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!