-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
98 lines (78 loc) · 2.42 KB
/
models.py
File metadata and controls
98 lines (78 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Core data models for the object detection system.
"""
from dataclasses import dataclass
from typing import Tuple, List, Optional
import numpy as np
@dataclass
class Point:
"""Represents a 2D point."""
x: float
y: float
@dataclass
class BoundingBox:
"""Represents a bounding box for detected objects."""
x: int
y: int
width: int
height: int
@property
def center(self) -> Point:
"""Get the center point of the bounding box."""
return Point(self.x + self.width // 2, self.y + self.height // 2)
@property
def area(self) -> int:
"""Get the area of the bounding box."""
return self.width * self.height
@dataclass
class Color:
"""Represents a color with RGB values and name."""
r: int
g: int
b: int
name: str
confidence: float = 1.0
def to_rgb_tuple(self) -> Tuple[int, int, int]:
"""Convert to RGB tuple."""
return (self.r, self.g, self.b)
def to_bgr_tuple(self) -> Tuple[int, int, int]:
"""Convert to BGR tuple (for OpenCV)."""
return (self.b, self.g, self.r)
@dataclass
class Shape:
"""Represents a detected shape with its properties."""
name: str
confidence: float = 1.0
vertices: int = 0
area_ratio: float = 0.0 # Ratio of contour area to bounding box area
aspect_ratio: float = 0.0 # Width to height ratio
@dataclass
class DetectedObject:
"""Represents a detected object with its properties."""
bounding_box: BoundingBox
contour: np.ndarray
color: Optional[Color] = None
shape: Optional[Shape] = None
confidence: float = 1.0
object_id: Optional[int] = None
@property
def center(self) -> Point:
"""Get the center point of the object."""
return self.bounding_box.center
@property
def area(self) -> int:
"""Get the area of the object."""
return self.bounding_box.area
@dataclass
class DetectionResult:
"""Container for detection results."""
objects: List[DetectedObject]
frame: np.ndarray
timestamp: float
def __len__(self) -> int:
"""Get the number of detected objects."""
return len(self.objects)
def get_objects_by_color(self, color_name: str) -> List[DetectedObject]:
"""Get objects with a specific color."""
return [obj for obj in self.objects
if obj.color and obj.color.name.lower() == color_name.lower()]