Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -283,14 +283,13 @@ async def predict_single_dog(image):
|
|
283 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
284 |
return top1_prob, topk_breeds, topk_probs_percent
|
285 |
|
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.
|
294 |
dogs = []
|
295 |
for result in results:
|
296 |
for box in result.boxes:
|
@@ -299,6 +298,15 @@ async def detect_multiple_dogs(image):
|
|
299 |
confidence = box.conf.item()
|
300 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
301 |
dogs.append((cropped_image, confidence, xyxy))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
302 |
return dogs
|
303 |
except Exception as e:
|
304 |
print(f"Error in detect_multiple_dogs: {e}")
|
@@ -312,11 +320,15 @@ async def predict(image):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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)
|
@@ -338,8 +350,9 @@ async def process_multiple_dogs_result(dogs, image):
|
|
338 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
339 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
340 |
|
341 |
-
|
342 |
-
draw.
|
|
|
343 |
|
344 |
if top1_prob >= 0.5:
|
345 |
breed = topk_breeds[0]
|
@@ -366,8 +379,9 @@ Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
|
|
366 |
async def process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image, box):
|
367 |
annotated_image = image.copy()
|
368 |
draw = ImageDraw.Draw(annotated_image)
|
369 |
-
|
370 |
-
draw.
|
|
|
371 |
|
372 |
if top1_prob >= 0.5:
|
373 |
breed = topk_breeds[0]
|
@@ -387,6 +401,16 @@ async def process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent,
|
|
387 |
else:
|
388 |
return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
389 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
390 |
async def show_details(choice):
|
391 |
if not choice:
|
392 |
return "Please select a breed to view details."
|
|
|
283 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
284 |
return top1_prob, topk_breeds, topk_probs_percent
|
285 |
|
286 |
+
|
287 |
async def detect_multiple_dogs(image):
|
288 |
try:
|
|
|
289 |
img = image.copy()
|
290 |
img.thumbnail((640, 640))
|
291 |
|
292 |
+
results = model_yolo(img, conf=0.2) # 降低閾值以檢測更多狗
|
293 |
dogs = []
|
294 |
for result in results:
|
295 |
for box in result.boxes:
|
|
|
298 |
confidence = box.conf.item()
|
299 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
300 |
dogs.append((cropped_image, confidence, xyxy))
|
301 |
+
|
302 |
+
# 如果只檢測到一隻狗,嘗試檢測其他可能的狗
|
303 |
+
if len(dogs) == 1:
|
304 |
+
# 使用整張圖像進行品種預測
|
305 |
+
full_image_prob, full_image_breeds, _ = await predict_single_dog(image)
|
306 |
+
if full_image_prob >= 0.3 and full_image_breeds[0] != dogs[0][0]:
|
307 |
+
# 如果整張圖像的預測結果不同且置信度較高,添加為第二隻狗
|
308 |
+
dogs.append((image, full_image_prob, [0, 0, image.width, image.height]))
|
309 |
+
|
310 |
return dogs
|
311 |
except Exception as e:
|
312 |
print(f"Error in detect_multiple_dogs: {e}")
|
|
|
320 |
if isinstance(image, np.ndarray):
|
321 |
image = Image.fromarray(image)
|
322 |
|
|
|
323 |
dogs = await detect_multiple_dogs(image)
|
324 |
|
325 |
if len(dogs) == 0:
|
326 |
+
# 如果沒有檢測到狗,嘗試對整張圖像進行預測
|
327 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
328 |
+
if top1_prob >= 0.3:
|
329 |
+
return await process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image, [0, 0, image.width, image.height])
|
330 |
+
else:
|
331 |
+
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)
|
332 |
elif len(dogs) == 1:
|
333 |
cropped_image, _, box = dogs[0]
|
334 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
|
|
350 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
351 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
352 |
|
353 |
+
optimized_box = optimize_box(box, image.size)
|
354 |
+
draw.rectangle(optimized_box, outline="red", width=3)
|
355 |
+
draw.text((optimized_box[0], optimized_box[1]), f"Dog {i}", fill="yellow", font=font)
|
356 |
|
357 |
if top1_prob >= 0.5:
|
358 |
breed = topk_breeds[0]
|
|
|
379 |
async def process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image, box):
|
380 |
annotated_image = image.copy()
|
381 |
draw = ImageDraw.Draw(annotated_image)
|
382 |
+
optimized_box = optimize_box(box, image.size)
|
383 |
+
draw.rectangle(optimized_box, outline="red", width=3)
|
384 |
+
draw.text((optimized_box[0], optimized_box[1]), "Dog", fill="yellow", font=ImageFont.load_default())
|
385 |
|
386 |
if top1_prob >= 0.5:
|
387 |
breed = topk_breeds[0]
|
|
|
401 |
else:
|
402 |
return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
403 |
|
404 |
+
def optimize_box(box, image_size):
|
405 |
+
x1, y1, x2, y2 = box
|
406 |
+
w, h = image_size
|
407 |
+
# 擴大邊界框以確保完整包含狗
|
408 |
+
x1 = max(0, x1 - 10)
|
409 |
+
y1 = max(0, y1 - 10)
|
410 |
+
x2 = min(w, x2 + 10)
|
411 |
+
y2 = min(h, y2 + 10)
|
412 |
+
return [x1, y1, x2, y2]
|
413 |
+
|
414 |
async def show_details(choice):
|
415 |
if not choice:
|
416 |
return "Please select a breed to view details."
|