DawnC commited on
Commit
bc16cb9
1 Parent(s): 9107a82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -53
app.py CHANGED
@@ -167,34 +167,56 @@ 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):
199
  keep = []
200
  boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
@@ -266,9 +288,76 @@ 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):
@@ -276,62 +365,30 @@ async def predict(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
 
337
  def show_details(choice, previous_output, initial_state):
 
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
+ async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.5):
198
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
199
  dogs = []
200
  boxes = []
201
  for box in results.boxes:
202
+ if box.cls == 16: # 狗類別
203
  xyxy = box.xyxy[0].tolist()
204
  confidence = box.conf.item()
205
  boxes.append((xyxy, confidence))
206
 
207
  if not boxes:
208
+ # 當沒檢測到狗時,使用完整圖片
209
  dogs.append((image, 1.0, [0, 0, image.width, image.height]))
210
  else:
211
  nms_boxes = non_max_suppression(boxes, iou_threshold)
 
212
  for box, confidence in nms_boxes:
213
  x1, y1, x2, y2 = box
 
 
 
 
 
214
  cropped_image = image.crop((x1, y1, x2, y2))
215
  dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
 
216
  return dogs
217
 
218
 
219
+
220
  def non_max_suppression(boxes, iou_threshold):
221
  keep = []
222
  boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
 
288
  return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
289
 
290
 
291
+ # async def predict(image):
292
+ # if image is None:
293
+ # return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
294
+
295
+ # try:
296
+ # if isinstance(image, np.ndarray):
297
+ # image = Image.fromarray(image)
298
+
299
+ # dogs = await detect_multiple_dogs(image)
300
+
301
+ # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
302
+ # explanations = []
303
+ # buttons = []
304
+ # annotated_image = image.copy()
305
+ # draw = ImageDraw.Draw(annotated_image)
306
+ # font = ImageFont.load_default()
307
+
308
+ # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
309
+ # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
310
+ # color = color_list[i % len(color_list)]
311
+ # draw.rectangle(box, outline=color, width=3)
312
+ # draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
313
+
314
+ # combined_confidence = detection_confidence * top1_prob
315
+
316
+ # if top1_prob >= 0.5:
317
+ # breed = topk_breeds[0]
318
+ # description = get_dog_description(breed)
319
+ # formatted_description = format_description(description, breed)
320
+ # explanations.append(f"Dog {i+1}: {formatted_description}")
321
+ # elif combined_confidence >= 0.2:
322
+ # dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
323
+ # dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
324
+ # explanations.append(dog_explanation)
325
+ # buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
326
+ # else:
327
+ # explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image.")
328
+
329
+ # final_explanation = "\n\n".join(explanations)
330
+ # if buttons:
331
+ # final_explanation += "\n\nClick on a button to view more information about the breed."
332
+ # initial_state = {
333
+ # "explanation": final_explanation,
334
+ # "buttons": buttons,
335
+ # "show_back": True,
336
+ # "image": annotated_image,
337
+ # "is_multi_dog": len(dogs) > 1,
338
+ # "dogs_info": explanations
339
+ # }
340
+ # return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), initial_state
341
+ # else:
342
+ # initial_state = {
343
+ # "explanation": final_explanation,
344
+ # "buttons": [],
345
+ # "show_back": False,
346
+ # "image": annotated_image,
347
+ # "is_multi_dog": len(dogs) > 1,
348
+ # "dogs_info": explanations
349
+ # }
350
+ # return final_explanation, annotated_image, gr.update(visible=False, choices=[]), initial_state
351
+
352
+ # except Exception as e:
353
+ # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
354
+ # print(error_msg)
355
+ # return error_msg, None, gr.update(visible=False, choices=[]), None
356
+
357
+
358
  async def predict(image):
359
  if image is None:
360
+ return "Please upload an image to start.", None, gr.update(visible=False), None
361
 
362
  try:
363
  if isinstance(image, np.ndarray):
 
365
 
366
  dogs = await detect_multiple_dogs(image)
367
 
 
368
  explanations = []
 
369
  annotated_image = image.copy()
370
  draw = ImageDraw.Draw(annotated_image)
371
+
372
+ # 針對每隻狗進行辨識
373
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
374
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
375
+ draw.rectangle(box, outline="#FF0000", width=3) # 標記框選
 
 
 
376
  combined_confidence = detection_confidence * top1_prob
377
 
378
+ if combined_confidence >= 0.5:
379
  breed = topk_breeds[0]
380
  description = get_dog_description(breed)
381
+ explanations.append(f"Dog {i+1}: {description}")
 
 
 
 
 
 
382
  else:
383
+ explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
384
+
385
+ # 返回最終解釋與圖片
386
+ return "\n".join(explanations), annotated_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
 
388
  except Exception as e:
389
+ error_msg = f"Error: {str(e)}"
390
  print(error_msg)
391
+ return error_msg, None
 
392
 
393
 
394
  def show_details(choice, previous_output, initial_state):