MCPcopy Create free account
hub / github.com/alpha2phi/python-apps / process_yolov5_ws

Function process_yolov5_ws

ml-yolo/backend/main.py:83–111  ·  view source on GitHub ↗
(websocket: WebSocket, client_id: int)

Source from the content-addressed store, hash-verified

81
82@app.websocket("/yolo_ws/{client_id}")
83async def process_yolov5_ws(websocket: WebSocket, client_id: int):
84 await conn_mgr.connect(websocket)
85 try:
86 while True:
87 data = await websocket.receive_text()
88
89 # Convert to PIL image
90 image = data[data.find(",") + 1:]
91 dec = base64.b64decode(image + "===")
92 image = Image.open(BytesIO(dec)).convert("RGB")
93
94 # Process the image
95 name = f"/data/{str(uuid.uuid4())}.png"
96 image.filename = name
97 classes, converted_img = yolov5(image)
98
99 result = {
100 "prediction": json.dumps(classes),
101 "output": base64_encode_img(converted_img),
102 }
103 # logging.info("-----", json.dumps(result))
104
105 # Send back the result
106 await conn_mgr.send_message(json.dumps(result), websocket)
107
108 # await conn_mgr.broadcast(f"Client #{client_id} says: {data}")
109 except WebSocketDisconnect:
110 conn_mgr.disconnect(websocket)
111 await conn_mgr.broadcast(f"Client #{client_id} left the chat")
112
113
114ws_client_html = """

Callers

nothing calls this directly

Calls 6

yolov5Function · 0.90
base64_encode_imgFunction · 0.70
connectMethod · 0.45
send_messageMethod · 0.45
disconnectMethod · 0.45
broadcastMethod · 0.45

Tested by

no test coverage detected