Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from PIL import Image, ImageDraw, ImageFont
|
|
10 |
from data_manager import get_dog_description
|
11 |
from urllib.parse import quote
|
12 |
from ultralytics import YOLO
|
|
|
13 |
|
14 |
|
15 |
# 下載YOLOv8預訓練模型
|
@@ -270,7 +271,19 @@ Please refer to the AKC's terms of use and privacy policy.*
|
|
270 |
"""
|
271 |
return formatted_description
|
272 |
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
image_tensor = preprocess_image(image)
|
275 |
with torch.no_grad():
|
276 |
output = model(image_tensor)
|
@@ -282,7 +295,7 @@ def predict_single_dog(image):
|
|
282 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
283 |
return top1_prob, topk_breeds, topk_probs_percent
|
284 |
|
285 |
-
def detect_multiple_dogs(image):
|
286 |
try:
|
287 |
results = model_yolo(image)
|
288 |
dogs = []
|
@@ -298,7 +311,7 @@ def detect_multiple_dogs(image):
|
|
298 |
print(f"Error in detect_multiple_dogs: {e}")
|
299 |
return []
|
300 |
|
301 |
-
def predict(image):
|
302 |
if image is None:
|
303 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
304 |
|
@@ -306,13 +319,11 @@ def predict(image):
|
|
306 |
if isinstance(image, np.ndarray):
|
307 |
image = Image.fromarray(image)
|
308 |
|
309 |
-
#
|
310 |
-
|
311 |
-
|
312 |
-
# Check if we need to use YOLO for multiple dogs
|
313 |
-
dogs = detect_multiple_dogs(image)
|
314 |
|
315 |
-
if len(dogs) <= 1: #
|
|
|
316 |
breed = topk_breeds[0]
|
317 |
description = get_dog_description(breed)
|
318 |
formatted_description = format_description(description, breed)
|
@@ -333,7 +344,7 @@ Click on a button below to view more information about each breed.
|
|
333 |
else:
|
334 |
return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
335 |
|
336 |
-
#
|
337 |
explanations = []
|
338 |
visible_buttons = []
|
339 |
annotated_image = image.copy()
|
@@ -341,7 +352,7 @@ Click on a button below to view more information about each breed.
|
|
341 |
font = ImageFont.load_default()
|
342 |
|
343 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
344 |
-
top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(cropped_image)
|
345 |
|
346 |
draw.rectangle(box, outline="red", width=3)
|
347 |
draw.text((box[0], box[1]), f"Dog {i}", fill="red", font=font)
|
|
|
10 |
from data_manager import get_dog_description
|
11 |
from urllib.parse import quote
|
12 |
from ultralytics import YOLO
|
13 |
+
import asyncio
|
14 |
|
15 |
|
16 |
# 下載YOLOv8預訓練模型
|
|
|
271 |
"""
|
272 |
return formatted_description
|
273 |
|
274 |
+
# 全局變量,用於存儲加載的模型
|
275 |
+
model = None
|
276 |
+
model_yolo = None
|
277 |
+
|
278 |
+
def load_models():
|
279 |
+
global model, model_yolo
|
280 |
+
model = # 加載您的品種分類模型
|
281 |
+
model_yolo = YOLO('yolov8n.pt')
|
282 |
+
|
283 |
+
# 在應用啟動時加載模型
|
284 |
+
load_models()
|
285 |
+
|
286 |
+
async def predict_single_dog(image):
|
287 |
image_tensor = preprocess_image(image)
|
288 |
with torch.no_grad():
|
289 |
output = model(image_tensor)
|
|
|
295 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
296 |
return top1_prob, topk_breeds, topk_probs_percent
|
297 |
|
298 |
+
async def detect_multiple_dogs(image):
|
299 |
try:
|
300 |
results = model_yolo(image)
|
301 |
dogs = []
|
|
|
311 |
print(f"Error in detect_multiple_dogs: {e}")
|
312 |
return []
|
313 |
|
314 |
+
async def predict(image):
|
315 |
if image is None:
|
316 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
317 |
|
|
|
319 |
if isinstance(image, np.ndarray):
|
320 |
image = Image.fromarray(image)
|
321 |
|
322 |
+
# 快速檢查是否有多個狗
|
323 |
+
dogs = await detect_multiple_dogs(image)
|
|
|
|
|
|
|
324 |
|
325 |
+
if len(dogs) <= 1: # 單狗或無狗情況
|
326 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
327 |
breed = topk_breeds[0]
|
328 |
description = get_dog_description(breed)
|
329 |
formatted_description = format_description(description, breed)
|
|
|
344 |
else:
|
345 |
return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
346 |
|
347 |
+
# 多狗情況
|
348 |
explanations = []
|
349 |
visible_buttons = []
|
350 |
annotated_image = image.copy()
|
|
|
352 |
font = ImageFont.load_default()
|
353 |
|
354 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
355 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
356 |
|
357 |
draw.rectangle(box, outline="red", width=3)
|
358 |
draw.text((box[0], box[1]), f"Dog {i}", fill="red", font=font)
|