Python 如何实时处理图像并输出结果的实时视频?

Python 如何实时处理图像并输出结果的实时视频?,python,matplotlib,raspberry-pi,computer-vision,Python,Matplotlib,Raspberry Pi,Computer Vision,我有一个带摄像头的Rasberry Pi,正在使用RPi Cam网络界面将视频流传送到我的浏览器。我运行一个脚本来读取图像,并像下面那样处理它们。运行代码将打开一个窗口,其中显示当前处理的图像。当我关闭窗口时,我会得到一张经过更新处理的图像 不过,我想做的是输出处理后图像的连续视频。我应该采取什么方法来做到这一点? while True: image = io.imread('http://[ip-address]/cam_pic.php') image_gray = cv2.

我有一个带摄像头的Rasberry Pi,正在使用RPi Cam网络界面将视频流传送到我的浏览器。我运行一个脚本来读取图像,并像下面那样处理它们。运行代码将打开一个窗口,其中显示当前处理的图像。当我关闭窗口时,我会得到一张经过更新处理的图像

不过,我想做的是输出处理后图像的连续视频。我应该采取什么方法来做到这一点?

while True: 
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    image_with_detected_faces = faces_draw(image, faces)
    plt.imshow(image_with_detected_faces)
    plt.show()
import cv2
import matplotlib.pyplot as plt

def grab_frame():
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    return faces_draw(image, faces)

#create axes
ax1 = plt.subplot(111)

#create image plot
im1 = ax1.imshow(grab_frame())

plt.ion()

while True:
    im1.set_data(grab_frame())
    plt.pause(0.2)

plt.ioff() # due to infinite loop, this gets never called.
plt.show()

你可能想看看这个问题:它直接使用视频捕获。如果您想从http读取图像,可以将其更改为以下选项之一

交互模式

while True: 
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    image_with_detected_faces = faces_draw(image, faces)
    plt.imshow(image_with_detected_faces)
    plt.show()
import cv2
import matplotlib.pyplot as plt

def grab_frame():
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    return faces_draw(image, faces)

#create axes
ax1 = plt.subplot(111)

#create image plot
im1 = ax1.imshow(grab_frame())

plt.ion()

while True:
    im1.set_data(grab_frame())
    plt.pause(0.2)

plt.ioff() # due to infinite loop, this gets never called.
plt.show()
FuncAnimation

import cv2
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

def grab_frame():
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    return faces_draw(image, faces)

#create axes
ax1 = plt.subplot(111)

#create axes
im1 = ax1.imshow(grab_frame())

def update(i):
    im1.set_data(grab_frame())

ani = FuncAnimation(plt.gcf(), update, interval=200)
plt.show()

这个github解释了如何使用tensorflow对连续视频进行分类,可能会有所帮助。这里有很多缺少的信息,这使得这个问题很难回答。您可以使用
plt.savefig()
而不是
plt.show()
保存图像,然后我建议您使用
ffmpeg
(或类似)将图像转换为视频。为了得到一个合理的结果,您的处理时间必须相当低,以获得一个合适的帧速率。此外,您可能希望在设定的时间捕获图像,而不是尽快捕获,以获得更好的“流”。最后,这当然不会产生实时视频。@JohanL这很有帮助,谢谢。我应该向我的问题中添加哪些信息?例如,如果我建议的方法,使用
ffmpeg
而不获取实时流是可以的。另外,您期望的帧速率是多少?序列要存储多长时间?您应该在RPi上完成所有处理,还是可以将其卸载到功能更强大的服务器上?另外,是否要对视频进行编码以减小其大小,等等。?