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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -2,48 +2,47 @@ import gradio as gr
2
  from ultralyticsplus import YOLO, render_result
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
 
23
  def detect_leaves(image):
24
  # Convert image format
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)
33
- render = render_result(model=model, image='temp_image.jpg', result=results[0])
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__":
 
2
  from ultralyticsplus import YOLO, render_result
3
  import cv2
4
  import torch
5
+ import ultralytics
6
+ import ultralyticsplus
7
 
8
  # Check versions
9
  print(f"Torch version: {torch.__version__}")
10
+ print(f"Ultralytics version: {ultralytics.__version__}")
11
+ print(f"UltralyticsPlus version: {ultralyticsplus.__version__}")
12
 
13
+ # Load model
14
+ model = YOLO('foduucom/plant-leaf-detection-and-classification')
 
 
 
 
 
 
 
 
15
 
16
+ # Model configuration
17
+ model.overrides['conf'] = 0.25 # Confidence threshold
18
+ model.overrides['iou'] = 0.45 # IoU threshold
19
+ model.overrides['agnostic_nms'] = False
20
+ model.overrides['max_det'] = 1000
21
 
22
  def detect_leaves(image):
23
  # Convert image format
24
+ img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
 
25
 
26
+ # Perform prediction
27
+ results = model.predict(img)
28
 
29
+ # Get results
30
  num_leaves = len(results[0].boxes)
31
+ rendered_img = render_result(model=model, image=img, result=results[0])
32
 
33
+ # Convert back to RGB for Gradio
34
+ return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
35
 
36
+ # Create Gradio interface
37
  interface = gr.Interface(
38
  fn=detect_leaves,
39
  inputs=gr.Image(label="Upload Plant Image"),
40
  outputs=[
41
+ gr.Image(label="Detected Leaves"),
42
+ gr.Number(label="Number of Leaves Found")
43
  ],
44
+ title="πŸƒ Plant Leaf Detection & Counting",
45
+ description="Upload an image of a plant to detect and count its leaves"
46
  )
47
 
48
  if __name__ == "__main__":