a `e,@sddlZddlZddlZddlZddlmZddlmZddl m Z dddZ dd d Z dd d Z dddZedddddZddZddZdS)N) lru_cache)log) get_originFcCs*|r|St|}|dur"t||S|SdS)z :param reconstruction_json path to OpenSfM's reconstruction.json :return GSD value rounded. If GSD cannot be computed, or ignore_gsd is set, it returns a default value. N)"opensfm_reconstruction_average_gsdround)reconstruction_json default_valuendigits ignore_gsdgsdr /code/opendm/gsd.py rounded_gsd s  r?c Cs\d}d}|rd}nt||||d}|D]} t| j|}t| j|}q&ttt|||S)a :param photos images database :param target_resolution resolution the user wants have in cm / pixel :param reconstruction_json path to OpenSfM's reconstruction.json :param gsd_error_estimate percentage of estimated error in the GSD calculation to set an upper bound on resolution. :param ignore_gsd if set to True, simply return the largest side of the largest image in the images database. :return A dimension in pixels calculated by taking the image_scale_factor and applying it to the size of the largest image. Returned value is never higher than the size of the largest side of the largest image. r?)has_gcp)image_scale_factormaxwidthheightintmathceil) photostarget_resolutionrgsd_error_estimater r max_widthZ max_heightisfpr r r image_max_sizes  rcCs`t||d}|durX|dkrX|d|}tdt||}d}||krP|d}q>|dSdSdS)a@ :param target_resolution resolution the user wants have in cm / pixel :param reconstruction_json path to OpenSfM's reconstruction.json :param gsd_error_estimate percentage of estimated error in the GSD calculation to set an upper bound on resolution. :return A down-scale (<= 1) value to apply to images to achieve the target resolution by comparing the current GSD of the reconstruction. If a GSD cannot be computed, it just returns 1. Returned scale values are never higher than 1 and are always obtained by dividing by 2 (e.g. 0.5, 0.25, etc.)  use_all_shotsNrrrg@)rminabs)rrrrr rretr r r r0s   r皙?rcCsx|r|St||p|d}|dur`|d||}||ks<|rZtd||d|||S|Sntd||SdS)a :param resolution resolution in cm / pixel :param reconstruction_json path to OpenSfM's reconstruction.json :param gsd_error_estimate percentage of estimated error in the GSD calculation to set an upper bound on resolution. :param gsd_scaling scaling of estimated GSD. :param ignore_gsd when set to True, forces the function to just return resolution. :param ignore_resolution when set to True, forces the function to return a value based on GSD. :return The max value between resolution and the GSD computed from the reconstruction. If a GSD cannot be computed, or ignore_gsd is set to True, it just returns resolution. Units are in cm / pixel. r Nr"zjMaximum resolution set to {} * (GSD - {}%) ({:.2f} cm / pixel, requested resolution was {:.2f} cm / pixel)dz:Cannot calculate GSD, using requested resolution of {:.2f})rr ODM_WARNINGformat) resolutionrrZ gsd_scalingr Zignore_resolutionrr r r r cap_resolutionEs   r+)maxsizecCs\tj|st|dt|}t|}Wdn1s@0Y|d}g}|dD]"}|d|}||ddq^t |}g} |dD]} |d| } |s| dd d kr|d | d } t | } | d}| d | d }|st d|dS| t|||| dqt| dkrXt| }|dkrPt dt|SdS)a Computes the average Ground Sampling Distance of an OpenSfM reconstruction. :param reconstruction_json path to OpenSfM's reconstruction.json :return Ground Sampling Distance value (cm / pixel) or None if a GSD estimate cannot be compute z does not exist.Nrpoints coordinatesshotsgps_dopi?Bcamerascamerafocalfocal_xzNCannot parse focal values from %s. This is likely an unsupported camera model.rz=Negative GSD estimated, this might indicate a flipped Z-axis.)ospathisfileIOErroropenjsonloadappendnpmediangetrrr(calculate_gsd_from_focal_ratiolenmeanr$)rr!fdatareconstructionZ point_heightsZpointIdpointZ ground_heightZgsdsZ shotImageshotr3Z shot_originZ shot_height focal_ratiorCr r r rds>   (        rcCs |dkrt||||SdSdS)aO :param sensor_width in millimeters :param flight_height in meters :param focal_length in millimeters :param image_width in pixels :return Ground Sampling Distance >>> round(calculate_gsd(13.2, 100, 8.8, 5472), 2) 2.74 >>> calculate_gsd(13.2, 100, 0, 2000) >>> calculate_gsd(13.2, 100, 8.8, 0) rN)rA) sensor_width flight_heightZ focal_length image_widthr r r calculate_gsds rMcCs$|dks|dkrdS|d||S)z :param focal_ratio focal length (mm) / sensor_width (mm) :param flight_height in meters :param image_width in pixels :return Ground Sampling Distance rNr'r )rIrKrLr r r rAsrA)NrF)rFF)rF)r&rFFF)F)r6r;numpyr>rZ repoze.lruropendmr opendm.shotsrrrrr+rrMrAr r r r s         0