muskangoyal06 commited on
Commit
0770f2c
·
verified ·
1 Parent(s): f46dad9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -33
app.py CHANGED
@@ -1,44 +1,29 @@
 
1
  import gradio as gr
2
- import torch
3
- from ultralytics.nn.tasks import DetectionModel
4
- from torch.nn.modules.container import Sequential
5
- from ultralytics.nn.modules import Conv
6
- from torch.nn.modules.conv import Conv2d # Import Conv2d
7
 
8
- # Whitelist the globals to bypass the pickle errors (only do this if you trust the source!)
9
- torch.serialization.add_safe_globals([DetectionModel, Sequential, Conv, Conv2d])
10
 
11
- from ultralyticsplus import YOLO, render_result
12
- from PIL import Image
13
-
14
- # Load the YOLOv8s Leaf Detection and Classification model from Hugging Face
15
- model = YOLO('foduucom/plant-leaf-detection-and-classification')
16
-
17
- # Set recommended model parameters as per the model card
18
- model.overrides['conf'] = 0.25 # NMS confidence threshold
19
- model.overrides['iou'] = 0.45 # NMS IoU threshold
20
- model.overrides['agnostic_nms'] = False # NMS class-agnostic setting
21
- model.overrides['max_det'] = 1000 # Maximum detections per image
22
-
23
- def count_leaves(image):
24
- # Convert the input to a PIL image (ensuring RGB)
25
- image = Image.open(image).convert("RGB")
26
-
27
- # Perform inference with the model
28
- results = model.predict(image)
29
 
30
- # Count the detected leaves using the bounding boxes from the first result
31
- num_leaves = len(results[0].boxes)
32
 
33
- return f"Number of leaves detected: {num_leaves}"
34
 
35
- # Build a Gradio interface for the leaf counter
36
  iface = gr.Interface(
37
- fn=count_leaves,
38
- inputs=gr.Image(type="filepath"),
39
  outputs="text",
40
- title="Leaf Counter",
41
- description="Upload an image of a plant and the model will detect and count the number of leaves."
42
  )
43
 
44
  if __name__ == "__main__":
 
1
+ from ultralytics import YOLOvv8
2
  import gradio as gr
 
 
 
 
 
3
 
4
+ # Load the pretrained model directly using YOLOvv8.from_pretrained
5
+ model = YOLOvv8.from_pretrained("foduucom/plant-leaf-detection-and-classification")
6
 
7
+ def predict_leaves(image_path):
8
+ """
9
+ Given an image file path, run prediction on it using the YOLOvv8 model.
10
+ The model will automatically save the result if configured.
11
+ """
12
+ # Run the prediction; you can pass additional kwargs as needed (like save=True)
13
+ results = model.predict(source=image_path, save=True)
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Optionally, count the detected leaves using the first result
16
+ count = len(results[0].boxes)
17
 
18
+ return f"Detected leaves: {count}"
19
 
20
+ # Build a Gradio Interface for the leaf detection app
21
  iface = gr.Interface(
22
+ fn=predict_leaves,
23
+ inputs=gr.Image(type="filepath"), # Users can upload an image file
24
  outputs="text",
25
+ title="Leaf Detection & Classification",
26
+ description="Upload an image to detect and count leaves using the YOLOvv8 model."
27
  )
28
 
29
  if __name__ == "__main__":