Skip to content

Commit 0cfc8d1

Browse files
committed
整体项目重构
1 parent de0afe7 commit 0cfc8d1

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

README.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,39 @@
3232
cd ddddocr-api
3333
```
3434

35-
2. **构建 Docker 镜像 [一键docker环境服务器购买,可一元试用](https://app.rainyun.com/apps/rcs/buy) **
36-
```bash
37-
docker build -t ddddocr-api .
38-
```
35+
2. **启动服务**
36+
37+
有三种方式可以启动应用:
38+
39+
a. 使用 docker启动:
40+
1. 构建 Docker 镜像 [一键docker环境服务器购买,可一元试用](https://app.rainyun.com/apps/rcs/buy)
41+
2. 打包镜像
42+
```bash
43+
docker build -t ddddocr-api .
44+
```
45+
3. 启动镜像
46+
```bash
47+
docker run -d -p 8000:8000 --name ddddocr-api-container ddddocr-api
48+
```
49+
50+
b. 使用 python 命令直接运行:
51+
```bash
52+
python app/main.py
53+
```
54+
55+
b. 使用 uvicorn(支持热重载,适合开发):
56+
```bash
57+
uvicorn app.main:app --reload
58+
```
3959

40-
3. **启动服务**
41-
```bash
42-
docker run -d -p 8000:8000 --name ddddocr-api-container ddddocr-api
43-
```
4460

45-
4. **验证服务**
61+
3. **验证服务**
4662
```bash
4763
curl http://localhost:8000/docs
4864
```
4965
> 如果成功,您将看到 Swagger UI 文档页面。
5066

51-
5. **停止服务**
67+
4. **停止服务**
5268

5369
- 如果使用 Docker:
5470
```bash
@@ -60,7 +76,7 @@
6076
docker-compose down
6177
```
6278

63-
6. **查看日志**
79+
5. **查看日志**
6480

6581
- 如果使用 Docker:
6682
```bash

app/main.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1+
import uvicorn
12
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
2-
from fastapi.responses import JSONResponse
33
from typing import Optional, Union
44
import base64
5-
from .models import OCRRequest, SlideMatchRequest, DetectionRequest, APIResponse
6-
from .services import ocr_service
5+
from app.models import OCRRequest, SlideMatchRequest, DetectionRequest, APIResponse
6+
from app.services import ocr_service
77

88
app = FastAPI()
99

10+
from starlette.datastructures import UploadFile as StarletteUploadFile
1011

11-
def decode_image(image: Union[UploadFile, str, None]) -> bytes:
12-
if isinstance(image, UploadFile):
13-
return image.file.read()
12+
13+
async def decode_image(image: Union[UploadFile, StarletteUploadFile, str, None]) -> bytes:
14+
if image is None:
15+
raise HTTPException(status_code=400, detail="No image provided")
16+
17+
if isinstance(image, (UploadFile, StarletteUploadFile)):
18+
return await image.read()
1419
elif isinstance(image, str):
1520
try:
21+
# 检查是否是 base64 编码的图片
22+
if image.startswith(('data:image/', 'data:application/')):
23+
# 移除 MIME 类型前缀
24+
image = image.split(',')[1]
1625
return base64.b64decode(image)
1726
except:
1827
raise HTTPException(status_code=400, detail="Invalid base64 string")
19-
elif image is None:
20-
raise HTTPException(status_code=400, detail="No image provided")
2128
else:
2229
raise HTTPException(status_code=400, detail="Invalid image input")
2330

@@ -34,7 +41,7 @@ async def ocr_endpoint(
3441
if file is None and image is None:
3542
return APIResponse(code=400, message="Either file or image must be provided")
3643

37-
image_bytes = decode_image(file or image)
44+
image_bytes = await decode_image(file or image)
3845
result = ocr_service.ocr_classification(image_bytes, probability, charsets, png_fix)
3946
return APIResponse(code=200, message="Success", data=result)
4047
except Exception as e:
@@ -53,8 +60,8 @@ async def slide_match_endpoint(
5360
if (target_file is None and target is None) or (background_file is None and background is None):
5461
return APIResponse(code=400, message="Both target and background must be provided")
5562

56-
target_bytes = decode_image(target_file or target)
57-
background_bytes = decode_image(background_file or background)
63+
target_bytes = await decode_image(target_file or target)
64+
background_bytes = await decode_image(background_file or background)
5865
result = ocr_service.slide_match(target_bytes, background_bytes, simple_target)
5966
return APIResponse(code=200, message="Success", data=result)
6067
except Exception as e:
@@ -70,8 +77,12 @@ async def detection_endpoint(
7077
if file is None and image is None:
7178
return APIResponse(code=400, message="Either file or image must be provided")
7279

73-
image_bytes = decode_image(file or image)
80+
image_bytes = await decode_image(file or image)
7481
bboxes = ocr_service.detection(image_bytes)
7582
return APIResponse(code=200, message="Success", data=bboxes)
7683
except Exception as e:
7784
return APIResponse(code=500, message=str(e))
85+
86+
87+
if __name__ == "__main__":
88+
uvicorn.run(app, host="0.0.0.0", port=8000)

0 commit comments

Comments
 (0)