Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mapillary_tools/exif_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions mapillary_tools/geotag/image_extractors/exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
9 changes: 8 additions & 1 deletion mapillary_tools/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dataclasses
import enum
import hashlib
import math
import typing as T
import uuid
from pathlib import Path
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_sequence_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down