3

I am trying to capture a video and convert into frames using python and openCV. I followed the steps that need to done to import openCV2 libraries for python in windows 8 which is as follows:

Download Python, Numpy, OpenCV from their official sites.

Extract OpenCV (will be extracted to a folder opencv)

Copy ..\opencv\build\python\x86\2.7\cv2.pyd

Paste it in C:\Python27\Lib\site-packages

Open Python IDLE or terminal, and type

I have done the above steps to import opencv libraries into python.

But when I import CV2 libraries and try to capture a video, I was unable to generate frames using the function cv.CaptureFromFile() and accessing the frames using cv.QueryFrame() functions. I was unable to capture the frames. Please find the code below.

import sys
import os
import numpy as np
import PIL
from matplotlib import pyplot as plt
import cv2.cv as cv

winname = "myWindow"
win = cv.NamedWindow(winname, cv.CV_WINDOW_AUTOSIZE)

invideo = cv.CaptureFromFile("chayya1.avi")

totalNumberOfFrames = int(cv.GetCaptureProperty(invideo, cv.CV_CAP_PROP_FRAME_COUNT))
framesprocessing = totalNumberOfFrames
print(totalNumberOfFrames)
while (True):
    im = cv.QueryFrame(invideo)
    cv.ShowImage(winname, im)
    if cv.WaitKey() == 27: # ASCII 27 is the ESC key
        break
del invideo
cv.DestroyWindow(winname)

The output is getting processed without any errors but the frames are not generating and even the print statement for frame count is also zero.

I just want to what is procedure for installing OpenCV for python in windows and accessing the frames of a video using the obove mentioned functions.

Thanks in advance

user3366134
  • 31
  • 1
  • 3
  • Have you tried a different video or webcam? Also, do you have a current version of ffmpeg or gstreamer installed? – Fookatchu Mar 01 '14 at 07:56
  • I have tried using webcam and it is working. When I give video file as input the function is not capturing the frames – user3366134 Mar 01 '14 at 19:12
  • seems that the video you want to parse cannot be decoded. I would try a different one and/or install/update ffmpeg – Fookatchu Mar 02 '14 at 12:49
  • I have checked this with multiple videos. Could you please provide the process to install ffmpeg – user3366134 Mar 03 '14 at 02:16

1 Answers1

1

Using opencv to read video files

As mentioned in the tutorial here, the recommended approach now is to use cv2 (not cv as you did) to read video files using, e.g.

import numpy as np
import cv2

cap = cv2.VideoCapture("chayya1.avi")

while(cap.isOpened()):
    ret, frame = cap.read()

    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Python & OpenCV installation on windows

For a simple solution to installing OpenCV's python bindings on windows you might want to try the Python(xy) distribution which bundles a bunch of python modules including opencv.

Python's opencv bindings are included with opencv, so if you didn't get any import errors then your opencv setup is likely to be correct.

Alternative bindings

If you don't like the native bindings, you could try using pip from the command line to install 3rd party bindings, though as berak pointed out this is not generally recommended;

pip install pyopencv  

Pip itself can be installed as outlined in this answer

Community
  • 1
  • 1
jmetz
  • 10,341
  • 3
  • 25
  • 38
  • 1
    wait, pyopencv is not opencv's native python wrapper, but a 3rd party one ? (that would be a very bad idea ) – berak Mar 01 '14 at 08:57
  • Thanks for your comment @berak, updated post accordingly – jmetz Mar 04 '14 at 10:17
  • You can also `pip install opencv-python` for official wrapper – Eran W May 22 '19 at 11:45
  • @EranW - it seems not..."Unofficial pre-built OpenCV packages for Python."? Taken from the pypi page for `opencv-python`: https://pypi.org/project/opencv-python/ – jmetz Jun 07 '19 at 14:10