fix(OCR处理): 增强OCR结果处理并改进错误日志
Browse files确保OCR结果能正确处理不同类型返回,并添加详细的错误日志记录
清理临时文件时增加异常保护
app.py
CHANGED
|
@@ -324,11 +324,26 @@ async def upload_text(file: UploadFile = File(...)):
|
|
| 324 |
if os.path.exists(temp_file_path):
|
| 325 |
os.remove(temp_file_path)
|
| 326 |
|
|
|
|
|
|
|
| 327 |
logger.info(f"OCR识别结果: {result}")
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 330 |
logger.error(f"OCR识别错误: {e}")
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
|
| 334 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 324 |
if os.path.exists(temp_file_path):
|
| 325 |
os.remove(temp_file_path)
|
| 326 |
|
| 327 |
+
# 检查结果类型并进行适当处理
|
| 328 |
+
logger.info(f"OCR识别结果类型: {type(result)}")
|
| 329 |
logger.info(f"OCR识别结果: {result}")
|
| 330 |
+
|
| 331 |
+
# 确保返回的是有效的JSON
|
| 332 |
+
if isinstance(result, str):
|
| 333 |
+
return {"result": result}
|
| 334 |
+
elif isinstance(result, dict):
|
| 335 |
+
return {"result": result}
|
| 336 |
+
else:
|
| 337 |
+
return {"result": str(result)}
|
| 338 |
except Exception as e:
|
| 339 |
+
# 清理临时文件
|
| 340 |
+
if os.path.exists(temp_file_path):
|
| 341 |
+
os.remove(temp_file_path)
|
| 342 |
logger.error(f"OCR识别错误: {e}")
|
| 343 |
+
import traceback
|
| 344 |
+
|
| 345 |
+
logger.error(f"错误堆栈: {traceback.format_exc()}")
|
| 346 |
+
raise HTTPException(status_code=500, detail=f"服务器内部错误: {str(e)}")
|
| 347 |
|
| 348 |
|
| 349 |
from fastapi.staticfiles import StaticFiles
|