Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -192,6 +192,7 @@ async def predict_single_dog(image):
|
|
| 192 |
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
| 193 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
|
|
|
| 195 |
|
| 196 |
async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.45):
|
| 197 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
|
@@ -207,8 +208,8 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.45):
|
|
| 207 |
if not boxes:
|
| 208 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 209 |
else:
|
| 210 |
-
#
|
| 211 |
-
sorted_boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
| 212 |
|
| 213 |
for box, confidence in sorted_boxes:
|
| 214 |
x1, y1, x2, y2 = box
|
|
@@ -223,41 +224,6 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.45):
|
|
| 223 |
|
| 224 |
return dogs
|
| 225 |
|
| 226 |
-
def merge_boxes(boxes, iou_threshold=0.5):
|
| 227 |
-
merged = []
|
| 228 |
-
while boxes:
|
| 229 |
-
base_box = boxes.pop(0)
|
| 230 |
-
i = 0
|
| 231 |
-
while i < len(boxes):
|
| 232 |
-
if calculate_iou(base_box[0], boxes[i][0]) > iou_threshold:
|
| 233 |
-
base_box = merge_two_boxes(base_box, boxes.pop(i))
|
| 234 |
-
else:
|
| 235 |
-
i += 1
|
| 236 |
-
merged.append(base_box)
|
| 237 |
-
return merged
|
| 238 |
-
|
| 239 |
-
def calculate_iou(box1, box2):
|
| 240 |
-
x1 = max(box1[0], box2[0])
|
| 241 |
-
y1 = max(box1[1], box2[1])
|
| 242 |
-
x2 = min(box1[2], box2[2])
|
| 243 |
-
y2 = min(box1[3], box2[3])
|
| 244 |
-
|
| 245 |
-
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
| 246 |
-
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
| 247 |
-
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
| 248 |
-
|
| 249 |
-
iou = intersection / float(area1 + area2 - intersection)
|
| 250 |
-
return iou
|
| 251 |
-
|
| 252 |
-
def merge_two_boxes(box1, box2):
|
| 253 |
-
return (
|
| 254 |
-
[min(box1[0][0], box2[0][0]),
|
| 255 |
-
min(box1[0][1], box2[0][1]),
|
| 256 |
-
max(box1[0][2], box2[0][2]),
|
| 257 |
-
max(box1[0][3], box2[0][3])],
|
| 258 |
-
max(box1[1], box2[1]) # 取較高的置信度
|
| 259 |
-
)
|
| 260 |
-
|
| 261 |
|
| 262 |
async def process_single_dog(image):
|
| 263 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
|
@@ -456,6 +422,70 @@ async def process_single_dog(image):
|
|
| 456 |
# iface.launch()
|
| 457 |
|
| 458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
async def predict(image):
|
| 460 |
if image is None:
|
| 461 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
@@ -490,7 +520,10 @@ async def predict(image):
|
|
| 490 |
explanations.append(dog_explanation)
|
| 491 |
buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
|
| 492 |
else:
|
| 493 |
-
|
|
|
|
|
|
|
|
|
|
| 494 |
|
| 495 |
final_explanation = "\n\n".join(explanations)
|
| 496 |
if buttons:
|
|
|
|
| 192 |
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
| 193 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
+
|
| 196 |
|
| 197 |
async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.45):
|
| 198 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
|
|
|
| 208 |
if not boxes:
|
| 209 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 210 |
else:
|
| 211 |
+
# 按置信度排序並選擇所有框
|
| 212 |
+
sorted_boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
| 213 |
|
| 214 |
for box, confidence in sorted_boxes:
|
| 215 |
x1, y1, x2, y2 = box
|
|
|
|
| 224 |
|
| 225 |
return dogs
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
async def process_single_dog(image):
|
| 229 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
|
|
|
| 422 |
# iface.launch()
|
| 423 |
|
| 424 |
|
| 425 |
+
# async def predict(image):
|
| 426 |
+
# if image is None:
|
| 427 |
+
# return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
| 428 |
+
|
| 429 |
+
# try:
|
| 430 |
+
# if isinstance(image, np.ndarray):
|
| 431 |
+
# image = Image.fromarray(image)
|
| 432 |
+
|
| 433 |
+
# dogs = await detect_multiple_dogs(image)
|
| 434 |
+
|
| 435 |
+
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 436 |
+
# explanations = []
|
| 437 |
+
# buttons = []
|
| 438 |
+
# annotated_image = image.copy()
|
| 439 |
+
# draw = ImageDraw.Draw(annotated_image)
|
| 440 |
+
# font = ImageFont.load_default()
|
| 441 |
+
|
| 442 |
+
# for i, (cropped_image, _, box) in enumerate(dogs):
|
| 443 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
| 444 |
+
# color = color_list[i % len(color_list)]
|
| 445 |
+
# draw.rectangle(box, outline=color, width=3)
|
| 446 |
+
# draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
| 447 |
+
|
| 448 |
+
# if top1_prob >= 0.5:
|
| 449 |
+
# breed = topk_breeds[0]
|
| 450 |
+
# description = get_dog_description(breed)
|
| 451 |
+
# formatted_description = format_description(description, breed)
|
| 452 |
+
# explanations.append(f"Dog {i+1}: {formatted_description}")
|
| 453 |
+
# elif top1_prob >= 0.2:
|
| 454 |
+
# dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
| 455 |
+
# dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 456 |
+
# explanations.append(dog_explanation)
|
| 457 |
+
# buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
|
| 458 |
+
# else:
|
| 459 |
+
# explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
| 460 |
+
|
| 461 |
+
# final_explanation = "\n\n".join(explanations)
|
| 462 |
+
# if buttons:
|
| 463 |
+
# final_explanation += "\n\nClick on a button to view more information about the breed."
|
| 464 |
+
# initial_state = {
|
| 465 |
+
# "explanation": final_explanation,
|
| 466 |
+
# "buttons": buttons,
|
| 467 |
+
# "show_back": True,
|
| 468 |
+
# "image": annotated_image,
|
| 469 |
+
# "is_multi_dog": len(dogs) > 1,
|
| 470 |
+
# "dogs_info": explanations
|
| 471 |
+
# }
|
| 472 |
+
# return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
| 473 |
+
# else:
|
| 474 |
+
# initial_state = {
|
| 475 |
+
# "explanation": final_explanation,
|
| 476 |
+
# "buttons": [],
|
| 477 |
+
# "show_back": False,
|
| 478 |
+
# "image": annotated_image,
|
| 479 |
+
# "is_multi_dog": len(dogs) > 1,
|
| 480 |
+
# "dogs_info": explanations
|
| 481 |
+
# }
|
| 482 |
+
# return final_explanation, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
| 483 |
+
|
| 484 |
+
# except Exception as e:
|
| 485 |
+
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 486 |
+
# print(error_msg)
|
| 487 |
+
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
| 488 |
+
|
| 489 |
async def predict(image):
|
| 490 |
if image is None:
|
| 491 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
|
| 520 |
explanations.append(dog_explanation)
|
| 521 |
buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
|
| 522 |
else:
|
| 523 |
+
if len(dogs) == 1:
|
| 524 |
+
explanations.append("The image is unclear or does not contain a recognized dog breed.")
|
| 525 |
+
else:
|
| 526 |
+
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
| 527 |
|
| 528 |
final_explanation = "\n\n".join(explanations)
|
| 529 |
if buttons:
|