o
    Ej(                     @  sv   d dl mZ d dlmZ h dZdZdZdZd&d
dZd'ddZ	d(ddZ
d)ddZd*ddZd+dd Zd,d#d$Zd%S )-    )annotations)Any>   walksleeptail_wag       @@       frame_qadict[str, Any]returnc                 C  s  g }d}t  }| dg D ]M}t|dd}t|dd}dd |dg D }t|}d}	g }
|tv rY|d	 d
k rY|d dk rY|dksM|d tk rYd}	|
d |d |dkr{|d dk r{|d dk r{|d tk r{d}	|
d |d |dkr|d dk r|d dk r|d dk rd}	|
d |d |dkr|d dk r|d dk rd}	|
d |d |dkr|d dk s|d dk rd}	|
d  |d |dkr|d d!k s|d" dk rd}	|
d# |d |dkr|d$ dkr|d tk rd}	|
d% |d |dkr+|d&kr+t|r+d}	|
d' |d |dkrC|d( dk rCd}	|
d) |d |oG|	}||	|
|d*}|rV||d< || q|t	||d+S ),NTrowsaction 	loop_modec                 S  s   g | ]	}t |tr|qS  )
isinstancedict.0framer   r   E/opt/sixxie/releases/current/services/pet_builder/action_semantics.py
<listcomp>   s    z-evaluate_action_semantics.<locals>.<listcomp>framesbbox_motion
   area_variation_ratiogQ?r   visible_leg_contact_motionFstatic_action_rowstatic_action_rowswidth_variation   center_motionwalk_pose_not_visibleweak_action_semanticsr   height_variationsleep_pose_not_visibler   tail_motion_not_visiblemedian_aspect_ratio?low_wide_frame_count   sleep_pose_too_upright?side_view_frame_countwalk_pose_too_uprightvisible_leg_contact_frame_countwalk_leg_motion_too_weaktwo_frame_copywalk_repeated_contact_phasetail_visible_frame_counttail_not_visible_in_most_frames)r   okreasonsmetrics)r6   failure_reasonsr   )
setgetstr_row_metricsSTRICT_ACTIONSLEG_MOTION_THRESHOLD_PXappendadd+_walk_repeats_first_and_third_contact_phasesorted)r
   r   r6   r9   rowr   r   r   r8   row_okr7   
row_resultr   r   r   evaluate_action_semantics   s   





 













