Reference for ultralytics/trackers/utils/gmc.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/trackers/utils/gmc.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.trackers.utils.gmc.GMC
Generalized Motion Compensation (GMC) class for tracking and object detection in video frames.
This class provides methods for tracking and detecting objects based on several tracking algorithms including ORB, SIFT, ECC, and Sparse Optical Flow. It also supports downscaling of frames for computational efficiency.
Attributes:
Name | Type | Description |
---|---|---|
method |
str
|
The method used for tracking. Options include 'orb', 'sift', 'ecc', 'sparseOptFlow', 'none'. |
downscale |
int
|
Factor by which to downscale the frames for processing. |
prevFrame |
ndarray
|
Stores the previous frame for tracking. |
prevKeyPoints |
List
|
Stores the keypoints from the previous frame. |
prevDescriptors |
ndarray
|
Stores the descriptors from the previous frame. |
initializedFirstFrame |
bool
|
Flag to indicate if the first frame has been processed. |
Methods:
Name | Description |
---|---|
apply |
Applies the chosen method to a raw frame and optionally uses provided detections. |
applyEcc |
Applies the ECC algorithm to a raw frame. |
applyFeatures |
Applies feature-based methods like ORB or SIFT to a raw frame. |
applySparseOptFlow |
Applies the Sparse Optical Flow method to a raw frame. |
reset_params |
Resets the internal parameters of the GMC object. |
Examples:
Create a GMC object and apply it to a frame
>>> gmc = GMC(method="sparseOptFlow", downscale=2)
>>> frame = np.array([[1, 2, 3], [4, 5, 6]])
>>> processed_frame = gmc.apply(frame)
>>> print(processed_frame)
array([[1, 2, 3],
[4, 5, 6]])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
The method used for tracking. Options include 'orb', 'sift', 'ecc', 'sparseOptFlow', 'none'. |
'sparseOptFlow'
|
downscale
|
int
|
Downscale factor for processing frames. |
2
|
Examples:
Initialize a GMC object with the 'sparseOptFlow' method and a downscale factor of 2
Source code in ultralytics/trackers/utils/gmc.py
apply
Apply object detection on a raw frame using the specified method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_frame
|
ndarray
|
The raw frame to be processed, with shape (H, W, C). |
required |
detections
|
List | None
|
List of detections to be used in the processing. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
Processed frame with applied object detection. |
Examples:
>>> gmc = GMC(method="sparseOptFlow")
>>> raw_frame = np.random.rand(480, 640, 3)
>>> processed_frame = gmc.apply(raw_frame)
>>> print(processed_frame.shape)
(480, 640, 3)
Source code in ultralytics/trackers/utils/gmc.py
applyEcc
Apply the ECC (Enhanced Correlation Coefficient) algorithm to a raw frame for motion compensation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_frame
|
ndarray
|
The raw frame to be processed, with shape (H, W, C). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The processed frame with the applied ECC transformation. |
Examples:
>>> gmc = GMC(method="ecc")
>>> processed_frame = gmc.applyEcc(np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]))
>>> print(processed_frame)
[[1. 0. 0.]
[0. 1. 0.]]
Source code in ultralytics/trackers/utils/gmc.py
applyFeatures
Apply feature-based methods like ORB or SIFT to a raw frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_frame
|
ndarray
|
The raw frame to be processed, with shape (H, W, C). |
required |
detections
|
List | None
|
List of detections to be used in the processing. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
Processed frame. |
Examples:
>>> gmc = GMC(method="orb")
>>> raw_frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
>>> processed_frame = gmc.applyFeatures(raw_frame)
>>> print(processed_frame.shape)
(2, 3)
Source code in ultralytics/trackers/utils/gmc.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
applySparseOptFlow
Apply Sparse Optical Flow method to a raw frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_frame
|
ndarray
|
The raw frame to be processed, with shape (H, W, C). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Processed frame with shape (2, 3). |
Examples:
>>> gmc = GMC()
>>> result = gmc.applySparseOptFlow(np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]))
>>> print(result)
[[1. 0. 0.]
[0. 1. 0.]]
Source code in ultralytics/trackers/utils/gmc.py
reset_params
Reset the internal parameters including previous frame, keypoints, and descriptors.