
    j                        d Z 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m	Z	m
Z
mZ ddlZddlZddlmZ 	 	 	 	 	 	 dd
e	e         de	e         de	e         de	e         de	e         de
e         dedededededeeeej        e
e         f         f         fdZej        dede	e         fd            Zd Zedk    r e             dS dS )a1  Custom `rd convert` variant that can skip depth and/or exclude cameras.

The slow part of `rd convert` is per-frame depth computation: either the ZED
SDK's NEURAL_LIGHT pass, or a learned stereo network (FFS / TRI Stereo). This
script monkey-patches `raiden.converter._extract_svo2_synchronized` with a
no-depth version that only writes RGB pngs + `timestamps.npy`. The rest of
raiden's convert pipeline (timestamp alignment, lowdim pkl construction,
metadata, split files) runs unchanged.

Flags:
  --skip-depth          Skip depth entirely for ALL cameras (no `depth/` dir).
  --exclude-cameras     Comma-separated camera names to skip whole-SVO2 (no
                        rgb, no depth). Useful for skipping wrist cameras
                        when you don't need them. Excluded SVO2 files are
                        temporarily renamed to `.svo2.skip` during the run
                        and restored afterwards.
  --reconvert           Re-convert episodes that are already marked converted.
  --stereo_method       If --skip-depth is NOT set, passes through to raiden.

Usage (run on the YAM or wherever your data lives):
    python ~/cameron/yam_control/convert_no_depth.py \
        --task ~/cameron/yam_control/data/raw/cube_in_carton \
        --skip-depth --exclude-cameras left_wrist_camera,right_wrist_camera
    N)Path)DictListOptionalTuple+P zed      ?   c64	svo_pathsnamesrgb_dirs
depth_dirsflips
max_framessync_threshold_nsstereo_method	ffs_scale	ffs_iterstri_stereo_variantreturnc                    !"# ddl m |D ]}|                    dd           fdt          ||           D             !d !                                D             #t          d#fd|D                         t          t          ||                    }t          t          ||                    }d	 |D             }d
 |D              d |D             "dt          ddf !"fd}|D ]} ||           d}t          j                    }	 ddl	m	}  |#r!t          #                                          ndd          }n# t          $ r d}Y nw xY w	 |||k    rnnt           fd|D                       rnQt           fd|D                       }d}|rld}|D ]I} |         \  }}||z
  |k    r3 ||            |          nd}t          | |         d                   }Jt           fd|D                       rn|lt           fd|D                       rn|D ]} |         \  }}||         rt          j        |t          j                  }t          j        t          ||         |ddz            |           ||                             |            ||           |dz  }|r|                    d           x|r|                                 t          j                    |z
  }t          d| d|dd|t          |d          z  dd            i }|D ]}t-          j        ||         t,          j        !          }t-          j        ||         d"z  |           	 !|                                         }n# t          $ r d}Y nw xY w||f||<   !|                                          |S )#zDrop-in replacement for raiden.converter._extract_svo2_synchronized
    that writes only RGB pngs + timestamps.npy. No depth at all.r   )	ZedCameraT)parentsexist_okc                 H    i | ]\  }}|                     ||d           S )F)compute_sdk_depth)from_svo).0namesvo_pathr   s      7/home/robot-lab/cameron/yam_control/convert_no_depth.py
<dictcomp>z%_no_depth_extract.<locals>.<dictcomp>:   sD     " " "D( 	i  x5 II" " "    c                 >    i | ]\  }}||                                 S  )get_total_frames)r    r!   cams      r#   r$   z%_no_depth_extract.<locals>.<dictcomp>>   s*    OOOYT3D#..00OOOr%   z   [no-depth] Frames per camera: c                 "    i | ]}||         S r'   r'   )r    ntotal_framess     r#   r$   z%_no_depth_extract.<locals>.<dictcomp>?   s    .Q.Q.Qaq,q/.Q.Q.Qr%   c                     i | ]}|g S r'   r'   r    r+   s     r#   r$   z%_no_depth_extract.<locals>.<dictcomp>E   s    #9#9#9aAr#9#9#9r%   c                     i | ]}|d S Nr'   r.   s     r#   r$   z%_no_depth_extract.<locals>.<dictcomp>F   s    ;S;S;SAt;S;S;Sr%   c                     i | ]}|d S )Fr'   r.   s     r#   r$   z%_no_depth_extract.<locals>.<dictcomp>G   s    555!Q555r%   r!   r   Nc                     |          rd | <   d S |          }|                                 sd| <   d | <   d S |                                }t          |j                  |j        f| <   d S )NT)grab	get_frameinttimestamp_nscolor)r!   r)   framecam_bufcamsdones      r#   _advancez#_no_depth_extract.<locals>._advanceI   sy    : 	 GDMF4jxxzz 	DJ GDMFU/00%+>r%   )tqdmz  extracting (rgb only))totaldescc              3   ,   K   | ]}|         d u V  d S r0   r'   r    r+   r9   s     r#   	<genexpr>z$_no_depth_extract.<locals>.<genexpr>d   ,      11awqzT!111111r%   c              3   4   K   | ]}|         d          V  dS )r   Nr'   rA   s     r#   rB   z$_no_depth_extract.<locals>.<genexpr>h   s+      55!
1555555r%   Fc              3   ,   K   | ]}|         d u V  d S r0   r'   rA   s     r#   rB   z$_no_depth_extract.<locals>.<genexpr>t   s,      55!71:%555555r%   c              3   ,   K   | ]}|         d u V  d S r0   r'   rA   s     r#   rB   z$_no_depth_extract.<locals>.<genexpr>v   rC   r%   010dz.png   z  [no-depth] z synchronized frames in z.1fzs (gư>z fps))dtypeztimestamps.npy)raiden.cameras.zedr   mkdirzipitemsprintdictstrtimer=   minvalues	Exceptionanymaxcv2rotate
ROTATE_180imwriteappendupdateclosenpasarrayint64saveget_camera_info)$r   r   r   r   r   r   r   r   r   r   r   drgb_dir_mapflip_mapcam_tsr<   r!   	frame_idxt_startr=   pbar	latest_tsadvancedr+   ts_rgbdtoutts_arrinfor   r9   r:   r;   r,   s$                                  @@@@@r#   _no_depth_extractrs   &   s!    -,,,,, - -	t,,,," " " "!%33" " "D PO$**,,OOOL	
T.Q.Q.Q.Q5.Q.Q.Q
T
TUUUs5(++,,KCu%%&&H $:#95#9#9#9F;S;SU;S;S;SG55u555D
?s 
?t 
? 
? 
? 
? 
? 
? 
? 
?   IikkGtK#l1133444!24 4 4   #!i:&=&=1111511111 	 5555u55555	 	H > >
Ar>$555HQKKKqz)#H #Iwqz!} = =I5555u55555   	 1111511111 	  	 	AajGB{ 6jcn55KKN	-D-D-D-DDEEsKKK1IR   HQKKKKQ	 	KKNNNG#J  

	w	B	 3) 3 3R