rG   r   r<   frame_reportcandidate_indexintfloatc                 C  s"  | dpg d}t|dkrdS tdt|d t|d  d }tdt|d t|d  d }tdt| d	p<d}tdt| d
pHd}t| dpRd}d}	|t|d }
|	t|d d7 }	|	t|
t|  d 8 }	|	td|d d 8 }	|dk r|	d| d 8 }	|	|d 8 }	|	S )Nsource_bbox)r   r   r   r      g      r         r+   foreground_pixelscomponent_countbbox_fill_ratio      ?        g     @g      I@g      $@g      @gffffff?g{Gz?)r;   lenmaxrJ   rK   minabs_target_ratio)r   rH   rI   bboxwidthheightpixelsrQ   
fill_ratioscoreaspect_ratior   r   r   score_candidate_frame`   s"   ""ra   c                 C  s   | dv rdS | dkrdS dS )N>   r   r   r         @sitg?rS   r   )r   r   r   r   rY   w   s
   rY   r   list[dict[str, Any]]c                 C  sf  dd | D }|s>i dddddddddd	d
d	dd	dd	ddddddddddddddddddS dd |D }dd |D }dd |D }dd |D }dd t ||D }dd t ||D }dd | D }dd | D }	|rt|t| nd}
|	rt|	t|	 nd}|rt|nd}|rt|nd}i dtt|t| t|t| dtt|t| t|t| dt|t| dt|t| d|| t|d d
t|dt|dt|dtdd  |D dtd!d  |D dtd"d  |D dt|d|
dt|	d|dtt|t|	dt|
|S )#Nc                 S  s&   g | ]}t |d tr|d qS )rL   )r   r;   listr   r   r   r   r      s   & z _row_metrics.<locals>.<listcomp>r   r   r"   r    r%   r   rT   r(   min_aspect_ratiomax_aspect_ratior*   r.   r4   #lower_foreground_center_frame_countlower_foreground_center_motion"foot_foreground_center_frame_countfoot_foreground_center_motionr0   r   c                 S  s.   g | ]}t d t|d t|d   d qS )r   rN   rO   rV   rJ   r   boxr   r   r   r         . c                 S  s.   g | ]}t d t|d t|d  d qS )r   r+   rO   rl   rm   r   r   r   r      ro   c                 S  s(   g | ]}t |d  t |d  d qS )r   rN   rJ   rm   r   r   r   r         ( c                 S  s(   g | ]}t |d  t |d  d qS )rO   r+   rN   rp   rm   r   r   r   r      rq   c                 S  s   g | ]\}}|| qS r   r   r   r[   r\   r   r   r   r      s    c                 S  s   g | ]\}}|t |d  qS )rO   )rV   rr   r   r   r   r      s    c                 S  <   g | ]}| d durt| dpdtkrt|d  qS )lower_foreground_center_xNlower_foreground_pixel_countr   )r;   rJ   LOWER_FOREGROUND_MIN_PIXELSrK   r   r   r   r   r          
c                 S  rs   )foot_foreground_center_xNfoot_foreground_pixel_countr   )r;   rJ   FOOT_FOREGROUND_MIN_PIXELSrK   r   r   r   r   r      rw   rO   c                 s      | ]	}|d krdV  qdS )r)   rO   Nr   r   ratior   r   r   	<genexpr>       z_row_metrics.<locals>.<genexpr>c                 s  r{   )r-   rO   Nr   r|   r   r   r   r~      r   c                 s  r{   )g      ?rO   Nr   r|   r   r   r   r~      r   )ziprV   rW   _mediansumrU   )r   boxeswidthsheights	centers_x	centers_yareasaspect_ratioslower_centersfoot_centerslower_motionfoot_motionmax_areamin_arear   r   r   r=      s   	
""	

r=   boolc                 C  s  t | dk rdS | d }| d }|d}|d}t|tr$t|ts&dS t |dks2t |dkr4dS t|}t|}d }|d urP|d urPtt|t| }tdd t||D }|dkrh|d u pg|d	kS |d u rndS t	|d t	|d  d
 }	t	|d t	|d  d
 }
t	|d t	|d
  d
 }t	|d t	|d
  d
 }t	|d t	|d  d }t	|d t	|d  d }|d	kot|| dkot|	|
 dkot|| dkS )Nr+   Fr   rN   rL   rM   c                 s  s(    | ]\}}t t|t| V  qd S )N)rX   rJ   )r   abr   r   r   r~      s   & z>_walk_repeats_first_and_third_contact_phase.<locals>.<genexpr>rb   rO   r      )
rU   r;   r   re   _walk_contact_center_xrX   rK   rV   r   rJ   )r   firstthird	first_box	third_boxfirst_contactthird_contactcontact_delta	box_deltafirst_widththird_widthfirst_heightthird_heightfirst_center_xthird_center_xr   r   r   rB      sB   

rB   r   float | Nonec                 C  sd   |  d}|d urt|  dpdtkrt|S |  d}|d ur0t|  dp(dtkr0t|S d S )Nrx   ry   r   rt   ru   )r;   rJ   rz   rK   rv   )r   footlowerr   r   r   r      s   

r   valueslist[float]c                 C  sH   | sdS t | }t|d }t|d r|| S ||d  ||  d S )NrT   rN   rO   )rC   rU   )r   sorted_valuesmiddler   r   r   r      s   r   N)r
   r   r   r   )r   r<   rH   r   rI   rJ   r   rK   )r   r<   r   rK   )r   rd   r   r   )r   rd   r   r   )r   r   r   r   )r   r   r   rK   )
__future__r   typingr   r>   r?   rv   rz   rG   ra   rY   r=   rB   r   r   r   r   r   r   <module>   s    

T


A
&
