o
    2PFj                     @  s  d dl m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mZ dZdZeZd	Zd
ZeG dd dZdddZdddddZddddd Zdd#d$Zd%dd&dd)d*Zdd/d0Zdd1d2Zdd3d4Zdd5d6Zdd8d9Zdd:d;Zdd<d=ZddDdEZ ddFdGZ!ddKdLZ"ddMddQdRZ#ddSdTZ$ddUdVdd[d\Z%dd^d_Z&dd`daZ'ddcddZ(ddgdhZ)ddodpZ*ddrdsZ+ddydzZ,dd~dZ-dddZ.dddZ/dS )    )annotationsN)deque)	dataclass)Any)ALLOWED_ACTIONSDEFAULT_FRAMES_PER_ACTION      s   PNG

g?c                   @  s   e Zd ZU ded< ded< dS )ActionAtlasResultbytesimage_bytesdict[str, Any]qaN)__name__
__module____qualname____annotations__ r   r   A/opt/sixxie/releases/current/services/pet_builder/action_atlas.pyr
      s   
 r
   action_imagesdict[str, bytes]returnc              	   C  s<  t t }ttt }dg||  }g }d}d}d }ttD ]i\}}	| |	}
|
r,t|
ng }t|tk r:d}|p9d}|d t }|t|7 }g }t|D ]+\}}t|d |d |d }t	||||| t
|}|||d	 |d
 d| qL||	t|t||d q||t tttt||d}tt||||dS )Nr   r   r   r   r   TFnot_enough_framespixelswidthheightbboxforeground_pixels)framesource_bboxr   )actioncomponent_countselected_framesframesokfailure_reason
cell_widthcell_heightframes_per_actionaction_countselected_frame_countrowsr   r   )
CELL_WIDTHFRAMES_PER_ACTIONCELL_HEIGHTlenr   	enumerateget_extract_components_fit_component_to_cell_paste_cell_lower_foreground_metricsappendr
   _write_rgba_png)r   atlas_widthatlas_heightatlasr-   r,   r&   r'   	row_indexr!   r   
componentsselectedframe_reportsframe_index	componentfittedlower_metricsr   r   r   r   compose_action_atlas   sZ   


rF   F)allow_stable_pet_partsaction_framesdict[str, list[bytes]]rG   boolc                C  sx  t t }ttt }dg||  }g }d}d}d }ttD ]\}	}
t| |
g }t|tk r6d}|p5d}|d t }g }g }t|D ]a\}}t|}|s`d}|pSd}|	|dd ddd qDt
||
|d\}}|d	 rsd}|prd
}td|d |d  }t|}|	||t||d |d	 |dd|d |d |d | d	| qDdd |D }t|}d}|D ]`}d|vr|	|d |d |d |d d q|d}|t|k r|| nd }|d7 }t|d |d |d |t|dkrdndd\}}t|||	t|d | |d7 }|	i || q|	|
t|t||d q||t tttt||d}tt||||d S )!Nr   r   TFr   missing_frame_foreground)r   r"   r    r   missingr!   rG   fragmented_foregroundfragmented_frame_foreground   r   r   accepted_component_countstable_pet_parts_mergedr   r   )	r   rC   r"   rQ   rN   rR   r    r   bbox_fill_ratioc                 S  s   g | ]
}d |v r|d  qS )rC   r   .0itemr   r   r   
<listcomp>   s    z.compose_action_frame_atlas.<locals>.<listcomp>rC   r   r"   r    )r   r"   r    r   r   row_normalized_areafit_to_cellscalefit_mode)r!   input_framesr#   r$   r%   r.   )r/   r0   r1   r2   r   r3   listr4   r5   r9   _merge_frame_componentsmaxr8   _row_normalized_fit_scalespop"_fit_component_to_cell_with_reportr7   intr
   r:   )rH   rG   r;   r<   r=   r-   r,   r&   r'   r>   r!   frame_imagesselected_imagesrA   prepared_framesrB   r   r?   rC   component_report	bbox_arearE   valid_components
row_scalesvalid_indexrV   	fit_scalerD   
fit_reportr   r   r   r   compose_action_frame_atlasN   s   	







