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

Function process_ocr_ws

ocr-ml-viewer/backend/main.py:71–100  ·  view source on GitHub ↗
(websocket: WebSocket, client_id: int)

Source from the content-addressed store, hash-verified

69
70@app.websocket("/ocr_ws/{client_id}")
71async 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
103if __name__ == "__main__":

Callers

nothing calls this directly

Calls 6

base64_encode_imgFunction · 0.70
connectMethod · 0.45
infoMethod · 0.45
send_messageMethod · 0.45
disconnectMethod · 0.45
broadcastMethod · 0.45

Tested by

no test coverage detected