huggingkhalil commited on
Commit
2d06f1e
·
verified ·
1 Parent(s): e4c1dd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -10
app.py CHANGED
@@ -11,41 +11,62 @@ model = YOLO('best.pt')
11
  # Define class names based on your model
12
  class_names = ['null', 'pollen', 'queen', 'queen_cell', 'varroa']
13
 
14
- def predict_bee(image):
15
  """
16
  Predict bee type and health status from uploaded image
17
  """
18
  try:
 
 
 
 
 
19
  # Convert PIL image to numpy array
20
  img_array = np.array(image)
 
 
 
 
 
 
21
 
22
- # Run inference
23
- results = model(img_array)
24
 
25
  # Process results
26
  predictions = []
27
- annotated_image = img_array.copy()
28
 
29
- for result in results:
 
 
 
30
  # Get boxes, scores, and classes
31
  boxes = result.boxes
32
 
 
33
  if boxes is not None:
34
- for box in boxes:
 
 
 
 
35
  # Get coordinates
36
  x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
37
 
38
  # Get confidence and class
39
- confidence = box.conf[0].cpu().numpy()
40
  class_id = int(box.cls[0].cpu().numpy())
41
 
 
 
42
  # Get class name
43
  class_name = class_names[class_id] if class_id < len(class_names) else f"Class_{class_id}"
44
 
45
  # Add to predictions
46
  predictions.append({
47
  'class': class_name,
48
- 'confidence': float(confidence),
49
  'bbox': [int(x1), int(y1), int(x2), int(y2)]
50
  })
51
 
@@ -60,6 +81,10 @@ def predict_bee(image):
60
  (x1 + label_size[0], y1), color, -1)
61
  cv2.putText(annotated_image, label, (x1, y1 - 5),
62
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
 
 
 
 
63
 
64
  # Convert back to PIL Image
65
  annotated_image = Image.fromarray(annotated_image)
@@ -127,6 +152,15 @@ def create_interface():
127
  height=400
128
  )
129
 
 
 
 
 
 
 
 
 
 
130
  submit_btn = gr.Button(
131
  "🔍 Analyze Hive",
132
  variant="primary",
@@ -173,13 +207,19 @@ def create_interface():
173
  # Event handlers
174
  submit_btn.click(
175
  fn=predict_bee,
176
- inputs=image_input,
177
  outputs=[image_output, text_output]
178
  )
179
 
180
  image_input.change(
181
  fn=predict_bee,
182
- inputs=image_input,
 
 
 
 
 
 
183
  outputs=[image_output, text_output]
184
  )
185
 
 
11
  # Define class names based on your model
12
  class_names = ['null', 'pollen', 'queen', 'queen_cell', 'varroa']
13
 
14
+ def predict_bee(image, confidence_threshold=0.25):
15
  """
16
  Predict bee type and health status from uploaded image
17
  """
18
  try:
19
+ # Debug: Print image info
20
+ print(f"Input image size: {image.size}")
21
+ print(f"Input image mode: {image.mode}")
22
+ print(f"Confidence threshold: {confidence_threshold}")
23
+
24
  # Convert PIL image to numpy array
25
  img_array = np.array(image)
26
+ print(f"Array shape: {img_array.shape}")
27
+
28
+ # Ensure image is in RGB format
29
+ if len(img_array.shape) == 3 and img_array.shape[2] == 3:
30
+ # Convert RGB to BGR for OpenCV compatibility
31
+ img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
32
 
33
+ # Run inference with explicit parameters
34
+ results = model(img_array, conf=confidence_threshold, iou=0.45, verbose=True)
35
 
36
  # Process results
37
  predictions = []
38
+ annotated_image = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB) # Convert back to RGB for display
39
 
40
+ print(f"Number of results: {len(results)}")
41
+
42
+ for i, result in enumerate(results):
43
+ print(f"Processing result {i}")
44
  # Get boxes, scores, and classes
45
  boxes = result.boxes
46
 
47
+ print(f"Boxes object: {boxes}")
48
  if boxes is not None:
49
+ print(f"Number of detections: {len(boxes)}")
50
+
51
+ for j, box in enumerate(boxes):
52
+ print(f"Processing box {j}")
53
+
54
  # Get coordinates
55
  x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
56
 
57
  # Get confidence and class
58
+ confidence = float(box.conf[0].cpu().numpy())
59
  class_id = int(box.cls[0].cpu().numpy())
60
 
61
+ print(f"Detection: class_id={class_id}, confidence={confidence:.3f}, bbox=[{x1},{y1},{x2},{y2}]")
62
+
63
  # Get class name
64
  class_name = class_names[class_id] if class_id < len(class_names) else f"Class_{class_id}"
65
 
66
  # Add to predictions
67
  predictions.append({
68
  'class': class_name,
69
+ 'confidence': confidence,
70
  'bbox': [int(x1), int(y1), int(x2), int(y2)]
71
  })
72
 
 
81
  (x1 + label_size[0], y1), color, -1)
82
  cv2.putText(annotated_image, label, (x1, y1 - 5),
83
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
84
+ else:
85
+ print("No boxes detected")
86
+
87
+ print(f"Total predictions: {len(predictions)}")
88
 
89
  # Convert back to PIL Image
90
  annotated_image = Image.fromarray(annotated_image)
 
152
  height=400
153
  )
154
 
155
+ confidence_slider = gr.Slider(
156
+ minimum=0.1,
157
+ maximum=0.9,
158
+ value=0.25,
159
+ step=0.05,
160
+ label="Confidence Threshold",
161
+ info="Lower values = more detections (but may include false positives)"
162
+ )
163
+
164
  submit_btn = gr.Button(
165
  "🔍 Analyze Hive",
166
  variant="primary",
 
207
  # Event handlers
208
  submit_btn.click(
209
  fn=predict_bee,
210
+ inputs=[image_input, confidence_slider],
211
  outputs=[image_output, text_output]
212
  )
213
 
214
  image_input.change(
215
  fn=predict_bee,
216
+ inputs=[image_input, confidence_slider],
217
+ outputs=[image_output, text_output]
218
+ )
219
+
220
+ confidence_slider.change(
221
+ fn=predict_bee,
222
+ inputs=[image_input, confidence_slider],
223
  outputs=[image_output, text_output]
224
  )
225