Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,10 @@ import gradio as gr
|
|
6 |
from torchvision.models import efficientnet_v2_m, EfficientNet_V2_M_Weights
|
7 |
import torch.nn.functional as F
|
8 |
from torchvision import transforms
|
9 |
-
from PIL import Image
|
10 |
from data_manager import get_dog_description
|
11 |
from urllib.parse import quote
|
12 |
-
# os.system('pip install ultralytics')
|
13 |
from ultralytics import YOLO
|
14 |
-
from PIL import ImageDraw
|
15 |
|
16 |
|
17 |
# 下載YOLOv8預訓練模型
|
@@ -273,7 +271,6 @@ Please refer to the AKC's terms of use and privacy policy.*
|
|
273 |
return formatted_description
|
274 |
|
275 |
def predict_single_dog(image):
|
276 |
-
# 直接使用模型進行預測,無需通過 YOLO
|
277 |
image_tensor = preprocess_image(image)
|
278 |
with torch.no_grad():
|
279 |
output = model(image_tensor)
|
@@ -285,20 +282,21 @@ def predict_single_dog(image):
|
|
285 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
286 |
return top1_prob, topk_breeds, topk_probs_percent
|
287 |
|
288 |
-
|
289 |
def detect_multiple_dogs(image):
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
|
|
|
|
302 |
|
303 |
def predict(image):
|
304 |
if image is None:
|
@@ -340,12 +338,13 @@ Click on a button below to view more information about each breed.
|
|
340 |
visible_buttons = []
|
341 |
annotated_image = image.copy()
|
342 |
draw = ImageDraw.Draw(annotated_image)
|
|
|
343 |
|
344 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
345 |
top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(cropped_image)
|
346 |
|
347 |
draw.rectangle(box, outline="red", width=3)
|
348 |
-
draw.text((box[0], box[1]), f"Dog {i}", fill="red", font=
|
349 |
|
350 |
if top1_prob >= 0.5:
|
351 |
breed = topk_breeds[0]
|
|
|
6 |
from torchvision.models import efficientnet_v2_m, EfficientNet_V2_M_Weights
|
7 |
import torch.nn.functional as F
|
8 |
from torchvision import transforms
|
9 |
+
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預訓練模型
|
|
|
271 |
return formatted_description
|
272 |
|
273 |
def predict_single_dog(image):
|
|
|
274 |
image_tensor = preprocess_image(image)
|
275 |
with torch.no_grad():
|
276 |
output = model(image_tensor)
|
|
|
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 = []
|
289 |
+
for result in results:
|
290 |
+
for box in result.boxes:
|
291 |
+
if box.cls == 16: # COCO dataset class for dog is 16
|
292 |
+
xyxy = box.xyxy[0].tolist()
|
293 |
+
confidence = box.conf.item()
|
294 |
+
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
295 |
+
dogs.append((cropped_image, confidence, xyxy))
|
296 |
+
return dogs
|
297 |
+
except Exception as e:
|
298 |
+
print(f"Error in detect_multiple_dogs: {e}")
|
299 |
+
return []
|
300 |
|
301 |
def predict(image):
|
302 |
if image is None:
|
|
|
338 |
visible_buttons = []
|
339 |
annotated_image = image.copy()
|
340 |
draw = ImageDraw.Draw(annotated_image)
|
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)
|
348 |
|
349 |
if top1_prob >= 0.5:
|
350 |
breed = topk_breeds[0]
|