Skip to content

Commit

Permalink
fix typing & remove test
Browse files Browse the repository at this point in the history
  • Loading branch information
yibeichan committed Oct 19, 2024
1 parent 7bf5ff8 commit 85576b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
40 changes: 16 additions & 24 deletions facetracker/face_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


class FaceTracker:
"""A class for tracking faces across video frames."""

Expand Down Expand Up @@ -49,29 +48,22 @@ def expand_box(self, box: List[float], expansion_ratio: float = 0.1) -> List[flo

@staticmethod
def calculate_iou(box1: List[float], box2: List[float]) -> float:
"""Calculate Intersection over Union (IoU) between two bounding boxes.
Args:
box1 (List[float]): First bounding box coordinates.
box2 (List[float]): Second bounding box coordinates.
Returns:
float: IoU value.
"""
box1 = torch.tensor(box1, device=device, dtype=torch.float32)
box2 = torch.tensor(box2, device=device, dtype=torch.float32)

x1_inter = torch.max(box1[0], box2[0])
y1_inter = torch.max(box1[1], box2[1])
x2_inter = torch.min(box1[2], box2[2])
y2_inter = torch.min(box1[3], box2[3])

inter_area = torch.max(
torch.tensor(0.0, device=device), x2_inter - x1_inter
) * torch.max(torch.tensor(0.0, device=device), y2_inter - y1_inter)

box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])
"""Calculate Intersection over Union (IoU) between two bounding boxes."""
box1_tensor = torch.tensor(box1, device=device, dtype=torch.float32)
box2_tensor = torch.tensor(box2, device=device, dtype=torch.float32)

x1_inter = torch.max(box1_tensor[0], box2_tensor[0])
y1_inter = torch.max(box1_tensor[1], box2_tensor[1])
x2_inter = torch.min(box1_tensor[2], box2_tensor[2])
y2_inter = torch.min(box1_tensor[3], box2_tensor[3])

inter_area = (
torch.clamp(x2_inter - x1_inter, min=0) *
torch.clamp(y2_inter - y1_inter, min=0)
)

box1_area = (box1_tensor[2] - box1_tensor[0]) * (box1_tensor[3] - box1_tensor[1])
box2_area = (box2_tensor[2] - box2_tensor[0]) * (box2_tensor[3] - box2_tensor[1])

iou = inter_area / (box1_area + box2_area - inter_area + 1e-6)
return iou.item()
Expand Down
15 changes: 0 additions & 15 deletions tests/test_app.py

This file was deleted.

0 comments on commit 85576b0

Please sign in to comment.