ro   r!   strr   r   r   c                C  sb  t |\}}}td|t }tt|||}|tk}g }	g }
ttD ]}|| }|td kr0|n|d | }t|||d|| |}t||| |}|sU|
|ddd q"t|| |d\}}t	|}t
|d d | t
|d d t
|d d | t
|d d g|d< |
|t||d |d	 |d
d|d |d dt| |d	 st||| ||dr|	| q"g }g }t|	tk}t|	D ]N\}}t|d |d |d }|ttt| ||t
|
| ddt
|
| ddt|
| d	dt|
| d
d|d |d dt| q|| tdd |
D t|	|||
|r-d dS ddS )NrP   r   )r   r"   r   rM   r         rQ   rN   rR   Fr   )r   r"   rQ   rN   rR   r   r    allow_near_full_widthr   r   r   r"   )r   r"   rQ   rN   rR   r    r   c                 s  s     | ]}t |d dV  qdS )r"   r   Nrd   r4   )rU   reportr   r   r   	<genexpr>      z.extract_action_strip_frames.<locals>.<genexpr>slot_foreground_invalid)r&   r!   r"   r#   r$   reportsslot_reportsr'   )_read_rgba_pngr`   r0   r2   _extract_components_from_pixelsrange_crop_pixelsr9   r_   dictrd   r4   r8   _slot_component_is_usabler3   r6   r:   r/   r1   rJ   sum)r!   r   rG   image_widthimage_heightr   
slot_widthglobal_component_countallow_near_full_slot_widthr@   r{   rB   leftrightslot_pixelsr?   rC   rh   r$   rz   r&   rD   r   r   r   extract_action_strip_frames   s   



r   bytes | Nonelist[dict[str, Any]]c                 C  s"   | sg S t | \}}}t|||S N)r|   r}   )r   r   r   r   r   r   r   r5     s   r5    rM   r?   %tuple[dict[str, Any], dict[str, Any]]c          	        s  t dd | D | stdtdtd fdd| D }|s,t| dd	 d
g}t|dd	 d
 t fdd|D }|oMt fdd|D }|sR|r\ t|dddfS t fdd|D }|r|rt|| rt|t|dddfS  t|dddfS t dpdtdt dd |D  }t|dko|dk }|r|rt|| rt|t|dddfS  t|dddfS t|t|dddfS )Nc                 s  s(    | ]}t d t|dpd V  qdS )r   r   Nr`   rd   r4   rU   rC   r   r   r   rw     s   & z*_merge_frame_components.<locals>.<genexpr>zcomponents are required   {Gz?c                   s&   g | ]}t |d pd kr|qS )r   r   ru   r   )significant_thresholdr   r   rW     s
    z+_merge_frame_components.<locals>.<listcomp>c                 S     t | dpdS Nr   r   ru   rV   r   r   r   <lambda>#      z)_merge_frame_components.<locals>.<lambda>keyc                 S  r   r   ru   r   r   r   r   r   $  r   c                 3  $    | ]}| urt  |V  qd S r   )"_component_is_cutoff_edge_fragmentr   maintotal_pixelsr   r   rw   %      
c                 3  r   r   )"_component_is_side_sliver_fragmentr   r   r   r   rw   *  r   TF)rQ   rN   rR   c                 3  r   r   ) _component_is_detached_from_mainr   r   r   r   rw   5  s   " r   r   rP   c                 s  "    | ]}t |d pdV  qdS r   r   Nru   rT   r   r   r   rw   B       g(\?)	r   
ValueErrorr`   rd   anyr2   &_components_look_like_stable_pet_parts_merge_componentsr4   )	r?   r!   rG   significantedge_fragmentside_sliver_fragmentdetached
main_ratio
fragmentedr   )r   r   r   r   r_     sl   
*r_   r   rC   r   rd   c                 C  s   t |dpd}|tdt |d k rdS dd | dg d	D }d
d |dg d	D }tdt|d |d t|d |d  d }tdt|d |d t|d |d  d }|dkpe|dkS )Nr   r   d   r   Fc                 S     g | ]}t |qS r   rd   rU   valuer   r   r   rW   [      z4_component_is_detached_from_main.<locals>.<listcomp>r   r   c                 S  r   r   r   r   r   r   r   rW   \  r   rq   rP   rr      rd   r4   r`   min)r   rC   r   component_pixelsmain_boxcomponent_boxgap_xgap_yr   r   r   r   W  s   ..r   c                 C  sb  t |dpd}|tdt |d k rdS |tdt |d kr#dS dd	 | d
g dD }dd	 |d
g dD }tdt |dpFd}tdt |dpRd}d}|d |kpv|d || d kpv|d |kpv|d || d k}	|	s{dS tdt|d |d t|d |d  d }
tdt|d |d t|d |d  d }|
dkp|dkS )Nr   r   P   r   FrP   {Gz?c                 S  r   r   r   r   r   r   r   rW   h  r   z6_component_is_cutoff_edge_fragment.<locals>.<listcomp>r   r   c                 S  r   r   r   r   r   r   r   rW   i  r   source_widthsource_heightr   rq   rr      r   )r   rC   r   r   r   r   r   r   edge_marginnear_canvas_edger   r   r   r   r   r   b  s,   
..r   c                 C  s  t |dpd}|tdt |d k rdS |tdt |d kr#dS dd	 | d
g dD }dd	 |d
g dD }td|d |d  d }td|d |d  d }td|d |d  d }td|d |d  d }	||d krydS |	|d krdS tdt|d |d t|d |d  d }
tdt|d |d t|d |d  d }|d |d  d }|d |d  d }||d |d  k p||d |d  kp|d |d d kp|d |d d k}||d |d  k}|dkp|
tdt |d k}|o|o|S )Nr   r   r   r   FrP   r   c                 S  r   r   r   r   r   r   r   rW     r   z6_component_is_side_sliver_fragment.<locals>.<listcomp>r   r   c                 S  r   r   r   r   r   r   r   rW     r   rq   rr   gQ?gzG?
ףp=
?r   g?g      ?r   )r   rC   r   r   r   r   
main_widthmain_heightcomponent_widthcomponent_height	overlap_xr   component_center_xcomponent_center_y	side_band
lower_bandtenuous_horizontal_contactr   r   r   r   z  s:   .. r   c                 C  s  | t vrdS t|dk st|dkrdS tdtdd |D }t|dp'd| }|d	k r1dS d
d |dg dD }td|d |d  d }td|d |d  d }|D ]~}	|	|u raqZt|	dphd}
|
td| }|dkrx dS t|	dkr dS dd |	dg dD }tdt|d |d t|d |d  d }tdt|d |d t|d |d  d }|tdt|d kr dS |tdt|d kr dS qZdS )NFrq      rP   c                 s  r   r   ru   rT   r   r   r   rw     r   z9_components_look_like_stable_pet_parts.<locals>.<genexpr>r   r   g
ףp=
?c                 S  r   r   r   r   r   r   r   rW     r   z:_components_look_like_stable_pet_parts.<locals>.<listcomp>r   r   rr   r   gq=
ףp?c                 S  r   r   r   r   r   r   r   rW     r   H   gQ?T)r   r2   r`   r   rd   r4   _component_fill_ratior   )r!   r?   r   r   significant_totalr   r   r   r   rC   r   component_ratior   r   r   r   r   r   r     s:   ..r   floatc                 C  sP   t dt| dp	d}t dt| dpd}t| dpdt d||  S )NrP   r   r   r   r   r   )rC   r   r   r   r   r   r     s    r   c                 C  sJ  t dd | D }t dd | D }tdd | D }tdd | D }|| d }|| d }dg||  }d}| D ]]}	t|	d	 }
t|	d
 }t|	d d | }t|	d d | }|	d }t|D ]2}t|
