o
    i                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ ddlmZ 		dd	e	d
edee dededdfddZdd ZdS )    N)Image)
Prediction)logger   )#_depths_to_world_points_with_colors      D@upper_bound_resize
prediction
export_dirimage_pathsconf_thresh_percentileprocess_res_methodreturnc              	   C   s0  t | j|}t| j| j| j| j| j|\}}t|}t	
d| d t| j}	| jjdd \}
}t|	|
|}|| j|k }t }g }t|D ]}||| t || }|| qJt|	D ],}t|| j\}}| j| }|dr|d d  || 9  < |dd  ||
 9  < n|dkrtdtd	| t |d
 |d |d |d g}| j| }tt|d dd df |d ddf }t }|d |_tjj|_ ||_!||_"||_#|$| t% }|j|_&|'|j( |)| t }|d |_*|j|_t+ }|j*|_,|j|_&|-|j. ||_/|0| g }|d d df 1t j2|k}t 3|d D ];}|| d d }|d  || 9  < |d  ||
 9  < || }|t4|| |5|j67|j*t|d  q<|j*|_,t8j9:|| |_;t<||_=|>| qc|?| d S )NzExporting to COLMAP with z pointsr      resize   cropz0COLMAP export for crop method is not implementedzUnknown process_res_method: )r   r   )r   r   )r   r   )r   r   r   )@np
percentileconfr   depth
intrinsics
extrinsicsprocessed_imageslenr   infoshape_create_xyfpycolmapZReconstructionrangeZadd_point3DZTrackappendr   opensizeendswithNotImplementedError
ValueErrorarrayZRigid3dZ
Rotation3dZCameraZ	camera_idZCameraModelIdZPINHOLEmodelwidthheightparamsZ
add_cameraZRigZrig_idZadd_ref_sensorZ	sensor_idZadd_rigZimage_idFrameframe_idZadd_data_idZdata_idZrig_from_worldZ	add_frameastypeint32wherePoint2DZpoint3DtrackZadd_elementospathbasenamenameZPoint2DListZpoints2D	add_imagewrite)r	   r
   r   r   r   Zconf_threshpointscolorsZ
num_points
num_frameshw
points_xyfZreconstructionZpoint3d_idsZvidxZ
point3d_idZfidxZorig_wZorig_h	intrinsicZpycolmap_intri	extrinsicZcam_from_worldcameraZrigimageframeZpoint2d_listZpoints_in_frameZpoint2d rC   B/data/cameron/da3_repo/src/depth_anything_3/utils/export/colmap.pyexport_to_colmap   s   



.





rE   c           
      C   s   t j||ft jd\}}|t jddddf }|t jddddf }t || ||f}t || ||f}t j| t jdddt jt jf }t || ||f}t j|||fdd}	|	S )zV
    Creates a grid of pixel coordinates and frame indices (fidx) for all frames.
    )dtypeN)axis)r   indicesr.   newaxisbroadcast_toarangestack)
r:   r)   r(   y_gridx_gridx_coordsy_coordsZf_idxZf_coordsr=   rC   rC   rD   r      s   "r   )r   r   )r2   r   cv2cvnumpyr   PILr   depth_anything_3.specsr   depth_anything_3.utils.loggerr   glbr   strlistfloatrE   r   rC   rC   rC   rD   <module>   s0   
f