Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过websocket从piCam发送Raspberry Pi帧_Python_Websocket_Raspberry Pi - Fatal编程技术网

Python 通过websocket从piCam发送Raspberry Pi帧

Python 通过websocket从piCam发送Raspberry Pi帧,python,websocket,raspberry-pi,Python,Websocket,Raspberry Pi,我想通过websocket以base64格式从piCam发送帧 我有以下简单的代码: import websocket import time import picamera import io import base64 import StringIO class MyClass: ws = '' picam = '' stream = '' def __init__(self): self.init() def on_messag

我想通过websocket以base64格式从piCam发送帧

我有以下简单的代码:

import websocket
import time
import picamera
import io
import base64
import StringIO

class MyClass:
    ws = ''
    picam = ''
    stream = ''

    def __init__(self):
        self.init()

    def on_message(self,ws , message):
        print ws + "ok"
        print message

    def on_error(self, ws, error):
        print error

    def on_close(self, ws):
        print "down"
        exit()

    def on_open(self, ws):
        print "opening connection"
        ws.send("Hello.")
        self.main()

    def main(self):
        print "main"
        output = StringIO.StringIO()
        while True:
            output.seek(0)
            self.picam.capture(output, format="jpeg")
            encoded_string = base64.b64encode(output.getvalue())
            self.ws.send("{\"Image\":\""+encoded_string+"\"}")
            time.sleep(0.2)
            output.flush()

    def init(self):
        print "init"
        websocket.enableTrace(True)
        self.picam = picamera.PiCamera()
        self.picam.resolution = (640, 480)
        self.stream = io.BytesIO()
        self.picam.start_preview()
        self.ws = websocket.WebSocketApp("ws://xxxxx.",
                              on_message = self.on_message,
                              on_error = self.on_error,
                              on_close = self.on_close,
                              on_open= self.on_open)
        self.ws.run_forever()
启动后,它会在约1-2秒内发送一个图像

  • 当我尝试将var base64图像字符串放入类中并发送它时,它每~0.2秒发送一次

  • 当我尝试只从piCam捕获图像而不发送websocket时,可以每~0.2秒捕获一次


  • 我不明白为什么组合工作得这么慢?

    尝试使用捕获方法的
    使用\u video\u port
    选项:

    self.picam.capture(output, format="jpeg", use_video_port=True)