from .result import Result
from dataclasses import InitVar, dataclass, field
from datetime import datetime, timedelta
from typing import Any, Optional
[docs]
@dataclass(slots=True)
class AnnotationTimestamp:
"""
Represents a single annotation timestamp in a Ground Truth JSON file.
Attributes:
annotation_id (int): Unique identifier for the annotation.
timestamp (int): The timestamp of the annotation in seconds from the UNIX epoch.
timestamp_datetime (datetime): The timestamp of the annotation as a datetime object.
normalised_timestamp (timedelta): The normalised timestamp of the annotation as a timedelta object.
image_id (int): Identifier of the image containing the annotation.
image_filename (str): Filename of the image corresponding to the image ID.
alpha_for_image (float): The alpha value for the image corresponding to the image ID.
"""
annotation_id: int = field(init=False)
timestamp: int = field(init=False)
timestamp_datetime: datetime = field(init=False)
normalised_timestamp: timedelta = field(init=False)
image_id: int = field(init=False)
image_filename: str = field(init=False)
alpha_for_image: float = field(init=False)
annotation_timestamp_data: InitVar[dict[str, Any]]
result: InitVar[Result]
def __post_init__(self, annotation_timestamp_data: dict[str, Any], result: Result):
annotation_id = annotation_timestamp_data.get('uid')
if annotation_id is None:
raise ValueError("Annotation timestamp data does not contain 'id' key.")
self.annotation_id = annotation_id
timestamp = annotation_timestamp_data.get('timestamp')
if timestamp is None:
raise ValueError("Annotation timestamp data does not contain 'timestamp' key.")
self.timestamp = timestamp
timestamp_str: Optional[str] = annotation_timestamp_data.get('timestamp_iso')
if timestamp_str is None:
raise ValueError("Annotation timestamp data does not contain 'timestamp_iso' key.")
self.timestamp_datetime = datetime.fromisoformat(timestamp_str)
image_id = annotation_timestamp_data.get('img_id')
if image_id is None:
raise ValueError("Annotation timestamp data does not contain 'img_id' key.")
self.image_id = image_id
# Find the image filename corresponding to the image ID in the Ground Truths
image_filename = result.gts[0].image_id_to_filename.get(self.image_id)
if image_filename is None:
raise ValueError(f"Image ID {self.image_id} in annotation timestamp data not found in image ID to filename mapping.")
self.image_filename = image_filename
# Find the alpha value for all images from the Result object and store it in the AnnotationTimestamp object
alpha_for_image = result.alpha_per_image[self.image_filename]
if alpha_for_image is None:
raise ValueError(f"Alpha value for image {self.image_filename} not found in Result object.")
self.alpha_for_image = alpha_for_image