Python中的OpenCV 2.4-视频处理 [英] OpenCV 2.4 in python - Video processing

查看:89
本文介绍了Python中的OpenCV 2.4-视频处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目:在视频的每个帧上添加一个运行日期/时间戳. (数码摄像机的结果,父亲问我如何才能将时间戳(以毫秒为单位)永久添加到视频中.

The Project: Add a running date/time stamp on each and every frame of a video. (The result of digital video camera, and my father asked me how can he add the timestamp(to the milliseconds resolution) permanently to the video.

一个朋友向我指出了opencv(实际上是emgucv),由于我的偏好,我在python中尝试了opencv的运气.

A friend pointed me to opencv (emgucv actually) , and because of my preferences I tried my luck with opencv in python.

文档很la脚,我什至很难安装软件包(大约5个小时左右). 来源:

The documentation is lame, and I had even hard time(took me like 5 hours or so) just to install the package. Sources:

  • OpenCV 2.4 (willow garage): http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.2/
  • Numpy (I didn't even understood what does it do or why needed): http://sourceforge.net/projects/numpy/files/NumPy/1.6.2/numpy-1.6.2-win32-superpack-python2.7.exe/download
  • Active Python 2.7: http://downloads.activestate.com/ActivePython/releases/2.7.2.5/ActivePython-2.7.2.5-win32-x86.msi

我正在Windows 7 x64上工作,因此我不得不降级python以使用numpy(win64没有numpy版本)

I am working on Windows 7 x64 , so I had to downgrade my python to work with numpy(no numpy version for win64)

使用PyCharm IDE.

Working with PyCharm IDE.

最终的安装使我拥有文件C:\ Python27 \ Lib \ site-packages \ cv2.pyd

The resulting installation got me to have the file C:\Python27\Lib\site-packages\cv2.pyd

我正在尝试查找开始使用的文档,但是我非常困惑,不知道从哪里开始,所有示例都令人困惑-即:

I am trying to find documentation to start working with, but I am very confused and have no clue where to start from, all the examples are confusing - ie:

  • The "official" documentation is for c/c++, no python references: http://docs.opencv.org/doc/user_guide/ug_mat.html
  • Example for python that seems reasonable but not close to what i need: Python OpenCV 2.4 writes half-complete PNG video frames
  • Example that is close to what I need, but doesn't seem logical(cv and not cv2 ???): Writing video with OpenCV + Python + Mac

我的问题:

  1. 我做错什么了吗?这不是安装opencv的方法吗?
  2. 我在哪里可以找到好的文档?
  3. 假设我已经准备好我的文本(以字符串形式),有人可以尝试帮助我启动我的应用程序吗?

谢谢

推荐答案

使用OpenCV和Python,您的任务应该相对容易完成.看来您是OpenCV的新手,所以我将尽力保持我的解释透彻,但随时询问您是否需要任何澄清.

Your task should be relatively easy to accomplish using OpenCV and Python. It seems that you are new to OpenCV, so I will try and keep my explanation thorough, but feel free to ask if you need any clarity.

我不确定您是从实时摄像机视频源中获取数据,还是在对记录的录像进行后期处理.无论哪种方式...

I am not sure if you are getting your data from a live camera video feed, or are post-processing recorded footage. Either way...

获取数据. 如果使用实时供稿:

Get data. If using a live feed:

capture = cv2.VideoCapture(0)

如果使用录制的镜头:

capture = cv2.VideoCapture("your_reading_file.avi")

初始化视频编写器.在此问题上找到该问题以寻求编解码器的帮助-找到有效的编解码器是不平凡.我也使用Windows 7 x64,下面提到的编解码器是唯一对我有用的编解码器.另外,将变量fps设置为尽可能接近实际传入视频帧速率-开始写帧后就无法更改.

Initialise video writer. Look at this question for help with codecs- finding a working codec is not trivial. I am also using Windows 7 x64, and the below-mentioned codec was the only one that worked for me. Also, set the variable fps as close to the actual incoming video framerate as possible- you can not change it once you have started writing frames.

flag, frame = capture.read() # **EDIT:** to get frame size
width = np.size(frame, 1) #here is why you need numpy!  (remember to "import numpy as np")
height = np.size(frame, 0)
writer = cv2.VideoWriter(filename="your_writing_file.avi", 
fourcc=cv2.cv.CV_FOURCC('I', 'Y', 'U', 'V'), #this is the codec that works for me
fps=15, #frames per second, I suggest 15 as a rough initial estimate
frameSize=(width, height))

处理此数据并添加您的文本.最后,将编辑后的帧写入视频文件.

Process this data and add your text. Lastly, write the edited frame to a video file.

while True:
    flag, frame = capture.read() #Flag returns 1 for success, 0 for failure. Frame is the currently processed frame

    if flag == 0: #Something is wrong with your data, or the end of the video file was reached
        break 
    x = width/2
    y = height/2 #change to the desired coordinates
    text_color = (255,0,0) #color as (B,G,R)
    cv2.putText(frame, "your_string", (x,y), cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=1, lineType=cv2.CV_AA)

    writer.write(frame) #write to the video file

就这么简单!我几乎每天都使用上面的代码向视频文件中写入文本,因此它确实可以正常工作.我可以预见的唯一潜在问题是编解码器,但不幸的是,我对此并不了解很多.我希望这可以解决您的问题,随时问更多问题.

As simple as that! I use the above code to write text to video files almost daily, so it definitely works. The only potential problems I can foresee are with the codecs, which I unfortunately do not know a lot about. I hope that this may solve your problem, feel free to ask more questions.

编辑:对您评论的问题的回答.

Answers to your comment's questions.

1.)据我所知,您只能使用.avi,因为必须在OpenCV中使用未压缩的格式.恐怕我对使用其他(压缩)格式一无所知.也许您可以使用第三方程序进行转换前/转换后? frame异常的原因是我的错误,我已编辑答案以包括缺少的行.

1.) As far as I know, you can only use .avi because you have to use an uncompressed format with OpenCV. I am afraid I have no knowledge of using other (compressed) formats. Maybe you could use a third-party program to do pre/post-conversion? The reason for the frame exception was my mistake, I have edited the answer to include the missing line.

2.)恐怕我不知道如何读取元数据.如果我发现了,我会让你知道.我自己用于查找视频帧率的解决方案是让OpenCV使用该视频一次,并使用Time模块计算平均帧率.然后,可以在编写视频文件时使用此估算值.

