https://github.com/hu-zhiyu/ARCM
Raw File
Tip revision: ccd2205446fc22786852cc1a1c12bbc5448e8c0a authored by 胡智宇 on 22 May 2020, 13:27:05 UTC
update_2020_05_22
Tip revision: ccd2205
indoor3d_util.pyc
�
3|�]c@s�dZddlZddlZddlZddlZejjejje	��Z
ejje
dd�Zge
ejje
d��D]Zej�^q�Zd�ee�D�Zi
dddgd	6dddgd
6dddgd6dddgd6dddgd
6dddgd6dddgd6dddgd6dddgd6dddgd6dddgd6dddgd6dddgd6Zdddddd gZd!�eD�Zd"d#�Zeeed$�Zd%�Zd&�Zd'd'edd d(�Zd)�Zd'd'edd d*�Zd+�Z d'd'edd d,�Z!d-�Z"d.�Z#d/�Z$d0�Z%ed1�Z&edeed2�Z'd3�Z(dS(4s`
    Modified from: https://github.com/charlesq34/pointnet/blob/master/sem_seg/indoor3d_util.py
i����Ntdatas&Stanford3dDataset_v1.2_Aligned_Versionsmeta/class_names.txtcCsi|]\}}||�qS(((t.0titcls((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pys
<dictcomp>s	ii�tceilingtfloortwalltbeamtcolumnidtwindowi�tdoori�ixttabletchairtsofai
tbookcasetboardi2tclutteriii	iicCs&i|]}t|tj|��qS((t
g_class2colort	g_classestindex(RR((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pys
<dictcomp> s	ttxtcCsg}d}x�tjtjj|d��D]�}tjj|�jd�d}|tkred}ntj|�}tj	|j
ddf�t|}tj	|j
ddf�|}	|d7}|jtj
|||	gd��q+Wtj
|d�}
tj|
dd�dd!}|
dd�dd�fc|8<|d	kr�t|d
�}x�t|
j
d�D]r}
|jd|
|
df|
|
df|
|
df|
|
df|
|
d
f|
|
df|
|
dff�qgW|j�n/|dkr	tj||
�nd|GHt�dS(s Convert original dataset files to data_label file (each line is XYZRGBLG).
        We aggregated all the points from each instance in the room.
    Args:
        anno_path: path to annotations. e.g. Area_1/office_2/Annotations/
        out_filename: path to save collected points and labels (each line is XYZRGBLG)
        file_format: txt or numpy, determines what file format to save.
    Returns:
        None
    Note:
        the points are shifted before save, the most negative point is now at origin.
    is*.txtt_RitaxisiNRtws%f %f %f %d %d %d %d
iiiitnumpys9ERROR!! Unknown file format: %s, please use txt or numpy.(tglobtostpathtjointbasenametsplitRtnptloadtxttonestshapet
g_class2labeltappendtconcatenatetamintopentrangetwritetclosetsavetexit(t	anno_pathtout_filenametfile_formattpoints_listt
instanceidtfRtpointstlabelstinstancelabelst
data_labeltxyz_mintfoutR((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytcollect_point_label's6%	$ 
&"	''
cCs�tj|�}|dd�dd�f}|dd�dfjt�}t|d�}x&t|jd�D]}	t||	}
|r�||	tkr�qmn|r�||	dksm||	dkr�qmn|r|j	d||	df||	df||	df|
d|
d|
df�qm|j	d||	df||	df||	df||	d	f||	d
f||	dff�qmW|j
�dS(s For visualization of a room from data_label file,
    input_filename: each line is X Y Z R G B L
    out_filename: OBJ filename,
            visualize input file by coloring point with label color
        easy_view: only visualize furnitures and floor
    Niii����Risv %f %f %f %d %d %d
iiii(RR tastypetintR'R(R"t
g_label2colortg_easy_view_labelsR)R*(tinput_filenameR.tlabel_colort	easy_viewtno_wallR6RtlabelR8Rtcolor((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytpoint_label_to_objTs &	G	ZcCs�|jd}||kr)|t|�fS||kr^tjj||�}||df|fStjj|||�}||df}tj||gd�tt|��t|�fSdS(s� data is in N x ...
        we want to keep num_samplexC of them.
        if N > num_sample, we will randomly keep num_sample of them.
        if N < num_sample, we will randomly duplicate samples.
    i.N(R"R(RtrandomtchoiceR%tlist(Rt
num_sampletNtsampletdup_data((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytsample_datars
cCs6t||�\}}||}||}|||fS(N(RL(RRBtinslabelRHtnew_datatsample_indicest	new_labeltnew_inslabel((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytsample_data_label�s

g�?c	 Cs�||kst�tj|d�dd!}	g}
g}|s3ttj|	d||��d}ttj|	d||��d}
xet|�D]�}|ddkr�xt|
�D](}|
j||�|j||�q�Wq�xCt|
�ddd�D](}|
j||�|j||�qWq�Wn�ttj|	d|��}ttj|	d|��}
|dkr�||
|}nxbt|�D]T}tjj	||	d�}tjj	||	d�}|
j|�|j|�q�Wg}g}g}d}xZtt
|
��D]F}|
|}||}|dd�df||k|dd�df|k@}|dd�df||k|dd�df|k@}||@}tj|�dkr�qn||dd�f}||}||}t||||�\}}}|jtj
|d��|jtj
|d��|jtj
|d��qWtj|d�tj|d�tj|d�fS(s� Prepare block training data.
    Args:
        data: N x 6 numpy array, 012 are XYZ in meters, 345 are RGB in [0,1]
            assumes the data is shifted (min point is origin) and aligned
            (aligned with XYZ axis)
        label: N size uint8 numpy array from 0-12
        num_point: int, how many points to sample in each block
        block_size: float, physical size of the block in meters
        stride: float, stride for block sweeping
        random_sample: bool, if True, we will randomly sample blocks in the room
        sample_num: int, if random sample, how many blocks to sample
            [default: room area]
        sample_aug: if random sample, how much aug
    Returns:
        block_datas: K x num_point x 6 np array of XYZRGB, RGB is in [0,1]
        block_labels: K x num_point x 1 np array of uint8 labels
        
    TODO: for this version, blocking is in fixed, non-overlapping pattern.
    iiiiNi����id(tAssertionErrorRtamaxR;tceilR(R$tNoneREtuniformtlentsumRRtexpand_dimsR%( RRBRMt	num_pointt
block_sizetstridet
random_samplet
sample_numt
sample_augtlimitt	xbeg_listt	ybeg_listtnum_block_xtnum_block_yRtjRtxbegtybegtblock_data_listtblock_label_listtblock_inslabel_listtidxtxcondtycondtcondt
block_datatblock_labeltblock_inslabeltblock_data_sampledtblock_label_sampledtblock_inslabel_sampled((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2blocks�s\%% 


::


c		Cs|dd�dd�f}|dd�dd�fcd:<|dd�dfjtj�}t||||||||�S(s; room2block with input filename and RGB preprocessing.
    Niiig�o@i����(R:Rtuint8Rv(	R6R[R\R]R^R_R`RRB((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2blocks_plus�s
""cCsl|ddkr"tj|�}n.|ddkrDtj|�}ndGHt�t|||||||�S(Ni����RtnpysUnknown file type! exiting.(RR tloadR,Rx(tdata_label_filenameR[R\R]R^R_R`R6((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2blocks_wrapper�sc
Cs�|dd�dd�f}|dd�dd�fcd:<|dd�dfjtj�}|dd�dfjtj�}	t|dd�df�}
t|dd�df�}t|dd�d	f�}t|||	||||||�	\}
}}tj|
jd|d
f�}x7t|
jd�D]"}|
|dd�df|
||dd�df<|
|dd�df|||dd�df<|
|dd�d	f|||dd�df<t|
|dd�df�}t|
|dd�df�}|
|dd�dfc||d	8<|
|dd�dfc||d	8<q6W|
|dd�dd�dd�f<|||fS(
s� room2block, with input filename and RGB preprocessing.
        for each block centralize XYZ, add normalized XYZ as 678 channels
    Niiig�o@i����i����iii	ii(	R:RRwtmaxRvtzerosR"R(tmin(R6R[R\R]R^R_R`RRBRMt
max_room_xt
max_room_yt
max_room_zt
data_batchtlabel_batchtinslabel_batchtnew_data_batchtbtminxtminy((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2blocks_plus_normalized�s("""000'+%cCsl|ddkr"tj|�}n.|ddkrDtj|�}ndGHt�t|||||||�S(Ni����RRysUnknown file type! exiting.(RR RzR,R�(R{R[R\R]R^R_R`R6((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2blocks_wrapper_normalizedscCs�|jd}tj|�}tjj|�||dd�f}||}ttj|t|���}tj||df�}tj||f�}tj||f�}	x9t	|�D]+}
|
|}t
|
d||�}||}
|||�dd�f||
d|
�dd�f<|||!||
d|
�f<|||!|	|
d|
�f<|
|kr�tjj|||
�}||dd�f||
|
d�dd�f<||||
|
d�f<|||	|
|
d�f<q�q�W|||	fS(s' Prepare whole room samples.
    Args:
        data: N x 6 numpy array, 012 are XYZ in meters, 345 are RGB in [0,1]
            assumes the data is shifted (min point is origin) and
            aligned (aligned with XYZ axis)
        label: N size uint8 numpy array from 0-12
        sample_num_point: int, how many points to sample in each sample
    Returns:
        sample_datas: K x sample_num_point x 9
                     numpy array of XYZRGBX'Y'Z', RGB is in [0,1]
        sample_labels: K x sample_num_point x 1 np array of uint8 labels
    iNii(R"RtarangeREtshuffleR;RUtfloatR~R(RRF(RRBRMtsample_num_pointRItordert	batch_numtsample_datast
sample_labelstsample_inslabelsRtbeg_idxtend_idxtnumtmakeup_indices((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2sampless,




5/!c
Cs�|dd�dd�f}|dd�dd�fcd:<|dd�dfjtj�}|dd�dfjtj�}t|dd�df�}t|dd�df�}t|dd�d	f�}t||||�\}}	}
tj|jd|d
f�}x�t|jd�D]�}||dd�df|||dd�df<||dd�df|||dd�df<||dd�d	f|||dd�df<q'W||dd�dd�dd�f<||	|
fS(
s� room2sample, with input filename and RGB preprocessing.
        for each block centralize XYZ, add normalized XYZ as 678 channels
    Niiig�o@i����i����iii	ii(R:RRwR}R�R~R"R((
R6R[RRBRMR�R�R�R�R�R�R�R�((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2samples_plus_normalizedBs"""004%cCs]|ddkr"tj|�}n.|ddkrDtj|�}ndGHt�t||�S(Ni����RRysUnknown file type! exiting.(RR RzR,R�(R{R[R6((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytroom2samples_wrapper_normalized]scCs:g}x	tjtjj|d��D]�}tjj|�jd�d}|tkr_d}ntj|�}t	|}tj
|dd�dd�fdd�}tj|dd�dd�fdd�}tjtj
||tj|g�gd�d�}	|j|	�q%Wtj
|d�}
tj
|
dd�dd�fdd�}|
dd�dd�fc|8<|
dd�dd�fc|8<t|d	�}x�t|
jd�D]r}
|jd
|
|
df|
|
df|
|
df|
|
df|
|
d
f|
|
df|
|
dff�q�W|j�dS(s$ Compute bounding boxes from each instance in original dataset files on
        one room. **We assume the bbox is aligned with XYZ coordinate.**
    
    Args:
        anno_path: path to annotations. e.g. Area_1/office_2/Annotations/
        out_filename: path to save instance bounding boxes for that room.
            each line is x1 y1 z1 x2 y2 z2 label,
            where (x1,y1,z1) is the point on the diagonal closer to origin
    Returns:
        None
    Note:
        room points are shifted, the most negative point is now at origin.
    s*.txtRiRNiRiRs%f %f %f %f %f %f %d
iiii(RRRRRRRRR R#R&RTRZR%tarrayR$R'R(R"R)R*(R-R.tbbox_label_listR2RR3RBR7txyz_maxtins_bbox_labelt
bbox_labeltroom_xyz_minR8R((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytcollect_bounding_boxls.%	
++-+""	''cCsmtj|�}|dd�dd�f}|dd�dfjt�}d}d}xt|jd�D]�}|r�||tkr�qjn|dt||dt|�d}	|dt||dt|�d}
t	|	d�}t	|
d�}|j
d	tjj
|
��||d
d�f||dd
�f}
|
d}|
d}|
d}||df}||df}||df}tjt||d
t�d}d|}|j
d|�|j
d||||f�|j
d|||||f�|j
d||||||f�|j
d|||||f�|j
d|||f�|j
d||||f�|j
d|||||f�|j
d||||f�|j
d�d}|j
dd|d
|d|d|f�|j
dd|d|d|d|f�|j
dd|d|d|d
|f�|j
dd|d|d|d
|f�|j
dd|d|d|d|f�|j
dd|d|d|d|f�|j
d�|j
d|�|j
d|d|d|df�|j
d�|j�|j�|d7}|d7}qjWdS(sc Visualization of bounding boxes.
    
    Args:
        input_filename: each line is x1 y1 z1 x2 y2 z2 label
        out_filename_prefix: OBJ filename prefix,
            visualize object by g_label2color
        easy_view: if True, only visualize furniture and floor
    Returns:
        output a list of OBJ file and MTL files with the same prefix
    Niii����Rs.objs.mtlRs
mtllib %s
iiitdtypeg�o@s
material%ds
usemtl %s
sv %f %f %f
s
g default
sf %d %d %d %d
iiiis
s
newmtl %s
sKd %f %f %f
(RR R:R;R(R"R=RtstrR'R)RRRR�R<R�R*(R>tout_filename_prefixR@R�tbboxRBtv_cnttins_cntRtobj_filenametmtl_filenametfout_objtfout_mtltlengthtaR�tctxtytzRCtmaterial((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytbbox_label_to_obj�s^((*


!
"&""
------
&



cCsvtj|�}|dd�dd�f}|dk	r�t|�dksOt�tj|�}|dd�|f|dd�dd�f<|dd�|df|dd�dd�f<n|rQtj|dd�dd�fd�}|dd�dd�fc|d8<|dd�dd�fc|d8<|tj|d�:}n|dd�dfjt	�}	|d}
|d}t
|
d	�}t
|d	�}
|jd
tj
j|��d}d}x�t|jd�D]{}|r|	|tkrq�n|r&|	|tjd�kr&q�n||dd�f||dd�f}|d}|d}|d
}||df}||df}||d
f}tjt|	|dt�d}d|}|jd|�|jd||||f�|jd|||||f�|jd||||||f�|jd|||||f�|jd|||f�|jd||||f�|jd|||||f�|jd||||f�|jd�|jdd|d|d
|d|f�|jdd|d
|d|d|f�|jdd|d|d
|d|f�|jdd|d|d|d|f�|jdd|d|d|d|f�|jdd|d|d|d|f�|jd�|
jd|�|
jd|d|d|d
f�|
jd�|d7}|d7}q�W|j�|
j�dS(s� Visualization of bounding boxes.
    
    Args:
        input_filename: each line is x1 y1 z1 x2 y2 z2 label
        out_filename_prefix: OBJ filename prefix,
            visualize object by g_label2color
        easy_view: if True, only visualize furniture and floor
        permute: if not None, permute XYZ for rendering, e.g. [0 2 1]
        center: if True, move obj to have zero origin
    Returns:
        output a list of OBJ file and MTL files with the same prefix
    Niiig@i����s.objs.mtlRs
mtllib %s
RiiR�g�o@s
material%ds
usemtl %s
sv %f %f %f
s
g default
sf %d %d %d %d
iiiis
s
newmtl %s
sKd %f %f %f
(RR RVRXRSR�RTR}R:R;R'R)RRRR(R"R=RRR<R�R*(R>R�R@tpermutetcentert
exclude_tableR�R�R�RBR�R�R�R�R�R�RR�R�R�R�R�R�R�RCR�((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytbbox_label_to_obj_room�st
,3(&&

*


!
"&""
------
&


cCsg}xjtjtjj|d��D]J}tjj|�jd�d}|tkr_d}ntj|�}t	|}tj
|dd�dd�fdd�}tj|dd�dd�fdd�}	||	d}
|	|d}|
|dd�dd�f}tj|j
ddf�|}
tj|j
dd	f�|}|jtj||||
gd	��q%Wtj|d�}tj
|dd�dd�fdd�}|dd�dd�fc|8<|d
kr�t|d�}x�t|j
d�D]�}|jd||df||d	f||df||df||d
f||df||df||df||df||df||df||df||dff
�qW|j�n/|dkr�tj||�nd|GHt�dS(s� Compute bounding boxes from each instance in original dataset files on
        one room. **We assume the bbox is aligned with XYZ coordinate.**
        Save both the point XYZRGB and the bounding box for the point's
        parent element.
 
    Args:
        anno_path: path to annotations. e.g. Area_1/office_2/Annotations/
        out_filename: path to save instance bounding boxes for each point,
            plus the point's XYZRGBL
            each line is XYZRGBL offsetX offsetY offsetZ a b c,
            where cx = X+offsetX, cy=X+offsetY, cz=Z+offsetZ
            where (cx,cy,cz) is center of the box, a,b,c are distances from center
            to the surfaces of the box, i.e. x1 = cx-a, x2 = cx+a, y1=cy-b etc.
        file_format: output file format, txt or numpy
    Returns:
        None
    Note:
        room points are shifted, the most negative point is now at origin.
    s*.txtRiRNiRiiRRs'%f %f %f %d %d %d %d %f %f %f %f %f %f
iiiiii	i
iiRs9ERROR!! Unknown file format: %s, please use txt or numpy.(RRRRRRRRR R#R&RTR!R"R$R%R'R(R)R*R+R,(R-R.R/tpoint_bbox_listR2RR3RBR7R�t
xyz_centert	dimensiontxyz_offsetst
dimensionsR4t
point_bboxR�R8R((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pytcollect_point_bounding_box$sD%	
++   +"	''
'3
()t__doc__RRRRtsysRtdirnametabspatht__file__tBASE_DIRRt	DATA_PATHR'R�trstripRt	enumerateR#RR=R<R9tTruetFalseRDRLRRRVRvRxR|R�R�R�R�R�R�R�R�R�(((sC/home/huzhiyu/Documents/CodeCollection/ASIS-master/indoor3d_util.pyt<module>sT4-		Q			'			*?O
back to top