如何使用Opencv python在视频流中逐帧处理视频图像

Meh*_*ran 41 python video opencv video-streaming

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

你能告诉我怎样才能用OpenCV(Python)做到这一点?

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

这是我的代码:

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
Run Code Online (Sandbox Code Playgroud)

所以问题就是cap.grab()电话!当没有框架时它会返回False!即使我等了很长时间,它也不再捕捉帧了.

Meh*_*ran 47

看完文档后VideoCapture.我想你可以告诉VideoCapture我们下次调用VideoCapture.read()(或VideoCapture.grab())时要处理的帧.

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

这是代码

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
Run Code Online (Sandbox Code Playgroud)

  • 从OpenCV 3.0开始,将cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)更改为cap.get(cv2.CAP_PROP_POS_FRAMES)。 (2认同)

prt*_*tkp 12

用这个 -

import cv2
cap = cv2.VideoCapture('path of video file')
count = 0
while cap.isOpened():
    ret,frame = cap.read()
    cv2.imshow('window-name',frame)
    cv2.imwrite("frame%d.jpg" % count, frame)
    count = count + 1
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()  # destroy all the opened windows
Run Code Online (Sandbox Code Playgroud)


sri*_*gde 8

根据OpenCV 3.0及更高版本的最新更新,您需要在Mehran的代码中更改属性标识符:

cv2.cv.CV_CAP_PROP_POS_FRAMES
Run Code Online (Sandbox Code Playgroud)

cv2.CAP_PROP_POS_FRAMES
Run Code Online (Sandbox Code Playgroud)

同样适用于cv2.CAP_PROP_POS_FRAME_COUNT.

希望能帮助到你.


zen*_*poy 7

在openCV的文档中,有一个逐帧获取视频的示例.它是用c ++编写的,但将示例移植到python非常容易 - 您可以搜索每个fumction文档以了解如何在python中调用它们.

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • @Mehran你会如此温柔地分享python版本吗? (4认同)