Skip to content

Commit

Permalink
Fix http exception where there are valid images in a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed Jan 25, 2024
1 parent 9772858 commit 601ccf7
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ async def detect_age_single(websocket: WebSocket):

@app.post("/detect-age/multiple")
def detect_age_multiple(files: List[UploadFile] = File(...)):
if not all(file.content_type.startswith("image/") for file in files):
raise HTTPException(status_code=400, detail="Invalid file format. Please upload an image.")
try:
images = []
for file in files:
if not file.content_type.startswith("image/"):
continue
content = file.file.read()
image_array = cv2.imdecode(np.frombuffer(content, np.uint8), -1)

Expand All @@ -89,6 +89,9 @@ def detect_age_multiple(files: List[UploadFile] = File(...)):
output_image.seek(0)
images.append({"name": file.filename, "content": output_image.getvalue()})

if not images:
raise HTTPException(status_code=400, detail="No valid image files found.")

zip_file = io.BytesIO()
with zipfile.ZipFile(zip_file, 'w') as zipf:
for image in images:
Expand Down

0 comments on commit 601ccf7

Please sign in to comment.