-
Notifications
You must be signed in to change notification settings - Fork 885
Added functionality to pair images with Field Of View Cones #1090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aliaksandr960
wants to merge
26
commits into
mapillary:main
Choose a base branch
from
aliaksandr960:cones-to-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
f1e66e4
Add requrements and start-build docker scripts
aliaksandr960 d4b0216
Update docker file
aliaksandr960 ba1c74b
Updated exif reader to support ypr
aliaksandr960 043a306
Fixes
aliaksandr960 a01cf02
readme update
aliaksandr960 21a87a8
Updated readme
aliaksandr960 a5a272b
Readme update
aliaksandr960 8e38699
Readme update
aliaksandr960 98a9295
Readme update
aliaksandr960 03dbaea
Readme update, config update
aliaksandr960 02cedaf
ReadMe update
aliaksandr960 09c1d7b
Removed redundatnt pring
aliaksandr960 1d5f071
Readme update
aliaksandr960 544481a
Readme update
aliaksandr960 1fb82dc
Readme update
aliaksandr960 f237d50
Readme update
aliaksandr960 dc05c98
Edit readme
aliaksandr960 692c456
Removed redundat code
aliaksandr960 55eb9f3
Removed redundat code
aliaksandr960 af64267
Updated requrements
aliaksandr960 2c18263
Reverted readme
aliaksandr960 897bc2a
Reverted dockerfile
aliaksandr960 b1e108c
Removed redundatn images
aliaksandr960 023d057
Merge branch 'main' into cones-to-merge
aliaksandr960 23ccd1c
Removed redundant imports
aliaksandr960 bf9cde0
Removed redundant imports
aliaksandr960 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import numpy as np | ||
|
|
||
| import numpy as np | ||
| import math | ||
|
|
||
| import numpy as np | ||
| import trimesh | ||
|
|
||
| import math | ||
| from scipy.spatial.transform import Rotation as R | ||
|
|
||
| from shapely import MultiPoint, convex_hull | ||
| import shapely | ||
|
|
||
| import trimesh | ||
| import fiona | ||
| from fiona.crs import from_epsg | ||
|
|
||
| import numpy as np | ||
| import trimesh | ||
|
|
||
| import logging | ||
| logger: logging.Logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def extract_metadata(images, exifs, reference, camera_pairing_fov): | ||
|
|
||
| # Extract all the metadata for cone matching | ||
| metadata = [] | ||
| for i in images: | ||
| image_exif = exifs[i] | ||
| if not 'gps' in image_exif: | ||
| logger.warning(f'Image {i} no GPS -> skip') | ||
| continue | ||
| if not 'ypr' in image_exif: | ||
| logger.warning(f'Image {i} no YPR -> skip') | ||
| continue | ||
|
|
||
| annotation = { | ||
| 'fn': i, | ||
| 'lon': image_exif['gps']['longitude'], | ||
| 'lat': image_exif['gps']['latitude'], | ||
| 'alt': image_exif['gps']['altitude'], | ||
| 'yaw': image_exif['ypr']['yaw'], | ||
| 'pitch': image_exif['ypr']['pitch'], | ||
| 'roll': image_exif['ypr']['roll'], | ||
| 'fov': camera_pairing_fov, | ||
| } | ||
| metadata.append(annotation) | ||
|
|
||
| # Add relative positions | ||
| for i in metadata: | ||
| x, y, z = reference.to_topocentric(i['lat'], i['lon'], i['alt']) | ||
| i['x'] = x | ||
| i['y'] = y | ||
| i['z'] = z | ||
|
|
||
| return metadata | ||
|
|
||
|
|
||
| def save_cones_projection_as_gpkg(mesh_list, reference, output_file): | ||
| schema = { | ||
| 'geometry': 'Polygon', | ||
| 'properties': {'name': 'str'} | ||
| } | ||
|
|
||
| with fiona.open(output_file, 'w', driver='GPKG', crs=from_epsg(4326), schema=schema) as layer: | ||
| for cn, cc in mesh_list: | ||
| vertex_array = trimesh.convex.hull_points(cc) | ||
| ll_point_list = [] | ||
| for x, y, z in vertex_array: | ||
| lat, lon, _ = reference.to_lla(x, y, z) | ||
| ll_point_list.append((lon, lat)) | ||
| extent = convex_hull(MultiPoint(ll_point_list)) | ||
|
|
||
| layer.write({ | ||
| 'geometry': shapely.geometry.mapping(extent), | ||
| 'properties': {'name': cn} | ||
| }) | ||
|
|
||
|
|
||
| def create_fov_cone(point, yaw, pitch, roll, fov, cone_height, cone_sections=32): | ||
| """ | ||
| Point: x, y, z cone tip | ||
| Yaw 0 degrees -> top direction, increased clockwise | ||
| Pitch 0 degrees -> top to bottom direction | ||
| FOV - cone angle, degrees | ||
| """ | ||
|
|
||
| # Create a 3D cone | ||
| cone_radius = cone_height * np.tan(math.radians(fov / 2)) | ||
| cone = trimesh.creation.cone(radius=cone_radius, height=cone_height, sections=cone_sections) | ||
|
|
||
| # Moving cone tip to 0,0 | ||
| cone.apply_translation([0, 0, -cone_height]) | ||
|
|
||
| # Create transformation matrix | ||
| transform = np.eye(4) | ||
|
|
||
| # Rotate cone | ||
| euler_angles_xyz = (pitch, roll, -1 * yaw) | ||
| transform[:3, :3] = R.from_euler('xyz', euler_angles_xyz, degrees=True).as_matrix() # Rotation | ||
|
|
||
| # Shift cone | ||
| transform[:3, 3] = point # Translation | ||
| cone.apply_transform(transform) | ||
|
|
||
| return cone | ||
|
|
||
|
|
||
| def match_candidaes_by_fov_cones(metadata, volume_mesh, images_ref=None, images_cand=None): | ||
| all_images = [i['fn'] for i in metadata] | ||
| if images_ref is None: | ||
| images_ref = all_images | ||
| if images_cand is None: | ||
| images_cand = all_images | ||
|
|
||
|
|
||
| # Create cones | ||
| model_diagonal = math.dist(*volume_mesh.bounds) | ||
| cone_list = [] | ||
| for i in metadata: | ||
| fn = i['fn'] | ||
| x = i['x'] | ||
| y = i['y'] | ||
| z = i['z'] | ||
| yaw = i['yaw'] | ||
| pitch = i['pitch'] | ||
| roll = i['roll'] | ||
| fov = i['fov'] | ||
|
|
||
| cone = create_fov_cone(point=(x, y, z), | ||
| yaw=yaw, | ||
| pitch=pitch, | ||
| roll=roll, | ||
| fov=fov, | ||
| cone_height=model_diagonal) | ||
| cone_list.append((fn, cone)) | ||
|
|
||
| clipped_cone_list = [] | ||
|
|
||
| for fn, cone in cone_list: | ||
| clipped_cone_list.append((fn, trimesh.boolean.intersection([cone, volume_mesh]))) | ||
|
|
||
|
|
||
| # Find overlapping cones | ||
| pairing_map = dict() | ||
| unique_pairs = set() | ||
| for c_fn, c_cone in clipped_cone_list: | ||
| if c_fn not in images_ref: | ||
| continue | ||
| pairing_map[c_fn] = [] | ||
| for t_fn, t_cone in clipped_cone_list: | ||
| if t_fn not in images_cand: | ||
| continue | ||
| if c_fn == t_fn: | ||
| continue | ||
| intersection_volume = trimesh.boolean.intersection([c_cone, t_cone]).volume | ||
| if intersection_volume > 0: | ||
| pairing_map[c_fn].append(t_fn) | ||
| unique_pairs.add(tuple(sorted((t_fn, c_fn)))) | ||
|
|
||
| return pairing_map, unique_pairs, clipped_cone_list | ||
|
|
||
|
|
||
| def pairing_by_cones_from_dataset(ref_images, | ||
| cand_images, | ||
| exifs, | ||
| data, | ||
| config_override): | ||
| data.init_reference() | ||
| reference = data.load_reference() | ||
|
|
||
| overriden_config = data.config.copy() | ||
| overriden_config.update(config_override) | ||
|
|
||
| c = set() | ||
| dem_name = str(overriden_config["dem_path"]) | ||
| dem_hight = float(overriden_config["dem_hight"]) | ||
| pairing_fov = float(overriden_config["pairing_fov"]) | ||
|
|
||
| metadata = extract_metadata(set(ref_images + cand_images), | ||
| exifs, | ||
| reference, | ||
| pairing_fov) | ||
| volume_mesh, _ = data.load_demmesh(dem_name, dem_hight, reference=reference) | ||
|
|
||
| _, c, cone_list = match_candidaes_by_fov_cones( | ||
| metadata, | ||
| volume_mesh, | ||
| images_ref=ref_images, | ||
| images_cand=cand_images) | ||
|
|
||
| data.save_cones_projection_as_gpkg(cone_list, reference) | ||
|
|
||
| return c, {"num_pairs_cones": len(c),} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need such duplication of imports in lines 1-20 @aliaksandr960 ?