Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -166,6 +166,7 @@ def get_akc_breeds_link():
|
|
166 |
# return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
167 |
|
168 |
|
|
|
169 |
def predict(image):
|
170 |
if image is None:
|
171 |
return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -182,12 +183,16 @@ def predict(image):
|
|
182 |
if len(boxes) == 0:
|
183 |
return "The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
184 |
|
|
|
|
|
|
|
185 |
explanations = []
|
186 |
visible_buttons = []
|
187 |
|
|
|
188 |
for i, box in enumerate(boxes):
|
189 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
190 |
-
cropped_image = image.crop((x1, y1, x2, y2))
|
191 |
image_tensor = preprocess_image(cropped_image)
|
192 |
|
193 |
with torch.no_grad():
|
@@ -201,22 +206,26 @@ def predict(image):
|
|
201 |
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
202 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
203 |
|
204 |
-
#
|
205 |
if top1_prob >= 0.5:
|
206 |
breed = topk_breeds[0]
|
207 |
description = get_dog_description(breed)
|
208 |
-
|
209 |
-
|
210 |
-
# 信心低於 50% 的情況,返回前三個可能的品種,並顯示按鈕
|
211 |
-
else:
|
212 |
explanation = (
|
213 |
-
f"
|
214 |
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
215 |
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
216 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n"
|
217 |
-
"Click on a button to view more information about the breed."
|
218 |
)
|
219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
|
221 |
except Exception as e:
|
222 |
return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
166 |
# return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
167 |
|
168 |
|
169 |
+
|
170 |
def predict(image):
|
171 |
if image is None:
|
172 |
return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
183 |
if len(boxes) == 0:
|
184 |
return "The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
185 |
|
186 |
+
# 檢查 YOLO 偵測到的邊界框數量
|
187 |
+
print(f"Detected {len(boxes)} dogs in the image.")
|
188 |
+
|
189 |
explanations = []
|
190 |
visible_buttons = []
|
191 |
|
192 |
+
# 處理每個偵測到的狗
|
193 |
for i, box in enumerate(boxes):
|
194 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
195 |
+
cropped_image = image.crop((x1, y1, x2, y2)) # 裁剪出每隻狗的區域
|
196 |
image_tensor = preprocess_image(cropped_image)
|
197 |
|
198 |
with torch.no_grad():
|
|
|
206 |
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
207 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
208 |
|
209 |
+
# 根據置信度決定返回結果
|
210 |
if top1_prob >= 0.5:
|
211 |
breed = topk_breeds[0]
|
212 |
description = get_dog_description(breed)
|
213 |
+
explanations.append(f"Detected a dog: **{breed}** with {topk_probs_percent[0]} confidence.\n{format_description(description, breed)}")
|
214 |
+
elif 0.2 <= top1_prob < 0.5:
|
|
|
|
|
215 |
explanation = (
|
216 |
+
f"Detected a dog with moderate confidence. Here are the top 3 possible breeds:\n"
|
217 |
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
218 |
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
219 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n"
|
|
|
220 |
)
|
221 |
+
explanations.append(explanation)
|
222 |
+
visible_buttons.extend([f"More about {topk_breeds[0]}", f"More about {topk_breeds[1]}", f"More about {topk_breeds[2]}"])
|
223 |
+
else:
|
224 |
+
explanations.append("The image is too unclear or the breed is not in the dataset. Please upload a clearer image.")
|
225 |
+
|
226 |
+
# 將結果匯總後返回
|
227 |
+
final_explanation = "\n\n".join(explanations)
|
228 |
+
return final_explanation, gr.update(visible=len(visible_buttons) >= 1), gr.update(visible=len(visible_buttons) >= 2), gr.update(visible=len(visible_buttons) >= 3)
|
229 |
|
230 |
except Exception as e:
|
231 |
return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|