Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -251,33 +251,45 @@ def get_akc_breeds_link():
|
|
251 |
# iface.launch()
|
252 |
|
253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
def detect_dogs(image):
|
255 |
-
# 使用 YOLO 模型進行偵測
|
256 |
results = model_yolo(image)
|
257 |
-
|
258 |
-
# 打印 YOLO 偵測結果
|
259 |
-
print(f"YOLO detection results: {results}")
|
260 |
-
|
261 |
dogs = []
|
262 |
for result in results:
|
263 |
-
# 打印每個結果
|
264 |
-
print(f"Result: {result}")
|
265 |
for box in result.boxes:
|
266 |
-
#
|
267 |
-
print(f"Detected class: {box.cls}, Confidence: {box.conf}, Box coordinates: {box.xyxy}")
|
268 |
-
|
269 |
-
if box.cls == 16: # COCO 資料集中的狗類別是 16
|
270 |
xyxy = box.xyxy[0].tolist()
|
271 |
confidence = box.conf.item()
|
272 |
-
|
273 |
-
# 確認圖片裁切過程正確
|
274 |
-
print(f"Cropping image at coordinates: {xyxy}")
|
275 |
-
|
276 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
277 |
dogs.append((cropped_image, confidence, xyxy))
|
278 |
-
|
279 |
-
# 最後打印偵測到的狗的數量
|
280 |
-
print(f"Number of dogs detected: {len(dogs)}")
|
281 |
return dogs
|
282 |
|
283 |
|
@@ -301,6 +313,17 @@ def predict(image):
|
|
301 |
if isinstance(image, np.ndarray):
|
302 |
image = Image.fromarray(image)
|
303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
dogs = detect_dogs(image)
|
305 |
if len(dogs) == 0:
|
306 |
return "No dogs detected or the image is unclear. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -311,7 +334,7 @@ def predict(image):
|
|
311 |
draw = ImageDraw.Draw(annotated_image)
|
312 |
|
313 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
314 |
-
top1_prob, topk_breeds, topk_probs_percent =
|
315 |
|
316 |
draw.rectangle(box, outline="red", width=3)
|
317 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill="red")
|
@@ -319,7 +342,7 @@ def predict(image):
|
|
319 |
if top1_prob >= 0.5:
|
320 |
breed = topk_breeds[0]
|
321 |
description = get_dog_description(breed)
|
322 |
-
explanations.append(f"Dog {i+1}
|
323 |
elif 0.2 <= top1_prob < 0.5:
|
324 |
explanation = (
|
325 |
f"Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:\n"
|
@@ -338,22 +361,6 @@ def predict(image):
|
|
338 |
except Exception as e:
|
339 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
340 |
|
341 |
-
def format_description(description, breed):
|
342 |
-
if isinstance(description, dict):
|
343 |
-
formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
344 |
-
else:
|
345 |
-
formatted_description = description
|
346 |
-
|
347 |
-
akc_link = get_akc_breeds_link()
|
348 |
-
formatted_description += f"\n\n**Want to learn more about dog breeds?** [Visit the AKC dog breeds page]({akc_link}) and search for {breed} to find detailed information."
|
349 |
-
|
350 |
-
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
351 |
-
"You may need to search for the specific breed on that page. "
|
352 |
-
"I am not responsible for the content on external sites. "
|
353 |
-
"Please refer to the AKC's terms of use and privacy policy.*")
|
354 |
-
formatted_description += disclaimer
|
355 |
-
|
356 |
-
return formatted_description
|
357 |
|
358 |
def show_details(breed):
|
359 |
breed_name = breed.split("More about ")[-1]
|
|
|
251 |
# iface.launch()
|
252 |
|
253 |
|
254 |
+
def format_description(description, breed):
|
255 |
+
if isinstance(description, dict):
|
256 |
+
formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
257 |
+
else:
|
258 |
+
formatted_description = description
|
259 |
+
|
260 |
+
akc_link = get_akc_breeds_link()
|
261 |
+
formatted_description += f"\n\n**Want to learn more about dog breeds?**\n[Visit the AKC dog breeds page]({akc_link}) and search for {breed} to find detailed information."
|
262 |
+
|
263 |
+
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
264 |
+
"You may need to search for the specific breed on that page. "
|
265 |
+
"I am not responsible for the content on external sites. "
|
266 |
+
"Please refer to the AKC's terms of use and privacy policy.*")
|
267 |
+
formatted_description += disclaimer
|
268 |
+
|
269 |
+
return formatted_description
|
270 |
+
|
271 |
+
def predict_single_dog(image):
|
272 |
+
image_tensor = preprocess_image(image)
|
273 |
+
with torch.no_grad():
|
274 |
+
output = model(image_tensor)
|
275 |
+
logits = output[0] if isinstance(output, tuple) else output
|
276 |
+
probabilities = F.softmax(logits, dim=1)
|
277 |
+
topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
278 |
+
top1_prob = topk_probs[0][0].item()
|
279 |
+
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
280 |
+
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
281 |
+
return top1_prob, topk_breeds, topk_probs_percent
|
282 |
+
|
283 |
def detect_dogs(image):
|
|
|
284 |
results = model_yolo(image)
|
|
|
|
|
|
|
|
|
285 |
dogs = []
|
286 |
for result in results:
|
|
|
|
|
287 |
for box in result.boxes:
|
288 |
+
if box.cls == 16: # COCO dataset class for dog is 16
|
|
|
|
|
|
|
289 |
xyxy = box.xyxy[0].tolist()
|
290 |
confidence = box.conf.item()
|
|
|
|
|
|
|
|
|
291 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
292 |
dogs.append((cropped_image, confidence, xyxy))
|
|
|
|
|
|
|
293 |
return dogs
|
294 |
|
295 |
|
|
|
313 |
if isinstance(image, np.ndarray):
|
314 |
image = Image.fromarray(image)
|
315 |
|
316 |
+
# First, try single dog prediction
|
317 |
+
top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(image)
|
318 |
+
|
319 |
+
if top1_prob >= 0.5:
|
320 |
+
# If confident enough, use single dog prediction
|
321 |
+
breed = topk_breeds[0]
|
322 |
+
description = get_dog_description(breed)
|
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 |
+
# If not confident, use YOLO for multiple dog detection
|
327 |
dogs = detect_dogs(image)
|
328 |
if len(dogs) == 0:
|
329 |
return "No dogs detected or the image is unclear. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
334 |
draw = ImageDraw.Draw(annotated_image)
|
335 |
|
336 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
337 |
+
top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(cropped_image)
|
338 |
|
339 |
draw.rectangle(box, outline="red", width=3)
|
340 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill="red")
|
|
|
342 |
if top1_prob >= 0.5:
|
343 |
breed = topk_breeds[0]
|
344 |
description = get_dog_description(breed)
|
345 |
+
explanations.append(f"Dog {i+1}:\n**Breed**: {breed}\n{format_description(description, breed)}")
|
346 |
elif 0.2 <= top1_prob < 0.5:
|
347 |
explanation = (
|
348 |
f"Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:\n"
|
|
|
361 |
except Exception as e:
|
362 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
363 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
364 |
|
365 |
def show_details(breed):
|
366 |
breed_name = breed.split("More about ")[-1]
|