Source code for rapidtools.processing.street_objects
# Copyright (c) 2025 The University of Washington
#
# This file is part of rapidtools.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# You should have received a copy of the BSD 3-Clause License along with
# rapidtools. If not, see <http://www.opensource.org/licenses/>.
#
# Contributors:
# Contributors:
# Barbaros Cetiner
#
# Last updated:
# 10-05-2026
"""
Discover and photograph objects along a street-level survey.
Two pipeline components mirror the aerial detect / extract stages for
street-level imagery on Mapillary:
- :class:`MapillaryFeatureExtractor` (``DETECT``) finds objects of the
requested classes in every image of a region and places them on the ground.
Mapillary's own segmentation supplies the detections as metadata, so no
pixels are downloaded; SAM 3 is used only for classes the Mapillary
vocabulary lacks. Each object becomes a point
:class:`~rapidtools.core.PhysicalAsset` carrying its
:class:`~rapidtools.core.Observation` list.
- :class:`MapillaryObjectImageExtractor` (``EXTRACT_IMAGERY``) downloads the
best one or two images per object and crops the detection, attaching the
crops as :class:`~rapidtools.core.ImageAsset` objects so that
:class:`~rapidtools.processing.AssetAnalyzer` can judge each object.
Example:
>>> from rapidtools.core import BoundingBox, PhysicalAssetCollection
>>> from rapidtools.processing import (
... MapillaryFeatureExtractor, MapillaryObjectImageExtractor, Pipeline
... )
>>> spokane = BoundingBox(-117.5358, 47.6885, -117.4426, 47.7378)
>>> pipeline = Pipeline([
... MapillaryFeatureExtractor(
... classes=['vehicles'], access_token='MLY|...', region=spokane,
... start_date='2025-08-01', end_date='2025-09-30',
... ),
... MapillaryObjectImageExtractor(
... 'output/vehicle_crops', access_token='MLY|...'
... ),
... ])
>>> vehicles = pipeline.run(PhysicalAssetCollection()) # doctest: +SKIP
>>> vehicles[0].attributes['n_observations'] # doctest: +SKIP
4
"""
from __future__ import annotations
import difflib
import logging
import math
import threading
import warnings
from collections import Counter, defaultdict
from collections.abc import Callable, Iterable, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor, as_completed
from io import BytesIO
from pathlib import Path
from typing import Any
import numpy as np
from PIL import Image
from shapely import STRtree
from shapely.geometry import Point, Polygon
from tqdm import tqdm
from rapidtools.config import REQUESTS_TIMEOUT_VAL
from rapidtools.core import (
BoundingBox,
ImageAsset,
Observation,
OperationCanceled,
PhysicalAsset,
PhysicalAssetCollection,
PolygonRegion,
raise_if_canceled,
)
from rapidtools.data_sources import MapillaryClient, MapillaryLabels
from rapidtools.data_sources.mapillary_client import (
is_map_feature_value,
resolve_mapillary_token,
)
from . import outlines
from .aerial_evidence import AerialEvidence
from .reid import DEFAULT_MIN_WIDTH_PX, DEFAULT_REID_MODEL
from .step import Stage
from .street_footprints import footprint_sightings
from .street_fusion import fuse_observations, object_attributes
from .street_instances import discover_instances, mask_outlines
from .street_localization import (
DEFAULT_MAX_RANGE_M,
MIN_POLYGON_AREA_FRACTION,
cluster_observations,
estimate_ego_mask,
intersect_bearings,
local_projection,
localize,
polygon_angular_size,
simplify_polygon,
thin_frames,
)
from .street_structures import (
discover_structures,
is_structure_label,
structure_attributes,
)
from .street_tiles import cut_tile, ground_range_m, join_tile_masks, tile_layout
from .street_tracking import (
CameraFrame,
discover_objects,
estimate_attributes,
)
logger = logging.getLogger(__name__)
#: Mapillary labels that describe motor vehicles.
#: Motor vehicles. Trailers, caravans, boats, bicycles and Mapillary's
#: catch-all "other vehicle" (jet skis, equipment, towed loads) are not
#: vehicles in the sense a damage survey means; ask for them by name.
VEHICLE_LABELS: tuple[str, ...] = (
'object--vehicle--car',
'object--vehicle--truck',
'object--vehicle--bus',
'object--vehicle--motorcycle',
)
#: Everything Mapillary files under ``object--vehicle--*`` except bicycles
#: and rail vehicles, for callers who really want all of it.
ALL_VEHICLE_LABELS: tuple[str, ...] = VEHICLE_LABELS + (
'object--vehicle--other-vehicle',
'object--vehicle--vehicle-group',
'object--vehicle--trailer',
'object--vehicle--caravan',
'object--vehicle--boat',
)
# Plain-English class names that resolve without a language model. Keys are
# lower-case singular forms; simple plurals are stripped before lookup.
_CLASS_ALIASES: dict[str, tuple[str, ...]] = {
'vehicle': VEHICLE_LABELS,
'motor vehicle': VEHICLE_LABELS,
'all vehicle': ALL_VEHICLE_LABELS,
'any vehicle': ALL_VEHICLE_LABELS,
'car': ('object--vehicle--car',),
'truck': ('object--vehicle--truck',),
'bus': ('object--vehicle--bus',),
'trailer': ('object--vehicle--trailer', 'object--vehicle--caravan'),
'caravan': ('object--vehicle--caravan',),
'rv': ('object--vehicle--caravan',),
'motorcycle': ('object--vehicle--motorcycle',),
'bicycle': ('object--vehicle--bicycle',),
'bike': ('object--vehicle--bicycle',),
'boat': ('object--vehicle--boat',),
'pole': ('object--support--pole', 'object--support--utility-pole'),
'utility pole': ('object--support--utility-pole',),
'power pole': ('object--support--utility-pole',),
'street light': ('object--street-light',),
'streetlight': ('object--street-light',),
'traffic light': ('object--traffic-light--general',),
'traffic sign': ('object--traffic-sign--front', 'object--traffic-sign--back'),
'fire hydrant': ('object--fire-hydrant',),
'hydrant': ('object--fire-hydrant',),
'bench': ('object--bench',),
'trash can': ('object--trash-can',),
'mailbox': ('object--mailbox',),
'manhole': ('object--manhole',),
'catch basin': ('object--catch-basin',),
'driveway': ('construction--flat--driveway',),
'person': ('human--person--individual',),
'pedestrian': ('human--person--individual',),
'building': ('construction--structure--building',),
'bridge': ('construction--structure--bridge',),
'tunnel': ('construction--structure--tunnel',),
'fence': ('construction--barrier--fence',),
'guard rail': ('construction--barrier--guard-rail',),
'wall': ('construction--barrier--wall',),
}
# Metadata fetched for every image in the region. Detections are added when
# the Mapillary detection source is active.
_IMAGE_FIELDS: tuple[str, ...] = (
'captured_at',
'compass_angle',
'computed_compass_angle',
'geometry',
'computed_geometry',
'is_pano',
'camera_type',
'camera_parameters',
'computed_rotation',
'computed_altitude',
'width',
'height',
'sequence',
'thumb_1024_url',
'thumb_2048_url',
)
_DETECTION_FIELDS: tuple[str, ...] = ('detections.value', 'detections.geometry')
_PANO_TYPES = ('spherical', 'equirectangular')
_LOCALIZATION_METHODS = (
'auto',
'fusion',
'points',
'instances',
'footprints',
'tracks',
'voting',
'cluster',
)
_DETECTION_SOURCES = ('auto', 'mapillary', 'sam3', 'sam3_tiles', 'map_features')
# ``simplify_tolerance`` is capped at this share of an outline's longest side.
_RELATIVE_TOLERANCE = 0.01
# A structure found from the point cloud is a building the footprint layer
# lacks when less than this share of it lies on any one known footprint.
# Pieces of a mapped house reach past it; on a 1 km test area with a tenth
# of the houses taken out of the layer, 91 % of those were reported and
# nothing that overlapped a house still in the layer.
_OFF_FOOTPRINT_SHARE = 0.1
def mapillary_vocabulary() -> frozenset[str]:
"""
Every label in :class:`~rapidtools.data_sources.MapillaryLabels`.
Example:
>>> 'object--vehicle--car' in mapillary_vocabulary()
True
"""
return frozenset(
value
for name, value in vars(MapillaryLabels).items()
if not name.startswith('_') and isinstance(value, str) and '--' in value
)
def _known_footprints(source: Any) -> list[tuple[str | None, Polygon, dict[str, Any]]]:
"""
Identifier, polygon and attributes of every known footprint.
``source`` is a ``PhysicalAssetCollection`` (or any iterable of assets),
the path of a GeoJSON file, or polygons in longitude and latitude. Only
polygons are kept; of a footprint in several parts, the largest.
"""
if source is None:
return []
if isinstance(source, (str, Path)):
source = PhysicalAssetCollection.from_geojson(source)
known: list[tuple[str | None, Polygon, dict[str, Any]]] = []
for item in source:
shape = getattr(item, 'geometry', item)
parts = list(getattr(shape, 'geoms', [shape]))
parts = [p for p in parts if isinstance(p, Polygon) and not p.is_empty]
if not parts:
continue
attributes = dict(getattr(item, 'attributes', None) or {})
attributes.pop('observations', None)
asset_id = getattr(item, 'id', None)
known.append(
(
None if asset_id is None else str(asset_id),
max(parts, key=lambda p: p.area),
attributes,
)
)
return known
class _OffFootprints:
"""
Whether a shape lies off every known footprint.
True when less than ``_OFF_FOOTPRINT_SHARE`` of the shape's area lies on
any one footprint. Shapes and footprints are in longitude and latitude;
only the ratio of areas is used, which the degrees do not change.
"""
def __init__(self, footprints: Sequence[Polygon]):
self.footprints = list(footprints)
self.tree = STRtree(self.footprints)
def __call__(self, shape: Polygon) -> bool:
shape = shape if shape.is_valid else shape.buffer(0)
if shape.is_empty or shape.area <= 0:
return False
for n in self.tree.query(shape):
on = self.footprints[int(n)].intersection(shape).area
if on >= _OFF_FOOTPRINT_SHARE * shape.area:
return False
return True
def _mask_stack(masks: Any) -> np.ndarray:
"""An ``(n, height, width)`` boolean stack from a model's masks of one image."""
if masks is None:
return np.zeros((0, 1, 1), bool)
stack = np.asarray(masks)
if stack.ndim == 2:
stack = stack[None]
return stack.astype(bool)
def resolve_classes(
classes: Iterable[str], label_mapper: Any = None
) -> tuple[dict[str, tuple[str, ...]], list[str]]:
"""
Translate user class names into Mapillary labels.
Resolution order: an exact Mapillary label, a built-in alias
(``'cars'`` -> ``object--vehicle--car``), then ``label_mapper`` (a
:class:`~rapidtools.processing.MapillaryLabelMapper`) when given.
Args:
classes: Class names as the user typed them.
label_mapper: Optional object with ``map_classes(list[str])``.
Returns:
tuple[dict[str, tuple[str, ...]], list[str]]:
``(resolved, unresolved)`` where ``resolved`` maps each class to
its Mapillary labels and ``unresolved`` lists the classes no
source could translate.
Example:
>>> resolved, unresolved = resolve_classes(['Cars', 'debris pile'])
>>> resolved
{'Cars': ('object--vehicle--car',)}
>>> unresolved
['debris pile']
"""
vocabulary = mapillary_vocabulary()
resolved: dict[str, tuple[str, ...]] = {}
unresolved: list[str] = []
for cls in classes:
text = cls.strip()
key = text.lower()
if text in vocabulary:
resolved[cls] = (text,)
continue
singular = (
key[:-2] if key.endswith('es') and key[:-2] in _CLASS_ALIASES else key
)
singular = (
singular[:-1]
if singular.endswith('s') and singular[:-1] in _CLASS_ALIASES
else singular
)
if singular in _CLASS_ALIASES:
resolved[cls] = _CLASS_ALIASES[singular]
continue
if label_mapper is not None:
try:
labels = tuple(
lbl for lbl in label_mapper.map_classes([text]) if lbl in vocabulary
)
except Exception as exc: # noqa: BLE001 - mapper failures are not fatal
logger.warning(f"Label mapper failed for '{text}': {exc}")
labels = ()
if labels:
resolved[cls] = labels
continue
unresolved.append(cls)
return resolved, unresolved
def _slug(text: str) -> str:
"""Lower-case identifier-safe version of a class name."""
return ''.join(ch if ch.isalnum() else '_' for ch in text.strip().lower()).strip(
'_'
)
def _camera_pose(props: dict[str, Any]) -> tuple[float, float, float] | None:
"""Return ``(lon, lat, compass)`` from image properties, or ``None``."""
lon = lat = None
for key in ('computed_geometry', 'geometry'):
geom = props.get(key)
if isinstance(geom, dict) and geom.get('coordinates'):
lon, lat = geom['coordinates'][:2]
break
if lon is None:
lon, lat = props.get('longitude'), props.get('latitude')
# A key present with a null value must fall through to the raw angle:
compass = props.get('computed_compass_angle')
if compass is None:
compass = props.get('compass_angle')
if lon is None or lat is None or compass is None:
return None
return float(lon), float(lat), float(compass) % 360.0
def _is_pano(props: dict[str, Any]) -> bool:
return bool(props.get('is_pano')) or props.get('camera_type') in _PANO_TYPES
def _camera_frame(frame: dict[str, Any]) -> CameraFrame:
"""The light per-frame record kept for the negative-evidence check."""
params = frame.get('camera_parameters')
focal = float(params[0]) if params and params[0] else None
return CameraFrame(
image_id=frame['id'],
lon=frame['lon'],
lat=frame['lat'],
compass_angle=float(frame['compass'] or 0.0),
is_pano=bool(frame['is_pano']),
captured_at=frame['captured_at'],
sequence_id=frame['sequence'],
focal_norm=focal,
)
def _captured_at(props: dict[str, Any]) -> str | None:
value = props.get('captured_at')
if value is None:
value = props.get('capture_time_ms') # coverage tiles
if isinstance(value, (int, float)):
from datetime import UTC, datetime
return datetime.fromtimestamp(value / 1000.0, tz=UTC).isoformat()
return props.get('capture_date') or (str(value) if value is not None else None)
# Options MapillaryFeatureExtractor accepts beyond its named arguments, with
# their defaults (documented, by group, in the class docstring).
_OPTION_DEFAULTS: dict[str, Any] = {
# Occupancy fusion:
'fusion_range_m': 30.0,
'cell_m': None,
'occupancy_threshold': 0.6,
'strong_views': None,
'min_range_m': 2.0,
'bearing_sigma_deg': 0.75,
'aerial': None,
# Structures from the point cloud:
'structure_range_m': 40.0,
'min_gap_m': 1.0,
'structure_footprint': 'stepped',
'structure_views': 12,
'point_cloud_dir': None,
'footprints': None,
'report_unmapped': True,
'join_wings_by': 'touching',
'sam3_point_model': None,
# Frames and detections:
'ego_filter': True,
'ego_min_recurrence': 0.5,
'frame_batch_size': 200,
'simplify_tolerance': 0.002,
# SAM 3 detection:
'sam3_model': None,
'sam3_prompts': None,
'sam3_model_id': 'facebook/sam3',
'sam3_threshold': 0.5,
'sam3_mask_threshold': 0.5,
'sam3_image_size': '1024',
'sam3_batch_size': 4,
'sam3_tile_px': 1024,
'sam3_tile_overlap': 0.125,
'sam3_tile_elevation_deg': (-26.5, 0.5),
'sam3_tile_spacing_m': 10.0,
'keep_full_resolution': False,
'load_in_4bit': True,
'device': 'auto',
# Appearance re-identification:
'reid_model': DEFAULT_REID_MODEL,
'reid_min_similarity': None,
'reid_max_distance_m': 6.0,
'reid_min_width_px': DEFAULT_MIN_WIDTH_PX,
'reid_image_size': '2048',
'reid_embedder': None,
# Earlier methods:
'max_range_m': DEFAULT_MAX_RANGE_M,
'max_single_view_range_m': 30.0,
'witness_radius_m': 12.0,
'object_height_m': 1.5,
'min_area_fraction': None,
'cluster_radius_m': 4.0,
'max_merge_m': None,
'track_gap_frames': 2,
'min_path_distance_m': 1.5,
'object_size_m': 4.5,
'merge_pieces_of_neighbors': True,
'close_range_m': 20.0,
'min_sighting_deg': 6.0,
'report_single_views': False,
'abeam_window_deg': 0.0,
}
[docs]
class MapillaryFeatureExtractor:
"""
Discover objects along a Mapillary survey and place them on the ground.
The extractor lists every image in a region (optionally only RAPID
Facility uploads within a date window), reads Mapillary's segmentation
detections for the requested classes as metadata, estimates where each
detected object stands from the camera pose and the detection's position
in the frame, and merges repeated sightings into one asset per object.
Objects seen from two or more camera positions are triangulated;
single sightings fall back to the one-view estimate and are labeled as
such.
Detections of the collection vehicle itself recur at the same place in
every frame of a sequence and are removed before localization.
Classes outside the Mapillary vocabulary can be detected with SAM 3 on
downsampled thumbnails when ``detection_source`` allows it; the default
only does so for classes Mapillary cannot supply.
Args:
classes (Sequence[str]):
Objects to find, in plain English (``'cars'``, ``'utility
poles'``) or as exact Mapillary labels
(``'object--vehicle--truck'``).
access_token (str | Path | None):
Mapillary API token, the path of a file holding it, or ``None`` to
read the ``MAPILLARY_ACCESS_TOKEN`` environment variable. Not
needed when ``client`` is given.
region (BoundingBox | PolygonRegion | None):
Area to survey. May instead be passed when the extractor is
called.
start_date (str):
Inclusive lower bound on capture date (``YYYY-MM-DD``).
end_date (str):
Inclusive upper bound on capture date.
filter_rapid_only (bool):
Only use imagery uploaded by the RAPID Facility. Defaults to
``True``.
detection_source (str | Mapping[str, str]):
``'auto'`` (Mapillary map features for static point classes such
as utility poles and hydrants, Mapillary detections for the rest,
SAM 3 for unresolved classes), ``'mapillary'`` (detections only;
skip unresolved classes with a warning), ``'sam3'`` (SAM 3 for
every class), ``'sam3_tiles'`` (SAM 3 on tiles of the
full-resolution frames, for objects too small to find in a
thumbnail, such as fire hydrants; see the ``sam3_tile_*``
options) or ``'map_features'`` (only Mapillary's
pre-triangulated map features; other classes are skipped). A
mapping chooses per class and leaves the rest on ``'auto'``:
``{'buildings': 'sam3'}`` segments every building on its own
with SAM 3 while vehicles still come from Mapillary.
localization_method (str):
How sightings become objects. ``'auto'`` (default) uses
``'points'`` for buildings and other structures and
``'fusion'`` for everything else. ``'fusion'`` reads every
outline as a range scan and fuses all frames of a survey day in
a ground occupancy grid; objects are the cells most frames
agree are occupied, whatever their size and from however many
streets they were seen (see
:mod:`~rapidtools.processing.street_fusion`). ``'points'``
labels the survey's own structure-from-motion point cloud with
the outlines and reads structures off the labeled points as
footprint polygons (see
:mod:`~rapidtools.processing.street_structures`); it needs
panoramas Mapillary has reconstructed and falls back to
``'fusion'`` otherwise. ``'instances'`` is for sightings that
are one outline per object, as a segmentation model gives
(``detection_source='sam3'``): outlines in different frames are
linked into one object by the reconstructed points they share,
which keeps neighbors apart where a fence or a shed joins them
in Mapillary's building surface (see
:mod:`~rapidtools.processing.street_instances`). ``'auto'``
uses it for buildings detected with SAM 3. It downloads a
thumbnail of every frame and runs the model on each, so it
costs GPU time that ``'points'`` does not. ``'footprints'`` is for buildings
whose footprints are known (the ``footprints`` option): each
footprint is projected into the frames around it and the
outlines found there are its sightings, so nothing has to be
discovered, located or told apart (see
:mod:`~rapidtools.processing.street_footprints`). ``'auto'``
uses it for buildings whenever ``footprints`` is given, with
Mapillary's outlines or a model's. ``'tracks'`` is the earlier method:
detections linked frame to frame by bearing, triangulated and
merged by a set of rules tuned on vehicles (see
:mod:`~rapidtools.processing.street_tracking`). ``'voting'``
accumulates rays on a ground grid and takes the peaks, and
``'cluster'`` clusters single-view positions by radius; both
are baselines.
geometry (str):
Geometry written to each asset. ``'auto'`` (default) gives
structures a footprint polygon and everything else a point;
``'footprint'`` gives every object found by ``'fusion'`` or
``'points'`` its footprint polygon; ``'point'`` gives every
object a point (a structure's is the footprint center). The
footprint's size is always in the attributes.
min_observations (int):
Distinct frames an object must have been seen in; ``3`` is a
sound choice for a survey sampled every few meters. Defaults
to 1.
frame_spacing_m (float):
Keep one frame per this many meters along each sequence before
detecting; ``0`` keeps every frame. Defaults to 0.
camera_height_m (float):
Camera height above the road, used for single-view ranges.
Defaults to 2.4 (roof-mounted rig).
min_object_width_m (float | None):
Objects narrower than this, judged from the outline's angular
width at the estimated distance, are fragments or false
detections and are dropped. Resolution-independent and tolerant
of partial occlusion. ``None`` (the default) lets ``'fusion'``
take it per class from the sightings themselves, the typical
width of an outline between 0.2 and 1 m (1 m for vehicles,
about 0.3 m for fire hydrants), and is 1.0 for the other
methods.
assumed_depth_m (float):
Depth given to a structure's footprint when the survey saw
less of it; the ``depth_source`` attribute then reads
``'assumed'``. Defaults to 10.0.
reid (bool):
After localization, compare the appearance of objects from
different sequences that lie within ``reid_max_distance_m`` and
merge look-alikes, removing double counts from repeated passes.
Downloads one thumbnail per compared object and loads
``reid_model``. Appearance only ever adds merges: an object
without a sighting at least ``reid_min_width_px`` wide is left
as geometry placed it. Defaults to ``False``.
label_mapper (Any | None):
Optional :class:`~rapidtools.processing.MapillaryLabelMapper`
to translate class names the built-in aliases do not cover.
asset_type (str | None):
``asset_type`` attribute written to every asset. Defaults to the
class name each object was found under.
id_prefix (str):
Prefix of the generated asset IDs. Defaults to ``'street'``.
save_directory (str | Path):
Where SAM 3 thumbnails are cached when that source is used.
max_workers (int):
Threads for metadata and thumbnail downloads. Defaults to 10.
cancel_event (threading.Event | None):
Cooperative cancellation flag checked between images and batches.
client (MapillaryClient | None):
Pre-configured client; one is created from ``access_token``
otherwise.
The arguments above cover a first run. The options below are passed as
keywords in the same way (``MapillaryFeatureExtractor(...,
occupancy_threshold=0.7)``); none of them is needed to get started, and a
misspelled name raises a ``TypeError`` that suggests the closest match.
**Occupancy fusion** (``'fusion'``, the default method for objects):
- ``fusion_range_m`` (*float*): Farthest range at which a frame votes in
``'fusion'``. Defaults to 30.0.
- ``cell_m`` (*float | None*): Ground cell of the ``'fusion'`` grid.
``None`` picks a quarter of ``min_object_width_m``, between 0.1 and 0.5
m.
- ``occupancy_threshold`` (*float*): Share of the range-weighted votes on a
ground cell that must say "occupied" in ``'fusion'``. Defaults to 0.6.
- ``strong_views`` (*int | None*): Frames that, outlining the same spot
from three or more directions, keep an object the vote turns down
because most frames in range show nothing (a hydrant behind a bush, or
one the detector often misses). Such objects have a ``vote_share``
under ``occupancy_threshold`` and are worth a look by
:class:`~rapidtools.processing.verification.DetectionVerifier`. ``0``
keeps only what the vote accepts; ``None`` (the default) uses 5 for
classes with outlines under a meter wide and 0 for the rest.
- ``min_range_m`` (*float*): Closest plausible object. Defaults to 2.0.
- ``bearing_sigma_deg`` (*float*): One-sigma bearing error of a sighting.
Defaults to 0.75.
- ``aerial`` (*AerialEvidence | Mapping[str, AerialEvidence] | None*):
Detections of a class in an overhead image of the same area, fused with
the street frames in ``'fusion'`` (see
:class:`~rapidtools.processing.aerial_evidence.AerialEvidence`). A
mapping gives one per class (keys as in ``classes``); a single one is
used for every class placed by fusion. Assets then carry ``sources``
(``'both'``, ``'street'`` or ``'aerial'``); those only the image shows
have no observations. Defaults to ``None``.
**Structures from the point cloud** (``'points'``, the default for
buildings):
- ``structure_range_m`` (*float*): Farthest range at which a frame votes on
a point in ``'points'``, and from which a structure gets a sighting.
Defaults to 40.0.
- ``min_gap_m`` (*float*): Free ground that separates two wall pieces in
``'points'``. Pieces up to three times as far apart along the street
are joined when the frames in front of them outline them as one
building. Defaults to 1.0.
- ``structure_footprint`` (*str*): ``'stepped'`` (default) draws a
footprint whose front follows the setbacks of the facade (garage
forward, entrance back), with the unseen rear filled in;
``'rectangle'`` draws one facade-aligned rectangle; ``'observed'``
draws only the convex outline of what was seen.
- ``structure_views`` (*int*): Sightings kept per structure, spread over
the directions it was seen from. Defaults to 12.
- ``point_cloud_dir`` (*str | Path | None*): Where downloaded point clouds
are cached. Defaults to ``<save_directory>/point_clouds``.
- ``footprints`` (*PhysicalAssetCollection | str | Path | Sequence | None*):
Known building footprints: a collection, the path of a GeoJSON file,
or polygons in longitude and latitude. When given, buildings are
matched to them (``'footprints'``) and come back with the footprint's
own geometry, identifier and attributes, plus ``frames_expected`` (the
frames it should be visible in) and ``frames_seen``. Defaults to
``None``: buildings are discovered.
- ``report_unmapped`` (*bool*): With ``footprints``, also place the
buildings the layer does not have (``unmapped`` is ``True`` on them).
With a segmentation model these come from the masks that lie on no
footprint (``'instances'``); with Mapillary's outlines, from the
structures of ``'points'`` that lie off every footprint, which
downloads the point clouds and costs about a minute per thousand
frames. Either needs frames with a point cloud. Defaults to ``True``;
``False`` matches the layer only.
**Frames and detections** (every method):
- ``ego_filter`` (*bool*): Remove detections of the collection vehicle.
Defaults to ``True``.
- ``ego_min_recurrence`` (*float*): Fraction of a sequence's frames a box
must recur in to count as the collection vehicle. Defaults to 0.5.
- ``frame_batch_size`` (*int*): Frames whose metadata is fetched and
converted at a time. Each image's detection payload (every Mapillary
label, as encoded geometry) is dropped as soon as the requested classes
have been read from it, so memory use is bounded by one batch rather than
by the size of the survey. Defaults to 200.
- ``simplify_tolerance`` (*float*): Douglas-Peucker tolerance, in
normalized image units, applied to every detection outline before it is
stored (``0.002`` is about four pixels of a 2048-wide image), capped at 1
% of the outline's longest side so small, distant objects keep their
detail. Mapillary outlines trace every pixel step of a mask, so this cuts
the memory of a city-wide run several-fold without changing which objects
are found. ``0`` keeps the raw outlines. Defaults to 0.002.
**SAM 3 detection** (classes Mapillary does not segment, or
``detection_source='sam3'``):
- ``sam3_model`` (*Any | None*): Pre-loaded segmentation model: the local
SAM 3 wrapper, or anything else with the same
``run_inference``. The local wrapper is loaded on demand otherwise.
- ``sam3_prompts`` (*Mapping[str, str] | None*): The text the model is
asked for per class, where the class name is not it. SAM 3 wants a
singular noun: ``{'buildings': 'house'}``.
- ``join_wings_by`` (*str*): How ``'instances'`` decides that two masks
are wings of one building. ``'touching'`` (default): their outlines
touch in the frames in front of them. ``'prompt'``: a point-prompt
model, asked for the whole object at a few points on one, returns a
mask that covers the other; this joins neighbors far less often and
costs a few model calls per pair of candidate wings.
- ``sam3_point_model`` (*Any | None*): Pre-loaded point-prompt model for
``join_wings_by='prompt'``: anything with
``segment_at(image, points)``. SAM 3's is loaded on demand otherwise.
- ``sam3_model_id`` (*str*): Hugging Face repository of the SAM 3 weights.
Defaults to ``'facebook/sam3'``.
- ``sam3_threshold`` (*float*): SAM 3 detection confidence threshold.
Defaults to 0.5.
- ``sam3_mask_threshold`` (*float*): SAM 3 mask binarization threshold.
Defaults to 0.5.
- ``sam3_image_size`` (*str*): Thumbnail size SAM 3 runs on: ``'1024'``
(default) or ``'2048'``.
- ``sam3_batch_size`` (*int*): Thumbnails per SAM 3 inference call.
Defaults to 4.
- ``sam3_tile_px`` (*int*): Side of the tiles ``'sam3_tiles'`` cuts from
a full-resolution frame. Defaults to 1024.
- ``sam3_tile_overlap`` (*float*): Share of a tile the next one repeats,
so that an object on a seam is whole in one of them. Defaults to
0.125.
- ``sam3_tile_elevation_deg`` (*tuple[float, float]*): Band of a
panorama that is tiled, as its lowest and highest elevation. Defaults
to ``(-26.5, 0.5)``: one row of tiles for a 13,504 pixel panorama,
showing the ground from about 5 m out. Objects cut by the lower edge
of the band are left out, and frames say nothing about the ground
nearer than it shows. Perspective frames are tiled whole.
- ``sam3_tile_spacing_m`` (*float*): One frame per this many meters is
searched on tiles. Defaults to 10: SAM 3 finds a hydrant in three of
four full-resolution frames at 30 m, so few frames are needed.
- ``keep_full_resolution`` (*bool*): Keep the full-resolution frames
(about 7 MB per panorama) in ``save_directory`` after their tiles
were searched. Defaults to ``False``.
- ``load_in_4bit`` (*bool*): Load SAM 3 with 4-bit quantization. Defaults
to ``True``.
- ``device`` (*str*): Compute device for SAM 3 (``'auto'``, ``'cuda'``,
``'cpu'``).
**Appearance re-identification** (``reid=True``):
- ``reid_model`` (*str*): Hugging Face vision backbone for the embeddings.
Defaults to ``'openai/clip-vit-base-patch16'``; DINOv2 checkpoints work
too.
- ``reid_min_similarity`` (*float | None*): Cosine similarity at which two
crops count as the same object. ``None`` (the default) takes the value
recommended for ``reid_model``: 0.89 for CLIP, 0.83 for DINOv2.
- ``reid_max_distance_m`` (*float*): Only objects this close are compared.
Defaults to 6.0.
- ``reid_min_width_px`` (*float*): Narrowest detection whose crop is
embedded, in pixels of a 2048-pixel-wide thumbnail (scaled to
``reid_image_size``). On the Spokane survey, crops below this width
scored as alike whether or not they showed the same vehicle. ``0`` embeds
every object. Defaults to 100.
- ``reid_image_size`` (*str*): Thumbnail size the re-identification crops
are cut from: ``'2048'`` (default, the size the thresholds were tuned on)
or ``'1024'``.
- ``reid_embedder`` (*Any | None*): A ready
:class:`~rapidtools.processing.reid.AppearanceEmbedder` (or anything with
``embed(images)``) to use instead of loading ``reid_model``.
**Earlier methods** (``localization_method`` ``'tracks'``, ``'voting'`` or
``'cluster'`` only):
- ``max_range_m`` (*float*): Farthest triangulated position reported, and
the farthest single-view range kept as a prior. Defaults to 60.0.
- ``max_single_view_range_m`` (*float*): A single-view object whose nearest
sighting is farther than this is not reported; beyond it the ground-
contact range is a guess and the track tends to hop between neighboring
objects. ``0`` keeps them. Defaults to 30.0.
- ``witness_radius_m`` (*float*): Negative-evidence check: a weak object (a
single view, or a triangulation never seen closer than 25 m) that same-
day frames within this distance had in view, without any detection of the
class along its bearing, is dropped (at least two such frames,
outnumbering frames that did see it, tallied per survey day). ``0``
disables the check. Defaults to 12.0.
- ``object_height_m`` (*float*): Typical height of the class. When a
track's ground-contact ranges disagree with its triangulation, the track
is called moving only if enough of an object this tall is visible for
occlusion to be ruled out. Defaults to 1.5 (vehicles).
- ``min_area_fraction`` (*float | None*): Deprecated. Pixel-area floor
below which a detection is ignored; ``None`` (the default) keeps only a
tiny floor for decoding noise. Size is judged with ``min_object_width_m``
instead.
- ``cluster_radius_m`` (*float*): Sightings closer than this are the same
object in the legacy ``'cluster'`` method; with ``'tracks'`` it only sets
the default ``max_merge_m`` (1.5 times this value). Defaults to 4.0.
- ``max_merge_m`` (*float | None*): Farthest two estimates can be and still
be merged into one object. ``None`` uses ``1.5 * cluster_radius_m``.
- ``track_gap_frames`` (*int*): Frames a track may miss before it is
closed. Defaults to 2.
- ``min_path_distance_m`` (*float*): Objects closer than this to the line
the camera drove are dropped; the survey vehicle passed through that
spot. Defaults to 1.5.
- ``object_size_m`` (*float*): Object footprint used by the voting method.
Defaults to 4.5.
- ``merge_pieces_of_neighbors`` (*bool*): After localization, fold an
object whose closest sightings show a better-located neighbor into that
neighbor: the second track left by a split outline (a car cut in two by
a pole, or into a front and a rear) or by the tracker sliding between
vehicles lined up along the line of sight. Such pairs share frames, which
the position merge takes as proof of two objects, so they are judged on
what their sightings show (see
:func:`~rapidtools.processing.street_tracking.merge_pieces`). Defaults to
``True``.
- ``close_range_m`` (*float*): Range within which an object must have been
seen to be reported. Ground-contact ranges read from farther away are off
by a third and the tracker hops between vehicles lined up down the road,
so an object never seen this close whose closest rays point at a vehicle
within 15 m already placed from the close pass is a far sighting of that
vehicle, and is dropped unless it is well triangulated regardless
(parallax of 45 degrees or more and uncertainty of half a meter or less).
One whose rays point elsewhere, a car set back in a driveway or down a
side street, stays. The piece merge judges sightings by the same range.
``0`` keeps every object. Defaults to 20.
- ``min_sighting_deg`` (*float*): An object must have been seen at least
once with an outline this wide or tall, in degrees of the panorama (6
degrees is 34 pixels on a 2048-pixel thumbnail, 225 on the original).
What was never more than a speck cannot be identified by anyone and is
not reported; a drive-by survey sees anything worth counting properly in
some frame. ``0`` keeps every object. Defaults to 6.
- ``report_single_views`` (*bool*): Keep objects placed from ground-contact
ranges alone, without parallax (``localization='single_view'``). Their
positions are guesses; defaults to ``False``.
- ``abeam_window_deg`` (*float*): Objects are built only from the sightings
made while passing them, within this many degrees of abeam on either side
of the direction of travel. A drive-by survey passes everything worth
counting, and the passing frames carry the widest parallax and the
largest outlines; the frames looking ahead or behind down the road cannot
place anything and are where tracks slide between vehicles that line up.
Those sightings are attached afterward to the objects their rays point
at, so records and crops stay complete, but never create an object. Off
(``0``) by default: the track classification is tuned on tracks that
include the approach and a pass alone trips it, losing real vehicles on
the surveys tried. Defaults to 0.
Example:
>>> from rapidtools.core import BoundingBox
>>> extractor = MapillaryFeatureExtractor(
... classes=['vehicles'],
... access_token='MLY|...',
... start_date='2025-08-01',
... min_observations=2,
... )
>>> region = BoundingBox(-117.45, 47.69, -117.44, 47.70)
>>> vehicles = extractor(region) # doctest: +SKIP
>>> vehicles[0].attributes['localization'] # doctest: +SKIP
'triangulated'
"""
stage = Stage.DETECT
def __init__(
self,
classes: Sequence[str],
access_token: str | Path | None = None,
region: BoundingBox | PolygonRegion | None = None,
start_date: str = '',
end_date: str = '',
filter_rapid_only: bool = True,
detection_source: str | Mapping[str, str] = 'auto',
localization_method: str = 'auto',
geometry: str = 'auto',
min_observations: int = 1,
frame_spacing_m: float = 0.0,
camera_height_m: float = 2.4,
min_object_width_m: float | None = None,
assumed_depth_m: float = 10.0,
reid: bool = False,
label_mapper: Any = None,
asset_type: str | None = None,
id_prefix: str = 'street',
save_directory: str | Path = 'street_detections',
max_workers: int = 10,
cancel_event: threading.Event | None = None,
client: MapillaryClient | None = None,
**options: Any,
) -> None:
unknown = sorted(set(options) - set(_OPTION_DEFAULTS))
if unknown:
hints = []
for name in unknown:
close = difflib.get_close_matches(name, _OPTION_DEFAULTS, n=1)
hints.append(
f'{name!r} (did you mean {close[0]!r}?)' if close else repr(name)
)
raise TypeError(
f'MapillaryFeatureExtractor got unknown option(s): {", ".join(hints)}.'
)
o = {**_OPTION_DEFAULTS, **options}
if not classes:
raise ValueError('At least one class is required.')
if o['frame_batch_size'] < 1:
raise ValueError('frame_batch_size must be at least 1.')
if o['simplify_tolerance'] < 0:
raise ValueError('simplify_tolerance cannot be negative.')
chosen = (
list(detection_source.values())
if isinstance(detection_source, Mapping)
else [detection_source]
)
for value in chosen:
if value not in _DETECTION_SOURCES:
raise ValueError(
"detection_source must be 'auto', 'mapillary', 'sam3', "
"'sam3_tiles' or 'map_features' (for every class, or as a "
f'mapping from class to one of them), got {value!r}.'
)
if localization_method not in _LOCALIZATION_METHODS:
raise ValueError(
'localization_method must be one of '
f'{", ".join(repr(m) for m in _LOCALIZATION_METHODS)}, '
f'got {localization_method!r}.'
)
if geometry not in ('auto', 'point', 'footprint'):
raise ValueError(
f"geometry must be 'auto', 'point' or 'footprint', got {geometry!r}."
)
if o['structure_footprint'] not in ('stepped', 'rectangle', 'observed'):
raise ValueError(
"structure_footprint must be 'stepped', 'rectangle' or "
f"'observed', got {o['structure_footprint']!r}."
)
if client is None:
access_token = resolve_mapillary_token(access_token)
if min_observations < 1:
raise ValueError('min_observations must be at least 1.')
self.classes = list(classes)
self.region = region
self.start_date = start_date
self.end_date = end_date
self.filter_rapid_only = filter_rapid_only
self.detection_source = detection_source
self.label_mapper = label_mapper
self.camera_height_m = camera_height_m
self.min_range_m = o['min_range_m']
self.max_range_m = o['max_range_m']
self.max_single_view_range_m = o['max_single_view_range_m']
self.witness_radius_m = o['witness_radius_m']
self.min_object_width_m = min_object_width_m
self.object_height_m = o['object_height_m']
if o['min_area_fraction'] is not None:
warnings.warn(
'min_area_fraction is deprecated: object size is now judged in '
'meters once the distance is known (min_object_width_m). The '
'value is still applied as a pixel floor.',
DeprecationWarning,
stacklevel=2,
)
self.min_area_fraction = (
MIN_POLYGON_AREA_FRACTION
if o['min_area_fraction'] is None
else o['min_area_fraction']
)
self.cluster_radius_m = o['cluster_radius_m']
self.localization_method = localization_method
self.geometry = geometry
self.fusion_range_m = float(o['fusion_range_m'])
self.cell_m = o['cell_m']
aerial = o['aerial']
if not (
aerial is None
or isinstance(aerial, AerialEvidence)
or (
isinstance(aerial, Mapping)
and all(isinstance(v, AerialEvidence) for v in aerial.values())
)
):
raise TypeError(
'aerial must be an AerialEvidence or a mapping of class names to '
f'AerialEvidence, got {type(aerial).__name__}.'
)
self.aerial = aerial
self.strong_views = o['strong_views']
self.occupancy_threshold = float(o['occupancy_threshold'])
self.structure_range_m = float(o['structure_range_m'])
self.min_gap_m = float(o['min_gap_m'])
self.assumed_depth_m = float(assumed_depth_m)
self.structure_footprint = o['structure_footprint']
self.structure_views = int(o['structure_views'])
self.footprints = _known_footprints(o['footprints'])
self.report_unmapped = bool(o['report_unmapped'])
if o['join_wings_by'] not in ('touching', 'prompt'):
raise ValueError(
"join_wings_by must be 'touching' or 'prompt', "
f'got {o["join_wings_by"]!r}.'
)
self.join_wings_by = o['join_wings_by']
self.sam3_point_model = o['sam3_point_model']
if localization_method == 'footprints' and not self.footprints:
raise ValueError(
"localization_method='footprints' needs the buildings to match: "
'pass footprints= (a PhysicalAssetCollection, a GeoJSON file or '
'polygons in longitude and latitude).'
)
self.point_cloud_dir = (
Path(o['point_cloud_dir'])
if o['point_cloud_dir'] is not None
else Path(save_directory) / 'point_clouds'
)
# image ID -> (cluster ID, url) of the frame's point cloud:
self._clusters: dict[str, tuple[str, str]] = {}
self.bearing_sigma_deg = o['bearing_sigma_deg']
self.max_merge_m = o['max_merge_m']
self.track_gap_frames = o['track_gap_frames']
self.min_path_distance_m = o['min_path_distance_m']
self.object_size_m = o['object_size_m']
self.merge_pieces_of_neighbors = o['merge_pieces_of_neighbors']
self.close_range_m = float(o['close_range_m'])
self.min_sighting_deg = float(o['min_sighting_deg'])
self.report_single_views = o['report_single_views']
self.abeam_window_deg = float(o['abeam_window_deg'])
self.reid = reid
self.reid_model = o['reid_model']
self.reid_min_similarity = o['reid_min_similarity']
self.reid_max_distance_m = o['reid_max_distance_m']
self.reid_min_width_px = float(o['reid_min_width_px'])
self.reid_image_size = str(o['reid_image_size'])
self.reid_embedder = o['reid_embedder']
self.min_observations = min_observations
self.frame_spacing_m = frame_spacing_m
self.ego_filter = o['ego_filter']
self.ego_min_recurrence = o['ego_min_recurrence']
self.asset_type = asset_type
self.id_prefix = id_prefix
self.save_directory = Path(save_directory)
self.sam3_model = o['sam3_model']
self.sam3_prompts = dict(o['sam3_prompts'] or {})
self.sam3_model_id = o['sam3_model_id']
self.sam3_threshold = o['sam3_threshold']
self.sam3_mask_threshold = o['sam3_mask_threshold']
self.sam3_image_size = str(o['sam3_image_size'])
self.sam3_batch_size = o['sam3_batch_size']
self.sam3_tile_px = int(o['sam3_tile_px'])
self.sam3_tile_overlap = float(o['sam3_tile_overlap'])
self.sam3_tile_elevation_deg = tuple(o['sam3_tile_elevation_deg'])
self.sam3_tile_spacing_m = float(o['sam3_tile_spacing_m'])
self.keep_full_resolution = bool(o['keep_full_resolution'])
# Per class detected on tiles: the frames that were looked at, and
# the range from which their tiles show the ground.
self._examined: dict[str, set[str]] = {}
self._clear_from: dict[str, float] = {}
self.load_in_4bit = o['load_in_4bit']
self.device = o['device']
self.max_workers = max_workers
self.frame_batch_size = o['frame_batch_size']
self.simplify_tolerance = o['simplify_tolerance']
self.cancel_event = cancel_event
self.client = client or MapillaryClient(
access_token, save_dir=self.save_directory / 'images'
)
# ---------------------------------------------------------------- setup
def _plan_classes(
self,
) -> tuple[dict[str, tuple[str, ...]], list[str], dict[str, tuple[str, ...]]]:
"""
Split the requested classes three ways.
Returns:
tuple: ``(detections, sam3_prompts, map_features)``: classes read
from Mapillary's per-image detections, classes detected with
SAM 3, and classes served by Mapillary's pre-triangulated map
features (static point objects such as poles and hydrants).
"""
source = self._source_of
asked = [
cls for cls in self.classes if source(cls) not in ('sam3', 'sam3_tiles')
]
resolved, unresolved = resolve_classes(asked, self.label_mapper)
sam3 = [cls for cls in self.classes if source(cls) == 'sam3']
map_features: dict[str, tuple[str, ...]] = {}
for cls, labels in list(resolved.items()):
if source(cls) not in ('auto', 'map_features'):
continue
if labels and all(is_map_feature_value(v) for v in labels):
map_features[cls] = labels
del resolved[cls]
skipped = [
cls for cls in [*resolved, *unresolved] if source(cls) == 'map_features'
]
if skipped:
logger.warning(
'Not served as Mapillary map features: %s; these classes are '
"skipped. Use detection_source='auto' to detect them from "
'images.',
', '.join(repr(c) for c in skipped),
)
unlabeled = [cls for cls in unresolved if source(cls) == 'mapillary']
if unlabeled:
logger.warning(
'No Mapillary label for %s; these classes are skipped. Use '
"detection_source='auto' to detect them with SAM 3.",
', '.join(repr(c) for c in unlabeled),
)
resolved = {
cls: labels
for cls, labels in resolved.items()
if source(cls) != 'map_features'
}
sam3 += [cls for cls in unresolved if source(cls) == 'auto']
sam3 = [cls for cls in self.classes if cls in sam3] # in the order asked
return resolved, sam3, map_features
def _tiled_classes(self) -> list[str]:
"""Classes detected on full-resolution tiles (``'sam3_tiles'``)."""
return [cls for cls in self.classes if self._source_of(cls) == 'sam3_tiles']
def _source_of(self, cls: str) -> str:
"""Where one class's sightings come from (``detection_source``)."""
if isinstance(self.detection_source, Mapping):
return str(self.detection_source.get(cls, 'auto'))
return str(self.detection_source)
def _outlined_classes(self, sam3_classes: Sequence[str]) -> set[str]:
"""
The model-detected classes whose masks are kept as per-object outlines.
Such sightings are placed by ``'instances'``: every class when that
method is asked for, and in ``'auto'`` the classes that name
structures (buildings), which no single frame shows whole. With
``'footprints'`` the structure classes keep them too: a mask is then
given to the one footprint it lies on, and masks on none are the
buildings the layer does not have.
"""
if self.localization_method == 'instances':
return set(sam3_classes)
if self.localization_method not in ('auto', 'footprints'):
return set()
structures, _ = resolve_classes(list(sam3_classes), self.label_mapper)
return {
cls
for cls, labels in structures.items()
if labels and all(is_structure_label(v) for v in labels)
}
def _wants_point_clouds(
self, mapillary_classes: dict[str, tuple[str, ...]], sam3_classes: list[str]
) -> bool:
"""Whether any requested class will be placed from the point cloud."""
if self.localization_method == 'points':
return bool(mapillary_classes or sam3_classes)
if self._outlined_classes(sam3_classes):
return True
# With known footprints the point cloud only places what the layer
# lacks (``report_unmapped``).
if self.localization_method == 'footprints' and not self.report_unmapped:
return False
if self.localization_method not in ('auto', 'footprints'):
return False
return any(
labels and all(is_structure_label(v) for v in labels)
for labels in mapillary_classes.values()
)
def _method_for(self, obs_list: list[Observation]) -> str:
"""The localization method one class's sightings are placed with."""
if self.localization_method != 'auto':
return self.localization_method
# Per-object outlines (one mask per building from a segmentation
# model) are linked through the point cloud, whoever made them.
outlined = bool(obs_list) and all(
'instance' in (o.extra or {}) for o in obs_list
)
labels = {o.label for o in obs_list}
structure = bool(labels) and all(is_structure_label(v) for v in labels)
# Known footprints say which outline shows which building; nothing
# has to be discovered.
if self.footprints and (outlined or structure):
return 'footprints'
if outlined:
return 'instances'
return 'points' if structure else 'fusion'
@staticmethod
def _finish(
existing: PhysicalAssetCollection | None, assets: Iterable[PhysicalAsset]
) -> PhysicalAssetCollection:
"""Collection of ``existing`` (if any) plus ``assets``."""
collection = PhysicalAssetCollection()
if existing is not None:
collection.merge(existing)
for asset in assets:
collection.add(asset)
return collection
def _resolve_region(self, source: Any) -> BoundingBox | PolygonRegion:
region = (
source if isinstance(source, (BoundingBox, PolygonRegion)) else self.region
)
if region is None:
raise ValueError(
'A region is required: pass it to the constructor or call the '
'extractor with a BoundingBox or PolygonRegion.'
)
return region
# ---------------------------------------------------------------- run
def __call__(self, source: Any = None) -> PhysicalAssetCollection:
"""
Discover objects in the region and return them as a collection.
Args:
source (BoundingBox | PolygonRegion | PhysicalAssetCollection | None):
The region to survey, or an existing collection (the
pipeline passes one) whose assets are kept alongside the new
detections while the constructor's ``region`` is used.
Returns:
PhysicalAssetCollection: One point asset per detected object with
``asset_type``, ``label``, ``confidence``, ``n_observations``,
``localization`` and ``observations`` attributes.
"""
existing = source if isinstance(source, PhysicalAssetCollection) else None
region = self._resolve_region(source)
bbox = region if isinstance(region, BoundingBox) else region.get_bounding_box()
mapillary_classes, sam3_classes, map_feature_classes = self._plan_classes()
tiled_classes = self._tiled_classes()
self._examined, self._clear_from = {}, {}
if not (
mapillary_classes or sam3_classes or map_feature_classes or tiled_classes
):
logger.warning('Nothing to detect after class resolution.')
return existing or PhysicalAssetCollection()
map_assets = (
self._map_feature_assets(bbox, map_feature_classes)
if map_feature_classes
else []
)
if not mapillary_classes and not sam3_classes and not tiled_classes:
return self._finish(existing, map_assets)
# 1. Cheap pass: the coverage tiles list every image with its position,
# heading, date and sequence. Tiles are ~2 km wide, so clip to the
# region before anything per-image happens.
logger.info(
f'Listing Mapillary images in {bbox.bounds} '
f'({"RAPID only" if self.filter_rapid_only else "all uploaders"})...'
)
listed = self.client.fetch_images_in_bbox(
bbox,
start_date=self.start_date,
end_date=self.end_date,
filter_rapid_only=self.filter_rapid_only,
max_workers=self.max_workers,
)
raise_if_canceled(self.cancel_event, 'street-level detection')
inside = []
for image in listed:
pose = _camera_pose(image.properties)
if pose is not None and region.contains(Point(pose[0], pose[1])):
inside.append((image, pose))
if not inside:
hint = (
' Only RAPID Facility uploads were searched; pass '
"filter_rapid_only=False to use every contributor's imagery."
if self.filter_rapid_only
else ''
)
window = (
f' between {self.start_date or "the start"} and '
f'{self.end_date or "today"}'
if self.start_date or self.end_date
else ''
)
logger.warning(
f'No Mapillary images with a camera position inside the region{window}.'
+ hint
)
return self._finish(existing, map_assets)
# 2. Leave out second uploads of the same photo (same instant, same
# spot, another sequence): they would vote twice. Then thin along
# each sequence before paying for per-image metadata.
unique: dict[tuple[Any, int, int], Any] = {}
for image, pose in inside:
taken = image.properties.get('capture_time_ms')
key = (
(taken, round(pose[0] * 1e7), round(pose[1] * 1e7))
if taken is not None
else (str(image.id), 0, 0)
)
unique.setdefault(key, (image, pose))
if len(unique) < len(inside):
logger.info(
f'{len(inside) - len(unique)} of {len(inside)} images are second '
'uploads of a photo already listed and are left out.'
)
inside = list(unique.values())
keep = thin_frames(
[
(
str(image.id),
pose[0],
pose[1],
_captured_at(image.properties),
image.properties.get('sequence'),
)
for image, pose in inside
],
self.frame_spacing_m,
)
logger.info(
f'{len(listed)} images in the covering tiles, {len(inside)} inside the '
f'region, {len(keep)} kept after thinning.'
)
# 3. Metadata (and Mapillary detections) for the kept frames only,
# one batch at a time. A frame's detection payload lists every
# Mapillary label as encoded geometry, so it is read for the
# requested classes and dropped before the next batch arrives.
fields = list(_IMAGE_FIELDS)
if mapillary_classes:
fields += list(_DETECTION_FIELDS)
if self._wants_point_clouds(mapillary_classes, sam3_classes):
fields.append('sfm_cluster')
self._clusters = {}
ids = sorted(keep)
observations: dict[str, list[Observation]] = defaultdict(list)
sam3_frames: list[dict[str, Any]] = []
tiled_frames: list[dict[str, Any]] = []
camera_frames: list[CameraFrame] = [] # every frame, for negative evidence
n_frames = 0
batches = range(0, len(ids), self.frame_batch_size)
for start in tqdm(batches, desc='Frame batches', disable=len(batches) < 2):
raise_if_canceled(self.cancel_event, 'street-level detection')
rich = self.client.fetch_images_by_ids(
ids[start : start + self.frame_batch_size],
fields=fields,
save_to_disk=False,
max_workers=self.max_workers,
show_progress=len(batches) < 2,
)
frames = self._frames(rich)
del rich
n_frames += len(frames)
camera_frames.extend(_camera_frame(frame) for frame in frames)
for frame in frames:
cluster = frame.get('sfm_cluster') or {}
if cluster.get('id') and cluster.get('url'):
self._clusters[frame['id']] = (str(cluster['id']), cluster['url'])
if mapillary_classes:
for cls, obs in self._mapillary_observations(
frames, mapillary_classes
).items():
observations[cls].extend(obs)
for frame in frames:
frame['detections'] = None
if sam3_classes:
sam3_frames.extend(frames)
if tiled_classes:
tiled_frames.extend(frames)
if not n_frames:
logger.warning('No usable images (missing camera pose) in the region.')
return self._finish(existing, map_assets)
logger.info(f'{n_frames} frames with metadata; detections read.')
if sam3_classes:
for cls, obs in self._sam3_observations(
sam3_frames, sam3_classes, self._outlined_classes(sam3_classes)
).items():
observations[cls].extend(obs)
if tiled_classes:
for cls, obs in self._sam3_tile_observations(
tiled_frames, tiled_classes
).items():
observations[cls].extend(obs)
collection = self._finish(existing, map_assets)
counter = 0
for cls in self.classes:
if cls in map_feature_classes:
continue
obs_list = observations.get(cls, [])
if not obs_list:
logger.info(f"No detections for class '{cls}'.")
continue
# Only the frames a class was looked for in can say it is absent.
examined = self._examined.get(cls)
assets, counter = self._assets_for_class(
cls,
obs_list,
counter,
camera_frames
if examined is None
else [f for f in camera_frames if f.image_id in examined],
)
for asset in assets:
collection.add(asset)
logger.info(f"'{cls}': {len(obs_list)} sightings -> {len(assets)} objects.")
return collection
# ---------------------------------------------------------------- frames
def _frames(self, images) -> list[dict[str, Any]]:
"""Reduce image assets to the camera facts the geometry needs."""
frames = []
for image in images:
props = image.properties
pose = _camera_pose(props)
if pose is None:
continue
lon, lat, compass = pose
frames.append(
{
'id': str(image.id),
'lon': lon,
'lat': lat,
'compass': compass,
'is_pano': _is_pano(props),
'sequence': props.get('sequence'),
'captured_at': _captured_at(props),
'width': props.get('width'),
'height': props.get('height'),
'camera_type': props.get('camera_type'),
'camera_parameters': props.get('camera_parameters'),
# Full pose (OpenSfM axis-angle, world -> camera); lets the
# geometry account for the camera's pitch and roll:
'rotation': props.get('computed_rotation'),
'sfm_cluster': props.get('sfm_cluster'),
'detections': (props.get('detections') or {}).get('data'),
'thumb_url': props.get(f'thumb_{self.sam3_image_size}_url'),
}
)
return frames
def _observation(
self, frame: dict[str, Any], label: str, polygon, source: str, confidence=None
) -> Observation:
if source == 'mapillary' and self.simplify_tolerance > 0:
# Cap the tolerance at a share of the outline's size so small,
# distant objects keep the detail their localization needs:
xs = [x for x, _ in polygon]
ys = [y for _, y in polygon]
extent = max(max(xs) - min(xs), max(ys) - min(ys))
polygon = simplify_polygon(
polygon, min(self.simplify_tolerance, _RELATIVE_TOLERANCE * extent)
)
return Observation(
image_id=frame['id'],
label=label,
polygon=polygon,
camera_lon=frame['lon'],
camera_lat=frame['lat'],
compass_angle=frame['compass'],
is_pano=frame['is_pano'],
sequence_id=frame['sequence'],
captured_at=frame['captured_at'],
confidence=confidence,
source=source,
image_width=frame['width'],
image_height=frame['height'],
extra={
key: frame[source_key]
for key, source_key in (
('camera_parameters', 'camera_parameters'),
('camera_type', 'camera_type'),
('computed_rotation', 'rotation'),
)
if frame.get(source_key) is not None
},
)
# ---------------------------------------------------------------- mapillary
def _mapillary_observations(
self, frames: list[dict[str, Any]], classes: dict[str, tuple[str, ...]]
) -> dict[str, list[Observation]]:
"""Turn Mapillary detections into observations, fetching if needed."""
label_to_class: dict[str, str] = {}
for cls, labels in classes.items():
for label in labels:
label_to_class.setdefault(label, cls)
wanted = set(label_to_class)
def detections_for(frame):
data = frame.get('detections')
if data is None:
data = self.client.fetch_detections(frame['id'], values=wanted)
return frame, data or []
result: dict[str, list[Observation]] = defaultdict(list)
with ThreadPoolExecutor(max_workers=self.max_workers) as pool:
futures = [pool.submit(detections_for, f) for f in frames]
for future in tqdm(
as_completed(futures),
total=len(futures),
desc='Mapillary detections',
disable=len(frames) <= self.frame_batch_size,
):
raise_if_canceled(self.cancel_event, 'street-level detection')
frame, data = future.result()
for item in data:
label = item.get('value')
if label not in wanted or not item.get('geometry'):
continue
for polygon in MapillaryClient.decode_detection_polygons(
item['geometry']
):
result[label_to_class[label]].append(
self._observation(frame, label, polygon, 'mapillary')
)
return result
# ---------------------------------------------------------------- sam3
def _load_sam3(self):
if self.sam3_model is None:
from rapidtools.models import SAM3Inference
self.sam3_model = SAM3Inference(
model_id=self.sam3_model_id,
device=self.device,
load_in_4bit=self.load_in_4bit,
)
return self.sam3_model
def _download_thumbnail(
self, frame: dict[str, Any], size: str | None = None
) -> Path | None:
"""Fetch a frame's thumbnail (default ``sam3_image_size``) into the cache."""
listed = size is None or str(size) == self.sam3_image_size
size = self.sam3_image_size if size is None else str(size)
target_dir = self.save_directory / 'images'
target_dir.mkdir(parents=True, exist_ok=True)
target = target_dir / f'{frame["id"]}_{size}.jpg'
if target.is_file():
return target
# The address a frame was listed with is that of the default size.
url = (frame.get('thumb_url') if listed else None) or self.client.get_image_url(
frame['id'], size
)
if not url:
return None
return target if self.client._download_image(url, target) else None
def _sam3_observations(
self,
frames: list[dict[str, Any]],
classes: list[str],
outlined: Iterable[str] = (),
) -> dict[str, list[Observation]]:
"""
Detect classes with the segmentation model on thumbnails.
A class in ``outlined`` keeps the outline of every mask and the
number of the mask in its frame (``extra['instance']``), which is
what linking objects across frames needs; the others keep each
mask's bounding box. The model is whatever ``sam3_model`` holds:
anything with SAM 3's ``run_inference``.
"""
outlined = set(outlined)
model = self._load_sam3()
with ThreadPoolExecutor(max_workers=self.max_workers) as pool:
paths = list(
tqdm(
pool.map(self._download_thumbnail, frames),
total=len(frames),
desc='Thumbnails',
)
)
ready = [(f, p) for f, p in zip(frames, paths, strict=True) if p is not None]
result: dict[str, list[Observation]] = defaultdict(list)
for cls in classes:
for start in range(0, len(ready), self.sam3_batch_size):
raise_if_canceled(self.cancel_event, 'SAM 3 street-level detection')
batch = ready[start : start + self.sam3_batch_size]
output = model.run_inference(
[str(p) for _, p in batch],
prompt=self.sam3_prompts.get(cls, cls),
threshold=self.sam3_threshold,
mask_threshold=self.sam3_mask_threshold,
)
if output is None or not output.masks:
continue
scores = (getattr(output, 'raw_response', None) or {}).get('scores')
for n, ((frame, _), masks) in enumerate(
zip(batch, output.masks, strict=False)
):
if cls not in outlined:
for polygon in self._masks_to_polygons(masks):
result[cls].append(
self._observation(frame, cls, polygon, 'sam3')
)
continue
confidence = scores[n] if scores and n < len(scores) else None
for k, mask in enumerate(_mask_stack(masks)):
for ring in mask_outlines(mask):
obs = self._observation(
frame,
cls,
[(float(x), float(y)) for x, y in ring],
'sam3',
float(confidence[k])
if confidence is not None and k < len(confidence)
else None,
)
obs.extra['instance'] = k
result[cls].append(obs)
return result
def _tiles_of_frame(
self, frame: dict[str, Any], folder: Path
) -> (
tuple[dict[str, Any], int, int, list[tuple[tuple[int, int, int], Path]]] | None
):
"""
Download a frame at full resolution and write its tiles to ``folder``.
Returns the frame, its width and height in pixels and every tile
with its file, or ``None`` when the frame could not be had. The
full-resolution file is removed again unless it was there before or
``keep_full_resolution`` is set.
"""
target = self.save_directory / 'images' / f'{frame["id"]}_original.jpg'
cached = target.is_file()
path = self._download_thumbnail(frame, size='original')
if path is None:
return None
try:
with warnings.catch_warnings(), Image.open(path) as image:
# A panorama as it was taken is larger than Pillow expects
# a photo to be.
warnings.simplefilter('ignore', Image.DecompressionBombWarning)
width, height = image.size
layout = tile_layout(
width,
height,
bool(frame['is_pano']),
self.sam3_tile_px,
self.sam3_tile_overlap,
self.sam3_tile_elevation_deg,
)
top = min(t[1] for t in layout)
bottom = max(t[1] + t[2] for t in layout)
rows = np.asarray(image.convert('RGB').crop((0, top, width, bottom)))
except OSError:
return None
finally:
if not cached and not self.keep_full_resolution:
path.unlink(missing_ok=True)
tiles = []
for tile in layout:
file = folder / f'{frame["id"]}_{tile[0]}_{tile[1]}.jpg'
Image.fromarray(cut_tile(rows, tile, top)).save(file, quality=95)
tiles.append((tile, file))
return frame, width, height, tiles
def _sam3_tile_observations(
self, frames: list[dict[str, Any]], classes: list[str]
) -> dict[str, list[Observation]]:
"""
Detect classes with the segmentation model on full-resolution tiles.
One frame per ``sam3_tile_spacing_m`` is downloaded as it was
taken, cut into tiles (see :mod:`~rapidtools.processing.street_tiles`)
and every tile is shown to the model. The masks of a frame's tiles
are joined into outlines on the whole frame. The frames looked at
are remembered per class, since only they can say that a spot is
empty.
"""
keep = thin_frames(
[
(f['id'], f['lon'], f['lat'], f['captured_at'], f['sequence'])
for f in frames
],
self.sam3_tile_spacing_m,
)
frames = [f for f in frames if f['id'] in keep]
logger.info(
f'{len(frames)} frames (one per {self.sam3_tile_spacing_m:g} m) are '
f'searched at full resolution for {", ".join(classes)}.'
)
model = self._load_sam3()
folder = self.save_directory / 'tiles'
folder.mkdir(parents=True, exist_ok=True)
result: dict[str, list[Observation]] = defaultdict(list)
examined: set[str] = set()
n_tiles = 0
chunk = max(1, 2 * self.max_workers)
starts = range(0, len(frames), chunk)
for start in tqdm(
starts, desc='Full-resolution frames', disable=len(starts) < 2
):
raise_if_canceled(self.cancel_event, 'SAM 3 street-level detection')
with ThreadPoolExecutor(max_workers=self.max_workers) as pool:
cut = [
c
for c in pool.map(
lambda f: self._tiles_of_frame(f, folder),
frames[start : start + chunk],
)
if c is not None
]
files = [file for _, _, _, tiles in cut for _, file in tiles]
n_tiles += len(files)
for cls in classes:
masks: list[Any] = []
scores: list[Any] = []
for at in range(0, len(files), self.sam3_batch_size):
raise_if_canceled(self.cancel_event, 'SAM 3 street-level detection')
batch = files[at : at + self.sam3_batch_size]
output = model.run_inference(
[str(p) for p in batch],
prompt=self.sam3_prompts.get(cls, cls),
threshold=self.sam3_threshold,
mask_threshold=self.sam3_mask_threshold,
)
found = list(output.masks) if output and output.masks else []
conf = (
(getattr(output, 'raw_response', None) or {}).get('scores')
if output
else None
) or []
for n in range(len(batch)):
masks.append(found[n] if n < len(found) else None)
scores.append(conf[n] if n < len(conf) else None)
at = 0
for frame, width, height, tiles in cut:
layout = [tile for tile, _ in tiles]
for outline, score in join_tile_masks(
layout,
masks[at : at + len(tiles)],
scores[at : at + len(tiles)],
width,
height,
bool(frame['is_pano']),
):
result[cls].append(
self._observation(frame, cls, outline, 'sam3', score)
)
at += len(tiles)
for file in files:
file.unlink(missing_ok=True)
examined.update(frame['id'] for frame, _, _, _ in cut)
near = ground_range_m(self.camera_height_m, self.sam3_tile_elevation_deg)
for cls in classes:
self._examined[cls] = examined
if any(f['is_pano'] for f in frames):
self._clear_from[cls] = near
result.setdefault(cls, [])
logger.info(
f'{n_tiles} tiles of {len(examined)} frames searched; '
+ ', '.join(f'{len(result[c])} outlines of {c}' for c in classes)
+ '.'
)
return result
@staticmethod
def _masks_to_polygons(masks) -> list[list[tuple[float, float]]]:
"""Normalized bounding polygons of an ``(n, H, W)`` mask stack."""
if masks is None:
return []
arr = np.asarray(masks)
if arr.ndim == 2:
arr = arr[None]
polygons = []
for mask in arr:
ys, xs = np.nonzero(mask)
if xs.size == 0:
continue
h, w = mask.shape
x0, x1 = xs.min() / w, (xs.max() + 1) / w
y0, y1 = ys.min() / h, (ys.max() + 1) / h
polygons.append([(x0, y0), (x1, y0), (x1, y1), (x0, y1)])
return polygons
# ---------------------------------------------------------------- map features
def _map_feature_assets(
self, bbox: BoundingBox, classes: dict[str, tuple[str, ...]]
) -> list[PhysicalAsset]:
"""Point assets from Mapillary's pre-triangulated map features."""
assets: list[PhysicalAsset] = []
for cls, values in classes.items():
raise_if_canceled(self.cancel_event, 'street-level detection')
features = self.client.fetch_map_features(
bbox, values, start_date=self.start_date, end_date=self.end_date
)
counter = 0
for feature in features:
counter += 1
image_ids = list(feature.get('image_ids') or [])
attributes: dict[str, Any] = {
'asset_type': self.asset_type or _slug(cls),
'class': cls,
'label': feature.get('object_value'),
'source': 'mapillary_map_features',
'localization': 'map_feature',
'map_feature_id': str(feature.get('id')),
'n_observations': 0,
'n_images': len(image_ids),
}
if image_ids:
attributes['image_ids'] = image_ids
if feature.get('aligned_direction') is not None:
attributes['aligned_direction'] = round(
float(feature['aligned_direction']), 1
)
if feature.get('first_seen_at'):
attributes['first_seen'] = feature['first_seen_at']
if feature.get('last_seen_at'):
attributes['last_seen'] = feature['last_seen_at']
assets.append(
PhysicalAsset(
id=f'{self.id_prefix}_{_slug(cls)}_{counter:05d}',
geometry=Point(float(feature['lon']), float(feature['lat'])),
attributes=attributes,
)
)
logger.info(
f"'{cls}': {len(features)} Mapillary map features in the region."
)
return assets
# ---------------------------------------------------------------- assets
def _assets_for_class(
self,
cls: str,
obs_list: list[Observation],
counter: int,
camera_frames: Sequence[CameraFrame] | None = None,
) -> tuple[list[PhysicalAsset], int]:
"""Filter, localize and group one class's sightings into assets."""
if self.ego_filter:
ego = estimate_ego_mask(obs_list, min_recurrence=self.ego_min_recurrence)
if ego:
logger.info(
f"'{cls}': {len(ego)} sightings belong to the survey vehicle."
)
obs_list = [o for i, o in enumerate(obs_list) if i not in ego]
method = self._method_for(obs_list)
if method == 'footprints':
return self._assets_from_footprints(cls, obs_list, counter)
if method in ('points', 'instances'):
place = (
self._assets_from_points
if method == 'points'
else self._assets_from_instances
)
assets, counter, done = place(cls, obs_list, counter)
if done:
return assets, counter
method = 'fusion'
if method == 'fusion':
return self._assets_from_fusion(cls, obs_list, counter, camera_frames)
if method != 'cluster':
return self._assets_from_tracks(cls, obs_list, counter, camera_frames)
located = [
o
for o in obs_list
if localize(
o,
camera_height_m=self.camera_height_m,
min_range_m=self.min_range_m,
max_range_m=self.max_range_m,
min_area_fraction=self.min_area_fraction,
)
]
if len(located) < len(obs_list):
logger.info(
f"'{cls}': {len(obs_list) - len(located)} sightings had no plausible "
'ground position.'
)
assets = []
for members in cluster_observations(located, self.cluster_radius_m):
if len(members) < self.min_observations:
continue
counter += 1
assets.append(self._build_asset(cls, members, counter))
return assets, counter
def _describe(
self, cls: str, members: list[Observation], index: int
) -> tuple[str, dict[str, Any]]:
"""Asset ID and the attributes every localization method writes."""
confidences = [o.confidence for o in members if o.confidence is not None]
dates = sorted(o.captured_at for o in members if o.captured_at)
ranges = [o.range_m for o in members if o.range_m is not None]
labels = Counter(o.label for o in members).most_common(1)
sources = Counter(o.source for o in members).most_common(1)
attributes: dict[str, Any] = {
'asset_type': self.asset_type or _slug(cls),
'class': cls,
# An object only an overhead image shows has no sightings:
'label': labels[0][0] if labels else None,
'source': sources[0][0] if sources else 'aerial',
'n_observations': len(members),
'sequence_ids': sorted({o.sequence_id for o in members if o.sequence_id}),
'observations': [o.to_dict() for o in members],
}
if ranges:
attributes['min_range_m'] = round(min(ranges), 1)
if confidences:
attributes['confidence'] = round(max(confidences), 3)
if dates:
attributes['first_seen'], attributes['last_seen'] = dates[0], dates[-1]
return f'{self.id_prefix}_{_slug(cls)}_{index:05d}', attributes
def _assets_from_fusion(
self,
cls: str,
obs_list: list[Observation],
counter: int,
camera_frames: Sequence[CameraFrame] | None = None,
) -> tuple[list[PhysicalAsset], int]:
"""Place one class by occupancy fusion (see ``street_fusion``)."""
labels = {o.label for o in obs_list}
surface = bool(labels) and all(
v.startswith(('construction--', 'nature--')) for v in labels
)
# Classes that lie on the ground are read as plans seen aslant.
named, _ = resolve_classes([cls], self.label_mapper)
ground = {v for v in labels if v != cls} or set(named.get(cls) or ())
flat = bool(ground) and all(
v.startswith(('construction--flat--', 'marking--')) for v in ground
)
objects, counts, unproject = fuse_observations(
obs_list,
camera_frames,
camera_height_m=self.camera_height_m,
min_range_m=self.min_range_m,
max_range_m=self.fusion_range_m,
cell_m=self.cell_m,
occupancy_threshold=self.occupancy_threshold,
min_views=max(2, self.min_observations),
min_object_width_m=self.min_object_width_m,
split_instances=not (surface or flat),
strong_views=self.strong_views,
flat=flat,
bearing_sigma_deg=self.bearing_sigma_deg,
aerial=self._aerial_for(cls),
clear_from_m=self._clear_from.get(cls),
)
if 'aerial_detections' in counts:
logger.info(
f"'{cls}': {counts['aerial_detections']} overhead detections; "
f'{counts.get("from_both", 0)} objects seen from both, '
f'{counts.get("from_street", 0)} from the street only, '
f'{counts.get("from_aerial", 0)} from above only.'
)
logger.info(
f"'{cls}': {counts.get('scans', 0)} sightings fused over "
f'{counts.get("frames", 0)} frames on '
f'{counts.get("cell_mm", 0) / 1000:g} m cells (objects under '
f'{counts.get("min_width_mm", 0) / 1000:g} m dropped); '
f'{counts.get("blobs", 0)} occupied '
f'blobs, {counts.get("split", 0)} split where frames saw several '
f'outlines, {counts.get("fragment", 0)} too small dropped, '
f'{counts.get("few_views", 0)} seen in too few frames dropped, '
f'{counts.get("strongly_seen", 0)} kept on the agreement of many '
f'frames against the vote, '
f'{counts.get("merged_days", 0)} joined across survey days -> '
f'{counts["objects"]} objects.'
)
assets = []
for obj in objects:
counter += 1
asset_id, attributes = self._describe(cls, obj.members, counter)
attributes.update(object_attributes(obj))
if self.geometry == 'footprint':
geometry: Any = Polygon([unproject(x, y) for x, y in obj.footprint])
else:
geometry = Point(*unproject(obj.x, obj.y))
assets.append(
PhysicalAsset(id=asset_id, geometry=geometry, attributes=attributes)
)
if self.reid and len(assets) > 1:
# Appearance needs street crops: objects only seen from above stay.
seen = [a for a in assets if a.attributes.get('observations')]
unseen = [a for a in assets if not a.attributes.get('observations')]
if len(seen) > 1:
assets = self._merge_by_appearance(seen) + unseen
return assets, counter
def _aerial_for(self, cls: str) -> AerialEvidence | None:
"""The overhead evidence for one class, if any was given."""
if isinstance(self.aerial, Mapping):
return self.aerial.get(cls)
return self.aerial
def _assets_from_points(
self,
cls: str,
obs_list: list[Observation],
counter: int,
keep: Callable[[Polygon], bool] | None = None,
) -> tuple[list[PhysicalAsset], int, bool]:
"""
Place one class from the survey's point cloud (see ``street_structures``).
``keep``, when given, is asked about every structure's footprint (in
longitude and latitude) before it is numbered; structures it turns
down are left out.
Returns the assets, the counter and whether the method could run;
``False`` means no frame had a point cloud and the caller should
fall back to fusion.
"""
clusters = {
o.image_id: self._clusters[o.image_id]
for o in obs_list
if o.image_id in self._clusters
}
if not clusters:
logger.warning(
f"'{cls}': no frame has a Mapillary point cloud (sfm_cluster); "
'placing it by occupancy fusion instead, without footprints '
'measured from 3-D points.'
)
return [], counter, False
structures, counts, unproject = discover_structures(
obs_list,
clusters,
cache_dir=self.point_cloud_dir,
camera_height_m=self.camera_height_m,
max_range_m=self.structure_range_m,
min_gap_m=self.min_gap_m,
assumed_depth_m=self.assumed_depth_m,
footprint=self.structure_footprint,
max_views=self.structure_views,
max_workers=self.max_workers,
)
logger.info(
f"'{cls}': {counts.get('clusters', 0)} point clouds, "
f'{counts.get("points", 0)} points on the class after voting over '
f'{counts.get("frames", 0)} frames, {counts.get("fragments", 0)} wall '
f'fragments -> {counts["structures"]} structures.'
)
assets = []
for structure in structures:
if structure.n_images < self.min_observations:
continue
shape = Polygon([unproject(x, y) for x, y in structure.footprint])
if keep is not None and not keep(shape):
continue
counter += 1
asset_id, attributes = self._describe(cls, structure.members, counter)
attributes.update(structure_attributes(structure))
if self.geometry == 'point':
geometry: Any = Point(*unproject(structure.x, structure.y))
else:
geometry = shape
assets.append(
PhysicalAsset(id=asset_id, geometry=geometry, attributes=attributes)
)
return assets, counter, True
def _assets_from_footprints(
self, cls: str, obs_list: list[Observation], counter: int
) -> tuple[list[PhysicalAsset], int]:
"""
Give one class's sightings to known footprints (see ``street_footprints``).
Every footprint that was seen becomes an asset with its own
geometry, identifier and attributes. When ``report_unmapped`` is set
and the frames have point clouds, buildings the layer does not have
are placed too: from per-object outlines, the ones that lie on no
footprint (``'instances'``); from surface outlines, which claim no
single building, the structures of ``'points'`` that lie off every
footprint.
"""
views, unclaimed, counts = footprint_sightings(
obs_list,
[shape for _, shape, _ in self.footprints],
camera_height_m=self.camera_height_m,
max_range_m=self.structure_range_m,
max_views=self.structure_views,
)
unseen = sum(v.n_expected >= 3 and not v.n_seen for v in views)
logger.info(
f"'{cls}': {counts.get('footprints_seen', 0)} of the "
f'{counts.get("footprints_expected", 0)} footprints in view were seen '
f'in {counts.get("frames", 0)} frames; {unseen} in view of three or '
f'more frames have no sighting (hidden, or no longer standing); '
f'{counts.get("unclaimed", 0)} outlines lie on no footprint.'
)
assets = []
for (known_id, shape, known), view in zip(self.footprints, views, strict=True):
if not view.members or view.n_images < self.min_observations:
continue
counter += 1
asset_id, attributes = self._describe(cls, view.members, counter)
attributes = {
**known,
**attributes,
'localization': 'footprint',
'n_images': view.n_images,
'frames_expected': view.n_expected,
'frames_seen': view.n_seen,
}
assets.append(
PhysicalAsset(
id=known_id or asset_id, geometry=shape, attributes=attributes
)
)
if not self.report_unmapped:
return assets, counter
left = [obs_list[n] for n in unclaimed]
unmapped: list[PhysicalAsset] = []
if any('instance' in (o.extra or {}) for o in obs_list):
if any(o.image_id in self._clusters for o in left):
unmapped, counter, _ = self._assets_from_instances(cls, left, counter)
elif any(o.image_id in self._clusters for o in obs_list):
unmapped, counter, _ = self._assets_from_points(
cls,
obs_list,
counter,
keep=_OffFootprints([shape for _, shape, _ in self.footprints]),
)
logger.info(
f"'{cls}': {len(unmapped)} structures from the point cloud lie off "
'every footprint.'
)
for asset in unmapped:
asset.attributes['unmapped'] = True
assets.extend(unmapped)
return assets, counter
def _assets_from_instances(
self, cls: str, obs_list: list[Observation], counter: int
) -> tuple[list[PhysicalAsset], int, bool]:
"""
Place one class from per-object outlines (see ``street_instances``).
Returns the assets, the counter and whether the method could run;
``False`` means the sightings are not per-object outlines or no
frame had a point cloud, and the caller should fall back to fusion.
"""
clusters = {
o.image_id: self._clusters[o.image_id]
for o in obs_list
if o.image_id in self._clusters
}
outlined = any('instance' in (o.extra or {}) for o in obs_list)
if not clusters or not outlined:
logger.warning(
f"'{cls}': localization_method='instances' needs one outline per "
'object from a segmentation model (detection_source='
f"{{'{cls}': 'sam3'}}) and frames with a Mapillary point cloud "
'(sfm_cluster); placing it by occupancy fusion instead.'
)
return [], counter, False
structures, counts, unproject = discover_instances(
obs_list,
clusters,
cache_dir=self.point_cloud_dir,
camera_height_m=self.camera_height_m,
max_range_m=self.structure_range_m,
min_gap_m=self.min_gap_m,
assumed_depth_m=self.assumed_depth_m,
footprint=self.structure_footprint,
max_views=self.structure_views,
max_workers=self.max_workers,
whole_object=self._whole_object if self.join_wings_by == 'prompt' else None,
)
logger.info(
f"'{cls}': {counts.get('outlines', 0)} outlines in "
f'{counts.get("frames", 0)} frames linked through '
f'{counts.get("points", 0)} points of {counts.get("clusters", 0)} point '
f'clouds into {counts.get("tracks", 0)} tracks, '
f'{counts.get("joined", 0)} joined as wings of one structure -> '
f'{counts["structures"]} structures.'
)
assets = []
for structure in structures:
if structure.n_images < self.min_observations:
continue
counter += 1
asset_id, attributes = self._describe(cls, structure.members, counter)
attributes.update(structure_attributes(structure))
if self.geometry == 'point':
geometry: Any = Point(*unproject(structure.x, structure.y))
else:
geometry = Polygon([unproject(x, y) for x, y in structure.footprint])
assets.append(
PhysicalAsset(id=asset_id, geometry=geometry, attributes=attributes)
)
return assets, counter, True
def _whole_object(
self, image_id: str, points: np.ndarray
) -> list[list[np.ndarray]]:
"""
The candidate outlines of the object under a few points of a frame.
Asked of the point-prompt model (``sam3_point_model``, loaded on
first use) on the frame's cached thumbnail; no answer when the
thumbnail is not there.
"""
path = self.save_directory / 'images' / f'{image_id}_{self.sam3_image_size}.jpg'
if not path.is_file():
return []
if self.sam3_point_model is None:
from rapidtools.models.sam3 import SAM3PointInference
self.sam3_point_model = SAM3PointInference(
model_id=self.sam3_model_id, device=self.device
)
with Image.open(path) as image:
width, height = image.size
pixels = [[(float(x) * width, float(y) * height) for x, y in points]]
candidates: list[list[np.ndarray]] = []
for stack in self.sam3_point_model.segment_at(str(path), pixels):
candidates.extend(mask_outlines(mask) for mask in _mask_stack(stack))
return candidates
def _assets_from_tracks(
self,
cls: str,
obs_list: list[Observation],
counter: int,
camera_frames: Sequence[CameraFrame] | None = None,
) -> tuple[list[PhysicalAsset], int]:
"""Track, triangulate, merge (and optionally re-identify) one class."""
method = (
'tracks' if self.localization_method == 'auto' else self.localization_method
)
estimates, counts, unproject = discover_objects(
obs_list,
method=method,
max_single_view_range_m=self.max_single_view_range_m,
frames=camera_frames,
witness_radius_m=self.witness_radius_m,
camera_height_m=self.camera_height_m,
min_range_m=self.min_range_m,
max_range_m=self.max_range_m,
min_area_fraction=self.min_area_fraction,
bearing_sigma_deg=self.bearing_sigma_deg,
max_merge_m=self.max_merge_m or 1.5 * self.cluster_radius_m,
track_gap_frames=self.track_gap_frames,
min_path_distance_m=self.min_path_distance_m,
min_object_width_m=self.min_object_width_m or 1.0,
object_height_m=self.object_height_m,
object_size_m=self.object_size_m,
merge_pieces_of_neighbors=self.merge_pieces_of_neighbors,
close_range_m=self.close_range_m,
min_sighting_deg=self.min_sighting_deg,
report_single_views=self.report_single_views,
abeam_window_deg=self.abeam_window_deg,
)
logger.info(
f"'{cls}': {counts['with_bearing']} sightings with a bearing, "
f'{counts.get("duplicates", 0)} duplicate outlines dropped, '
f'{counts.get("approach", 0)} looking down the road set aside '
f'({counts.get("attached", 0)} attached afterward), '
f'{counts["tracks"]} tracks, {counts["moving"]} moving dropped, '
f'{counts["fragment"]} too small dropped, {counts["on_path"]} on the '
f'driven path dropped, {counts.get("pieces", 0)} pieces of a neighbor '
f'folded in, {counts.get("far_only", 0)} never seen up close dropped, '
f'{counts.get("far_single_view", 0) + counts.get("single_view", 0)} '
f'single views dropped, {counts.get("indiscernible", 0)} never seen '
f'larger than a speck dropped, {counts.get("unwitnessed", 0)} '
f'contradicted by nearby frames -> {counts["objects"]} objects.'
)
assets = []
for est in estimates:
if est.n_images < self.min_observations:
continue
counter += 1
assets.append(self._asset_from_estimate(cls, est, unproject, counter))
if self.reid and len(assets) > 1:
assets = self._merge_by_appearance(assets)
return assets, counter
def _asset_from_estimate(
self, cls: str, est: Any, unproject: Any, index: int
) -> PhysicalAsset:
"""Describe one tracked/voted object as a point asset."""
lon, lat = unproject(est.x, est.y)
members = est.members
ranges = [o.range_m for o in members if o.range_m is not None]
confidences = [o.confidence for o in members if o.confidence is not None]
dates = sorted(o.captured_at for o in members if o.captured_at)
attributes: dict[str, Any] = {
'asset_type': self.asset_type or _slug(cls),
'class': cls,
'label': Counter(o.label for o in members).most_common(1)[0][0],
'source': Counter(o.source for o in members).most_common(1)[0][0],
'n_observations': len(members),
'sequence_ids': sorted({o.sequence_id for o in members if o.sequence_id}),
'observations': [o.to_dict() for o in members],
}
attributes.update(estimate_attributes(est))
if ranges:
attributes['min_range_m'] = round(min(ranges), 1)
if confidences:
attributes['confidence'] = round(max(confidences), 3)
if dates:
attributes['first_seen'], attributes['last_seen'] = dates[0], dates[-1]
return PhysicalAsset(
id=f'{self.id_prefix}_{_slug(cls)}_{index:05d}',
geometry=Point(lon, lat),
attributes=attributes,
)
def _merge_by_appearance(self, assets: list[PhysicalAsset]) -> list[PhysicalAsset]:
"""Merge look-alike objects from different sequences (see ``reid``)."""
from .reid import (
AppearanceEmbedder,
ReidCandidate,
crop_observation,
merge_by_appearance,
merged_position,
pairs_to_compare,
views_for_reid,
)
by_key = {a.id: a for a in assets}
obs_by_key: dict[str, list[Observation]] = {}
candidates = []
for asset in assets:
obs = [
Observation.from_dict(r)
for r in asset.attributes.get('observations', [])
]
obs.sort(key=lambda o: o.range_m if o.range_m is not None else 1e9)
obs_by_key[asset.id] = obs
candidates.append(
ReidCandidate.from_observations(
asset.id, asset.geometry.centroid.x, asset.geometry.centroid.y, obs
)
)
pairs = pairs_to_compare(candidates, max_distance_m=self.reid_max_distance_m)
if not pairs:
return assets
crops: dict[str, Image.Image] = {}
thumb_width = int(self.reid_image_size)
for key in sorted({k for pair in pairs for k in pair}):
# Widest sighting first; none wide enough means no crop, and a
# candidate without a crop is never merged by appearance.
for sighting in views_for_reid(
obs_by_key[key], thumb_width, self.reid_min_width_px
):
path = self._download_thumbnail(
{'id': sighting.image_id, 'thumb_url': None},
size=self.reid_image_size,
)
if path is None:
continue
try:
with Image.open(path) as image:
crops[key] = crop_observation(
image.convert('RGB'), sighting.polygon
)
except OSError:
continue
break
embedder = self.reid_embedder or AppearanceEmbedder(
self.reid_model, device=self.device
)
groups = merge_by_appearance(
candidates,
crops,
embedder,
max_distance_m=self.reid_max_distance_m,
min_similarity=self.reid_min_similarity,
)
merged: list[PhysicalAsset] = []
n_merged = 0
for group in groups:
parts = [by_key[k] for k in group]
if len(parts) == 1:
merged.append(parts[0])
continue
n_merged += len(parts) - 1
lon, lat = merged_position(
[(a.geometry.centroid.x, a.geometry.centroid.y) for a in parts]
)
records = [r for a in parts for r in a.attributes.get('observations', [])]
attributes = dict(parts[0].attributes)
attributes.update(
{
'observations': records,
'n_observations': len(records),
'n_images': len({r['image_id'] for r in records}),
'sequence_ids': sorted(
{s for a in parts for s in a.attributes.get('sequence_ids', [])}
),
'reid_merged': len(parts),
}
)
if any(a.attributes.get('localization') == 'triangulated' for a in parts):
attributes['localization'] = 'triangulated'
dates = sorted(
d
for a in parts
for d in (a.attributes.get('first_seen'), a.attributes.get('last_seen'))
if d
)
if dates:
attributes['first_seen'], attributes['last_seen'] = dates[0], dates[-1]
merged.append(
PhysicalAsset(
id=parts[0].id, geometry=Point(lon, lat), attributes=attributes
)
)
if n_merged:
logger.info(
f'Appearance re-identification merged {n_merged} duplicate objects.'
)
return merged
def _build_asset(
self, cls: str, members: list[Observation], index: int
) -> PhysicalAsset:
"""Position and describe one object from its sightings (legacy method)."""
# Every member passed ``localize``, which sets all four of these:
rays: dict[str, tuple[float, float, float]] = {}
lons: list[float] = []
lats: list[float] = []
ranges: list[float] = []
for o in members:
if o.lon is None or o.lat is None or o.bearing is None or o.range_m is None:
raise ValueError(f'Observation {o.image_id!r} has not been localized.')
rays.setdefault(o.image_id, (o.camera_lon, o.camera_lat, o.bearing))
lons.append(o.lon)
lats.append(o.lat)
ranges.append(o.range_m)
project, unproject = local_projection(lons[0], lats[0])
lon = sum(lons) / len(members)
lat = sum(lats) / len(members)
localization, rms = 'single_view', None
if len(rays) >= 2:
fix = intersect_bearings(
list(rays.values()),
project,
unproject,
max_range_m=2 * self.max_range_m,
)
if fix is not None:
fx, fy, rms = fix
# Only trust the intersection when it agrees with the sightings:
px, py = project(fx, fy)
mx, my = project(lon, lat)
if math.hypot(px - mx, py - my) <= 3 * self.cluster_radius_m:
lon, lat, localization = fx, fy, 'triangulated'
else:
rms = None
confidences = [o.confidence for o in members if o.confidence is not None]
dates = sorted(o.captured_at for o in members if o.captured_at)
attributes: dict[str, Any] = {
'asset_type': self.asset_type or _slug(cls),
'class': cls,
'label': Counter(o.label for o in members).most_common(1)[0][0],
'source': Counter(o.source for o in members).most_common(1)[0][0],
'n_observations': len(members),
'n_images': len(rays),
'localization': localization,
'min_range_m': round(min(ranges), 1),
'sequence_ids': sorted({o.sequence_id for o in members if o.sequence_id}),
'observations': [o.to_dict() for o in members],
}
if rms is not None:
attributes['position_rms_m'] = round(rms, 2)
if confidences:
attributes['confidence'] = round(max(confidences), 3)
if dates:
attributes['first_seen'], attributes['last_seen'] = dates[0], dates[-1]
return PhysicalAsset(
id=f'{self.id_prefix}_{_slug(cls)}_{index:05d}',
geometry=Point(lon, lat),
attributes=attributes,
)
[docs]
class MapillaryObjectImageExtractor:
"""
Crop each detected object out of the street-level images that saw it.
Reads the ``observations`` written by :class:`MapillaryFeatureExtractor`,
picks the closest distinct views of every asset, downloads those images
at the requested thumbnail size, crops the detection with a margin and
attaches the crops as :class:`~rapidtools.core.ImageAsset` objects. The
detection outline can be drawn on the crop with the same ``outline_*``
options as :class:`~rapidtools.processing.AerialImageryExtractor`.
Args:
save_directory (str | Path):
Where the crops are written.
access_token (str | Path | None):
Mapillary API token, the path of a file holding it, or ``None`` to
read the ``MAPILLARY_ACCESS_TOKEN`` environment variable. Not
needed when ``client`` is given.
max_images_per_asset (int):
Views to keep per object, from distinct images. Defaults to 2.
view_selection (str):
Which views to keep: ``'widest'`` (default, by the outline's
angular width) or ``'closest'`` (by ground range). The widest
views show the most of the object and are what a verifying
model should judge; the closest can be an end-on sliver or a
bumper at 3 m, and on a labeled block they let a bush through
the verifier as a car where the widest views let nothing
through.
image_size (str):
Thumbnail size to download: ``'1024'``, ``'2048'`` or
``'original'``. Defaults to ``'2048'``.
crop_buffer (float | str):
Margin around the detection: a percentage of its longest side
(``'25%'``, the default), pixels, or a distance such as ``'1 m'``
converted at the object's range.
min_crop_px (int):
Crops are grown to at least this many pixels on their shorter
side. Defaults to 256.
overlay_asset_outline (bool):
Draw the detection outline on the crop. Defaults to ``False``.
outline_shape (str):
Outline shape, as in
:class:`~rapidtools.processing.AerialImageryExtractor`:
``'geometry'``, ``'bbox'``, ``'rotated_bbox'``, ``'convex_hull'``
or ``'corners'``. Defaults to ``'geometry'``.
outline_buffer (float | str):
Outline offset in pixels, percent, or a distance such as
``'0.5 m'`` converted at the object's range. Defaults to ``0``.
outline_width (int | float | str):
Stroke width in pixels or percent of the shorter crop side.
Defaults to ``6``.
outline_color (str | tuple[int, int, int]):
PIL color of the outline. Defaults to ``'red'``.
image_prefix (str):
Prefix of the crop filenames and image IDs. Defaults to
``'street'``.
max_workers (int):
Concurrent downloads. Each source image is downloaded once,
cropped for every asset that needs it and released, so at most
this many decoded images are in memory at a time. Defaults to 5.
cancel_event (threading.Event | None):
Cooperative cancellation flag checked between images.
client (MapillaryClient | None):
Pre-configured client; one is created from ``access_token``
otherwise.
Example:
>>> extractor = MapillaryObjectImageExtractor(
... 'output/vehicle_crops', access_token='MLY|...',
... max_images_per_asset=2, overlay_asset_outline=True,
... outline_shape='corners',
... )
>>> vehicles = extractor(vehicles) # doctest: +SKIP
>>> vehicles[0].image_assets[0].properties['range_m'] # doctest: +SKIP
6.1
"""
stage = Stage.EXTRACT_IMAGERY
def __init__(
self,
save_directory: str | Path,
access_token: str | Path | None = None,
max_images_per_asset: int = 2,
view_selection: str = 'widest',
image_size: str = '2048',
crop_buffer: float | str = '25%',
min_crop_px: int = 256,
overlay_asset_outline: bool = False,
outline_shape: str = 'geometry',
outline_buffer: float | str = 0,
outline_width: int | float | str = 6,
outline_color: str | tuple[int, int, int] = 'red',
image_prefix: str = 'street',
max_workers: int = 5,
cancel_event: threading.Event | None = None,
client: MapillaryClient | None = None,
) -> None:
if client is None:
access_token = resolve_mapillary_token(access_token)
if max_images_per_asset < 1:
raise ValueError('max_images_per_asset must be at least 1.')
if outline_shape not in outlines.OUTLINE_SHAPES:
raise ValueError(
f'outline_shape must be one of {outlines.OUTLINE_SHAPES}, '
f'got {outline_shape!r}.'
)
self.save_directory = Path(save_directory).resolve()
self.max_images_per_asset = max_images_per_asset
if view_selection not in ('closest', 'widest'):
raise ValueError(
f"view_selection must be 'closest' or 'widest', got {view_selection!r}."
)
self.view_selection = view_selection
self.image_size = str(image_size)
self._crop_buffer_spec = outlines.parse_outline_buffer(crop_buffer)
self.crop_buffer = crop_buffer
self.min_crop_px = min_crop_px
self.overlay_asset_outline = overlay_asset_outline
self.outline_shape = outline_shape
self._outline_buffer_spec = outlines.parse_outline_buffer(outline_buffer)
self._outline_width_spec = outlines.parse_outline_width(outline_width)
outlines.validate_outline_color(outline_color)
self.outline_color = outline_color
self.image_prefix = image_prefix
self.max_workers = max_workers
self.cancel_event = cancel_event
self.client = client or MapillaryClient(
access_token, save_dir=self.save_directory / 'source_images'
)
# ---------------------------------------------------------------- images
def _source_image(self, image_id: str) -> Image.Image | None:
"""Download the source image at the configured size."""
url = self.client.get_image_url(image_id, self.image_size)
if not url:
return None
try:
response = self.client.session.get(url, timeout=REQUESTS_TIMEOUT_VAL)
response.raise_for_status()
return Image.open(BytesIO(response.content)).convert('RGB')
except Exception as exc: # noqa: BLE001 - one bad image must not stop the run
logger.error(f'Could not download image {image_id}: {exc}')
return None
[docs]
@staticmethod
def select_observations(
observations: Sequence[Observation], limit: int, by: str = 'closest'
) -> list[Observation]:
"""
Pick the closest (or widest) views of an object, one per image.
Args:
observations: The object's sightings.
limit: How many to keep.
by: ``'closest'`` ranks by ground range, nearest first;
``'widest'`` by the outline's angular width, widest first.
Example:
>>> from rapidtools.core import Observation
>>> def obs(i, rng):
... o = Observation(str(i), 'car', [(0, 0), (1, 0), (1, 1)], 0, 0, 0)
... o.range_m = rng
... return o
>>> [o.image_id for o in MapillaryObjectImageExtractor.select_observations(
... [obs(1, 9.0), obs(2, 4.0), obs(2, 3.0), obs(3, 6.0)], 2)]
['2', '3']
"""
if by == 'widest':
ranked = sorted(
observations,
key=lambda o: (-polygon_angular_size(o)[0], -(o.confidence or 0.0)),
)
else:
ranked = sorted(
observations,
key=lambda o: (
o.range_m if o.range_m is not None else float('inf'),
-(o.confidence or 0.0),
),
)
chosen: list[Observation] = []
seen: set[str] = set()
for obs in ranked:
if obs.image_id in seen:
continue
chosen.append(obs)
seen.add(obs.image_id)
if len(chosen) == limit:
break
return chosen
def _crop(
self, image: Image.Image, obs: Observation
) -> tuple[
Image.Image, list[tuple[float, float]], tuple[int, int, int, int], float
]:
"""
Crop one detection out of its source image.
Returns the crop, the detection polygon in crop pixel coordinates,
the crop box in source pixels (after any seam roll) and the
approximate meters per pixel at the object's range.
"""
width, height = image.size
polygon = list(obs.polygon)
x0, _, x1, _ = obs.bbox
if obs.is_pano and (x1 - x0) > 0.5:
# The object straddles the panorama seam: roll the image by half a
# turn so it becomes contiguous, and shift the polygon to match.
arr = np.roll(np.asarray(image), width // 2, axis=1)
image = Image.fromarray(arr)
polygon = [((x + 0.5) % 1.0, y) for x, y in polygon]
xs = [x * width for x, _ in polygon]
ys = [y * height for _, y in polygon]
left, right, top, bottom = min(xs), max(xs), min(ys), max(ys)
extent = max(right - left, bottom - top, 1.0)
meters_per_pixel = 0.0
if obs.range_m and obs.is_pano:
meters_per_pixel = obs.range_m * 2 * math.pi / width
margin = outlines.resolve_buffer_px(
self._crop_buffer_spec, extent, meters_per_pixel
)
left, right = left - margin, right + margin
top, bottom = top - margin, bottom + margin
# Grow to the minimum size, centered on the detection:
for lo, hi, size, setter in (
(left, right, width, 'x'),
(top, bottom, height, 'y'),
):
span = hi - lo
if span < self.min_crop_px:
grow = (self.min_crop_px - span) / 2
lo, hi = lo - grow, hi + grow
lo, hi = max(0, int(math.floor(lo))), min(size, int(math.ceil(hi)))
if setter == 'x':
left, right = lo, hi
else:
top, bottom = lo, hi
box = (int(left), int(top), int(right), int(bottom))
crop = image.crop(box)
pixel_polygon = [(x - box[0], y - box[1]) for x, y in zip(xs, ys, strict=True)]
return crop, pixel_polygon, box, meters_per_pixel
def _chosen_views(self, asset: PhysicalAsset) -> list[Observation]:
"""The observations of one asset that will be cropped."""
records = asset.attributes.get('observations') or []
observations = [Observation.from_dict(r) for r in records]
return self.select_observations(
observations, self.max_images_per_asset, by=self.view_selection
)
def _process_image(
self, image_id: str, jobs: list[tuple[PhysicalAsset, Observation]]
) -> dict[tuple[str, str], ImageAsset]:
"""
Download one source image and crop every asset that was seen in it.
Returns the crops keyed by ``(asset id, image id)``; the image is
released when this returns.
"""
raise_if_canceled(self.cancel_event, 'street-level image extraction')
image = self._source_image(image_id)
if image is None:
return {}
crops: dict[tuple[str, str], ImageAsset] = {}
for asset, obs in jobs:
crops[(asset.id, image_id)] = self._crop_and_save(image, asset, obs)
return crops
def _crop_and_save(
self, image: Image.Image, asset: PhysicalAsset, obs: Observation
) -> ImageAsset:
"""Crop one observation out of its source image and write it."""
crop, pixel_polygon, box, mpp = self._crop(image, obs)
if self.overlay_asset_outline:
outlines.draw_outline(
crop,
pixel_polygon,
is_closed=True,
shape=self.outline_shape,
buffer=self._outline_buffer_spec,
width=self._outline_width_spec,
color=self.outline_color,
meters_per_pixel=mpp,
)
path = (
self.save_directory / f'{self.image_prefix}_{asset.id}_{obs.image_id}.jpg'
)
crop.save(path, quality=92)
return ImageAsset(
id=f'{asset.id}_{self.image_prefix}_{obs.image_id}',
path=path,
properties={
'image_id': obs.image_id,
'captured_at': obs.captured_at,
'label': obs.label,
'confidence': obs.confidence,
'range_m': obs.range_m,
'bearing': obs.bearing,
'crop_box': list(box),
'source_image_size': list(image.size),
'outline': [[round(x, 1), round(y, 1)] for x, y in pixel_polygon],
'source': obs.source,
},
)
def _remove_stale_crops(self, assets: Sequence[PhysicalAsset]) -> int:
"""
Delete this extractor's own crop files for ``assets`` from earlier runs.
Asset IDs are numbered afresh by every detection run, so a crop named
for ``street_vehicles_00002`` in the save directory may show a
different vehicle than today's ``street_vehicles_00002``. Only files
following this extractor's naming scheme (``<prefix>_<asset id>_<image
id>.jpg``) are touched.
"""
removed = 0
for asset in assets:
for path in self.save_directory.glob(
f'{self.image_prefix}_{asset.id}_*.jpg'
):
rest = path.stem[len(f'{self.image_prefix}_{asset.id}_') :]
if rest.isdigit():
path.unlink()
removed += 1
return removed
def __call__(
self, asset_collection: PhysicalAssetCollection
) -> PhysicalAssetCollection:
"""
Attach street-level crops to every asset that carries observations.
Args:
asset_collection (PhysicalAssetCollection):
Output of :class:`MapillaryFeatureExtractor`. Assets without
an ``observations`` attribute are left untouched.
Returns:
PhysicalAssetCollection: The same collection with crops attached.
"""
targets = [a for a in asset_collection if a.attributes.get('observations')]
if not targets:
logger.warning('No assets with street-level observations to crop.')
return asset_collection
# Plan the crops per source image so each image is downloaded once,
# served to every asset seen in it, and released:
chosen = {asset.id: self._chosen_views(asset) for asset in targets}
jobs: dict[str, list[tuple[PhysicalAsset, Observation]]] = defaultdict(list)
for asset in targets:
for obs in chosen[asset.id]:
jobs[obs.image_id].append((asset, obs))
logger.info(
f'Cropping up to {self.max_images_per_asset} views for '
f'{len(targets)} assets from {len(jobs)} images...'
)
self.save_directory.mkdir(parents=True, exist_ok=True)
stale = self._remove_stale_crops(targets)
if stale:
logger.info(
f'Removed {stale} crops left by an earlier run for assets with the '
'same IDs (asset numbering restarts with every run).'
)
crops: dict[tuple[str, str], ImageAsset] = {}
with ThreadPoolExecutor(max_workers=self.max_workers) as pool:
futures = {
pool.submit(self._process_image, image_id, image_jobs): image_id
for image_id, image_jobs in jobs.items()
}
try:
for future in tqdm(
as_completed(futures), total=len(futures), desc='Cropping objects'
):
image_id = futures[future]
try:
crops.update(future.result())
except Exception as exc:
if isinstance(exc, OperationCanceled):
raise
logger.error(f'Failed to crop image {image_id}: {exc}')
except OperationCanceled:
for future in futures:
future.cancel()
raise
# Attach the crops in selection order (closest or widest view first),
# whatever order the downloads finished in:
for asset in targets:
for obs in chosen[asset.id]:
crop = crops.get((asset.id, obs.image_id))
if crop is not None:
asset.add_image_assets(crop)
logger.info(f'Saved {len(crops)} crops to {self.save_directory}.')
return asset_collection