Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -167,33 +167,6 @@ async def predict_single_dog(image):
|
|
| 167 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 168 |
|
| 169 |
|
| 170 |
-
# async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
| 171 |
-
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 172 |
-
# dogs = []
|
| 173 |
-
# boxes = []
|
| 174 |
-
# for box in results.boxes:
|
| 175 |
-
# if box.cls == 16: # COCO dataset class for dog is 16
|
| 176 |
-
# xyxy = box.xyxy[0].tolist()
|
| 177 |
-
# confidence = box.conf.item()
|
| 178 |
-
# boxes.append((xyxy, confidence))
|
| 179 |
-
|
| 180 |
-
# if not boxes:
|
| 181 |
-
# dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 182 |
-
# else:
|
| 183 |
-
# nms_boxes = non_max_suppression(boxes, iou_threshold)
|
| 184 |
-
|
| 185 |
-
# for box, confidence in nms_boxes:
|
| 186 |
-
# x1, y1, x2, y2 = box
|
| 187 |
-
# w, h = x2 - x1, y2 - y1
|
| 188 |
-
# x1 = max(0, x1 - w * 0.05)
|
| 189 |
-
# y1 = max(0, y1 - h * 0.05)
|
| 190 |
-
# x2 = min(image.width, x2 + w * 0.05)
|
| 191 |
-
# y2 = min(image.height, y2 + h * 0.05)
|
| 192 |
-
# cropped_image = image.crop((x1, y1, x2, y2))
|
| 193 |
-
# dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
| 194 |
-
|
| 195 |
-
# return dogs
|
| 196 |
-
|
| 197 |
async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
| 198 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 199 |
dogs = []
|
|
@@ -212,19 +185,16 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
|
| 212 |
for box, confidence in nms_boxes:
|
| 213 |
x1, y1, x2, y2 = box
|
| 214 |
w, h = x2 - x1, y2 - y1
|
| 215 |
-
x1 = max(0, x1 - w * 0.
|
| 216 |
-
y1 = max(0, y1 - h * 0.
|
| 217 |
-
x2 = min(image.width, x2 + w * 0.
|
| 218 |
-
y2 = min(image.height, y2 + h * 0.
|
| 219 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 220 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
| 221 |
|
| 222 |
-
# 如果只检测到一只狗,但置信度较低,添加整张图片作为备选
|
| 223 |
-
if len(dogs) == 1 and dogs[0][1] < 0.5:
|
| 224 |
-
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 225 |
-
|
| 226 |
return dogs
|
| 227 |
|
|
|
|
| 228 |
def non_max_suppression(boxes, iou_threshold):
|
| 229 |
keep = []
|
| 230 |
boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
|
@@ -296,72 +266,6 @@ async def process_single_dog(image):
|
|
| 296 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
| 297 |
|
| 298 |
|
| 299 |
-
# async def predict(image):
|
| 300 |
-
# if image is None:
|
| 301 |
-
# return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
| 302 |
-
|
| 303 |
-
# try:
|
| 304 |
-
# if isinstance(image, np.ndarray):
|
| 305 |
-
# image = Image.fromarray(image)
|
| 306 |
-
|
| 307 |
-
# dogs = await detect_multiple_dogs(image)
|
| 308 |
-
|
| 309 |
-
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 310 |
-
# explanations = []
|
| 311 |
-
# buttons = []
|
| 312 |
-
# annotated_image = image.copy()
|
| 313 |
-
# draw = ImageDraw.Draw(annotated_image)
|
| 314 |
-
# font = ImageFont.load_default()
|
| 315 |
-
|
| 316 |
-
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
| 317 |
-
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
| 318 |
-
# color = color_list[i % len(color_list)]
|
| 319 |
-
# draw.rectangle(box, outline=color, width=3)
|
| 320 |
-
# draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
| 321 |
-
|
| 322 |
-
# combined_confidence = detection_confidence * top1_prob
|
| 323 |
-
|
| 324 |
-
# if top1_prob >= 0.5:
|
| 325 |
-
# breed = topk_breeds[0]
|
| 326 |
-
# description = get_dog_description(breed)
|
| 327 |
-
# formatted_description = format_description(description, breed)
|
| 328 |
-
# explanations.append(f"Dog {i+1}: {formatted_description}")
|
| 329 |
-
# elif combined_confidence >= 0.2:
|
| 330 |
-
# dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
| 331 |
-
# dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 332 |
-
# explanations.append(dog_explanation)
|
| 333 |
-
# buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
|
| 334 |
-
# else:
|
| 335 |
-
# explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image.")
|
| 336 |
-
|
| 337 |
-
# final_explanation = "\n\n".join(explanations)
|
| 338 |
-
# if buttons:
|
| 339 |
-
# final_explanation += "\n\nClick on a button to view more information about the breed."
|
| 340 |
-
# initial_state = {
|
| 341 |
-
# "explanation": final_explanation,
|
| 342 |
-
# "buttons": buttons,
|
| 343 |
-
# "show_back": True,
|
| 344 |
-
# "image": annotated_image,
|
| 345 |
-
# "is_multi_dog": len(dogs) > 1,
|
| 346 |
-
# "dogs_info": explanations
|
| 347 |
-
# }
|
| 348 |
-
# return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
| 349 |
-
# else:
|
| 350 |
-
# initial_state = {
|
| 351 |
-
# "explanation": final_explanation,
|
| 352 |
-
# "buttons": [],
|
| 353 |
-
# "show_back": False,
|
| 354 |
-
# "image": annotated_image,
|
| 355 |
-
# "is_multi_dog": len(dogs) > 1,
|
| 356 |
-
# "dogs_info": explanations
|
| 357 |
-
# }
|
| 358 |
-
# return final_explanation, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
| 359 |
-
|
| 360 |
-
# except Exception as e:
|
| 361 |
-
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 362 |
-
# print(error_msg)
|
| 363 |
-
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
| 364 |
-
|
| 365 |
async def predict(image):
|
| 366 |
if image is None:
|
| 367 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
@@ -383,9 +287,7 @@ async def predict(image):
|
|
| 383 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
| 384 |
color = color_list[i % len(color_list)]
|
| 385 |
draw.rectangle(box, outline=color, width=3)
|
| 386 |
-
|
| 387 |
-
if len(dogs) > 1:
|
| 388 |
-
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
| 389 |
|
| 390 |
combined_confidence = detection_confidence * top1_prob
|
| 391 |
|
|
@@ -393,23 +295,14 @@ async def predict(image):
|
|
| 393 |
breed = topk_breeds[0]
|
| 394 |
description = get_dog_description(breed)
|
| 395 |
formatted_description = format_description(description, breed)
|
| 396 |
-
|
| 397 |
-
explanations.append(f"Breed: {breed}\n{formatted_description}")
|
| 398 |
-
else:
|
| 399 |
-
explanations.append(f"Dog {i+1}: Breed: {breed}\n{formatted_description}")
|
| 400 |
elif combined_confidence >= 0.2:
|
| 401 |
-
|
| 402 |
-
dog_explanation = f"Top 3 possible breeds:\n"
|
| 403 |
-
else:
|
| 404 |
-
dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
| 405 |
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 406 |
explanations.append(dog_explanation)
|
| 407 |
-
buttons.extend([f"
|
| 408 |
else:
|
| 409 |
-
|
| 410 |
-
explanations.append("The image is unclear or the breed is not in the dataset. Please upload a clearer image.")
|
| 411 |
-
else:
|
| 412 |
-
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image.")
|
| 413 |
|
| 414 |
final_explanation = "\n\n".join(explanations)
|
| 415 |
if buttons:
|
|
@@ -440,6 +333,7 @@ async def predict(image):
|
|
| 440 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
| 441 |
|
| 442 |
|
|
|
|
| 443 |
def show_details(choice, previous_output, initial_state):
|
| 444 |
if not choice:
|
| 445 |
return previous_output, gr.update(visible=True), initial_state
|
|
|
|
| 167 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 168 |
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
| 171 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 172 |
dogs = []
|
|
|
|
| 185 |
for box, confidence in nms_boxes:
|
| 186 |
x1, y1, x2, y2 = box
|
| 187 |
w, h = x2 - x1, y2 - y1
|
| 188 |
+
x1 = max(0, x1 - w * 0.05)
|
| 189 |
+
y1 = max(0, y1 - h * 0.05)
|
| 190 |
+
x2 = min(image.width, x2 + w * 0.05)
|
| 191 |
+
y2 = min(image.height, y2 + h * 0.05)
|
| 192 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 193 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
return dogs
|
| 196 |
|
| 197 |
+
|
| 198 |
def non_max_suppression(boxes, iou_threshold):
|
| 199 |
keep = []
|
| 200 |
boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
|
|
|
| 266 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
| 267 |
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
async def predict(image):
|
| 270 |
if image is None:
|
| 271 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
|
| 287 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
| 288 |
color = color_list[i % len(color_list)]
|
| 289 |
draw.rectangle(box, outline=color, width=3)
|
| 290 |
+
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
|
|
|
|
|
|
| 291 |
|
| 292 |
combined_confidence = detection_confidence * top1_prob
|
| 293 |
|
|
|
|
| 295 |
breed = topk_breeds[0]
|
| 296 |
description = get_dog_description(breed)
|
| 297 |
formatted_description = format_description(description, breed)
|
| 298 |
+
explanations.append(f"Dog {i+1}: {formatted_description}")
|
|
|
|
|
|
|
|
|
|
| 299 |
elif combined_confidence >= 0.2:
|
| 300 |
+
dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
|
|
|
|
|
|
|
|
|
| 301 |
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 302 |
explanations.append(dog_explanation)
|
| 303 |
+
buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
|
| 304 |
else:
|
| 305 |
+
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image.")
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
final_explanation = "\n\n".join(explanations)
|
| 308 |
if buttons:
|
|
|
|
| 333 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
| 334 |
|
| 335 |
|
| 336 |
+
|
| 337 |
def show_details(choice, previous_output, initial_state):
|
| 338 |
if not choice:
|
| 339 |
return previous_output, gr.update(visible=True), initial_state
|