Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import torch
|
|
4 |
import torch.nn as nn
|
5 |
import gradio as gr
|
6 |
from torchvision.models import efficientnet_v2_m, EfficientNet_V2_M_Weights
|
7 |
-
from torchvision.ops import nms
|
8 |
import torch.nn.functional as F
|
9 |
from torchvision import transforms
|
10 |
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
@@ -250,7 +250,7 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4, me
|
|
250 |
confidences = []
|
251 |
|
252 |
for box in results.boxes:
|
253 |
-
if box.cls == 16: # COCO
|
254 |
xyxy = box.xyxy[0].tolist()
|
255 |
confidence = box.conf.item()
|
256 |
boxes.append(torch.tensor(xyxy))
|
@@ -260,7 +260,7 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4, me
|
|
260 |
boxes = torch.stack(boxes)
|
261 |
confidences = torch.tensor(confidences)
|
262 |
|
263 |
-
#
|
264 |
keep = nms(boxes, confidences, iou_threshold)
|
265 |
|
266 |
for i in keep:
|
@@ -269,7 +269,7 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4, me
|
|
269 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
270 |
dogs.append((cropped_image, confidence, xyxy))
|
271 |
|
272 |
-
#
|
273 |
merged_dogs = []
|
274 |
while dogs:
|
275 |
base_dog = dogs.pop(0)
|
|
|
4 |
import torch.nn as nn
|
5 |
import gradio as gr
|
6 |
from torchvision.models import efficientnet_v2_m, EfficientNet_V2_M_Weights
|
7 |
+
from torchvision.ops import nms, box_iou
|
8 |
import torch.nn.functional as F
|
9 |
from torchvision import transforms
|
10 |
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
|
|
250 |
confidences = []
|
251 |
|
252 |
for box in results.boxes:
|
253 |
+
if box.cls == 16: # COCO 數據集中狗的類別是 16
|
254 |
xyxy = box.xyxy[0].tolist()
|
255 |
confidence = box.conf.item()
|
256 |
boxes.append(torch.tensor(xyxy))
|
|
|
260 |
boxes = torch.stack(boxes)
|
261 |
confidences = torch.tensor(confidences)
|
262 |
|
263 |
+
# 應用非極大值抑制 (NMS)
|
264 |
keep = nms(boxes, confidences, iou_threshold)
|
265 |
|
266 |
for i in keep:
|
|
|
269 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
270 |
dogs.append((cropped_image, confidence, xyxy))
|
271 |
|
272 |
+
# 合併鄰近的邊界框
|
273 |
merged_dogs = []
|
274 |
while dogs:
|
275 |
base_dog = dogs.pop(0)
|