D ]+}|||
 |  }|d dkr|qk|| | | | }|| d dkr|d7 }|||< qkqeq;||||g||||dS )Nc                 s       | ]}t |d  d V  qdS )r   r   Nr   r   r   r   r   rw     rx   z$_merge_components.<locals>.<genexpr>c                 s  r   )r   rP   Nr   r   r   r   r   rw     rx   c                 s  r   )r   rq   Nr   r   r   r   r   rw     rx   c                 s  r   )r   rr   Nr   r   r   r   r   rw     rx   rP   r   r   r   r   r   r   rr   )r   r   r   r   r   )r   r`   rd   r~   )r?   min_xmin_ymax_xmax_yr   r   r   r   rC   r   r   offset_xoffset_yr   yxpixeltarget_indexr   r   r   r     s>   


r   c           
      C  s   t dt| dp	d}t dt| dpd}| d}|dks*|dks*t|ts1d dd ddS t|d }t|d }t||||\}}t||||\}}	||||	dS )Nr   r   r   r   )lower_foreground_center_xlower_foreground_pixel_countfoot_foreground_center_xfoot_foreground_pixel_countg(\?g(\?)r`   rd   r4   
isinstancer^   _foreground_center_x_from_y)
rC   r   r   r   lower_start_yfoot_start_ylower_centerlower_countfoot_center
foot_countr   r   r   r8     s$   
r8   r   list[tuple[int, int, int, int]]r   r   start_ytuple[float | None, int]c           
      C  s   d}d}t td||D ]/}|| }t |D ]$}| ||  }	t|	tr1t|	dk s1t|	d dkr2q||7 }|d7 }qq|rD|| |fS d |fS )N        r   r   rr   rP   )r~   r`   r   tupler2   rd   )
r   r   r   r   x_totalcountr   	row_startr   r   r   r   r   r     s   &
r   c                 C  s  dd | D }dg||  }g }t |D ]\}}|r|| rqt|g}d||< g }	|}
|}d}d}|r| }|	| || }|| }t|
|}
t||}t||}t||}|d |f|d |f||d f||d ffD ]-\}}|dk s||ks|dk s||krqo|| | }|| s|| sqod||< || qo|s4t|	dk rq||
 d }|| d }dg||  }|	D ]}|| }|| }||
 }|| }| | ||| | < q||
|||g|||||t|	d	 q|jd
d d |S )Nc                 S  s   g | ]}|d  dkqS )rr   r   r   )rU   r   r   r   r   rW         z3_extract_components_from_pixels.<locals>.<listcomp>FTr   rP   r   r   )r   r   r   r   r   r   r   c                 S  s   | d d | d d fS )Nr   rP   r   r   r   r   r   r   r   J  r   z1_extract_components_from_pixels.<locals>.<lambda>r   )r3   r   popleftr9   r   r`   r2   sort)r   r   r   
foregroundvisitedr?   indexis_foregroundqueuecomponent_indicesr   r   r   r   currentr   r   next_xnext_y
next_indexr   r   cropsource_indexsource_xsource_ycrop_xcrop_yr   r   r   r}     sl   





