File size: 8,687 Bytes
59f9119
e0a06d3
59f9119
 
e0a06d3
59f9119
e0a06d3
59f9119
 
 
e0a06d3
59f9119
9236c5b
e0a06d3
 
59f9119
e0a06d3
59f9119
 
 
 
c7fd1ca
e0a06d3
 
 
 
59f9119
e0a06d3
59f9119
 
e0a06d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f9119
e0a06d3
59f9119
e0a06d3
 
 
 
3b01a7f
e0a06d3
 
 
 
59f9119
e0a06d3
59f9119
 
e0a06d3
59f9119
e0a06d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f9119
e0a06d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f9119
e0a06d3
 
59f9119
 
e0a06d3
59f9119
e0a06d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f9119
e0a06d3
 
 
 
 
 
 
 
 
 
59f9119
e0a06d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f9119
e0a06d3
 
 
59f9119
e0a06d3
 
 
 
59f9119
 
e0a06d3
 
59f9119
 
e0a06d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import uvicorn
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.staticfiles import StaticFiles
import hashlib
import os
from enum import Enum
from paddleocr import PaddleOCR
from PIL import Image
import io
import numpy as np
from typing import Optional

app = FastAPI(docs_url='/')

# 确保输出目录存在
output_dir = 'output'
os.makedirs(output_dir, exist_ok=True)

class LangEnum(str, Enum):
    ch = "ch"
    en = "en"
    japan = "japan"
    korean = "korean"
    chinese_cht = "chinese_cht"
    fr = "fr"
    de = "de"

# OCR 实例缓存
ocr_cache = {}

def get_ocr_instance(lang: str = "ch", use_gpu: bool = False):
    """获取OCR实例,使用PP-OCRv5模型"""
    cache_key = f"v5_{lang}_{use_gpu}"
    
    if cache_key not in ocr_cache:
        # 使用PaddleOCR 3.0的新API + PP-OCRv5模型
        ocr_cache[cache_key] = PaddleOCR(
            ocr_version="PP-OCRv5",              # 指定使用PP-OCRv5版本
            lang=lang,
            text_detection_model_name="PP-OCRv5_server_det",      # 使用server版本检测模型
            text_recognition_model_name="PP-OCRv5_server_rec",    # 使用server版本识别模型
            use_doc_orientation_classify=False,   # 关闭文档方向分类
            use_doc_unwarping=False,             # 关闭文档矫正  
            use_textline_orientation=False,      # 关闭文本行方向分类
            device="gpu" if use_gpu else "cpu"
        )
    
    return ocr_cache[cache_key]

def validate_image(file: UploadFile):
    """验证上传的文件"""
    if not file.content_type or not file.content_type.startswith('image/'):
        raise HTTPException(status_code=400, detail="文件必须是图片格式")
    
    # 检查文件大小 (最大10MB)
    if hasattr(file, 'size') and file.size and file.size > 10 * 1024 * 1024:
        raise HTTPException(status_code=400, detail="图片文件大小不能超过10MB")

@app.post("/ocr")
async def ocr_recognition(
    file: UploadFile = File(...),
    lang: LangEnum = LangEnum.ch,
    use_gpu: bool = False
):
    """PP-OCRv5文字识别 - 支持5种文字类型的单模型"""
    try:
        validate_image(file)
        
        contents = await file.read()
        if not contents:
            raise HTTPException(status_code=400, detail="文件内容为空")
            
        # 转换图片格式
        image = Image.open(io.BytesIO(contents))
        if image.mode != 'RGB':
            image = image.convert('RGB')
            
        # 获取OCR实例
        ocr = get_ocr_instance(lang=lang, use_gpu=use_gpu)
        
        # 转换为numpy数组进行识别
        img_array = np.array(image)
        
        # 使用PP-OCRv5进行识别
        results = ocr.predict(img_array)
        
        if not results or len(results) == 0:
            return {
                "success": True,
                "message": "未检测到文字",
                "model_version": "PP-OCRv5",
                "language": lang,
                "count": 0,
                "results": []
            }

        # 处理识别结果
        result = results[0]  # 取第一个结果
        
        # 提取结果信息
        ocr_results = []
        if hasattr(result, 'json') and result.json:
            # 从result.json中提取信息
            result_data = result.json
            
            rec_texts = result_data.get('rec_texts', [])
            rec_scores = result_data.get('rec_scores', [])
            dt_polys = result_data.get('dt_polys', [])
            
            for i, (text, score, poly) in enumerate(zip(rec_texts, rec_scores, dt_polys)):
                ocr_results.append({
                    "id": i,
                    "text": text,
                    "confidence": round(float(score), 4),
                    "bbox": poly.tolist() if hasattr(poly, 'tolist') else poly
                })
        
        return {
            "success": True,
            "model_version": "PP-OCRv5",
            "language": lang,
            "count": len(ocr_results),
            "results": ocr_results
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"OCR识别失败: {str(e)}")

