Get video size in python-opencv

I can get the image size, for example:

import cv2

img = cv2.imread('my_image.jpg',0)
height, width = img.shape[:2]

What about the video?

+16
source share
5 answers

This gives widthboth the heightfile or camera as a floating-point number (so you may have to convert to an integer)

But it always gives me 0.0 FPS.

import cv2

vcap = cv2.VideoCapture('video.avi') # 0=camera

if vcap.isOpened(): 
    # get vcap property 
    width = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)   # float
    height = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) # float

    # or
    width = vcap.get(3)  # float
    height = vcap.get(4) # float

    # it gives me 0.0 :/
    fps = vcap.get(cv2.cv.CV_CAP_PROP_FPS)

It seems to work fps = vcap.get(7), but I checked this on only one file.


UPDATE 2019: Current cv2 uses slightly different names (but they have the same numbers: 3, 4, 5, 7)

import cv2

vcap = cv2.VideoCapture('video.avi') # 0=camera

if vcap.isOpened(): 
    width =  vcap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
    height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
    print('width, height:', width, height)
    #print(cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT) # 3, 4

    fps = vcap.get(cv2.CAP_PROP_FPS)
    print('fps:', fps)  # float
    #print(cv2.CAP_PROP_FPS) # 5

    fps = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
    print('frames count:', fps)  # float
    #print(cv2.CAP_PROP_FRAME_COUNT) # 7
+23
source
width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH )
height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT )
fps =  vcap.get(cv2.CAP_PROP_FPS)

or

width = vcap.get(3)
height = vcap.get(4)
fps = vcap.get(5)
+14
source

3.3.1 . : https://docs.opencv.org/3.3.1/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d

cv2.cv.CV_CAP_PROP_FRAME_WIDTH cv2.CAP_PROP_FRAME_WIDTH , , .

+10
cv2.__version__
'3.4.3' 

w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
+3

vcap.get(i), 0 21, OpenCV.

0

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


All Articles