[英]Raspberry Pi sending frame from piCam via websocket
I would like to send frames from piCam via websocket in base64 format. 我想通过websocket以pi64格式从piCam发送帧。
I have the following simple code: 我有以下简单的代码:
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()
After starting it sends one Image in ~1-2 sec. 启动后,它会在约1-2秒内发送一张图片。
When I try to place into class var base64 image string and send it, this sends every ~0.2 sec. 当我尝试将var base64图像字符串放入类中并将其发送时,这每隔0.2秒发送一次。
When I try to just capture the image from piCam without send websocket, it is ok to, captures every ~0.2 sec. 当我尝试仅从piCam捕获图像而不发送websocket时,可以每隔0.2秒捕获一次。
I do not understand why the combination works so slowly? 我不明白为什么组合会这么慢?
尝试使用捕获方法的use_video_port
选项:
self.picam.capture(output, format="jpeg", use_video_port=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.