Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -251,9 +251,10 @@ def get_akc_breeds_link():
|
|
251 |
|
252 |
|
253 |
|
|
|
254 |
def format_description(description, breed):
|
255 |
if isinstance(description, dict):
|
256 |
-
#
|
257 |
formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
258 |
else:
|
259 |
formatted_description = description
|
@@ -270,12 +271,9 @@ Please refer to the AKC's terms of use and privacy policy.*
|
|
270 |
"""
|
271 |
return formatted_description
|
272 |
|
273 |
-
|
274 |
-
|
275 |
async def predict_single_dog(image):
|
276 |
return await asyncio.to_thread(_predict_single_dog, image)
|
277 |
|
278 |
-
|
279 |
def _predict_single_dog(image):
|
280 |
image_tensor = preprocess_image(image)
|
281 |
with torch.no_grad():
|
@@ -288,13 +286,11 @@ def _predict_single_dog(image):
|
|
288 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
289 |
return top1_prob, topk_breeds, topk_probs_percent
|
290 |
|
291 |
-
|
292 |
-
async def detect_multiple_dogs(image):
|
293 |
-
return await asyncio.to_thread(_detect_multiple_dogs, image)
|
294 |
-
|
295 |
-
|
296 |
-
def _detect_multiple_dogs(image, conf_threshold=0.3):
|
297 |
# 調整 YOLO 模型的置信度閾值
|
|
|
|
|
|
|
298 |
results = model_yolo(image, conf=conf_threshold)
|
299 |
dogs = []
|
300 |
for result in results:
|
@@ -307,8 +303,6 @@ def _detect_multiple_dogs(image, conf_threshold=0.3):
|
|
307 |
dogs.append((cropped_image, confidence, xyxy))
|
308 |
return dogs
|
309 |
|
310 |
-
|
311 |
-
|
312 |
async def predict(image):
|
313 |
if image is None:
|
314 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -320,7 +314,6 @@ async def predict(image):
|
|
320 |
# 偵測圖片中的多個狗
|
321 |
dogs = await detect_multiple_dogs(image)
|
322 |
|
323 |
-
# 單一狗的情況
|
324 |
if len(dogs) == 0:
|
325 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
326 |
if top1_prob < 0.2:
|
@@ -330,7 +323,7 @@ async def predict(image):
|
|
330 |
formatted_description = format_description(description, breed)
|
331 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
332 |
|
333 |
-
#
|
334 |
if len(dogs) == 1:
|
335 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
336 |
breed = topk_breeds[0]
|
@@ -338,7 +331,7 @@ async def predict(image):
|
|
338 |
formatted_description = format_description(description, breed)
|
339 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
340 |
|
341 |
-
#
|
342 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
343 |
explanations = []
|
344 |
visible_buttons = []
|
@@ -346,16 +339,16 @@ async def predict(image):
|
|
346 |
draw = ImageDraw.Draw(annotated_image)
|
347 |
font = ImageFont.load_default()
|
348 |
|
349 |
-
#
|
350 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
351 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
352 |
|
353 |
-
#
|
354 |
-
color = color_list[i % len(color_list)]
|
355 |
draw.rectangle(box, outline=color, width=3)
|
356 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
357 |
|
358 |
-
#
|
359 |
if top1_prob >= 0.5:
|
360 |
breed = topk_breeds[0]
|
361 |
description = get_dog_description(breed)
|
@@ -379,8 +372,6 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
|
|
379 |
except Exception as e:
|
380 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
381 |
|
382 |
-
|
383 |
-
|
384 |
async def show_details(choice):
|
385 |
if not choice:
|
386 |
return "Please select a breed to view details."
|
@@ -397,6 +388,7 @@ async def show_details(choice):
|
|
397 |
|
398 |
|
399 |
|
|
|
400 |
with gr.Blocks(css="""
|
401 |
.container { max-width: 900px; margin: auto; padding: 20px; }
|
402 |
.gr-box { border-radius: 15px; }
|
|
|
251 |
|
252 |
|
253 |
|
254 |
+
# Update the format_description to handle descriptions more cleanly
|
255 |
def format_description(description, breed):
|
256 |
if isinstance(description, dict):
|
257 |
+
# 確保每一個描述項目換行顯示,並避免重複顯示 Breed
|
258 |
formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
259 |
else:
|
260 |
formatted_description = description
|
|
|
271 |
"""
|
272 |
return formatted_description
|
273 |
|
|
|
|
|
274 |
async def predict_single_dog(image):
|
275 |
return await asyncio.to_thread(_predict_single_dog, image)
|
276 |
|
|
|
277 |
def _predict_single_dog(image):
|
278 |
image_tensor = preprocess_image(image)
|
279 |
with torch.no_grad():
|
|
|
286 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
287 |
return top1_prob, topk_breeds, topk_probs_percent
|
288 |
|
289 |
+
async def detect_multiple_dogs(image, conf_threshold=0.3):
|
|
|
|
|
|
|
|
|
|
|
290 |
# 調整 YOLO 模型的置信度閾值
|
291 |
+
return await asyncio.to_thread(_detect_multiple_dogs, image, conf_threshold)
|
292 |
+
|
293 |
+
def _detect_multiple_dogs(image, conf_threshold):
|
294 |
results = model_yolo(image, conf=conf_threshold)
|
295 |
dogs = []
|
296 |
for result in results:
|
|
|
303 |
dogs.append((cropped_image, confidence, xyxy))
|
304 |
return dogs
|
305 |
|
|
|
|
|
306 |
async def predict(image):
|
307 |
if image is None:
|
308 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
314 |
# 偵測圖片中的多個狗
|
315 |
dogs = await detect_multiple_dogs(image)
|
316 |
|
|
|
317 |
if len(dogs) == 0:
|
318 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
319 |
if top1_prob < 0.2:
|
|
|
323 |
formatted_description = format_description(description, breed)
|
324 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
325 |
|
326 |
+
# 處理單一狗情況
|
327 |
if len(dogs) == 1:
|
328 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
329 |
breed = topk_breeds[0]
|
|
|
331 |
formatted_description = format_description(description, breed)
|
332 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
333 |
|
334 |
+
# 多狗處理
|
335 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
336 |
explanations = []
|
337 |
visible_buttons = []
|
|
|
339 |
draw = ImageDraw.Draw(annotated_image)
|
340 |
font = ImageFont.load_default()
|
341 |
|
342 |
+
# 遍歷每一隻狗
|
343 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
344 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
345 |
|
346 |
+
# 繪製方框
|
347 |
+
color = color_list[i % len(color_list)]
|
348 |
draw.rectangle(box, outline=color, width=3)
|
349 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
350 |
|
351 |
+
# 高置信度返回
|
352 |
if top1_prob >= 0.5:
|
353 |
breed = topk_breeds[0]
|
354 |
description = get_dog_description(breed)
|
|
|
372 |
except Exception as e:
|
373 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
374 |
|
|
|
|
|
375 |
async def show_details(choice):
|
376 |
if not choice:
|
377 |
return "Please select a breed to view details."
|
|
|
388 |
|
389 |
|
390 |
|
391 |
+
|
392 |
with gr.Blocks(css="""
|
393 |
.container { max-width: 900px; margin: auto; padding: 20px; }
|
394 |
.gr-box { border-radius: 15px; }
|