首页 > 解决方案 > 我无法将流从一台机器连接到另一台机器,因此我可以使用烧瓶在网页上显示流

问题描述

我想在一个 raspPi 中捕获视频,使用套接字将其流式传输到另一台树莓(或另一台带有 python 的机器),以便它可以使用烧瓶将其显示在网页上

客户端代码:(已经测试可以用vlc在服务器桌面显示)

import io
import socket
import struct
import time
import picamera


# create socket and bind host
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.1.205', 8000))
connection = client_socket.makefile('wb')

try:
    with picamera.PiCamera() as camera:
        camera.resolution = (320, 240)      # pi camera resolution
        camera.framerate = 15               # 15 frames/sec
        time.sleep(2)                       # give 2 secs for camera to initilize
        start = time.time()
        stream = io.BytesIO()

        # send jpeg format video stream
        for foo in camera.capture_continuous(stream, 'jpeg', use_video_port = True):
            connection.write(struct.pack('<L', stream.tell()))
            connection.flush()
            stream.seek(0)
            connection.write(stream.read())
            if time.time() - start > 600:
                break
            stream.seek(0)
            stream.truncate()
    connection.write(struct.pack('<L', 0))
finally:
    connection.close()
    client_socket.close()

服务器代码:(camerarece.py)

import time
import io
import threading
import picamera
import numpy as np

import socket
import datetime

class Camera2(object):
    try:
        thread = None  # background thread that reads frames from camera
        frame = None  # current frame is stored here by background thread
        last_access = 0  # time of last client access to the camera
        host = "192.168.1.205"
        port = 8000
        server_socket = socket.socket()
        server_socket.bind((host, port))
        server_socket.listen(0)
        connection, client_address = server_socket.accept()
        connection = connection.makefile('rb')
        host_name = socket.gethostname()
        host_ip = socket.gethostbyname(self.host_name)
    except Exception as e:
        print(e)
    def initialize(self):
        if Camera.thread is None:
            # start background frame thread
            Camera.thread = threading.Thread(target=self._thread)
            Camera.thread.start()

            # wait until frames start to be available
            while self.frame is None:
                time.sleep(0)

    def get_frame(self):
        Camera.last_access = time.time()
        self.initialize()
        return self.frame

    @classmethod
    def _thread(cls):



            #stream = io.BytesIO()
            try:

                #self.streaming()
                stream_bytes = b' '
                cls.frame = connection.read(1024)

                cls.thread = None
            except Exception as e:
                print(e)

上一个文件由以下主文件调用,以将接收到的提要流式传输到烧瓶网页:

from flask import Flask, render_template, Response
from camerarece import Camera2 (the previous block of code I posted)

app = Flask(__name__)

@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


def gen2():
    """Video streaming generator function."""
    while True:
        frame = Camera2.get_frame()
        #frame = cls.frame
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')        



@app.route('/input_feed')
def input_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen2(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='192.168.1.205', port =5000, debug=True, threaded=True)

我个人的猜测是我完全没有正确地将套接字流转换为烧瓶可用的东西。

感谢您的帮助,我意识到我在干草堆中丢了一根针。

标签: python-3.xflaskraspberry-picv2

解决方案


推荐阅读