Skip to content

Commit

Permalink
整体项目重构
Browse files Browse the repository at this point in the history
  • Loading branch information
sml2h3 committed Jul 25, 2024
1 parent de0afe7 commit 0cfc8d1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,39 @@
cd ddddocr-api
```

2. **构建 Docker 镜像 [一键docker环境服务器购买,可一元试用](https://app.rainyun.com/apps/rcs/buy) **
```bash
docker build -t ddddocr-api .
```
2. **启动服务**

有三种方式可以启动应用:

a. 使用 docker启动:
1. 构建 Docker 镜像 [一键docker环境服务器购买,可一元试用](https://app.rainyun.com/apps/rcs/buy)
2. 打包镜像
```bash
docker build -t ddddocr-api .
```
3. 启动镜像
```bash
docker run -d -p 8000:8000 --name ddddocr-api-container ddddocr-api
```

b. 使用 python 命令直接运行:
```bash
python app/main.py
```

b. 使用 uvicorn(支持热重载,适合开发):
```bash
uvicorn app.main:app --reload
```

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

4. **验证服务**
3. **验证服务**
```bash
curl http://localhost:8000/docs
```
> 如果成功,您将看到 Swagger UI 文档页面。

5. **停止服务**
4. **停止服务**

- 如果使用 Docker:
```bash
Expand All @@ -60,7 +76,7 @@
docker-compose down
```

6. **查看日志**
5. **查看日志**

- 如果使用 Docker:
```bash
Expand Down
35 changes: 23 additions & 12 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import uvicorn
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
from fastapi.responses import JSONResponse
from typing import Optional, Union
import base64
from .models import OCRRequest, SlideMatchRequest, DetectionRequest, APIResponse
from .services import ocr_service
from app.models import OCRRequest, SlideMatchRequest, DetectionRequest, APIResponse
from app.services import ocr_service

app = FastAPI()

from starlette.datastructures import UploadFile as StarletteUploadFile

def decode_image(image: Union[UploadFile, str, None]) -> bytes:
if isinstance(image, UploadFile):
return image.file.read()

async def decode_image(image: Union[UploadFile, StarletteUploadFile, str, None]) -> bytes:
if image is None:
raise HTTPException(status_code=400, detail="No image provided")

if isinstance(image, (UploadFile, StarletteUploadFile)):
return await image.read()
elif isinstance(image, str):
try:
# 检查是否是 base64 编码的图片
if image.startswith(('data:image/', 'data:application/')):
# 移除 MIME 类型前缀
image = image.split(',')[1]
return base64.b64decode(image)
except:
raise HTTPException(status_code=400, detail="Invalid base64 string")
elif image is None:
raise HTTPException(status_code=400, detail="No image provided")
else:
raise HTTPException(status_code=400, detail="Invalid image input")

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

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

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

image_bytes = decode_image(file or image)
image_bytes = await decode_image(file or image)
bboxes = ocr_service.detection(image_bytes)
return APIResponse(code=200, message="Success", data=bboxes)
except Exception as e:
return APIResponse(code=500, message=str(e))


if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

0 comments on commit 0cfc8d1

Please sign in to comment.