简体   繁体   English

使用Python和OpenCV进行视频处理

[英]Video Processing using Python and OpenCV

I am trying to capture a video and convert into frames using python and openCV. 我正在尝试捕获视频并使用python和openCV转换为帧。 I followed the steps that need to done to import openCV2 libraries for python in windows 8 which is as follows: 我按照以下步骤操作,在Windows 8中为python导入openCV2库,如下所示:

Download Python, Numpy, OpenCV from their official sites. 从他们的官方网站下载Python,Numpy,OpenCV。

Extract OpenCV (will be extracted to a folder opencv) 提取OpenCV(将解压缩到opencv文件夹)

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

Paste it in C:\\Python27\\Lib\\site-packages 将其粘贴到C:\\ Python27 \\ Lib \\ site-packages中

Open Python IDLE or terminal, and type 打开Python IDLE或终端,然后输入

I have done the above steps to import opencv libraries into python. 我已经完成了上述步骤,将opencv库导入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. 但是当我导入CV2库并尝试捕获视频时,我无法使用函数cv.CaptureFromFile()生成帧并使用cv.QueryFrame()函数访问帧。 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. 处理输出没有任何错误,但帧不生成,甚至帧计数的print语句也为零。

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. 我只想知道在Windows中为python安装OpenCV以及使用上述功能访问视频帧的过程。

Thanks in advance 提前致谢

Using opencv to read video files 使用opencv读取视频文件

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 正如在本教程中提到这里 ,推荐的方法是现在使用cv2 (不cv像你一样)使用读取视频文件,例如:

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 在Windows上安装Python和OpenCV

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 . 对于在Windows上安装OpenCV的python绑定的简单解决方案,您可能希望尝试捆绑包括opencv在内的一堆python模块的Python(xy)发行版

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. Python的opencv绑定包含在opencv中,所以如果你没有得到任何导入错误,那么你的opencv设置可能是正确的。

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来安装第三方绑定,但是berak指出这通常不建议使用;

pip install pyopencv  

Pip itself can be installed as outlined in this answer 可以按照本答案中的说明安装Pip本身

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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