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

Function process_ocr_ws

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

connectMethod · 0.45
infoMethod · 0.45
send_messageMethod · 0.45
disconnectMethod · 0.45
broadcastMethod · 0.45

Tested by

no test coverage detected