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

查看:1175
本文介绍了如何使用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()调用!如果没有框架,它将返回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天全站免登陆