SaniaE commited on
Commit
31f8a15
·
verified ·
1 Parent(s): 486cd00

added check for failed detections

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -6,7 +6,7 @@ import tensorflow as tf
6
  from PIL import Image
7
  from fastapi import FastAPI, File, UploadFile
8
  from fastapi.middleware.cors import CORSMiddleware
9
- from fastapi.responses import StreamingResponse
10
  from huggingface_hub import hf_hub_download, snapshot_download, login
11
 
12
  # 1. Run environment fixes immediately
@@ -69,15 +69,20 @@ async def predict(file: UploadFile = File(...)):
69
  with graph.as_default():
70
  results = model_eval.detect([image_np], verbose=0)
71
 
72
- # Process Image
73
- import cv2
 
 
 
 
 
 
74
  processed_img = visualize_detections(image_np, results)
75
-
76
  # Return Stream
77
  _, buffer = cv2.imencode('.jpg', processed_img)
78
  return StreamingResponse(io.BytesIO(buffer), media_type="image/jpeg")
79
 
80
-
81
  @app.post("/explain/occlusion")
82
  async def explain_occlusion(file: UploadFile = File(...)):
83
  # 1. Load and Downsample
 
6
  from PIL import Image
7
  from fastapi import FastAPI, File, UploadFile
8
  from fastapi.middleware.cors import CORSMiddleware
9
+ from fastapi.responses import StreamingResponse, Response
10
  from huggingface_hub import hf_hub_download, snapshot_download, login
11
 
12
  # 1. Run environment fixes immediately
 
69
  with graph.as_default():
70
  results = model_eval.detect([image_np], verbose=0)
71
 
72
+ # --- ADD THIS CHECK ---
73
+ # results[0] contains 'rois', 'masks', 'class_ids', and 'scores'
74
+ if len(results[0]['scores']) == 0:
75
+ # Returning a 204 status triggers 'response.status === 204' in your React code
76
+ return Response(status_code=204)
77
+ # ----------------------
78
+
79
+ # Process Image (Only happens if segments exist)
80
  processed_img = visualize_detections(image_np, results)
81
+
82
  # Return Stream
83
  _, buffer = cv2.imencode('.jpg', processed_img)
84
  return StreamingResponse(io.BytesIO(buffer), media_type="image/jpeg")
85
 
 
86
  @app.post("/explain/occlusion")
87
  async def explain_occlusion(file: UploadFile = File(...)):
88
  # 1. Load and Downsample