(websocket: WebSocket, client_id: int)
| 81 | |
| 82 | @app.websocket("/yolo_ws/{client_id}") |
| 83 | async 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 | |
| 114 | ws_client_html = """ |
nothing calls this directly
no test coverage detected