Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -249,10 +249,9 @@ def get_akc_breeds_link():
|
|
249 |
# if __name__ == "__main__":
|
250 |
# iface.launch()
|
251 |
|
252 |
-
|
253 |
-
|
254 |
def format_description(description, breed):
|
255 |
if isinstance(description, dict):
|
|
|
256 |
formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
257 |
else:
|
258 |
formatted_description = description
|
@@ -268,39 +267,6 @@ def format_description(description, breed):
|
|
268 |
|
269 |
return formatted_description
|
270 |
|
271 |
-
|
272 |
-
async def predict_single_dog(image):
|
273 |
-
return await asyncio.to_thread(_predict_single_dog, image)
|
274 |
-
|
275 |
-
def _predict_single_dog(image):
|
276 |
-
image_tensor = preprocess_image(image)
|
277 |
-
with torch.no_grad():
|
278 |
-
output = model(image_tensor)
|
279 |
-
logits = output[0] if isinstance(output, tuple) else output
|
280 |
-
probabilities = F.softmax(logits, dim=1)
|
281 |
-
topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
282 |
-
top1_prob = topk_probs[0][0].item()
|
283 |
-
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
284 |
-
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
285 |
-
return top1_prob, topk_breeds, topk_probs_percent
|
286 |
-
|
287 |
-
async def detect_multiple_dogs(image, conf_threshold=0.3):
|
288 |
-
# 調整 YOLO 模型的置信度閾值
|
289 |
-
return await asyncio.to_thread(_detect_multiple_dogs, image, conf_threshold)
|
290 |
-
|
291 |
-
def _detect_multiple_dogs(image, conf_threshold):
|
292 |
-
results = model_yolo(image, conf=conf_threshold)
|
293 |
-
dogs = []
|
294 |
-
for result in results:
|
295 |
-
for box in result.boxes:
|
296 |
-
if box.cls == 16: # COCO 資料集中狗的類別是 16
|
297 |
-
xyxy = box.xyxy[0].tolist()
|
298 |
-
confidence = box.conf.item()
|
299 |
-
if confidence >= conf_threshold: # 只保留置信度高於閾值的框
|
300 |
-
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
301 |
-
dogs.append((cropped_image, confidence, xyxy))
|
302 |
-
return dogs
|
303 |
-
|
304 |
async def predict(image):
|
305 |
if image is None:
|
306 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -333,7 +299,7 @@ async def predict(image):
|
|
333 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
334 |
"Click on a button to view more information about the breed."
|
335 |
)
|
336 |
-
return explanation, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
337 |
|
338 |
# 多狗處理
|
339 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
@@ -365,13 +331,15 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
|
|
365 |
3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
|
366 |
"""
|
367 |
explanations.append(explanation)
|
368 |
-
visible_buttons.
|
|
|
|
|
369 |
else:
|
370 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
371 |
|
372 |
final_explanation = "\n\n".join(explanations)
|
373 |
|
374 |
-
return final_explanation, annotated_image,
|
375 |
|
376 |
except Exception as e:
|
377 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -391,6 +359,7 @@ async def show_details(choice):
|
|
391 |
return f"An error occurred while showing details: {e}"
|
392 |
|
393 |
|
|
|
394 |
with gr.Blocks(css="""
|
395 |
.container { max-width: 900px; margin: auto; padding: 20px; }
|
396 |
.gr-box { border-radius: 15px; }
|
|
|
249 |
# if __name__ == "__main__":
|
250 |
# iface.launch()
|
251 |
|
|
|
|
|
252 |
def format_description(description, breed):
|
253 |
if isinstance(description, dict):
|
254 |
+
# 確保每一個描述項目換行顯示
|
255 |
formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
256 |
else:
|
257 |
formatted_description = description
|
|
|
267 |
|
268 |
return formatted_description
|
269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
async def predict(image):
|
271 |
if image is None:
|
272 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
299 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
300 |
"Click on a button to view more information about the breed."
|
301 |
)
|
302 |
+
return explanation, image, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
303 |
|
304 |
# 多狗處理
|
305 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
|
|
331 |
3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
|
332 |
"""
|
333 |
explanations.append(explanation)
|
334 |
+
visible_buttons.append(gr.update(visible=True, value=f"More about Dog {i+1}: {topk_breeds[0]}"))
|
335 |
+
visible_buttons.append(gr.update(visible=True, value=f"More about Dog {i+1}: {topk_breeds[1]}"))
|
336 |
+
visible_buttons.append(gr.update(visible=True, value=f"More about Dog {i+1}: {topk_breeds[2]}"))
|
337 |
else:
|
338 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
339 |
|
340 |
final_explanation = "\n\n".join(explanations)
|
341 |
|
342 |
+
return final_explanation, annotated_image, visible_buttons
|
343 |
|
344 |
except Exception as e:
|
345 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
359 |
return f"An error occurred while showing details: {e}"
|
360 |
|
361 |
|
362 |
+
|
363 |
with gr.Blocks(css="""
|
364 |
.container { max-width: 900px; margin: auto; padding: 20px; }
|
365 |
.gr-box { border-radius: 15px; }
|