muskangoyal06 commited on
Commit
7f97dd6
·
verified ·
1 Parent(s): a73bb42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -33
app.py CHANGED
@@ -1,40 +1,51 @@
1
- import torch
2
- from ultralytics.nn.tasks import DetectionModel
3
- from torch.nn.modules.container import Sequential
4
- from ultralytics.nn.modules import Conv # Import Conv
5
-
6
- # Whitelist safe globals for unpickling (only if you trust the model checkpoint!)
7
- torch.serialization.add_safe_globals([DetectionModel, Sequential, Conv])
8
-
9
- from huggingface_hub import hf_hub_download
10
- from ultralytics import YOLO
11
  import gradio as gr
12
- from PIL import Image
 
13
 
14
- # Download the weights file (ensure the filename is correct)
15
- weights_path = hf_hub_download(
16
- repo_id="foduucom/plant-leaf-detection-and-classification",
17
- filename="best.pt"
18
- )
19
 
20
- # Load the model using the local weights file
21
- model = YOLO(weights_path)
 
 
 
22
 
23
- def predict_leaves(image_path):
24
- # Run prediction on the provided image
25
- results = model.predict(source=image_path, save=True)
26
- # Count the number of detected leaves (using the first result)
27
- count = len(results[0].boxes)
28
- return f"Detected leaves: {count}"
 
 
 
 
 
 
 
 
 
29
 
30
- # Create a Gradio interface for your app
31
- iface = gr.Interface(
32
- fn=predict_leaves,
33
- inputs=gr.Image(type="filepath"),
34
- outputs="text",
35
- title="Leaf Detection & Classification",
36
- description="Upload an image to detect and count leaves using the YOLO model."
37
- )
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  if __name__ == "__main__":
40
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from ultralyticsplus import YOLO, render_result
3
+ import cv2
4
 
5
+ # Load the YOLO model
6
+ model = YOLO('foduucom/plant-leaf-detection-and-classification')
 
 
 
7
 
8
+ # Set model parameters
9
+ model.overrides['conf'] = 0.25 # NMS confidence threshold
10
+ model.overrides['iou'] = 0.45 # NMS IoU threshold
11
+ model.overrides['agnostic_nms'] = False # NMS class-agnostic
12
+ model.overrides['max_det'] = 1000 # maximum detections per image
13
 
14
+ def detect_leaves(image):
15
+ # Convert from Gradio's numpy array to image file
16
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
17
+ cv2.imwrite('temp_image.jpg', image)
18
+
19
+ # Perform prediction
20
+ results = model.predict('temp_image.jpg')
21
+
22
+ # Count number of leaves detected
23
+ num_leaves = len(results[0].boxes)
24
+
25
+ # Render results on image
26
+ render = render_result(model=model, image='temp_image.jpg', result=results[0])
27
+
28
+ return render, num_leaves
29
 
30
+ # Create Gradio interface
31
+ with gr.Blocks(title="Leaf Detection & Classification") as demo:
32
+ gr.Markdown("# 🍃 Plant Leaf Detection & Classification")
33
+ gr.Markdown("Upload a plant image to detect and count leaves")
34
+
35
+ with gr.Row():
36
+ with gr.Column():
37
+ input_image = gr.Image(label="Upload Plant Image")
38
+ submit_btn = gr.Button("Detect Leaves")
39
+
40
+ with gr.Column():
41
+ output_image = gr.Image(label="Detection Results")
42
+ leaf_count = gr.Number(label="Number of Leaves Detected")
43
+
44
+ submit_btn.click(
45
+ fn=detect_leaves,
46
+ inputs=[input_image],
47
+ outputs=[output_image, leaf_count]
48
+ )
49
 
50
  if __name__ == "__main__":
51
+ demo.launch(server_port=7860, share=False)