Now I try to make server using Raspberry Pi which send live stream image data to browser. The server side was written in Python & Tornado, while client side was written in HTML and javascript. Both use WebSocket. (I am a beginner of javascript.)
These are the codes
Server side:
class WSHandler(WebSocketHandler):
def initialize(self, camera):
self.camera = camera
cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_WIDTH, 480)
cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 360)
def open(self):
print("connection opened")
while True:
self.loop()
def loop(self):
img = self.camera.takeImage()
self.write_message(img, binary=True)
class Camera():
def __init__(self):
self.capture = cv.CaptureFromCAM(0)
def takeImage(self):
img = cv.QueryFrame(self.capture)
img = cv.EncodeImage(".jpg", img).tostring()
return img
def main():
camera = Camera()
app = tornado.web.Application([
(r"/camera", WSHandler, dict(camera=camera)),
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8080)
IOLoop.instance().start()
if __name__ == "__main__":
main()
Client side:
javascript(client.js)
var canvas = document.getElementById("liveCanvas");;
var context = canvas.getContext("2d");
var ws = new WebSocket("ws://localhost:8080/camera");
ws.onopen = function(){
console.log("connection was established");
};
ws.onmessage = function(evt){
context.drawImage(evt.data,0,0);
};
html(index.html)
<html>
<head>
<title>livecamera</title>
<canvas id="liveCanvas" width="480" height="360"></canvas>
<script type="text/javascript" src="./client.js"></script>
</head>
</html>
When I access this 'index.html' while the server is running, next error appeared.
Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature provided.
I guess, this is caused by mistaking of data format sent from server.
My questions are, What data format should be used? How should the server send data? How should the client receive data?