4 
r}   r   x0y0c           	      C  s>   g }t ||| D ]}|| | }|| |||   q	|S r   )r~   extend)	r   r   r
  r  r   r   croppedr   r   r   r   r   r   N  s
   r   rs   r   slot_heightrt   c                C  s   t dt| dd}t dt| dd}t dt| dd}|dk r'dS |t d|d k s9|t d|d k r;dS ||d	 krE|sEdS ||ksO||d
 krQdS dS )Nr   r   r   r   i  F   gQ?g\(\?gQ?Tr   )rC   r   r  rt   r   r   r   r   r   r   r   ]  s   $r   c                 C  s   t | ||\}}|S r   )rc   )r   r   r   cell_reportr   r   r   r6   r  s   r6   rY   rZ   r[   float | Noner\   6tuple[list[tuple[int, int, int, int]], dict[str, Any]]c                C  s@  t td }t td }|d u r!t|t|d |t|d d}ntt||t|d |t|d }t|d}tdt || }tdt || }t| ||||}	dgtt  }
t| d }t| d }t|D ]}t|D ]}|	|| |  |
|| t | | < qoqi|
|||| d || d g||tt|d|dfS )	NzG?rP         ?r   r   rq   r   )fitted_bboxfitted_widthfitted_heightrm   r\   )	rd   r/   r1   r   r`   r   _resize_nearestr~   round)r   r   r   r[   r\   	max_width
max_heighttarget_widthtarget_heightresizedr  r   r   r   r   r   r   r   rc   w  s.   "$
&rc   list[float]c           
   	   C  s   | sg S g }| D ].}t dt|dpd}t dt|dpd}tt||dd}||| | |  qt|}g }| D ]6}t dt|dpJd}t dt|dpVd}t dt|| }|| d }	|t|	t||dt q?|S )NrP   r   r   )r   r   r  g      ?)	r`   rd   r4   r   _max_cell_fit_scaler9   _medianr   MAX_ROW_NORMALIZE_UPSCALE)
r?   independently_fitted_areasrC   r   r   r[   target_areascalessource_areanormalized_scaler   r   r   ra     s"   ra   c                 C  s6   t td }t td }t|t| d |t|d S )Nr  rP   )rd   r/   r1   r   r`   )r   r   r  r  r   r   r   r!    s   r!  valuesc                 C  sH   | sdS t | }t|d }t|d r|| S ||d  ||  d S )Nr   rq   rP   )sortedr2   )r)  sorted_valuesmidpointr   r   r   r"    s   r"  r  r  c           
      C  s   ||kr||krt | S g }t|D ].}t|d t|| | }t|D ]}t|d t|| | }	|| || |	   q%q|S )NrP   )r^   r~   r   rd   r9   )
