Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -160,58 +160,31 @@ def preprocess_image(image_data):
|
|
| 160 |
- Convert to numpy array, add batch dim
|
| 161 |
"""
|
| 162 |
try:
|
| 163 |
-
#
|
| 164 |
-
image = Image.open(io.BytesIO(image_data)).convert(
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
# Y1, Y2 = 5, 205
|
| 170 |
-
|
| 171 |
-
# # Clamp to actual image bounds
|
| 172 |
-
# X1 = max(0, min(w - 1, X1))
|
| 173 |
-
# X2 = max(0, min(w - 1, X2))
|
| 174 |
-
# Y1 = max(0, min(h - 1, Y1))
|
| 175 |
-
# Y2 = max(0, min(h - 1, Y2))
|
| 176 |
-
|
| 177 |
-
# # Pillow crop box is (left, upper, right, lower) with right/lower EXCLUSIVE
|
| 178 |
-
# crop_box = (X1, Y1, X2 + 1, Y2 + 1)
|
| 179 |
-
# image = image.crop(crop_box)
|
| 180 |
|
| 181 |
# Resize to model input size
|
| 182 |
-
image = image.resize((224, 224), Image.
|
| 183 |
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
|
| 187 |
# Add batch dimension
|
| 188 |
-
|
|
|
|
| 189 |
|
| 190 |
# Model has Rescaling(1./255) layer, so no manual normalization
|
| 191 |
-
return
|
| 192 |
|
| 193 |
except Exception as e:
|
| 194 |
logger.error(f"Image preprocessing error: {e}")
|
| 195 |
raise HTTPException(status_code=400, detail=f"Image preprocessing failed: {e}")
|
| 196 |
|
| 197 |
|
| 198 |
-
@app.on_event("startup")
|
| 199 |
-
async def startup_event():
|
| 200 |
-
"""Load model on startup"""
|
| 201 |
-
global model
|
| 202 |
-
try:
|
| 203 |
-
model = load_model()
|
| 204 |
-
logger.info("API startup complete")
|
| 205 |
-
|
| 206 |
-
# Test model with dummy input
|
| 207 |
-
dummy_input = np.random.random((1, 224, 224, 3)).astype(np.float32)
|
| 208 |
-
_ = model.predict(dummy_input, verbose=0)
|
| 209 |
-
logger.info("Model test prediction successful")
|
| 210 |
-
|
| 211 |
-
except Exception as e:
|
| 212 |
-
logger.error(f"Startup failed: {e}")
|
| 213 |
-
raise
|
| 214 |
-
|
| 215 |
@app.get("/health")
|
| 216 |
async def health_check():
|
| 217 |
"""Health check endpoint"""
|
|
|
|
| 160 |
- Convert to numpy array, add batch dim
|
| 161 |
"""
|
| 162 |
try:
|
| 163 |
+
# Convert bytes to PIL Image
|
| 164 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 165 |
+
|
| 166 |
+
# Crop (x1=450, y1=400, x2=1090, y2=1060)
|
| 167 |
+
crop_box = (450, 400, 1090, 1060)
|
| 168 |
+
image = image.crop(crop_box)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
# Resize to model input size
|
| 171 |
+
image = image.resize((224, 224), Image.Resampling.LANCZOS)
|
| 172 |
|
| 173 |
+
# Normalize and expand dims
|
| 174 |
+
image = np.array(image).astype("float32")
|
| 175 |
|
| 176 |
# Add batch dimension
|
| 177 |
+
image = np.expand_dims(image, axis=0)
|
| 178 |
+
|
| 179 |
|
| 180 |
# Model has Rescaling(1./255) layer, so no manual normalization
|
| 181 |
+
return image
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
logger.error(f"Image preprocessing error: {e}")
|
| 185 |
raise HTTPException(status_code=400, detail=f"Image preprocessing failed: {e}")
|
| 186 |
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
@app.get("/health")
|
| 189 |
async def health_check():
|
| 190 |
"""Health check endpoint"""
|