2.) I'm afraid I have no idea how to read metadata. If I find out I will let you know. My own hackish solution for finding video framerate is to let OpenCV run through the video once, using the Time module to calculate the average framerate. This estimate can then be used when writing the video file.

3.)我发现生成的视频的大小可能与原始视频有很大不同,这取决于几个因素,最重要的是所选的fps与实际原始帧速率有多近.

3.) I have found that the size of the resulting video may differ significantly from the original depending on several factors, the most important being how close the chosen fps was to the actual original framerate.

4.)至于其他字体,有几种可用.我可以向您推荐此问题快速浏览.这是相关的文档:

4.) As for other fonts, there are several available. I can refer you to this question for a quick overview. Here is the relevant documentation:

fontFace – Font type. One of FONT_HERSHEY_SIMPLEX, 
FONT_HERSHEY_PLAIN, 
FONT_HERSHEY_DUPLEX, 
FONT_HERSHEY_COMPLEX, 
FONT_HERSHEY_TRIPLEX, 
FONT_HERSHEY_COMPLEX_SMALL, 
FONT_HERSHEY_SCRIPT_SIMPLEX, or 
FONT_HERSHEY_SCRIPT_COMPLEX, 
where each of the font ID’s can be combined with FONT_HERSHEY_ITALIC to get the slanted letters.

这篇关于Python中的OpenCV 2.4-视频处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