Skip to content

Instantly share code, notes, and snippets.

@cyrusbehr
Created November 6, 2020 00:11
Show Gist options
  • Save cyrusbehr/a598f71e5c2074571a8b111f6f97dd36 to your computer and use it in GitHub Desktop.
Save cyrusbehr/a598f71e5c2074571a8b111f6f97dd36 to your computer and use it in GitHub Desktop.
view the snapshot
import websocket
import json
import cv2
import numpy as np
import base64
try:
import thread
except ImportError:
import _thread as thread
import time
def on_message(ws, message):
data = json.loads(message)
# print(data)
encoded_data = data['snapshot']
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow('image',img)
cv2.waitKey(1)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
def run(*args):
while True:
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://192.168.0.11:8091",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment