muskangoyal06 commited on
Commit
445566f
·
verified ·
1 Parent(s): e3e70fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -1,24 +1,30 @@
 
 
 
 
 
 
1
  from huggingface_hub import hf_hub_download
2
  from ultralytics import YOLO
3
  import gradio as gr
4
 
5
  # Download the weights file (e.g., "best.pt") from the Hugging Face Hub
6
  weights_path = hf_hub_download(
7
- repo_id="foduucom/plant-leaf-detection-and-classification",
8
  filename="best.pt"
9
  )
10
 
11
- # Load the model using the local weights file
12
  model = YOLO(weights_path)
13
 
14
  def predict_leaves(image_path):
15
- # Run the model prediction on the provided image path
16
  results = model.predict(source=image_path, save=True)
17
- # Count the number of detected leaves (using the first result)
18
  count = len(results[0].boxes)
19
  return f"Detected leaves: {count}"
20
 
21
- # Create a Gradio interface
22
  iface = gr.Interface(
23
  fn=predict_leaves,
24
  inputs=gr.Image(type="filepath"),
 
1
+ import torch
2
+ from ultralytics.nn.tasks import DetectionModel
3
+
4
+ # Allow the DetectionModel global for safe unpickling (only if you trust the source!)
5
+ torch.serialization.add_safe_globals([DetectionModel])
6
+
7
  from huggingface_hub import hf_hub_download
8
  from ultralytics import YOLO
9
  import gradio as gr
10
 
11
  # Download the weights file (e.g., "best.pt") from the Hugging Face Hub
12
  weights_path = hf_hub_download(
13
+ repo_id="foduucom/plant-leaf-detection-and-classification",
14
  filename="best.pt"
15
  )
16
 
17
+ # Load the YOLO model using the local weights file
18
  model = YOLO(weights_path)
19
 
20
  def predict_leaves(image_path):
21
+ # Run the prediction on the provided image path
22
  results = model.predict(source=image_path, save=True)
23
+ # Count the number of detected leaves (assuming results[0].boxes is available)
24
  count = len(results[0].boxes)
25
  return f"Detected leaves: {count}"
26
 
27
+ # Create a Gradio interface for your app
28
  iface = gr.Interface(
29
  fn=predict_leaves,
30
  inputs=gr.Image(type="filepath"),