From fd8318b559f6576fdd573d32ee31b25ad7d49a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B0=E6=B6=9B?= <550947002@qq.com> Date: Wed, 14 Aug 2024 12:02:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dfile=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E6=98=AF=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 532a4bc..9053fa6 100644 --- a/app/main.py +++ b/app/main.py @@ -38,7 +38,7 @@ async def ocr_endpoint( png_fix: bool = Form(False) ): try: - if file.size == 0 and image is None: + if file and file.file._file is None and image is None: return APIResponse(code=400, message="Either file or image must be provided") image_bytes = await decode_image(file or image) @@ -74,7 +74,7 @@ async def detection_endpoint( image: Optional[str] = Form(None) ): try: - if file.size == 0 and image is None: + if file and file.file._file is None and image is None: return APIResponse(code=400, message="Either file or image must be provided") image_bytes = await decode_image(file or image) From ed139fbedca095ebcaabf7f2fa78d7c5b2476166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B0=E6=B6=9B?= <550947002@qq.com> Date: Wed, 14 Aug 2024 16:53:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=BB=91=E5=9D=97?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=9A=84=E5=85=A5=E5=8F=82=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index 9053fa6..7e24788 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,4 @@ +from starlette.datastructures import UploadFile as StarletteUploadFile import uvicorn from fastapi import FastAPI, File, UploadFile, HTTPException, Form from typing import Optional, Union @@ -7,8 +8,6 @@ from services import ocr_service app = FastAPI() -from starlette.datastructures import UploadFile as StarletteUploadFile - async def decode_image(image: Union[UploadFile, StarletteUploadFile, str, None]) -> bytes: if image is None: @@ -24,7 +23,8 @@ async def decode_image(image: Union[UploadFile, StarletteUploadFile, str, None]) image = image.split(',')[1] return base64.b64decode(image) except: - raise HTTPException(status_code=400, detail="Invalid base64 string") + raise HTTPException( + status_code=400, detail="Invalid base64 string") else: raise HTTPException(status_code=400, detail="Invalid image input") @@ -42,7 +42,8 @@ async def ocr_endpoint( return APIResponse(code=400, message="Either file or image must be provided") image_bytes = await decode_image(file or image) - result = ocr_service.ocr_classification(image_bytes, probability, charsets, png_fix) + result = ocr_service.ocr_classification( + image_bytes, probability, charsets, png_fix) return APIResponse(code=200, message="Success", data=result) except Exception as e: return APIResponse(code=500, message=str(e)) @@ -57,12 +58,12 @@ async def slide_match_endpoint( simple_target: bool = Form(False) ): try: - if (background is None and target is None) or (background_file.size == 0 and target_file.size == 0): + if (background is None and background_file and background_file.file._file is None) or (target is None and target_file and target_file.file._file is None): return APIResponse(code=400, message="Both target and background must be provided") - 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) + result = ocr_service.slide_match( + target_bytes, background_bytes, simple_target) return APIResponse(code=200, message="Success", data=result) except Exception as e: return APIResponse(code=500, message=str(e))