muskangoyal06 commited on
Commit
f305096
Β·
verified Β·
1 Parent(s): 24ad789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -37
app.py CHANGED
@@ -1,67 +1,54 @@
1
  import gradio as gr
2
  from ultralyticsplus import YOLO, render_result
3
  import cv2
4
- import torch
5
  import time
6
 
7
- # Check GPU availability
8
- print(f"CUDA available: {torch.cuda.is_available()}")
9
- print(f"CUDA device count: {torch.cuda.device_count()}")
10
- if torch.cuda.is_available():
11
- print(f"Current device: {torch.cuda.current_device()}")
12
- print(f"Device name: {torch.cuda.get_device_name(0)}")
13
 
14
- # Load model with GPU acceleration
15
- model = YOLO('foduucom/plant-leaf-detection-and-classification').to('cuda' if torch.cuda.is_available() else 'cpu')
16
-
17
- # Model configuration
18
- model.overrides['conf'] = 0.25
19
- model.overrides['iou'] = 0.45
20
- model.overrides['agnostic_nms'] = False
21
- model.overrides['max_det'] = 1000
22
 
23
  def detect_leaves(image):
24
  start_time = time.time()
25
 
26
- # Convert image and check processing time
27
- print("Converting image format...")
28
  img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
29
- print(f"Conversion time: {time.time() - start_time:.2f}s")
30
 
31
- # Perform prediction with timing
32
- print("Starting prediction...")
33
- pred_start = time.time()
34
- results = model.predict(img, imgsz=640) # Fixed inference size
35
- print(f"Prediction time: {time.time() - pred_start:.2f}s")
 
36
 
37
  # Process results
38
  num_leaves = len(results[0].boxes)
39
- print(f"Detected {num_leaves} leaves")
40
-
41
- # Render results
42
- print("Rendering results...")
43
- render_start = time.time()
44
  rendered_img = render_result(model=model, image=img, result=results[0])
45
- print(f"Rendering time: {time.time() - render_start:.2f}s")
46
 
47
- # Convert back to RGB
48
  return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
49
 
50
- # Create Gradio interface with queue
51
  interface = gr.Interface(
52
  fn=detect_leaves,
53
- inputs=gr.Image(label="Upload Plant Image"),
54
  outputs=[
55
- gr.Image(label="Detected Leaves"),
56
- gr.Number(label="Number of Leaves Found")
57
  ],
58
- title="πŸƒ Plant Leaf Detection",
59
  allow_flagging="never"
60
  )
61
 
62
  if __name__ == "__main__":
63
  interface.launch(
64
  server_port=7860,
65
- share=False,
66
- enable_queue=True # Essential for heavy computations
67
  )
 
1
  import gradio as gr
2
  from ultralyticsplus import YOLO, render_result
3
  import cv2
 
4
  import time
5
 
6
+ # Load model with automatic device detection
7
+ model = YOLO('foduucom/plant-leaf-detection-and-classification')
 
 
 
 
8
 
9
+ # Optimize model configuration
10
+ model.overrides.update({
11
+ 'conf': 0.25,
12
+ 'iou': 0.45,
13
+ 'imgsz': 640,
14
+ 'device': '0' if model.device.type != 'cpu' else 'cpu'
15
+ })
 
16
 
17
  def detect_leaves(image):
18
  start_time = time.time()
19
 
20
+ # Convert image format
 
21
  img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
 
22
 
23
+ # Predict with optimized settings
24
+ results = model.predict(
25
+ source=img,
26
+ verbose=False, # Disable unnecessary logging
27
+ stream=False # Disable streaming mode
28
+ )
29
 
30
  # Process results
31
  num_leaves = len(results[0].boxes)
 
 
 
 
 
32
  rendered_img = render_result(model=model, image=img, result=results[0])
 
33
 
34
+ print(f"Total processing time: {time.time() - start_time:.2f}s")
35
  return cv2.cvtColor(rendered_img, cv2.COLOR_BGR2RGB), num_leaves
36
 
37
+ # Create lightweight interface
38
  interface = gr.Interface(
39
  fn=detect_leaves,
40
+ inputs=gr.Image(label="Plant Image"),
41
  outputs=[
42
+ gr.Image(label="Detection Result", width=600),
43
+ gr.Number(label="Leaves Count")
44
  ],
45
+ title="πŸƒ Leaf Detection",
46
  allow_flagging="never"
47
  )
48
 
49
  if __name__ == "__main__":
50
  interface.launch(
51
  server_port=7860,
52
+ show_error=True,
53
+ enable_queue=True
54
  )