Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -176,6 +176,7 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
|
| 176 |
dogs.append((cropped_image, confidence, xyxy))
|
| 177 |
return dogs
|
| 178 |
|
|
|
|
| 179 |
async def process_single_dog(image):
|
| 180 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
| 181 |
if top1_prob < 0.2:
|
|
@@ -183,7 +184,8 @@ async def process_single_dog(image):
|
|
| 183 |
"explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
|
| 184 |
"buttons": [],
|
| 185 |
"show_back": False,
|
| 186 |
-
"image": None
|
|
|
|
| 187 |
}
|
| 188 |
return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 189 |
|
|
@@ -196,7 +198,8 @@ async def process_single_dog(image):
|
|
| 196 |
"explanation": formatted_description,
|
| 197 |
"buttons": [],
|
| 198 |
"show_back": False,
|
| 199 |
-
"image": image
|
|
|
|
| 200 |
}
|
| 201 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 202 |
else:
|
|
@@ -216,7 +219,8 @@ async def process_single_dog(image):
|
|
| 216 |
"explanation": explanation,
|
| 217 |
"buttons": buttons,
|
| 218 |
"show_back": True,
|
| 219 |
-
"image": image
|
|
|
|
| 220 |
}
|
| 221 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
| 222 |
|
|
@@ -230,20 +234,20 @@ async def predict(image):
|
|
| 230 |
|
| 231 |
if len(dogs) <= 1:
|
| 232 |
return await process_single_dog(image)
|
| 233 |
-
|
| 234 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 235 |
explanations = []
|
| 236 |
buttons = []
|
| 237 |
annotated_image = image.copy()
|
| 238 |
draw = ImageDraw.Draw(annotated_image)
|
| 239 |
font = ImageFont.load_default()
|
| 240 |
-
|
| 241 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
| 242 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
| 243 |
color = color_list[i % len(color_list)]
|
| 244 |
draw.rectangle(box, outline=color, width=3)
|
| 245 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
| 246 |
-
|
| 247 |
breed = topk_breeds[0]
|
| 248 |
if top1_prob >= 0.5:
|
| 249 |
description = get_dog_description(breed)
|
|
@@ -256,7 +260,7 @@ async def predict(image):
|
|
| 256 |
buttons.extend([gr.update(visible=True, value=f"Dog {i+1}: More about {breed}") for breed in topk_breeds[:3]])
|
| 257 |
else:
|
| 258 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
| 259 |
-
|
| 260 |
final_explanation = "\n\n".join(explanations)
|
| 261 |
if buttons:
|
| 262 |
final_explanation += "\n\nClick on a button to view more information about the breed."
|
|
@@ -264,7 +268,9 @@ async def predict(image):
|
|
| 264 |
"explanation": final_explanation,
|
| 265 |
"buttons": buttons,
|
| 266 |
"show_back": True,
|
| 267 |
-
"image": annotated_image
|
|
|
|
|
|
|
| 268 |
}
|
| 269 |
return (final_explanation, annotated_image,
|
| 270 |
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
|
@@ -277,7 +283,9 @@ async def predict(image):
|
|
| 277 |
"explanation": final_explanation,
|
| 278 |
"buttons": [],
|
| 279 |
"show_back": False,
|
| 280 |
-
"image": annotated_image
|
|
|
|
|
|
|
| 281 |
}
|
| 282 |
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 283 |
except Exception as e:
|
|
@@ -286,6 +294,7 @@ async def predict(image):
|
|
| 286 |
return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
| 287 |
|
| 288 |
|
|
|
|
| 289 |
# async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4, merge_threshold=0.5):
|
| 290 |
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 291 |
# dogs = []
|
|
@@ -446,24 +455,38 @@ def show_details(choice, previous_output, initial_state):
|
|
| 446 |
breed = choice.split("More about ")[-1]
|
| 447 |
description = get_dog_description(breed)
|
| 448 |
formatted_description = format_description(description, breed)
|
| 449 |
-
initial_state["
|
|
|
|
| 450 |
return formatted_description, gr.update(visible=True), initial_state
|
| 451 |
except Exception as e:
|
| 452 |
error_msg = f"An error occurred while showing details: {e}"
|
| 453 |
-
print(error_msg)
|
| 454 |
return error_msg, gr.update(visible=True), initial_state
|
| 455 |
|
| 456 |
def go_back(state):
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
state
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
with gr.Blocks() as iface:
|
| 469 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
|
|
|
| 176 |
dogs.append((cropped_image, confidence, xyxy))
|
| 177 |
return dogs
|
| 178 |
|
| 179 |
+
|
| 180 |
async def process_single_dog(image):
|
| 181 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
| 182 |
if top1_prob < 0.2:
|
|
|
|
| 184 |
"explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
|
| 185 |
"buttons": [],
|
| 186 |
"show_back": False,
|
| 187 |
+
"image": None,
|
| 188 |
+
"is_multi_dog": False
|
| 189 |
}
|
| 190 |
return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 191 |
|
|
|
|
| 198 |
"explanation": formatted_description,
|
| 199 |
"buttons": [],
|
| 200 |
"show_back": False,
|
| 201 |
+
"image": image,
|
| 202 |
+
"is_multi_dog": False
|
| 203 |
}
|
| 204 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 205 |
else:
|
|
|
|
| 219 |
"explanation": explanation,
|
| 220 |
"buttons": buttons,
|
| 221 |
"show_back": True,
|
| 222 |
+
"image": image,
|
| 223 |
+
"is_multi_dog": False
|
| 224 |
}
|
| 225 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
| 226 |
|
|
|
|
| 234 |
|
| 235 |
if len(dogs) <= 1:
|
| 236 |
return await process_single_dog(image)
|
| 237 |
+
|
| 238 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 239 |
explanations = []
|
| 240 |
buttons = []
|
| 241 |
annotated_image = image.copy()
|
| 242 |
draw = ImageDraw.Draw(annotated_image)
|
| 243 |
font = ImageFont.load_default()
|
| 244 |
+
|
| 245 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
| 246 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
| 247 |
color = color_list[i % len(color_list)]
|
| 248 |
draw.rectangle(box, outline=color, width=3)
|
| 249 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
| 250 |
+
|
| 251 |
breed = topk_breeds[0]
|
| 252 |
if top1_prob >= 0.5:
|
| 253 |
description = get_dog_description(breed)
|
|
|
|
| 260 |
buttons.extend([gr.update(visible=True, value=f"Dog {i+1}: More about {breed}") for breed in topk_breeds[:3]])
|
| 261 |
else:
|
| 262 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
| 263 |
+
|
| 264 |
final_explanation = "\n\n".join(explanations)
|
| 265 |
if buttons:
|
| 266 |
final_explanation += "\n\nClick on a button to view more information about the breed."
|
|
|
|
| 268 |
"explanation": final_explanation,
|
| 269 |
"buttons": buttons,
|
| 270 |
"show_back": True,
|
| 271 |
+
"image": annotated_image,
|
| 272 |
+
"is_multi_dog": True,
|
| 273 |
+
"dogs_info": explanations
|
| 274 |
}
|
| 275 |
return (final_explanation, annotated_image,
|
| 276 |
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
|
|
|
| 283 |
"explanation": final_explanation,
|
| 284 |
"buttons": [],
|
| 285 |
"show_back": False,
|
| 286 |
+
"image": annotated_image,
|
| 287 |
+
"is_multi_dog": True,
|
| 288 |
+
"dogs_info": explanations
|
| 289 |
}
|
| 290 |
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 291 |
except Exception as e:
|
|
|
|
| 294 |
return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
| 295 |
|
| 296 |
|
| 297 |
+
|
| 298 |
# async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4, merge_threshold=0.5):
|
| 299 |
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 300 |
# dogs = []
|
|
|
|
| 455 |
breed = choice.split("More about ")[-1]
|
| 456 |
description = get_dog_description(breed)
|
| 457 |
formatted_description = format_description(description, breed)
|
| 458 |
+
initial_state["current_description"] = formatted_description # ไฟๅญ็ถๅ้กฏ็คบ็ๆ่ฟฐ
|
| 459 |
+
initial_state["show_back"] = True # ็ขบไฟ back ๆ้ๅฏ่ฆ
|
| 460 |
return formatted_description, gr.update(visible=True), initial_state
|
| 461 |
except Exception as e:
|
| 462 |
error_msg = f"An error occurred while showing details: {e}"
|
| 463 |
+
print(error_msg)
|
| 464 |
return error_msg, gr.update(visible=True), initial_state
|
| 465 |
|
| 466 |
def go_back(state):
|
| 467 |
+
if state.get("is_multi_dog", False):
|
| 468 |
+
# ๆขๅพฉๅฐๅค็ๆ
ๅข็ๅๅง็ๆ
|
| 469 |
+
buttons = state.get("buttons", [])
|
| 470 |
+
return (
|
| 471 |
+
state["explanation"],
|
| 472 |
+
state["image"],
|
| 473 |
+
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
| 474 |
+
buttons[1] if len(buttons) > 1 else gr.update(visible=False),
|
| 475 |
+
buttons[2] if len(buttons) > 2 else gr.update(visible=False),
|
| 476 |
+
gr.update(visible=False), # ้ฑ่ back ๆ้
|
| 477 |
+
state
|
| 478 |
+
)
|
| 479 |
+
else:
|
| 480 |
+
# ๅฎ็ๆ
ๅข๏ผไธ้่ฆ็นๆฎ่็
|
| 481 |
+
return (
|
| 482 |
+
state["explanation"],
|
| 483 |
+
state["image"],
|
| 484 |
+
gr.update(visible=False),
|
| 485 |
+
gr.update(visible=False),
|
| 486 |
+
gr.update(visible=False),
|
| 487 |
+
gr.update(visible=False),
|
| 488 |
+
state
|
| 489 |
+
)
|
| 490 |
|
| 491 |
with gr.Blocks() as iface:
|
| 492 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|