Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -328,15 +328,30 @@ async def predict_tomato(file: UploadFile = File(...)):
|
|
| 328 |
"message": "Please upload a clear image of a PLANT leaf."
|
| 329 |
})
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
)
|
| 337 |
-
|
| 338 |
-
image = Image.fromarray(image_array)
|
| 339 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
# 1. Standard Resize
|
| 342 |
image = image.resize((224, 224))
|
|
|
|
| 328 |
"message": "Please upload a clear image of a PLANT leaf."
|
| 329 |
})
|
| 330 |
|
| 331 |
+
# --- SMART BACKGROUND REMOVAL ---
|
| 332 |
+
image_array = np.array(image).astype(np.float32)
|
| 333 |
+
|
| 334 |
+
R = image_array[:, :, 0]
|
| 335 |
+
G = image_array[:, :, 1]
|
| 336 |
+
B = image_array[:, :, 2]
|
| 337 |
+
|
| 338 |
+
# Brightness: pixel must be very bright overall
|
| 339 |
+
brightness = (R + G + B) / 3.0
|
| 340 |
+
is_bright = brightness > 200
|
| 341 |
+
|
| 342 |
+
# Saturation: true background has no color — R, G, B are all similar
|
| 343 |
+
# A green leaf pixel will have G much higher than R and B
|
| 344 |
+
channel_range = (
|
| 345 |
+
np.maximum(np.maximum(R, G), B) -
|
| 346 |
+
np.minimum(np.minimum(R, G), B)
|
| 347 |
)
|
| 348 |
+
is_unsaturated = channel_range < 30 # low color variation = background
|
|
|
|
| 349 |
|
| 350 |
+
# Only replace pixels that are BOTH bright AND colorless
|
| 351 |
+
background_mask = is_bright & is_unsaturated
|
| 352 |
+
image_array[background_mask] = [0, 0, 0]
|
| 353 |
+
|
| 354 |
+
image = Image.fromarray(image_array.astype(np.uint8))
|
| 355 |
|
| 356 |
# 1. Standard Resize
|
| 357 |
image = image.resize((224, 224))
|