简体   繁体   中英

Video Processing using Python and OpenCV

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

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, eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM