使用Python在两台计算机之间流式传输实时视频 [英] Stream realtime video between 2 computers using Python

查看:1230
本文介绍了使用Python在两台计算机之间流式传输实时视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实时发送我的Macbook Air网络摄像头视频,并在另一台计算机上使用python接收该视频.这样做的动机是能够将实时图像识别任务卸载到服务器.服务器需要访问python中的实时视频帧,以便随后将这些帧传递给我的图像识别算法(一个深度神经网络).

I am trying to send my macbook air webcam video in realtime and receive it using python on another computer. The motivation for this is to be able to offload realtime image recognition tasks to a server. The server requires having access to the realtime video frames in python so that I can then pass the frames to my image recognition algorithm (a deep neural net).

我能够使用 https://github.com/atuldo/videoStream 成功完成此操作 它使用套接字库以字符串格式发送视频帧.但是,这种方法的帧率非常低(请参见下面的代码段).

I was able to do this successfully using https://github.com/atuldo/videoStream which uses the socket library to send the video frames in string format. However, this method results is a very low frame rate (see below for code snippet).

尝试1:

from flask import Flask, render_template, Response
from camera import VideoCamera

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

我还使用了 https://github.com/log0/video_streaming_with_flask_example 视频到网页.以chrome浏览网页时,它的帧频比我的第一个解决方案好得多.但是,此解决方案无法接收python中的视频,因此需要我从网页中读取视频,但我不确定该如何处理.

I also used this https://github.com/log0/video_streaming_with_flask_example which uses flask to stream the video to a web page. Looking at the webpage in chrome it has a much better frame rate than my first solution. However, this solution does not receive the video in python and would require me to read the video from the webpage, which I am not exactly sure how to go about.

尝试2(我将原始github代码中的pygame替换为cv2,以便能够访问我的Mac网络摄像头):

Attempt 2 (I replaced pygame from original github code with cv2 to be able to access my mac webcam) :

### Server
import socket
from threading import *
import socket
import pygame
import pygame.camera
from pygame.locals import *

import cv2

def server():
    #host_name = socket.gethostname()
    host_name = 'localhost'
    print "\nServer started at " + str(socket.gethostbyname(host_name)) + " at port " + str(90) 
    #port = 90
    port = 1024
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.bind(("",port))
    serversocket.listen(10)
    #pygame.camera.init()
    #cam = pygame.camera.Camera(0,(640,480),"RGB")
    #cam.start()
    #img = pygame.Surface((640,480))
    video = cv2.VideoCapture(0)

    while True:
        connection, address = serversocket.accept()
        print "GOT_CONNECTION"
        #cam.get_image(img)
        #data = pygame.image.tostring(img,"RGB")
        success, image = video.read()
        data = cv2.imencode('.jpg', image)[1].tostring()
        connection.sendall(data)
        connection.close()

server()

###Reciever (snippet, for full code please refer to the github link above)

        clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        clientsocket.connect((self.ipAddress, self.port))
        received = []
        while True:
            recvd_data = clientsocket.recv(230400)
            if not recvd_data:
                break
            else:
                received.append(recvd_data)
        dataset = ''.join(received)
        #image = pygame.image.fromstring(dataset,(640, 480),"RGB") # convert received image from string
        nparr = np.fromstring(dataset, np.uint8)
        image = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
        cv2.imwrite('foo.jpg', image)
        #pygame.image.save(image, "foo.jpg")
        self.ids.image_source.reload()

视频流在网页上的显示方式如下:

The video stream is displayed on the webpage like so:

<html>
  <head>
    <title>Video Streaming Demonstration</title>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
    <img id="bg" src="{{ url_for('video_feed') }}">
  </body>
</html>

如何通过两种方法之一获得较高的帧速率,或者有更好的方法来解决此问题?

How can I get a high frame rate with either one of the two approaches or is there a better way to go about this?

推荐答案

原因可能是您VideoCapture设备未配置更高的帧速率,或者根本不支持更高的帧速率.

The reason may be that you VideoCapture device is not configured for higher framerate or it does not support higher framerate at all.

您指定了非常低的帧频,我不知道这对您意味着什么,但是我用2台相机对其进行了测试.通过稍微修改代码,我可以将第一台摄像机的最大速度提高到15fps,但是第二台摄像机以25fps的速度运行而没有问题.这些是我的预期数字.

You specified very low frame rate, I don't know what this means to you, but I tested it with my 2 cameras. With slightly modified code, I can max out the first camera to 15fps, but the second one is running at 25fps without problems. These are expected numbers for me.

这篇关于使用Python在两台计算机之间流式传输实时视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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