如何使用 OpenCV 和 Python 在视频流中逐帧处理视频图像 [英] How to process images of a video, frame by frame, in video streaming using OpenCV and Python

查看:136
本文介绍了如何使用 OpenCV 和 Python 在视频流中逐帧处理视频图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OpenCV 的初学者.我想对上传到我的服务器的视频的帧进行一些图像处理.我只想读取可用的帧并将它们写入目录中.然后,等待视频的另一部分上传并将帧写入目录.并且,我应该等待每一帧完全上传然后将其写入文件.

I am a beginner in OpenCV. I want to do some image processing on the frames of a video which is being uploaded to my server. I just want to read the available frames and write them in a directory. Then, wait for the other part of the video to be uploaded and write the frames to the directory. And , I should wait for each frame to be completely uploaded then write it to a file.

你能告诉我如何用 OpenCV (Python) 做到这一点吗?

Can you tell me how can I do it with OpenCV (Python)?

编辑 1:我编写了这段代码,用于从文件中捕获视频,同时将新数据附加到文件末尾.换句话说,out.mp4 文件不是一个完整的视频,另一个程序正在其上写入新的帧.我要做的是,等待其他程序写入新的帧,然后读取它们并显示它们.

Edit 1: I wrote this code for capturing the video from a file, while new data are being appended at the end of the file. In other words, the out.mp4 file is not a complete video and another program is writing new frames on it. What I'm going to do is, wait for the other program to write new frames then read them and display them.

这是我的代码:

import cv2
cap = cv2.VideoCapture("./out.mp4")

while True:
    if cap.grab():
        flag, frame = cap.retrieve()
        if not flag:
            continue
        else:
            cv2.imshow('video', frame)
    if cv2.waitKey(10) == 27:
        break

所以问题在于 cap.grab() 调用!当没有frame时,会返回False!而且它不会再捕捉帧,即使我等了很长时间.

So the problem is the cap.grab() call! When there is no frame, it will return False! And it won't capture frames anymore, even if I wait for a long time.

推荐答案

阅读文档后VideoCapture.我发现你可以告诉 VideoCapture,下次我们调用 VideoCapture.read()(或 VideoCapture.grab()).

After reading the documentation of VideoCapture. I figured out that you can tell VideoCapture, which frame to process next time we call VideoCapture.read() (or VideoCapture.grab()).

问题在于,当您想要read()一个未准备好的帧时,VideoCapture 对象会卡在该帧上并且永远不会继续.所以你必须强制它从前一帧重新开始.

The problem is that when you want to read() a frame which is not ready, the VideoCapture object stuck on that frame and never proceed. So you have to force it to start again from the previous frame.

这是代码

import cv2

cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
    cap = cv2.VideoCapture("./out.mp4")
    cv2.waitKey(1000)
    print "Wait for the header"

pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
    flag, frame = cap.read()
    if flag:
        # The frame is ready and already captured
        cv2.imshow('video', frame)
        pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
        print str(pos_frame)+" frames"
    else:
        # The next frame is not ready, so we try to read it again
        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
        print "frame is not ready"
        # It is better to wait for a while for the next frame to be ready
        cv2.waitKey(1000)

    if cv2.waitKey(10) == 27:
        break
    if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        # If the number of captured frames is equal to the total number of frames,
        # we stop
        break

这篇关于如何使用 OpenCV 和 Python 在视频流中逐帧处理视频图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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