Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -34,9 +34,474 @@ from html_templates import (
|
|
34 |
from urllib.parse import quote
|
35 |
from ultralytics import YOLO
|
36 |
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
|
39 |
-
model_yolo = YOLO('yolov8l.pt')
|
40 |
|
41 |
history_manager = UserHistoryManager()
|
42 |
|
@@ -122,20 +587,37 @@ class BaseModel(nn.Module):
|
|
122 |
logits = self.classifier(attended_features)
|
123 |
return logits, attended_features
|
124 |
|
125 |
-
# Initialize model
|
126 |
-
num_classes = len(dog_breeds)
|
127 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
128 |
-
|
129 |
-
# Initialize base model
|
130 |
-
model = BaseModel(num_classes=num_classes, device=device).to(device)
|
131 |
-
|
132 |
-
# Load model path
|
133 |
-
model_path = '124_best_model_dog.pth'
|
134 |
-
checkpoint = torch.load(model_path, map_location=device)
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
# Image preprocessing function
|
141 |
def preprocess_image(image):
|
@@ -152,6 +634,7 @@ def preprocess_image(image):
|
|
152 |
|
153 |
return transform(image).unsqueeze(0)
|
154 |
|
|
|
155 |
async def predict_single_dog(image):
|
156 |
"""
|
157 |
Predicts the dog breed using only the classifier.
|
@@ -160,6 +643,9 @@ async def predict_single_dog(image):
|
|
160 |
Returns:
|
161 |
tuple: (top1_prob, topk_breeds, relative_probs)
|
162 |
"""
|
|
|
|
|
|
|
163 |
image_tensor = preprocess_image(image).to(device)
|
164 |
|
165 |
with torch.no_grad():
|
@@ -183,9 +669,12 @@ async def predict_single_dog(image):
|
|
183 |
|
184 |
return probabilities[0], breeds[:3], relative_probs
|
185 |
|
186 |
-
|
187 |
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
|
188 |
-
|
|
|
|
|
|
|
189 |
dogs = []
|
190 |
boxes = []
|
191 |
for box in results.boxes:
|
@@ -265,7 +754,7 @@ def create_breed_comparison(breed1: str, breed2: str) -> dict:
|
|
265 |
|
266 |
return comparison_data
|
267 |
|
268 |
-
|
269 |
async def predict(image):
|
270 |
"""
|
271 |
Main prediction function that handles both single and multiple dog detection.
|
@@ -276,6 +765,9 @@ async def predict(image):
|
|
276 |
Returns:
|
277 |
tuple: (html_output, annotated_image, initial_state)
|
278 |
"""
|
|
|
|
|
|
|
279 |
if image is None:
|
280 |
return format_warning_html("Please upload an image to start."), None, None
|
281 |
|
|
|
34 |
from urllib.parse import quote
|
35 |
from ultralytics import YOLO
|
36 |
import traceback
|
37 |
+
import spaces
|
38 |
+
|
39 |
+
# model_yolo = YOLO('yolov8l.pt')
|
40 |
+
|
41 |
+
# history_manager = UserHistoryManager()
|
42 |
+
|
43 |
+
# dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
|
44 |
+
# "Appenzeller", "Australian_Terrier", "Bedlington_Terrier", "Bernese_Mountain_Dog", "Bichon_Frise",
|
45 |
+
# "Blenheim_Spaniel", "Border_Collie", "Border_Terrier", "Boston_Bull", "Bouvier_Des_Flandres",
|
46 |
+
# "Brabancon_Griffon", "Brittany_Spaniel", "Cardigan", "Chesapeake_Bay_Retriever",
|
47 |
+
# "Chihuahua", "Dachshund", "Dandie_Dinmont", "Doberman", "English_Foxhound", "English_Setter",
|
48 |
+
# "English_Springer", "EntleBucher", "Eskimo_Dog", "French_Bulldog", "German_Shepherd",
|
49 |
+
# "German_Short-Haired_Pointer", "Gordon_Setter", "Great_Dane", "Great_Pyrenees",
|
50 |
+
# "Greater_Swiss_Mountain_Dog","Havanese", "Ibizan_Hound", "Irish_Setter", "Irish_Terrier",
|
51 |
+
# "Irish_Water_Spaniel", "Irish_Wolfhound", "Italian_Greyhound", "Japanese_Spaniel",
|
52 |
+
# "Kerry_Blue_Terrier", "Labrador_Retriever", "Lakeland_Terrier", "Leonberg", "Lhasa",
|
53 |
+
# "Maltese_Dog", "Mexican_Hairless", "Newfoundland", "Norfolk_Terrier", "Norwegian_Elkhound",
|
54 |
+
# "Norwich_Terrier", "Old_English_Sheepdog", "Pekinese", "Pembroke", "Pomeranian",
|
55 |
+
# "Rhodesian_Ridgeback", "Rottweiler", "Saint_Bernard", "Saluki", "Samoyed",
|
56 |
+
# "Scotch_Terrier", "Scottish_Deerhound", "Sealyham_Terrier", "Shetland_Sheepdog", "Shiba_Inu",
|
57 |
+
# "Shih-Tzu", "Siberian_Husky", "Staffordshire_Bullterrier", "Sussex_Spaniel",
|
58 |
+
# "Tibetan_Mastiff", "Tibetan_Terrier", "Walker_Hound", "Weimaraner",
|
59 |
+
# "Welsh_Springer_Spaniel", "West_Highland_White_Terrier", "Yorkshire_Terrier",
|
60 |
+
# "Affenpinscher", "Basenji", "Basset", "Beagle", "Black-and-Tan_Coonhound", "Bloodhound",
|
61 |
+
# "Bluetick", "Borzoi", "Boxer", "Briard", "Bull_Mastiff", "Cairn", "Chow", "Clumber",
|
62 |
+
# "Cocker_Spaniel", "Collie", "Curly-Coated_Retriever", "Dhole", "Dingo",
|
63 |
+
# "Flat-Coated_Retriever", "Giant_Schnauzer", "Golden_Retriever", "Groenendael", "Keeshond",
|
64 |
+
# "Kelpie", "Komondor", "Kuvasz", "Malamute", "Malinois", "Miniature_Pinscher",
|
65 |
+
# "Miniature_Poodle", "Miniature_Schnauzer", "Otterhound", "Papillon", "Pug", "Redbone",
|
66 |
+
# "Schipperke", "Silky_Terrier", "Soft-Coated_Wheaten_Terrier", "Standard_Poodle",
|
67 |
+
# "Standard_Schnauzer", "Toy_Poodle", "Toy_Terrier", "Vizsla", "Whippet",
|
68 |
+
# "Wire-Haired_Fox_Terrier"]
|
69 |
+
|
70 |
+
|
71 |
+
# class MultiHeadAttention(nn.Module):
|
72 |
+
|
73 |
+
# def __init__(self, in_dim, num_heads=8):
|
74 |
+
# super().__init__()
|
75 |
+
# self.num_heads = num_heads
|
76 |
+
# self.head_dim = max(1, in_dim // num_heads)
|
77 |
+
# self.scaled_dim = self.head_dim * num_heads
|
78 |
+
# self.fc_in = nn.Linear(in_dim, self.scaled_dim)
|
79 |
+
# self.query = nn.Linear(self.scaled_dim, self.scaled_dim)
|
80 |
+
# self.key = nn.Linear(self.scaled_dim, self.scaled_dim)
|
81 |
+
# self.value = nn.Linear(self.scaled_dim, self.scaled_dim)
|
82 |
+
# self.fc_out = nn.Linear(self.scaled_dim, in_dim)
|
83 |
+
|
84 |
+
# def forward(self, x):
|
85 |
+
# N = x.shape[0]
|
86 |
+
# x = self.fc_in(x)
|
87 |
+
# q = self.query(x).view(N, self.num_heads, self.head_dim)
|
88 |
+
# k = self.key(x).view(N, self.num_heads, self.head_dim)
|
89 |
+
# v = self.value(x).view(N, self.num_heads, self.head_dim)
|
90 |
+
|
91 |
+
# energy = torch.einsum("nqd,nkd->nqk", [q, k])
|
92 |
+
# attention = F.softmax(energy / (self.head_dim ** 0.5), dim=2)
|
93 |
+
|
94 |
+
# out = torch.einsum("nqk,nvd->nqd", [attention, v])
|
95 |
+
# out = out.reshape(N, self.scaled_dim)
|
96 |
+
# out = self.fc_out(out)
|
97 |
+
# return out
|
98 |
+
|
99 |
+
# class BaseModel(nn.Module):
|
100 |
+
# def __init__(self, num_classes, device='cuda' if torch.cuda.is_available() else 'cpu'):
|
101 |
+
# super().__init__()
|
102 |
+
# self.device = device
|
103 |
+
# self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1)
|
104 |
+
# self.feature_dim = self.backbone.classifier[1].in_features
|
105 |
+
# self.backbone.classifier = nn.Identity()
|
106 |
+
|
107 |
+
# self.num_heads = max(1, min(8, self.feature_dim // 64))
|
108 |
+
# self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads)
|
109 |
+
|
110 |
+
# self.classifier = nn.Sequential(
|
111 |
+
# nn.LayerNorm(self.feature_dim),
|
112 |
+
# nn.Dropout(0.3),
|
113 |
+
# nn.Linear(self.feature_dim, num_classes)
|
114 |
+
# )
|
115 |
+
|
116 |
+
# self.to(device)
|
117 |
+
|
118 |
+
# def forward(self, x):
|
119 |
+
# x = x.to(self.device)
|
120 |
+
# features = self.backbone(x)
|
121 |
+
# attended_features = self.attention(features)
|
122 |
+
# logits = self.classifier(attended_features)
|
123 |
+
# return logits, attended_features
|
124 |
+
|
125 |
+
# # Initialize model
|
126 |
+
# num_classes = len(dog_breeds)
|
127 |
+
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
128 |
+
|
129 |
+
# # Initialize base model
|
130 |
+
# model = BaseModel(num_classes=num_classes, device=device).to(device)
|
131 |
+
|
132 |
+
# # Load model path
|
133 |
+
# model_path = '124_best_model_dog.pth'
|
134 |
+
# checkpoint = torch.load(model_path, map_location=device)
|
135 |
+
|
136 |
+
# # Load model state
|
137 |
+
# model.load_state_dict(checkpoint['base_model'], strict=False)
|
138 |
+
# model.eval()
|
139 |
+
|
140 |
+
# # Image preprocessing function
|
141 |
+
# def preprocess_image(image):
|
142 |
+
# # If the image is numpy.ndarray turn into PIL.Image
|
143 |
+
# if isinstance(image, np.ndarray):
|
144 |
+
# image = Image.fromarray(image)
|
145 |
+
|
146 |
+
# # Use torchvision.transforms to process images
|
147 |
+
# transform = transforms.Compose([
|
148 |
+
# transforms.Resize((224, 224)),
|
149 |
+
# transforms.ToTensor(),
|
150 |
+
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
151 |
+
# ])
|
152 |
+
|
153 |
+
# return transform(image).unsqueeze(0)
|
154 |
+
|
155 |
+
# async def predict_single_dog(image):
|
156 |
+
# """
|
157 |
+
# Predicts the dog breed using only the classifier.
|
158 |
+
# Args:
|
159 |
+
# image: PIL Image or numpy array
|
160 |
+
# Returns:
|
161 |
+
# tuple: (top1_prob, topk_breeds, relative_probs)
|
162 |
+
# """
|
163 |
+
# image_tensor = preprocess_image(image).to(device)
|
164 |
+
|
165 |
+
# with torch.no_grad():
|
166 |
+
# # Get model outputs (只使用logits,不需要features)
|
167 |
+
# logits = model(image_tensor)[0] # 如果model仍返回tuple,取第一個元素
|
168 |
+
# probs = F.softmax(logits, dim=1)
|
169 |
+
|
170 |
+
# # Classifier prediction
|
171 |
+
# top5_prob, top5_idx = torch.topk(probs, k=5)
|
172 |
+
# breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
|
173 |
+
# probabilities = [prob.item() for prob in top5_prob[0]]
|
174 |
+
|
175 |
+
# # Calculate relative probabilities
|
176 |
+
# sum_probs = sum(probabilities[:3]) # 只取前三個來計算相對概率
|
177 |
+
# relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
|
178 |
+
|
179 |
+
# # Debug output
|
180 |
+
# print("\nClassifier Predictions:")
|
181 |
+
# for breed, prob in zip(breeds[:5], probabilities[:5]):
|
182 |
+
# print(f"{breed}: {prob:.4f}")
|
183 |
+
|
184 |
+
# return probabilities[0], breeds[:3], relative_probs
|
185 |
+
|
186 |
+
|
187 |
+
# async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
|
188 |
+
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
189 |
+
# dogs = []
|
190 |
+
# boxes = []
|
191 |
+
# for box in results.boxes:
|
192 |
+
# if box.cls == 16: # COCO dataset class for dog is 16
|
193 |
+
# xyxy = box.xyxy[0].tolist()
|
194 |
+
# confidence = box.conf.item()
|
195 |
+
# boxes.append((xyxy, confidence))
|
196 |
+
|
197 |
+
# if not boxes:
|
198 |
+
# dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
199 |
+
# else:
|
200 |
+
# nms_boxes = non_max_suppression(boxes, iou_threshold)
|
201 |
+
|
202 |
+
# for box, confidence in nms_boxes:
|
203 |
+
# x1, y1, x2, y2 = box
|
204 |
+
# w, h = x2 - x1, y2 - y1
|
205 |
+
# x1 = max(0, x1 - w * 0.05)
|
206 |
+
# y1 = max(0, y1 - h * 0.05)
|
207 |
+
# x2 = min(image.width, x2 + w * 0.05)
|
208 |
+
# y2 = min(image.height, y2 + h * 0.05)
|
209 |
+
# cropped_image = image.crop((x1, y1, x2, y2))
|
210 |
+
# dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
211 |
+
|
212 |
+
# return dogs
|
213 |
+
|
214 |
+
# def non_max_suppression(boxes, iou_threshold):
|
215 |
+
# keep = []
|
216 |
+
# boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
217 |
+
# while boxes:
|
218 |
+
# current = boxes.pop(0)
|
219 |
+
# keep.append(current)
|
220 |
+
# boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
221 |
+
# return keep
|
222 |
+
|
223 |
+
|
224 |
+
# def calculate_iou(box1, box2):
|
225 |
+
# x1 = max(box1[0], box2[0])
|
226 |
+
# y1 = max(box1[1], box2[1])
|
227 |
+
# x2 = min(box1[2], box2[2])
|
228 |
+
# y2 = min(box1[3], box2[3])
|
229 |
+
|
230 |
+
# intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
231 |
+
# area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
232 |
+
# area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
233 |
+
|
234 |
+
# iou = intersection / float(area1 + area2 - intersection)
|
235 |
+
# return iou
|
236 |
+
|
237 |
+
|
238 |
+
|
239 |
+
# def create_breed_comparison(breed1: str, breed2: str) -> dict:
|
240 |
+
# breed1_info = get_dog_description(breed1)
|
241 |
+
# breed2_info = get_dog_description(breed2)
|
242 |
+
|
243 |
+
# # 標準化數值轉換
|
244 |
+
# value_mapping = {
|
245 |
+
# 'Size': {'Small': 1, 'Medium': 2, 'Large': 3, 'Giant': 4},
|
246 |
+
# 'Exercise_Needs': {'Low': 1, 'Moderate': 2, 'High': 3, 'Very High': 4},
|
247 |
+
# 'Care_Level': {'Low': 1, 'Moderate': 2, 'High': 3},
|
248 |
+
# 'Grooming_Needs': {'Low': 1, 'Moderate': 2, 'High': 3}
|
249 |
+
# }
|
250 |
+
|
251 |
+
# comparison_data = {
|
252 |
+
# breed1: {},
|
253 |
+
# breed2: {}
|
254 |
+
# }
|
255 |
+
|
256 |
+
# for breed, info in [(breed1, breed1_info), (breed2, breed2_info)]:
|
257 |
+
# comparison_data[breed] = {
|
258 |
+
# 'Size': value_mapping['Size'].get(info['Size'], 2), # 預設 Medium
|
259 |
+
# 'Exercise_Needs': value_mapping['Exercise_Needs'].get(info['Exercise Needs'], 2), # 預設 Moderate
|
260 |
+
# 'Care_Level': value_mapping['Care_Level'].get(info['Care Level'], 2),
|
261 |
+
# 'Grooming_Needs': value_mapping['Grooming_Needs'].get(info['Grooming Needs'], 2),
|
262 |
+
# 'Good_with_Children': info['Good with Children'] == 'Yes',
|
263 |
+
# 'Original_Data': info
|
264 |
+
# }
|
265 |
+
|
266 |
+
# return comparison_data
|
267 |
+
|
268 |
+
|
269 |
+
# async def predict(image):
|
270 |
+
# """
|
271 |
+
# Main prediction function that handles both single and multiple dog detection.
|
272 |
+
|
273 |
+
# Args:
|
274 |
+
# image: PIL Image or numpy array
|
275 |
+
|
276 |
+
# Returns:
|
277 |
+
# tuple: (html_output, annotated_image, initial_state)
|
278 |
+
# """
|
279 |
+
# if image is None:
|
280 |
+
# return format_warning_html("Please upload an image to start."), None, None
|
281 |
+
|
282 |
+
# try:
|
283 |
+
# if isinstance(image, np.ndarray):
|
284 |
+
# image = Image.fromarray(image)
|
285 |
+
|
286 |
+
# # Detect dogs in the image
|
287 |
+
# dogs = await detect_multiple_dogs(image)
|
288 |
+
# color_scheme = get_color_scheme(len(dogs) == 1)
|
289 |
+
|
290 |
+
# # Prepare for annotation
|
291 |
+
# annotated_image = image.copy()
|
292 |
+
# draw = ImageDraw.Draw(annotated_image)
|
293 |
+
|
294 |
+
# try:
|
295 |
+
# font = ImageFont.truetype("arial.ttf", 24)
|
296 |
+
# except:
|
297 |
+
# font = ImageFont.load_default()
|
298 |
+
|
299 |
+
# dogs_info = ""
|
300 |
+
|
301 |
+
# # Process each detected dog
|
302 |
+
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
303 |
+
# color = color_scheme if len(dogs) == 1 else color_scheme[i % len(color_scheme)]
|
304 |
+
|
305 |
+
# # Draw box and label on image
|
306 |
+
# draw.rectangle(box, outline=color, width=4)
|
307 |
+
# label = f"Dog {i+1}"
|
308 |
+
# label_bbox = draw.textbbox((0, 0), label, font=font)
|
309 |
+
# label_width = label_bbox[2] - label_bbox[0]
|
310 |
+
# label_height = label_bbox[3] - label_bbox[1]
|
311 |
+
|
312 |
+
# # Draw label background and text
|
313 |
+
# label_x = box[0] + 5
|
314 |
+
# label_y = box[1] + 5
|
315 |
+
# draw.rectangle(
|
316 |
+
# [label_x - 2, label_y - 2, label_x + label_width + 4, label_y + label_height + 4],
|
317 |
+
# fill='white',
|
318 |
+
# outline=color,
|
319 |
+
# width=2
|
320 |
+
# )
|
321 |
+
# draw.text((label_x, label_y), label, fill=color, font=font)
|
322 |
+
|
323 |
+
# # Predict breed
|
324 |
+
# top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
|
325 |
+
# combined_confidence = detection_confidence * top1_prob
|
326 |
+
|
327 |
+
# # Format results based on confidence with error handling
|
328 |
+
# try:
|
329 |
+
# if combined_confidence < 0.2:
|
330 |
+
# dogs_info += format_error_message(color, i+1)
|
331 |
+
# elif top1_prob >= 0.45:
|
332 |
+
# breed = topk_breeds[0]
|
333 |
+
# description = get_dog_description(breed)
|
334 |
+
# # Handle missing breed description
|
335 |
+
# if description is None:
|
336 |
+
# # 如果沒有描述,創建一個基本描述
|
337 |
+
# description = {
|
338 |
+
# "Name": breed,
|
339 |
+
# "Size": "Unknown",
|
340 |
+
# "Exercise Needs": "Unknown",
|
341 |
+
# "Grooming Needs": "Unknown",
|
342 |
+
# "Care Level": "Unknown",
|
343 |
+
# "Good with Children": "Unknown",
|
344 |
+
# "Description": f"Identified as {breed.replace('_', ' ')}"
|
345 |
+
# }
|
346 |
+
# dogs_info += format_single_dog_result(breed, description, color)
|
347 |
+
# else:
|
348 |
+
# # 修改format_multiple_breeds_result的調用,包含錯誤處理
|
349 |
+
# dogs_info += format_multiple_breeds_result(
|
350 |
+
# topk_breeds,
|
351 |
+
# relative_probs,
|
352 |
+
# color,
|
353 |
+
# i+1,
|
354 |
+
# lambda breed: get_dog_description(breed) or {
|
355 |
+
# "Name": breed,
|
356 |
+
# "Size": "Unknown",
|
357 |
+
# "Exercise Needs": "Unknown",
|
358 |
+
# "Grooming Needs": "Unknown",
|
359 |
+
# "Care Level": "Unknown",
|
360 |
+
# "Good with Children": "Unknown",
|
361 |
+
# "Description": f"Identified as {breed.replace('_', ' ')}"
|
362 |
+
# }
|
363 |
+
# )
|
364 |
+
# except Exception as e:
|
365 |
+
# print(f"Error formatting results for dog {i+1}: {str(e)}")
|
366 |
+
# dogs_info += format_error_message(color, i+1)
|
367 |
+
|
368 |
+
# # Wrap final HTML output
|
369 |
+
# html_output = format_multi_dog_container(dogs_info)
|
370 |
+
|
371 |
+
# # Prepare initial state
|
372 |
+
# initial_state = {
|
373 |
+
# "dogs_info": dogs_info,
|
374 |
+
# "image": annotated_image,
|
375 |
+
# "is_multi_dog": len(dogs) > 1,
|
376 |
+
# "html_output": html_output
|
377 |
+
# }
|
378 |
+
|
379 |
+
# return html_output, annotated_image, initial_state
|
380 |
+
|
381 |
+
# except Exception as e:
|
382 |
+
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
383 |
+
# print(error_msg)
|
384 |
+
# return format_warning_html(error_msg), None, None
|
385 |
+
|
386 |
+
|
387 |
+
# def show_details_html(choice, previous_output, initial_state):
|
388 |
+
# """
|
389 |
+
# Generate detailed HTML view for a selected breed.
|
390 |
+
|
391 |
+
# Args:
|
392 |
+
# choice: str, Selected breed option
|
393 |
+
# previous_output: str, Previous HTML output
|
394 |
+
# initial_state: dict, Current state information
|
395 |
+
|
396 |
+
# Returns:
|
397 |
+
# tuple: (html_output, gradio_update, updated_state)
|
398 |
+
# """
|
399 |
+
# if not choice:
|
400 |
+
# return previous_output, gr.update(visible=True), initial_state
|
401 |
+
|
402 |
+
# try:
|
403 |
+
# breed = choice.split("More about ")[-1]
|
404 |
+
# description = get_dog_description(breed)
|
405 |
+
# html_output = format_breed_details_html(description, breed)
|
406 |
+
|
407 |
+
# # Update state
|
408 |
+
# initial_state["current_description"] = html_output
|
409 |
+
# initial_state["original_buttons"] = initial_state.get("buttons", [])
|
410 |
+
|
411 |
+
# return html_output, gr.update(visible=True), initial_state
|
412 |
+
|
413 |
+
# except Exception as e:
|
414 |
+
# error_msg = f"An error occurred while showing details: {e}"
|
415 |
+
# print(error_msg)
|
416 |
+
# return format_warning_html(error_msg), gr.update(visible=True), initial_state
|
417 |
+
|
418 |
+
# def main():
|
419 |
+
# with gr.Blocks(css=get_css_styles()) as iface:
|
420 |
+
# # Header HTML
|
421 |
+
|
422 |
+
# gr.HTML("""
|
423 |
+
# <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
|
424 |
+
# <h1 style='font-size: 2.5em; margin-bottom: 10px; color: #2D3748;'>
|
425 |
+
# 🐾 PawMatch AI
|
426 |
+
# </h1>
|
427 |
+
# <h2 style='font-size: 1.2em; font-weight: normal; color: #4A5568; margin-top: 5px;'>
|
428 |
+
# Your Smart Dog Breed Guide
|
429 |
+
# </h2>
|
430 |
+
# <div style='width: 50px; height: 3px; background: linear-gradient(90deg, #4299e1, #48bb78); margin: 15px auto;'></div>
|
431 |
+
# <p style='color: #718096; font-size: 0.9em;'>
|
432 |
+
# Powered by AI • Breed Recognition • Smart Matching • Companion Guide
|
433 |
+
# </p>
|
434 |
+
# </header>
|
435 |
+
# """)
|
436 |
+
|
437 |
+
# # 先創建歷史組件實例(但不創建標籤頁)
|
438 |
+
# history_component = create_history_component()
|
439 |
+
|
440 |
+
# with gr.Tabs():
|
441 |
+
# # 1. 品種檢測標籤頁
|
442 |
+
# example_images = [
|
443 |
+
# 'Border_Collie.jpg',
|
444 |
+
# 'Golden_Retriever.jpeg',
|
445 |
+
# 'Saint_Bernard.jpeg',
|
446 |
+
# 'Samoyed.jpg',
|
447 |
+
# 'French_Bulldog.jpeg'
|
448 |
+
# ]
|
449 |
+
# detection_components = create_detection_tab(predict, example_images)
|
450 |
+
|
451 |
+
# # 2. 品種比較標籤頁
|
452 |
+
# comparison_components = create_comparison_tab(
|
453 |
+
# dog_breeds=dog_breeds,
|
454 |
+
# get_dog_description=get_dog_description,
|
455 |
+
# breed_health_info=breed_health_info,
|
456 |
+
# breed_noise_info=breed_noise_info
|
457 |
+
# )
|
458 |
+
|
459 |
+
# # 3. 品種推薦標籤頁
|
460 |
+
# recommendation_components = create_recommendation_tab(
|
461 |
+
# UserPreferences=UserPreferences,
|
462 |
+
# get_breed_recommendations=get_breed_recommendations,
|
463 |
+
# format_recommendation_html=format_recommendation_html,
|
464 |
+
# history_component=history_component
|
465 |
+
# )
|
466 |
+
|
467 |
+
|
468 |
+
# # 4. 最後創建歷史記錄標籤頁
|
469 |
+
# create_history_tab(history_component)
|
470 |
+
|
471 |
+
# # Footer
|
472 |
+
# gr.HTML('''
|
473 |
+
# <div style="
|
474 |
+
# display: flex;
|
475 |
+
# align-items: center;
|
476 |
+
# justify-content: center;
|
477 |
+
# gap: 20px;
|
478 |
+
# padding: 20px 0;
|
479 |
+
# ">
|
480 |
+
# <p style="
|
481 |
+
# font-family: 'Arial', sans-serif;
|
482 |
+
# font-size: 14px;
|
483 |
+
# font-weight: 500;
|
484 |
+
# letter-spacing: 2px;
|
485 |
+
# background: linear-gradient(90deg, #555, #007ACC);
|
486 |
+
# -webkit-background-clip: text;
|
487 |
+
# -webkit-text-fill-color: transparent;
|
488 |
+
# margin: 0;
|
489 |
+
# text-transform: uppercase;
|
490 |
+
# display: inline-block;
|
491 |
+
# ">EXPLORE THE CODE →</p>
|
492 |
+
# <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/PawMatchAI" style="text-decoration: none;">
|
493 |
+
# <img src="https://img.shields.io/badge/GitHub-PawMatch_AI-007ACC?logo=github&style=for-the-badge">
|
494 |
+
# </a>
|
495 |
+
# </div>
|
496 |
+
# ''')
|
497 |
+
|
498 |
+
# return iface
|
499 |
+
|
500 |
+
# if __name__ == "__main__":
|
501 |
+
# iface = main()
|
502 |
+
# iface.launch()
|
503 |
|
504 |
|
|
|
505 |
|
506 |
history_manager = UserHistoryManager()
|
507 |
|
|
|
587 |
logits = self.classifier(attended_features)
|
588 |
return logits, attended_features
|
589 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
590 |
|
591 |
+
class ModelManager:
|
592 |
+
_instance = None
|
593 |
+
_initialized = False
|
594 |
+
|
595 |
+
def __init__(self):
|
596 |
+
self.model_yolo = None # YOLO模型
|
597 |
+
self.model = None # 品種分類模型
|
598 |
+
|
599 |
+
@classmethod
|
600 |
+
async def get_instance(cls):
|
601 |
+
if not cls._instance:
|
602 |
+
cls._instance = cls()
|
603 |
+
return cls._instance
|
604 |
+
|
605 |
+
@spaces.GPU
|
606 |
+
async def initialize(self):
|
607 |
+
# 只初始化一次模型
|
608 |
+
if not self._initialized:
|
609 |
+
# 初始化 YOLO
|
610 |
+
self.model_yolo = YOLO('yolov8l.pt')
|
611 |
+
|
612 |
+
# 初始化品種分類模型
|
613 |
+
num_classes = len(dog_breeds)
|
614 |
+
self.model = BaseModel(num_classes=num_classes)
|
615 |
+
checkpoint = torch.load('124_best_model_dog.pth')
|
616 |
+
self.model.load_state_dict(checkpoint['base_model'], strict=False)
|
617 |
+
self.model.eval()
|
618 |
+
|
619 |
+
self._initialized = True
|
620 |
+
|
621 |
|
622 |
# Image preprocessing function
|
623 |
def preprocess_image(image):
|
|
|
634 |
|
635 |
return transform(image).unsqueeze(0)
|
636 |
|
637 |
+
@spaces.GPU
|
638 |
async def predict_single_dog(image):
|
639 |
"""
|
640 |
Predicts the dog breed using only the classifier.
|
|
|
643 |
Returns:
|
644 |
tuple: (top1_prob, topk_breeds, relative_probs)
|
645 |
"""
|
646 |
+
manager = await ModelManager.get_instance()
|
647 |
+
await manager.initialize()
|
648 |
+
|
649 |
image_tensor = preprocess_image(image).to(device)
|
650 |
|
651 |
with torch.no_grad():
|
|
|
669 |
|
670 |
return probabilities[0], breeds[:3], relative_probs
|
671 |
|
672 |
+
@spaces.GPU
|
673 |
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
|
674 |
+
manager = await ModelManager.get_instance()
|
675 |
+
await manager.initialize()
|
676 |
+
|
677 |
+
results = manager.model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
678 |
dogs = []
|
679 |
boxes = []
|
680 |
for box in results.boxes:
|
|
|
754 |
|
755 |
return comparison_data
|
756 |
|
757 |
+
@spaces.GPU
|
758 |
async def predict(image):
|
759 |
"""
|
760 |
Main prediction function that handles both single and multiple dog detection.
|
|
|
765 |
Returns:
|
766 |
tuple: (html_output, annotated_image, initial_state)
|
767 |
"""
|
768 |
+
manager = await ModelManager.get_instance()
|
769 |
+
await manager.initialize()
|
770 |
+
|
771 |
if image is None:
|
772 |
return format_warning_html("Please upload an image to start."), None, None
|
773 |
|