Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -286,7 +286,11 @@ async def predict_single_dog(image):
|
|
286 |
# 偵測多隻狗的函數
|
287 |
async def detect_multiple_dogs(image):
|
288 |
try:
|
289 |
-
|
|
|
|
|
|
|
|
|
290 |
dogs = []
|
291 |
for result in results:
|
292 |
for box in result.boxes:
|
@@ -308,18 +312,16 @@ async def predict(image):
|
|
308 |
if isinstance(image, np.ndarray):
|
309 |
image = Image.fromarray(image)
|
310 |
|
311 |
-
#
|
312 |
dogs = await detect_multiple_dogs(image)
|
313 |
|
314 |
if len(dogs) == 0:
|
315 |
return "No dogs detected in the image. Please upload a clear image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
316 |
elif len(dogs) == 1:
|
317 |
-
|
318 |
-
cropped_image, _, _ = dogs[0]
|
319 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
320 |
-
return await process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image,
|
321 |
else:
|
322 |
-
# Multiple dogs detected
|
323 |
return await process_multiple_dogs_result(dogs, image)
|
324 |
|
325 |
except Exception as e:
|
|
|
286 |
# 偵測多隻狗的函數
|
287 |
async def detect_multiple_dogs(image):
|
288 |
try:
|
289 |
+
# 調整圖像大小以加快處理速度
|
290 |
+
img = image.copy()
|
291 |
+
img.thumbnail((640, 640))
|
292 |
+
|
293 |
+
results = model_yolo(img, conf=0.3) # 降低置信度閾值以檢測更多狗
|
294 |
dogs = []
|
295 |
for result in results:
|
296 |
for box in result.boxes:
|
|
|
312 |
if isinstance(image, np.ndarray):
|
313 |
image = Image.fromarray(image)
|
314 |
|
315 |
+
# 使用 YOLO 檢測狗
|
316 |
dogs = await detect_multiple_dogs(image)
|
317 |
|
318 |
if len(dogs) == 0:
|
319 |
return "No dogs detected in the image. Please upload a clear image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
320 |
elif len(dogs) == 1:
|
321 |
+
cropped_image, _, box = dogs[0]
|
|
|
322 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
323 |
+
return await process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image, box)
|
324 |
else:
|
|
|
325 |
return await process_multiple_dogs_result(dogs, image)
|
326 |
|
327 |
except Exception as e:
|