(websocket: WebSocket, client_id: int)
| 69 | |
| 70 | @app.websocket("/ocr_ws/{client_id}") |
| 71 | async def process_ocr_ws(websocket: WebSocket, client_id: int): |
| 72 | await conn_mgr.connect(websocket) |
| 73 | try: |
| 74 | while True: |
| 75 | received = await websocket.receive_text() |
| 76 | parsed_data = json.loads(received) |
| 77 | data = parsed_data["data"] |
| 78 | |
| 79 | # Convert to PIL image |
| 80 | image = data[data.find(",") + 1 :] |
| 81 | dec = base64.b64decode(image + "===") |
| 82 | image = Image.open(BytesIO(dec)) |
| 83 | # image.save("/data/capture.png") |
| 84 | |
| 85 | # Process the image |
| 86 | result, processed_img = recognizer.recognize(image) |
| 87 | logging.info(f"{result =}") |
| 88 | processed_img = Image.fromarray(np.uint8(processed_img)).convert("RGB") |
| 89 | encoded_img = base64_encode_img(processed_img) |
| 90 | result = { |
| 91 | "extracted": json.dumps(str(result)), |
| 92 | "processed_img": encoded_img, |
| 93 | } |
| 94 | |
| 95 | # Send back the result |
| 96 | await conn_mgr.send_message(json.dumps(result), websocket) |
| 97 | |
| 98 | except WebSocketDisconnect: |
| 99 | conn_mgr.disconnect(websocket) |
| 100 | await conn_mgr.broadcast(f"Client #{client_id} left the chat") |
| 101 | |
| 102 | |
| 103 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected