Last active
November 11, 2023 14:56
-
-
Save lwzm/490a03954c2ec0444ae1b4774c873de1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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