本项目实现一个简单的服务器和客户端接口,用来调用MacOS上的Vision框架的OCR功能。Vision框架的OCR在中文生僻字识别方面准确率比较高。
This project implements a simple server and client interface to call the OCR function of the Vision framework on MacOS. This OCR has a relatively high accuracy in recognizing rare Chinese characters.
git clone https://github.com/hinson/VisionOCR.git
cd VisionOCRpip install .pip install ".[server]"python -m uvicorn vision_ocr.server:app --host 0.0.0.0 --port 9394import requests
url = "http://localhost:9394/ocr?lang=zh-cn"
with open("test.png", "rb") as f:
files = {"file": f}
response = requests.post(url, files=files)
print(response.json())from vision_ocr import OCRlient, OCRResult
image_paths = ["example1.png", "example2.jpg"]with OCRClient(base_url="http://localhost:9394", lang="zh-cn") as client:
# Single file
result: OCRResult = client.recognize(image_paths[0])
print(f"Sync result: {result.text}")
# Batch processing
results: list[OCRResult] = client.recognize_batch(image_paths)
for i, result in enumerate(results):
print(f"Result {i+1} ({result.file_name}): {result.success}")
if result.success:
print(f"Text: {result.text}")
else:
print(f"Error: {result.error}")import asyncio
async def example_async_usage():
async with OCRClient(base_url="http://localhost:9394", lang="zh-cn") as client:
# Single image from bytes
with open(image_paths[0], "rb") as f:
result: OCRResult = await client.recognize_async(f.read())
print(f"Async result: {result.text}")
# Batch processing
results: list[OCRResult] = await client.recognize_batch_async(image_paths)
for i, result in enumerate(results):
print(f"Result {i+1} ({result.file_name}): {result.success}")
if result.success:
print(f"Text: {result.text}")
else:
print(f"Error: {result.error}")
asyncio.run(example_async_usage())