r   r   r   r  r  outputr   r  r   r  r   r   r   r    s   r  r=   r;   r>   rB   r  Nonec           	      C  sT   |t  }|t }ttD ]}tt D ]}||t  |  | || | | | < qqd S r   )r/   r1   r~   )	r=   r;   r>   rB   r  start_xr   r   r   r   r   r   r7     s   &r7   0tuple[int, int, list[tuple[int, int, int, int]]]c              
   C  sN  |  ts	tdtt}d  } } } }}g }|d t| krttd| ||d  d }| |d |d  }	|d }
|
| }| |
| }|d }|	dkr]td|\}}}}}}}n|	d	krg|| n|	d
krln|d t| ks!d |||||hv rtd|dks|dvs|dkrtd|dkrdnd}t|| }t	d
|}g }t|}d}tt|D ]&}|| }|d7 }t||||  }||7 }t|||| || |}qg }|D ]=}tdt||D ]2}|dkr	||| ||d  ||d  ||d  f q||| ||d  ||d  df qqt|t||fS )Nnot_pngr  >Ir   r         IHDR>IIBBBBB   IDAT   IENDmissing_png_header>   rq   r   unsupported_png_formatr   rr       rP   rq      )
startswithPNG_SIGNATUREr   r2   structunpackr9   rd   zlib
decompressjoin	bytearrayr~   _unfilter_row)r   positionr   r   	bit_depth
color_type	interlace
idat_partslengthkind
data_startdata_enddata_compression_filterchannelsstriderawr-   previousoffset_filter_typerowr   r   r   r   r   r|     sZ   


0(r|   rX  rC  rT  rW  bytes_per_pixelc                 C  s   |dkrd S t t| D ]h}||kr| ||  nd}|| }||kr(|||  nd}|dkr9| | | d@ | |< q|dkrH| | | d@ | |< q|dkr[| | || d  d@ | |< q|dkrn| | t||| d@ | |< qtd| d S )Nr   rP   r;  rq   rr   r   unsupported_png_filter_)r~   r2   _paethr   )rX  rT  rW  rY  r   r   up
upper_leftr   r   r   rD    s    rD  r   r\  r]  c                 C  sT   | | | }t ||  }t || }t || }||kr"||kr"| S ||kr(|S |S r   )abs)r   r\  r]  estimateleft_distanceup_distanceupper_left_distancer   r   r   r[     s   r[  c           	      C  s   t  }t|D ]#}|d |||  |d |   D ]\}}}}|||||f qqttdtd| |ddddd tdt	t
| tdd	 S )
Nr   rP   r4  r5  r3  r   r6  r7  r:  )rC  r~   r9   r  r=  
_png_chunkr>  packr@  compressr   )	r   r   r   rS  r   redgreenbluealphar   r   r   r:   ,  s   
$r:   rK  rN  c                 C  s2   t dt||  | t dt| | d@  S )Nr2  l    )r>  rd  r2   binasciicrc32)rK  rN  r   r   r   rc  :  s   2rc  )r   r   r   r
   )rH   rI   rG   rJ   r   r
   )r!   rp   r   r   rG   rJ   r   r   )r   r   r   r   )r?   r   r!   rp   rG   rJ   r   r   )r   r   rC   r   r   rd   r   rJ   )
r!   rp   r?   r   r   r   r   rd   r   rJ   )rC   r   r   r   )r?   r   r   r   )rC   r   r   r   )
r   r   r   rd   r   rd   r   rd   r   r   )r   r   r   rd   r   rd   r   r   )r   r   r   rd   r
  rd   r  rd   r   rd   r   rd   r   r   )
rC   r   r   rd   r  rd   rt   rJ   r   rJ   )r   r   r   rd   r   rd   r   r   )r   r   r   rd   r   rd   r[   r  r\   rp   r   r  )r?   r   r   r   )r   rd   r   rd   r   r   )r)  r   r   r   )r   r   r   rd   r   rd   r  rd   r  rd   r   r   )r=   r   r;   rd   r>   rd   rB   rd   r  r   r   r.  )r   r   r   r0  )
rX  rC  rT  rC  rW  rd   rY  rd   r   r.  )r   rd   r\  rd   r]  rd   r   rd   )r   rd   r   rd   r   r   r   r   )rK  r   rN  r   r   r   )0
__future__r   rj  r>  r@  collectionsr   dataclassesr   typingr   packages.pet_package_schemar   r   r/   r1   r0   r=  r#  r
   rF   ro   r   r5   r_   r   r   r   r   r   r   r8   r   r}   r   r   r6   rc   ra   r!  r"  r  r7   r|   rD  r[  r:   rc  r   r   r   r   <module>   sd    
7q
Q

D



$

!


?


!






1

