DawnC commited on
Commit
9543b48
1 Parent(s): af9f5fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -44
app.py CHANGED
@@ -167,32 +167,32 @@ async def predict_single_dog(image):
167
  return top1_prob, topk_breeds, topk_probs_percent
168
 
169
 
170
- async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.6):
171
- results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
172
- dogs = []
173
- boxes = []
174
- for box in results.boxes:
175
- if box.cls == 16: # COCO dataset class for dog is 16
176
- xyxy = box.xyxy[0].tolist()
177
- confidence = box.conf.item()
178
- boxes.append((xyxy, confidence))
179
 
180
- if not boxes:
181
- dogs.append((image, 1.0, [0, 0, image.width, image.height]))
182
- else:
183
- nms_boxes = non_max_suppression(boxes, iou_threshold)
184
 
185
- for box, confidence in nms_boxes:
186
- x1, y1, x2, y2 = box
187
- w, h = x2 - x1, y2 - y1
188
- x1 = max(0, x1 - w * 0.05)
189
- y1 = max(0, y1 - h * 0.05)
190
- x2 = min(image.width, x2 + w * 0.05)
191
- y2 = min(image.height, y2 + h * 0.05)
192
- cropped_image = image.crop((x1, y1, x2, y2))
193
- dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
194
 
195
- return dogs
196
 
197
 
198
  def non_max_suppression(boxes, iou_threshold):
@@ -266,16 +266,87 @@ async def process_single_dog(image):
266
  return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
267
 
268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  async def predict(image):
270
  if image is None:
271
- return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
272
 
273
  try:
274
  if isinstance(image, np.ndarray):
275
  image = Image.fromarray(image)
276
 
277
- dogs = await detect_multiple_dogs(image)
278
-
 
 
 
 
279
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
280
  explanations = []
281
  buttons = []
@@ -307,25 +378,9 @@ async def predict(image):
307
  final_explanation = "\n\n".join(explanations)
308
  if buttons:
309
  final_explanation += "\n\nClick on a button to view more information about the breed."
310
- initial_state = {
311
- "explanation": final_explanation,
312
- "buttons": buttons,
313
- "show_back": True,
314
- "image": annotated_image,
315
- "is_multi_dog": len(dogs) > 1,
316
- "dogs_info": explanations
317
- }
318
- return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), initial_state
319
  else:
320
- initial_state = {
321
- "explanation": final_explanation,
322
- "buttons": [],
323
- "show_back": False,
324
- "image": annotated_image,
325
- "is_multi_dog": len(dogs) > 1,
326
- "dogs_info": explanations
327
- }
328
- return final_explanation, annotated_image, gr.update(visible=False, choices=[]), initial_state
329
 
330
  except Exception as e:
331
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
 
167
  return top1_prob, topk_breeds, topk_probs_percent
168
 
169
 
170
+ # async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.6):
171
+ # results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
172
+ # dogs = []
173
+ # boxes = []
174
+ # for box in results.boxes:
175
+ # if box.cls == 16: # COCO dataset class for dog is 16
176
+ # xyxy = box.xyxy[0].tolist()
177
+ # confidence = box.conf.item()
178
+ # boxes.append((xyxy, confidence))
179
 
180
+ # if not boxes:
181
+ # dogs.append((image, 1.0, [0, 0, image.width, image.height]))
182
+ # else:
183
+ # nms_boxes = non_max_suppression(boxes, iou_threshold)
184
 
185
+ # for box, confidence in nms_boxes:
186
+ # x1, y1, x2, y2 = box
187
+ # w, h = x2 - x1, y2 - y1
188
+ # x1 = max(0, x1 - w * 0.05)
189
+ # y1 = max(0, y1 - h * 0.05)
190
+ # x2 = min(image.width, x2 + w * 0.05)
191
+ # y2 = min(image.height, y2 + h * 0.05)
192
+ # cropped_image = image.crop((x1, y1, x2, y2))
193
+ # dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
194
 
195
+ # return dogs
196
 
197
 
198
  def non_max_suppression(boxes, iou_threshold):
 
266
  return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
267
 
268
 
269
+ # async def predict(image):
270
+ # if image is None:
271
+ # return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
272
+
273
+ # try:
274
+ # if isinstance(image, np.ndarray):
275
+ # image = Image.fromarray(image)
276
+
277
+ # dogs = await detect_multiple_dogs(image)
278
+
279
+ # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
280
+ # explanations = []
281
+ # buttons = []
282
+ # annotated_image = image.copy()
283
+ # draw = ImageDraw.Draw(annotated_image)
284
+ # font = ImageFont.load_default()
285
+
286
+ # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
287
+ # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
288
+ # color = color_list[i % len(color_list)]
289
+ # draw.rectangle(box, outline=color, width=3)
290
+ # draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
291
+
292
+ # combined_confidence = detection_confidence * top1_prob
293
+
294
+ # if top1_prob >= 0.5:
295
+ # breed = topk_breeds[0]
296
+ # description = get_dog_description(breed)
297
+ # formatted_description = format_description(description, breed)
298
+ # explanations.append(f"Dog {i+1}: {formatted_description}")
299
+ # elif combined_confidence >= 0.2:
300
+ # dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
301
+ # dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
302
+ # explanations.append(dog_explanation)
303
+ # buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
304
+ # else:
305
+ # explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image.")
306
+
307
+ # final_explanation = "\n\n".join(explanations)
308
+ # if buttons:
309
+ # final_explanation += "\n\nClick on a button to view more information about the breed."
310
+ # initial_state = {
311
+ # "explanation": final_explanation,
312
+ # "buttons": buttons,
313
+ # "show_back": True,
314
+ # "image": annotated_image,
315
+ # "is_multi_dog": len(dogs) > 1,
316
+ # "dogs_info": explanations
317
+ # }
318
+ # return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), initial_state
319
+ # else:
320
+ # initial_state = {
321
+ # "explanation": final_explanation,
322
+ # "buttons": [],
323
+ # "show_back": False,
324
+ # "image": annotated_image,
325
+ # "is_multi_dog": len(dogs) > 1,
326
+ # "dogs_info": explanations
327
+ # }
328
+ # return final_explanation, annotated_image, gr.update(visible=False, choices=[]), initial_state
329
+
330
+ # except Exception as e:
331
+ # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
332
+ # print(error_msg)
333
+ # return error_msg, None, gr.update(visible=False, choices=[]), None
334
+
335
+
336
  async def predict(image):
337
  if image is None:
338
+ return "Please upload an image to start.", None, gr.update(visible=False), None
339
 
340
  try:
341
  if isinstance(image, np.ndarray):
342
  image = Image.fromarray(image)
343
 
344
+ # 根據圖像尺寸判斷是否應該使用YOLO框選
345
+ if image.width > 500: # 假設寬度超過500像素才考慮多狗情況
346
+ dogs = await detect_multiple_dogs(image)
347
+ else:
348
+ dogs = [(image, 1.0, [0, 0, image.width, image.height])]
349
+
350
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
351
  explanations = []
352
  buttons = []
 
378
  final_explanation = "\n\n".join(explanations)
379
  if buttons:
380
  final_explanation += "\n\nClick on a button to view more information about the breed."
381
+ return final_explanation, annotated_image, gr.update(visible=True, choices=buttons)
 
 
 
 
 
 
 
 
382
  else:
383
+ return final_explanation, annotated_image, gr.update(visible=False, choices=[])
 
 
 
 
 
 
 
 
384
 
385
  except Exception as e:
386
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"