Whitehawk
Whitehawk

Reputation: 46

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

Upvotes: 3

Views: 3682

Answers (3)

Ruchika Mattoo
Ruchika Mattoo

Reputation: 21

try using this :

    cam=cv2.VideoCapture('MyVideo.mov')
    while(True):
      ret, img = cam.read()
      print ("frame", img)

      if cv2.waitKey(100) & 0xFF == ord('q'):
      break

    cam.release()
    cv2.destroyAllWindows()

Upvotes: 0

ThisGuyCantEven
ThisGuyCantEven

Reputation: 1267

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()

Upvotes: 1

Saransh Kejriwal
Saransh Kejriwal

Reputation: 2533

Since your code shows no issues on Mac, try using other file extensions (eg. mp4 or wmv) on your Windows system, for testing. If your video is loaded then, that means OpenCV is correctly configured on your Windows, but apparently there is no driver to play .mov files

Upvotes: 0

Related Questions