usama1355 commited on
Commit
bb486a4
·
verified ·
1 Parent(s): d41b951

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -7
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
- image_array = np.array(image)
332
- white_mask = (
333
- (image_array[:, :, 0] > 240) &
334
- (image_array[:, :, 1] > 240) &
335
- (image_array[:, :, 2] > 240)
 
 
 
 
 
 
 
 
 
 
 
336
  )
337
- image_array[white_mask] = [0, 0, 0]
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))