Spaces:
Paused
Paused
add file
Browse files
main.py
CHANGED
|
@@ -4,6 +4,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 4 |
from rapidocr_onnxruntime import RapidOCR
|
| 5 |
import io
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
# det_model_path同理
|
| 8 |
model = RapidOCR()
|
| 9 |
|
|
@@ -30,8 +31,8 @@ app.add_middleware(
|
|
| 30 |
@app.get("/")
|
| 31 |
def read_root():
|
| 32 |
return {"Hello": "World!"}
|
| 33 |
-
|
| 34 |
-
@app.post("/ocr")
|
| 35 |
async def ocr(file: UploadFile = File(...)):
|
| 36 |
# 读取上传的文件内容
|
| 37 |
contents = await file.read()
|
|
@@ -39,5 +40,10 @@ async def ocr(file: UploadFile = File(...)):
|
|
| 39 |
image = Image.open(io.BytesIO(contents))
|
| 40 |
# 将图像转换为numpy数组
|
| 41 |
np_array = np.array(image)
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from rapidocr_onnxruntime import RapidOCR
|
| 5 |
import io
|
| 6 |
import numpy as np
|
| 7 |
+
import pandas as pd
|
| 8 |
# det_model_path同理
|
| 9 |
model = RapidOCR()
|
| 10 |
|
|
|
|
| 31 |
@app.get("/")
|
| 32 |
def read_root():
|
| 33 |
return {"Hello": "World!"}
|
| 34 |
+
|
| 35 |
+
@app.post("/ocr")
|
| 36 |
async def ocr(file: UploadFile = File(...)):
|
| 37 |
# 读取上传的文件内容
|
| 38 |
contents = await file.read()
|
|
|
|
| 40 |
image = Image.open(io.BytesIO(contents))
|
| 41 |
# 将图像转换为numpy数组
|
| 42 |
np_array = np.array(image)
|
| 43 |
+
ocr_result, elapse = model(np_array)
|
| 44 |
+
dt_boxes, rec_res, scores = list(zip(*ocr_result))
|
| 45 |
+
out_df = pd.DataFrame(
|
| 46 |
+
[[box, rec, score] for box, rec, score in zip(dt_boxes, rec_res, scores)],
|
| 47 |
+
columns=("box", "rec", "score"),
|
| 48 |
+
)
|
| 49 |
+
return out_df.to_dict(orient='records')
|