Spaces:
Sleeping
Sleeping
add file
Browse files
main.py
CHANGED
|
@@ -5,27 +5,23 @@ 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 |
|
|
|
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
# 2、声明一个 源 列表;重点:要包含跨域的客户端 源
|
| 14 |
origins = [
|
| 15 |
"https://hycjack-fastapi-rapidocr.hf.space/",
|
| 16 |
"http://localhost",
|
| 17 |
"http://localhost:7860",
|
| 18 |
-
# 客户端的源
|
| 19 |
"http://127.0.0.1:7860"
|
| 20 |
]
|
| 21 |
|
| 22 |
-
# 3、配置 CORSMiddleware
|
| 23 |
app.add_middleware(
|
| 24 |
CORSMiddleware,
|
| 25 |
-
allow_origins=origins,
|
| 26 |
-
allow_credentials=True,
|
| 27 |
-
allow_methods=["*"],
|
| 28 |
-
allow_headers=["*"]
|
| 29 |
)
|
| 30 |
|
| 31 |
@app.get("/")
|
|
@@ -34,11 +30,8 @@ def read_root():
|
|
| 34 |
|
| 35 |
@app.post("/ocr")
|
| 36 |
async def ocr(file: UploadFile = File(...)):
|
| 37 |
-
# 读取上传的文件内容
|
| 38 |
contents = await file.read()
|
| 39 |
-
# 使用Pillow打开图像
|
| 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))
|
|
|
|
| 5 |
import io
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
model = RapidOCR()
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 12 |
origins = [
|
| 13 |
"https://hycjack-fastapi-rapidocr.hf.space/",
|
| 14 |
"http://localhost",
|
| 15 |
"http://localhost:7860",
|
|
|
|
| 16 |
"http://127.0.0.1:7860"
|
| 17 |
]
|
| 18 |
|
|
|
|
| 19 |
app.add_middleware(
|
| 20 |
CORSMiddleware,
|
| 21 |
+
allow_origins=origins,
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"],
|
| 24 |
+
allow_headers=["*"]
|
| 25 |
)
|
| 26 |
|
| 27 |
@app.get("/")
|
|
|
|
| 30 |
|
| 31 |
@app.post("/ocr")
|
| 32 |
async def ocr(file: UploadFile = File(...)):
|
|
|
|
| 33 |
contents = await file.read()
|
|
|
|
| 34 |
image = Image.open(io.BytesIO(contents))
|
|
|
|
| 35 |
np_array = np.array(image)
|
| 36 |
ocr_result, elapse = model(np_array)
|
| 37 |
dt_boxes, rec_res, scores = list(zip(*ocr_result))
|