python - 使用 Python 在两台计算机之间流式传输实时视频

标签 python sockets opencv flask video-streaming

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

我能够使用 https://github.com/atuldo/videoStream 成功地做到这一点 它使用套接字库以字符串格式发送视频帧。但是,此方法的结果是帧速率非常低(请参见下面的代码片段)。

尝试 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它使用 flask 将视频流式传输到网页。查看 chrome 中的网页,它的帧速率比我的第一个解决方案好得多。但是,此解决方案不接收 python 中的视频,需要我从网页上读取视频,我不确定如何去做。

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

### 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()

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

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

如何使用这两种方法中的任何一种获得高帧率,或者是否有更好的方法来解决这个问题?

最佳答案

原因可能是您的VideoCapture设备没有配置更高的帧率或者它根本不支持更高的帧率。

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

关于python - 使用 Python 在两台计算机之间流式传输实时视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44611874/

相关文章:

python - 有没有一种方法可以在不使用 Python API 的情况下执行 PyObject 的深层复制(例如通过 C、Rust 等)?

java - 文件传输后保留文件名

c++ - 在 opencv 中使用 Mat::at(i,j) 获取二维 Mat 对象

dll - OpenCV版本的有意混合

python - 使用 Google map 进行地理编码时出现 403 错误

python - 如何在 SQLAlchemy (ORM) 中为 3 个表建立多对多关系模型?

python - Pyspark groupby 并返回整行

c++ - 通过用户定义的套接字向另一个终端发送 NS-3 数据包

linux 不检测死的 tcp 连接

python - 在 Python 中将 OpenCV 框架流式传输到 HTML