我无法将一台机器上的流插入套接字,因此可以使用flask在网页上显示该流

时间:2019-04-18 19:24:33

标签: python-3.x flask raspberry-pi cv2

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

客户端代码:(已经过测试,我可以使用它在服务器桌面上与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)

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

感谢您的帮助,我意识到我在大海捞针的情况下会掉针。

0 个答案:

没有答案