diff --git a/mapillary_tools/exif_read.py b/mapillary_tools/exif_read.py index 08ecbd8c..a18eab89 100644 --- a/mapillary_tools/exif_read.py +++ b/mapillary_tools/exif_read.py @@ -405,6 +405,10 @@ def extract_capture_time(self) -> datetime.datetime | None: def extract_direction(self) -> float | None: raise NotImplementedError + @abc.abstractmethod + def extract_exif_datetime(self) -> datetime.datetime | None: + raise NotImplementedError + @abc.abstractmethod def extract_lon_lat(self) -> tuple[float, float] | None: raise NotImplementedError diff --git a/mapillary_tools/geotag/image_extractors/exif.py b/mapillary_tools/geotag/image_extractors/exif.py index fe31d358..8422190d 100644 --- a/mapillary_tools/geotag/image_extractors/exif.py +++ b/mapillary_tools/geotag/image_extractors/exif.py @@ -81,10 +81,19 @@ def extract(self) -> types.ImageMetadata: ) unix_time = _capture_unix_time(capture_time) + exif_time: float | None = None + exif_datetime = exif.extract_exif_datetime() + if exif_datetime is not None: + try: + exif_time = _capture_unix_time(exif_datetime) + except exceptions.MapillaryGeoTaggingError: + pass + image_metadata = types.ImageMetadata( filename=self.image_path, filesize=utils.get_file_size(self.image_path), time=unix_time, + exif_time=exif_time, lat=lat, lon=lon, alt=exif.extract_altitude(), diff --git a/mapillary_tools/types.py b/mapillary_tools/types.py index 98c26036..2dae1fea 100644 --- a/mapillary_tools/types.py +++ b/mapillary_tools/types.py @@ -9,6 +9,7 @@ import dataclasses import enum import hashlib +import math import typing as T import uuid from pathlib import Path @@ -42,6 +43,11 @@ class ImageMetadata(geo.Point): width: int | None = None height: int | None = None filesize: int | None = None + # The EXIF capture time (with subseconds) in Unix time, if the image has + # one. It is not written anywhere: the capture time may come from GPS, + # which usually has no subseconds, so this keeps images taken within the + # same second in order when sorting (see sort_key()) + exif_time: float | None = None # Fields starting with MAP* will be written to the image EXIF MAPSequenceUUID: str | None = None @@ -64,7 +70,8 @@ def sort_key(self): """ For sorting images in a sequence """ - return (self.time, self.filename.name) + exif_time = self.exif_time if self.exif_time is not None else math.inf + return (self.time, exif_time, self.filename.name) @dataclasses.dataclass diff --git a/tests/unit/test_sequence_processing.py b/tests/unit/test_sequence_processing.py index 2d86fcdd..0ca3e466 100644 --- a/tests/unit/test_sequence_processing.py +++ b/tests/unit/test_sequence_processing.py @@ -319,6 +319,43 @@ def test_sequences_sorted(tmpdir: py.path.local): assert abs(x - y) < 0.00001, (x, y) +def test_sequences_sorted_by_exif_time_within_same_time(tmpdir: py.path.local): + # GPS times have no subseconds, so images taken within the same second + # share the capture time; OpenCamera names the second one "... (1).jpg", + # which sorts before the first one by filename + curdir = tmpdir.mkdir("hello_exif").mkdir("world_exif") + sequence: T.List[types.ImageMetadata] = [ + _make_image_metadata( + Path(curdir) / Path("IMG_170430.jpg"), 1, 1.00000, 30, exif_time=30.2 + ), + _make_image_metadata( + Path(curdir) / Path("IMG_170431 (1).jpg"), 1, 1.00006, 31, exif_time=31.6 + ), + _make_image_metadata( + Path(curdir) / Path("IMG_170431.jpg"), 1, 1.00003, 31, exif_time=31.1 + ), + _make_image_metadata( + Path(curdir) / Path("IMG_170432.jpg"), 1, 1.00009, 32, exif_time=32.3 + ), + ] + metadatas = psp.process_sequence_properties( + sequence, + cutoff_distance=100, + cutoff_time=10, + interpolate_directions=False, + duplicate_distance=0, + duplicate_angle=0, + ) + image_metadatas = [d for d in metadatas if isinstance(d, types.ImageMetadata)] + assert [ + "IMG_170430.jpg", + "IMG_170431.jpg", + "IMG_170431 (1).jpg", + "IMG_170432.jpg", + ] == [d.filename.name for d in image_metadatas] + assert [30, 31, 31.5, 32] == [d.time for d in image_metadatas] + + def test_cut_sequences(tmpdir: py.path.local): curdir = tmpdir.mkdir("hello11").mkdir("world22") sequence: T.List[types.ImageMetadata] = [