G 3 3#b$--'
23 3 3 4 4 4 9;C  F1IRX666
A!116:::	7**,,DD 	 	 	DDD	$AQJs$   6E EE:NN$#N$task_direxclude_namesc              #     K   |sdV  dS g }	 |                                  D ]}|                                s|dz  }|                                s1|D ]R}|| dz  }|                                r4|| dz  }|                    |           |                    ||f           S|r t          dt          |           d           dV  |D ].\  }}|                                r|                    |           /|r"t          dt          |           d           dS dS # |D ].\  }}|                                r|                    |           /|r!t          dt          |           d           w w xY w)u   Rename `<name>.svo2` → `<name>.svo2.skip` for each excluded camera so
    raiden's globbing doesn't see them. Restored on exit.Ncamerasz.svo2z
.svo2.skipz)  --exclude-cameras: temporarily renamed z svo2 file(s)z  --exclude-cameras: restored )iterdiris_direxistsrenamer[   rN   len)rt   ru   renamedrec_dircams_dirr!   srcdsts           r#   _temporarily_exclude_svosr      s       ')GP'')) 	/ 	/G>>## *H??$$ % / /D/::<< /"%8%8%88CJJsOOONNC:.../  	[Yc'llYYYZZZ 	  	 HCzz||  

3 	PN3w<<NNNOOOOO	P 	P   	  	 HCzz||  

3 	PN3w<<NNNOOOO	Ps   CD& &AE<c                  8   t          j                    } |                     ddt          d           |                     dd t          d           |                     dd	d
           |                     ddt          d           |                     dd	           |                     ddg dd           |                                 }t          |j                                                  }d |j        	                    d          D             }|j
        r t          d           t          t          _        t          ||          5  t          j        t          |          |j        |j        |j                   d d d            d S # 1 swxY w Y   d S )Nz--taskTzCPath to data/raw/<task_name>/ (the dir containing episode subdirs).)requiredtypehelpz--output_dirzOWhere to write processed episodes. Default: <task_parent>/processed/<task_name>)defaultr   r   z--skip-depth
store_truez6Skip depth processing for all cameras (RGB pngs only).)actionr   z--exclude-cameras zhComma-separated camera NAMES (not roles) to skip entirely (e.g. 'left_wrist_camera,right_wrist_camera').z--reconvert)r   z--stereo_methodr	   )r	   ffs
tri_stereoz+Only relevant when --skip-depth is NOT set.)r   choicesr   c                 ^    g | ]*}|                                 |                                 +S r'   )strip)r    ss     r#   
<listcomp>zmain.<locals>.<listcomp>   s-    UUU117799UQWWYYUUUr%   ,zD  --skip-depth: patching raiden.converter._extract_svo2_synchronized)rt   
output_dirr   	reconvert)argparseArgumentParseradd_argumentrP   
parse_argsr   taskresolveexclude_camerassplit
skip_depthrN   rs   rcv_extract_svo2_synchronizedr   convert_taskr   r   r   )pargsrt   ru   s       r#   mainr      s   !!ANN8d]  _ _ _NN>4ci  k k kNN>,P  R R RNN&I  J J J NN=N666NN$e777E  G G G <<>>DDI&&((HUU(<(B(B3(G(GUUUM  ;TUUU):&	"8]	;	; 
 
]],n		
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   5FFF__main__)Nr   r	   r
   r   r   )__doc__r   
contextlibosrQ   pathlibr   typingr   r   r   r   rW   numpyr^   raiden.converter	converterr   rP   boolr5   floatndarrayrO   rs   contextmanagerr   r   __name__r'   r%   r#   <module>r      s   0      				        . . . . . . . . . . . . 



           !%'#q qDzq9q 4jq T
	q
 :q q q q q q q 
#uRZ$/0
01q q q qh P PT#Y P P P P@
 
 
D zDFFFFF r%   