Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,108 @@
|
|
| 1 |
-
# Remove unnecessary OpenCV imports and conversions
|
| 2 |
import gradio as gr
|
| 3 |
from ultralyticsplus import YOLO, render_result
|
| 4 |
import numpy as np
|
| 5 |
import time
|
| 6 |
import torch
|
| 7 |
|
| 8 |
-
# System
|
| 9 |
print("\n" + "="*40)
|
| 10 |
print(f"PyTorch: {torch.__version__}")
|
| 11 |
-
print(f"CUDA: {torch.cuda.is_available()}")
|
| 12 |
print("="*40 + "\n")
|
| 13 |
|
| 14 |
-
# Load model
|
| 15 |
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
|
|
|
|
|
|
| 16 |
model.overrides.update({
|
| 17 |
-
'conf': 0.
|
| 18 |
-
'iou': 0.
|
| 19 |
-
'imgsz':
|
|
|
|
|
|
|
| 20 |
'device': 'cuda' if torch.cuda.is_available() else 'cpu',
|
|
|
|
| 21 |
'half': torch.cuda.is_available()
|
| 22 |
})
|
| 23 |
|
| 24 |
-
def
|
| 25 |
try:
|
| 26 |
start_time = time.time()
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
results = model.predict(
|
| 30 |
-
source=
|
|
|
|
| 31 |
verbose=False,
|
| 32 |
-
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
print(f"Processing time: {time.time()-start_time:.2f}s")
|
| 39 |
-
return
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
print(f"Error: {str(e)}")
|
| 43 |
-
return
|
| 44 |
|
| 45 |
-
#
|
| 46 |
interface = gr.Interface(
|
| 47 |
-
fn=
|
| 48 |
-
inputs=gr.Image(),
|
| 49 |
-
outputs=[
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
interface.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralyticsplus import YOLO, render_result
|
| 3 |
import numpy as np
|
| 4 |
import time
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# System Configuration
|
| 8 |
print("\n" + "="*40)
|
| 9 |
print(f"PyTorch: {torch.__version__}")
|
| 10 |
+
print(f"CUDA Available: {torch.cuda.is_available()}")
|
| 11 |
print("="*40 + "\n")
|
| 12 |
|
| 13 |
+
# Load model with optimized parameters for leaf counting
|
| 14 |
model = YOLO('foduucom/plant-leaf-detection-and-classification')
|
| 15 |
+
|
| 16 |
+
# Custom configuration for leaf counting
|
| 17 |
model.overrides.update({
|
| 18 |
+
'conf': 0.15, # Lower confidence threshold for better recall
|
| 19 |
+
'iou': 0.25, # Lower IoU threshold for overlapping leaves
|
| 20 |
+
'imgsz': 1280, # Higher resolution for small leaves
|
| 21 |
+
'agnostic_nms': False,
|
| 22 |
+
'max_det': 300, # Higher maximum detections
|
| 23 |
'device': 'cuda' if torch.cuda.is_available() else 'cpu',
|
| 24 |
+
'classes': None, # Detect all classes (leaves only in this model)
|
| 25 |
'half': torch.cuda.is_available()
|
| 26 |
})
|
| 27 |
|
| 28 |
+
def count_leaves(image):
|
| 29 |
try:
|
| 30 |
start_time = time.time()
|
| 31 |
|
| 32 |
+
# Preprocessing - enhance contrast
|
| 33 |
+
image = np.array(image)
|
| 34 |
+
lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
|
| 35 |
+
l, a, b = cv2.split(lab)
|
| 36 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 37 |
+
cl = clahe.apply(l)
|
| 38 |
+
limg = cv2.merge((cl,a,b))
|
| 39 |
+
enhanced_img = cv2.cvtColor(limg, cv2.COLOR_LAB2RGB)
|
| 40 |
+
|
| 41 |
+
# Prediction with overlap handling
|
| 42 |
results = model.predict(
|
| 43 |
+
source=enhanced_img,
|
| 44 |
+
augment=True, # Test time augmentation
|
| 45 |
verbose=False,
|
| 46 |
+
agnostic_nms=False,
|
| 47 |
+
overlap_mask=False
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# Post-processing for overlapping leaves
|
| 51 |
+
boxes = results[0].boxes
|
| 52 |
+
valid_boxes = []
|
| 53 |
+
|
| 54 |
+
# Filter small detections and merge overlapping
|
| 55 |
+
for box in boxes:
|
| 56 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 57 |
+
w = x2 - x1
|
| 58 |
+
h = y2 - y1
|
| 59 |
+
|
| 60 |
+
# Filter too small boxes (adjust based on your leaf sizes)
|
| 61 |
+
if w > 20 and h > 20:
|
| 62 |
+
valid_boxes.append(box)
|
| 63 |
+
|
| 64 |
+
# Improved NMS for overlapping leaves
|
| 65 |
+
from utils.nms import non_max_suppression
|
| 66 |
+
final_boxes = non_max_suppression(
|
| 67 |
+
torch.stack([b.xywh[0] for b in valid_boxes]),
|
| 68 |
+
conf_thres=0.1,
|
| 69 |
+
iou_thres=0.15,
|
| 70 |
+
multi_label=False
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
num_leaves = len(final_boxes)
|
| 74 |
+
|
| 75 |
+
# Visual validation
|
| 76 |
+
debug_img = enhanced_img.copy()
|
| 77 |
+
for box in final_boxes:
|
| 78 |
+
x1, y1, x2, y2 = map(int, box[:4])
|
| 79 |
+
cv2.rectangle(debug_img, (x1, y1), (x2, y2), (0,255,0), 2)
|
| 80 |
|
| 81 |
print(f"Processing time: {time.time()-start_time:.2f}s")
|
| 82 |
+
return debug_img, num_leaves
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
print(f"Error: {str(e)}")
|
| 86 |
+
return image, 0
|
| 87 |
|
| 88 |
+
# Gradio interface with visualization
|
| 89 |
interface = gr.Interface(
|
| 90 |
+
fn=count_leaves,
|
| 91 |
+
inputs=gr.Image(label="Input Image"),
|
| 92 |
+
outputs=[
|
| 93 |
+
gr.Image(label="Detection Visualization"),
|
| 94 |
+
gr.Number(label="Estimated Leaf Count")
|
| 95 |
+
],
|
| 96 |
+
title="π Advanced Leaf Counter",
|
| 97 |
+
description="Specialized for overlapping leaves and dense foliage",
|
| 98 |
+
examples=[
|
| 99 |
+
["sample_leaf1.jpg"],
|
| 100 |
+
["sample_leaf2.jpg"]
|
| 101 |
+
]
|
| 102 |
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
+
interface.launch(
|
| 106 |
+
server_port=7860,
|
| 107 |
+
share=False
|
| 108 |
+
)
|