python cv2.VideoCapture does not open

python-2.7 video opencv3.1

4836 просмотра

4 ответа

407 Репутация автора

I have a few avi files which I'm trying to read frame by frame. I have used opencv 3.1.0 in order to read frames:

import cv2
cap = cv2.VideoCapture(file_path)

and then I just read the frames of cap. This works fine on some of the videos. I took a few more videos recently and the same code comes up empty. I checked:

cap.isOpened() # return False

and it seems like the video is not opened properly. As far as I'm concerned the only thing that changed is that the new videos are in color whereas the old ones were not. I don't know how that would change anything in the code. I checked that the videos open up just fine in other software (imageJ) so I'm confident the video itself is not the issue.

Any ideas why the new videos are not opening properly? I have not been able to find any information on how to debug this issue. Any advice would be greatly appreciated.

Автор: Eyal S. Источник Размещён: 18.07.2016 09:39

Ответы (4)


2 плюса

407 Репутация автора

I switched to imageio instead of opencv and everything works well

Автор: Eyal S. Размещён: 20.07.2016 08:53

0 плюса

458 Репутация автора

The problem is that you never actually open your cap. I have not done this from a video file but to use the camera it looks something like this:

import cv2
#instantiate videocapture object (0 is for the default webcam)
cap = cv2.VideoCapture(0)

while(True):
    #read frame from video file
    ret, frame = cap.read()
    cv2.imshow('frame',frame)

    #if esc key pressed or no more video input
    if cv2.waitKey(1) & 0xFF == 27 or ret==False:
        break

# When everything done, release the capture

cv2.destroyAllWindows()
cap.release() 
Автор: ThisGuyCantEven Размещён: 30.09.2016 06:30

1 плюс

11 Репутация автора

I was making the same mistake.

Solution.

  1. Use Pycharm
  2. Install Anaconda3
  3. conda install -c conda-forge opencv ( open the CMD as an administrator )
  4. Select Anaconda3 in PyCharm ( File -> Settings -> Project Interpreter )

AND TEST

def VideoPlayer():
    cap = cv2.VideoCapture("D:\OpenCv4Programmers\datas\TomAndJerry.mp4")

    while (True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the resulting frame
        cv2.imshow('frame', gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
Автор: Taha AHISKALI Размещён: 03.10.2017 11:18

0 плюса

1 Репутация автора

I'm using Windows, Python 2.7.4. I tried Opencv installation but it did not work, I also installed ffmpeg. Then I tried this:

pip install opencv-python

This worked for me.

Автор: zqttan Размещён: 23.04.2018 05:08
Вопросы из категории :
32x32