@app.post("/ocr_table") 
async def table_recognition(
    file: UploadFile = File(...),
    lang: LangEnum = LangEnum.ch,
    use_gpu: bool = False
):
    """PP-StructureV3表格识别"""
    try:
        validate_image(file)
        
        contents = await file.read()
        if not contents:
            raise HTTPException(status_code=400, detail="文件内容为空")
            
        # 计算文件哈希
        file_hash = hashlib.sha256(contents).hexdigest()[:12]
        
        # 转换图片格式
        image = Image.open(io.BytesIO(contents))
        if image.mode != 'RGB':
            image = image.convert('RGB')
        
        # 使用PP-StructureV3进行表格识别
        # 这里需要单独的表格识别产线
        from paddleocr import PPStructure
        
        # 获取表格识别实例
        table_key = f"table_v3_{lang}_{use_gpu}"
        if table_key not in ocr_cache:
            ocr_cache[table_key] = PPStructure(
                table=True,
                lang=lang,
                device="gpu" if use_gpu else "cpu",
                show_log=True
            )
        
        table_engine = ocr_cache[table_key]
        img_array = np.array(image)
        result = table_engine(img_array)
        
        # 保存结果
        try:
            from paddleocr import save_structure_res
            save_structure_res(result, output_dir, file_hash)
        except Exception as save_error:
            print(f"保存结果文件失败: {save_error}")
        
        # 处理结果
        tables = []
        images = []
        texts = []
        
        for item in result:
            item_type = item.get('type', '')
            bbox = item.get('bbox', [])
            res = item.get('res', {})
            
            if item_type == 'table':
                tables.append({
                    "type": item_type,
                    "bbox": bbox,
                    "html": res.get('html', ''),
                    "confidence": res.get('confidence', 0.0)
                })
            elif item_type == 'figure':
                images.append({
                    "type": item_type, 
                    "bbox": bbox
                })
            else:
                texts.append({
                    "type": item_type,
                    "bbox": bbox,
                    "text": res.get('text', '') if isinstance(res, dict) else str(res)
                })
        
        return {
            "success": True,
            "model_version": "PP-StructureV3",
            "language": lang,
            "hash": file_hash,
            "summary": {
                "total_elements": len(result),
                "tables": len(tables),
                "images": len(images), 
                "texts": len(texts)
            },
            "tables": tables,
            "images": images,
            "texts": texts
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"表格识别失败: {str(e)}")

@app.get("/health")
async def health_check():
    """健康检查接口"""
    return {
        "status": "healthy",
        "ocr_version": "PP-OCRv5",
        "structure_version": "PP-StructureV3", 
        "cache_instances": len(ocr_cache),
        "supported_languages": [lang.value for lang in LangEnum]
    }

@app.get("/models")
async def get_model_info():
    """获取模型信息"""
    return {
        "ocr_models": {
            "PP-OCRv5_server_det": "高精度文本检测模型",
            "PP-OCRv5_server_rec": "高精度文本识别模型 - 支持中英日韩繁5种文字类型"
        },
        "structure_models": {
            "PP-StructureV3": "通用文档解析方案 - 支持表格、图像、文本混合识别"
        },
        "features": {
            "multi_language": "单模型支持5种文字类型",
            "handwriting": "显著提升手写体识别能力", 
            "accuracy_improvement": "相比PP-OCRv4提升13个百分点"
        }
    }

@app.get("/")
async def root():
    """根路径"""
    return {
        "message": "PP-OCRv5 OCR API 服务正常运行",
        "version": "3.0",
        "models": "PP-OCRv5 + PP-StructureV3",
        "docs": "/docs"
    }

# 挂载静态文件服务
app.mount("/output", StaticFiles(directory=output_dir, follow_symlink=True, html=True), name="output")

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