Unable to read or play video in OpenCV + Python using VideoCapture

import cv2
import numpy as np

cap = cv2.VideoCapture('traffic.avi')

retval, frame = cap.read()

print retval

================ RESTART: J:\Python For DIP\traffic_video.py ================
False
>>> 

The value retval is always False, which means that the video is not read by the command. It must be true for reading frames. I do not know what to do. However, when I use my default webcam, it becomes True. I tried a lot of videos and had the same problem. Note. I installed ffmpeg correctly.

Note. This is not complete code, at this step I only check cap.read () either True or False

+4
source share
2 answers

This method is 100% guaranteed.

OpenCV, , , 2.4.11. , Python:

>>> from cv2 import __version__
>>> __version__
'2.4.11'
>>> 

C:\opencv\build\x86\vc12\bin opencv_ffmpeg2411.dll. , Python ex: C:\Python27 opencv_ffmpeg2411.dll

opencv_ffmpeg2411.dll, opencv , opencv_ffmpeg ( opencv ).dll

Python ,

import numpy as np
import cv2

# Capture video from file
cap = cv2.VideoCapture('your video')

while True:

    ret, frame = cap.read()

    if ret == True:

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cv2.imshow('frame',gray)


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

    else:
        break

cap.release()
cv2.destroyAllWindows()

, :

+11

Python . Enthought opencv_ffmpeg Python.

:

C:\Users\USERNAME\AppData\Local\Programs\Python\Python35-32

RIGHT:

C:\Users\USERNAME\AppData\Local\Enthought\Canopy\User

, , Python.

+1

Source: https://habr.com/ru/post/1627575/


All Articles