Skip to content

Instantly share code, notes, and snippets.

@lwzm
Last active November 11, 2023 14:56
Show Gist options
  • Save lwzm/490a03954c2ec0444ae1b4774c873de1 to your computer and use it in GitHub Desktop.
Save lwzm/490a03954c2ec0444ae1b4774c873de1 to your computer and use it in GitHub Desktop.
from datetime import datetime
# pip install fastapi uvicorn python-multipart
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import PlainTextResponse
# pip install paddlepaddle paddleocr
# pip install opencv-python-headless
from paddleocr import PaddleOCR, draw_ocr
app = FastAPI()
# https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/doc/doc_ch/quickstart.md
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
@app.post("/")
async def create_upload_file(file: UploadFile = File(...)):
x = await file.read()
print(len(x))
fn = datetime.now().strftime('%y%m%d_%H%M%S.%f')
fn = 'tmp.jpg'
with open(fn, "wb") as f:
f.write(x)
result = ocr.ocr(fn)
l = []
for i in result:
for x in i:
l.append(x[-1][0])
txt = "\n".join(l)
return PlainTextResponse(txt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment