muskangoyal06 commited on
Commit
7c5f722
Β·
verified Β·
1 Parent(s): 39be674

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -32
app.py CHANGED
@@ -3,20 +3,20 @@ from ultralyticsplus import YOLO, render_result
3
  import cv2
4
  import torch
5
 
6
- # Verify torch version
7
- print(f"Using torch version: {torch.__version__}")
 
8
 
9
- # Load model with compatibility fix
10
  def load_model():
11
- try:
12
- model = YOLO('foduucom/plant-leaf-detection-and-classification')
13
- model.overrides['conf'] = 0.25
14
- model.overrides['iou'] = 0.45
15
- model.overrides['agnostic_nms'] = False
16
- model.overrides['max_det'] = 1000
17
- return model
18
- except Exception as e:
19
- raise RuntimeError("Error loading model. Please check the requirements versions.") from e
20
 
21
  model = load_model()
22
 
@@ -25,8 +25,8 @@ def detect_leaves(image):
25
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
26
  cv2.imwrite('temp_image.jpg', image)
27
 
28
- # Perform prediction
29
- results = model.predict('temp_image.jpg')
30
 
31
  # Process results
32
  num_leaves = len(results[0].boxes)
@@ -34,23 +34,17 @@ def detect_leaves(image):
34
 
35
  return render, num_leaves
36
 
37
- # Create Gradio interface
38
- with gr.Blocks(theme=gr.themes.Soft(), title="Leaf Detection") as demo:
39
- gr.Markdown("## πŸƒ Plant Leaf Detection & Counter")
40
- gr.Markdown("Upload a plant image to analyze leaf count and species")
41
-
42
- with gr.Row():
43
- input_image = gr.Image(label="Input Image", type="numpy")
44
- output_image = gr.Image(label="Detected Leaves", interactive=False)
45
-
46
- leaf_count = gr.Number(label="Total Leaves Detected", precision=0)
47
-
48
- submit_btn = gr.Button("Analyze Image", variant="primary")
49
- submit_btn.click(
50
- fn=detect_leaves,
51
- inputs=[input_image],
52
- outputs=[output_image, leaf_count]
53
- )
54
 
55
  if __name__ == "__main__":
56
- demo.launch(server_port=7860, show_error=True)
 
3
  import cv2
4
  import torch
5
 
6
+ # Check versions
7
+ print(f"Torch version: {torch.__version__}")
8
+ print(f"Ultralytics version: {YOLO.__version__}")
9
 
10
+ # Load model with safe serialization
11
  def load_model():
12
+ model = YOLO('foduucom/plant-leaf-detection-and-classification')
13
+ model.overrides.update({
14
+ 'conf': 0.25,
15
+ 'iou': 0.45,
16
+ 'agnostic_nms': False,
17
+ 'max_det': 1000
18
+ })
19
+ return model
 
20
 
21
  model = load_model()
22
 
 
25
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
26
  cv2.imwrite('temp_image.jpg', image)
27
 
28
+ # Predict with numpy fix
29
+ results = model.predict(source='temp_image.jpg', imgsz=640)
30
 
31
  # Process results
32
  num_leaves = len(results[0].boxes)
 
34
 
35
  return render, num_leaves
36
 
37
+ # Create simplified interface
38
+ interface = gr.Interface(
39
+ fn=detect_leaves,
40
+ inputs=gr.Image(label="Upload Plant Image"),
41
+ outputs=[
42
+ gr.Image(label="Detection Result"),
43
+ gr.Number(label="Leaves Count")
44
+ ],
45
+ title="πŸƒ Plant Leaf Detector",
46
+ description="Upload a plant image to detect and count leaves"
47
+ )
 
 
 
 
 
 
48
 
49
  if __name__ == "__main__":
50
+ interface.launch(server_port=7860)