Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -288,76 +288,9 @@ async def process_single_dog(image):
|
|
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,30 +298,61 @@ async def predict(image):
|
|
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 |
-
|
|
|
|
|
|
|
376 |
combined_confidence = detection_confidence * top1_prob
|
377 |
|
378 |
-
if
|
379 |
breed = topk_breeds[0]
|
380 |
description = get_dog_description(breed)
|
381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
else:
|
383 |
-
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
384 |
-
|
385 |
-
|
386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
except Exception as e:
|
389 |
-
error_msg = f"
|
390 |
print(error_msg)
|
391 |
-
return error_msg, None
|
392 |
|
393 |
|
394 |
def show_details(choice, previous_output, initial_state):
|
|
|
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):
|
|
|
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 |
def show_details(choice, previous_output, initial_state):
|