Ask Your Question
0

isOpened() returns false on files

asked 2019-10-28 11:17:17 -0600

Hey everyone, I've been trying to run the following code:

import cv2
cap = cv2.VideoCapture("asdf.mp4")
while cap.isOpened():
    ret, frame = cap.read()

    cv2.imshow('frame', frame)
cap.release()
cv2.destroyAllWindows()

However, cap.isOpened() returns false for anything that is not 0 (webcam). I tried to type both relative and absolute path, but none works. Am I missing something?

Thanks, Tal

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-10-28 11:41:49 -0600

supra56 gravatar image

updated 2019-10-28 11:42:53 -0600

You are missing waitKey():

import cv2
cap = cv2.VideoCapture("road.avi")
while cap.isOpened():
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Yeah, I know, but my code doesn't even reach to that point since cap.isOpened() returns false.

TalBarami gravatar imageTalBarami ( 2019-10-28 11:52:37 -0600 )edit

Didn't you added cv2.waiKey()?

supra56 gravatar imagesupra56 ( 2019-10-28 12:00:15 -0600 )edit

No, it fails even before I need to. If I type:

cap = cv2.VideoCapture("asdf.mp4") cap.isOpened() ---> returns false

TalBarami gravatar imageTalBarami ( 2019-10-28 12:26:17 -0600 )edit

Is your file is in the same directory? Type ls -l asdf.mp4

supra56 gravatar imagesupra56 ( 2019-10-28 13:02:41 -0600 )edit
1

Yes, it is. I also tried to I'm using windows and cv2 was installed with Anaconda for python 3.6.

TalBarami gravatar imageTalBarami ( 2019-10-28 16:44:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-10-28 11:17:17 -0600

Seen: 5,544 times

Last updated: Oct 28 '19