
    Wpf5                     N   d Z ddlZddlZddlmZmZmZmZmZ ddl	Z	ddl
mZ ddlZddlmZ ddlmZ ddlmZ dZdZdZd	Zd
ZdZdZdZej         G d d                      Zdedededededeeef         f         f
dZ ee           e            fdej         dej!        dedefdZ"d ee           e            dfdej         dej#        deeeeef                           deeeeeef         f                  d eeeeeef         ef         f         d!e$fd"Z%d#d$d% e            fdej         d&ej         d'ej         d(eeef         d)eeef         d*ed+efd,Z&d- Z'd eed./           eed./          d0d0fdej#        deeeeef                           ded ed1ed2efd3Z(dS )4z!MediaPipe solution drawing utils.    N)ListMappingOptionalTupleUnion)detection_pb2)landmark_pb2)location_data_pb2      ?   )   r   r   r   r   r   )r   r      )r      r   )r   r   r   c                   L    e Zd ZU eZeeeef         ed<   dZeed<   dZ	eed<   dS )DrawingSpeccolor   	thicknesscircle_radiusN)
__name__
__module____qualname__WHITE_COLORr   r   int__annotations__r   r        h/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/mediapipe/python/solutions/drawing_utils.pyr   r   '   sO          !,%sC}	+++)S-r   r   normalized_xnormalized_yimage_widthimage_heightreturnc                     dt           dt          fd} ||           r ||          sdS t          t          j        | |z            |dz
            }t          t          j        ||z            |dz
            }||fS )z4Converts normalized value pair to pixel coordinates.valuer$   c                 n    | dk    st          j        d|           o| dk     pt          j        d|           S )Nr      )mathisclose)r&   s    r   is_valid_normalized_valuezC_normalized_to_pixel_coordinates.<locals>.is_valid_normalized_value7   sA    AI/a// Neai 7M6:l1e6L6LNr   Nr(   )floatboolminr)   floor)r    r!   r"   r#   r+   x_pxy_pxs          r    _normalized_to_pixel_coordinatesr2   1   s    Nu N N N N N $
#L
1
1 
#
#L
1
1 4	TZ{233[1_	E	E$	TZ|344lQ6F	G	G$	tr   r   image	detectionkeypoint_drawing_specbbox_drawing_specc                    |j         sdS | j        d         t          k    rt          d          | j        \  }}}|j         }|j        t
          j        j        k    rt          d          |j        D ]E}t          |j
        |j        ||          }	t          j        | |	|j        |j        |j                   F|                    d          sdS |j        }
t          |
j        |
j        ||          }t          |
j        |
j        z   |
j        |
j        z   ||          }t          j        | |||j        |j                   dS )a  Draws the detction bounding box and keypoints on the image.

  Args:
    image: A three channel BGR image represented as numpy ndarray.
    detection: A detection proto message to be annotated on the image.
    keypoint_drawing_spec: A DrawingSpec object that specifies the keypoints'
      drawing settings such as color, line thickness, and circle radius.
    bbox_drawing_spec: A DrawingSpec object that specifies the bounding box's
      drawing settings such as color and line thickness.

  Raises:
    ValueError: If one of the followings:
      a) If the input image is not three channel BGR.
      b) If the location data is not relative data.
  Nr   0Input image must contain three channel bgr data.z?LocationData must be relative for this drawing funtion to work.relative_bounding_box)location_datashape_BGR_CHANNELS
ValueErrorformatr
   LocationDataRELATIVE_BOUNDING_BOXrelative_keypointsr2   xycv2circler   r   r   HasFieldr:   xminyminwidthheight	rectangle)r4   r5   r6   r7   
image_rows
image_cols_locationkeypointkeypoint_pxr:   rect_start_pointrect_end_points                r   draw_detectionrU   D   s   ( 
	  
F
[^}$$
G
H
HH#k*j!$(_)6LLL
IK K K - M Mh28:xz3=zK KKJuk#8#F$*,A,KM M M M 
		2	3	3 
F"85 "7"<j  4 #8#>> #8#?? . -'!'):)DF F F F Fr   Tlandmark_listconnectionslandmark_drawing_specconnection_drawing_specis_drawing_landmarksc                 Z   |sdS | j         d         t          k    rt          d          | j         \  }}}i }	t          |j                  D ]s\  }
}|                    d          r|j        t          k     s%|                    d          r|j        t          k     rPt          |j        |j        ||          }|r||	|
<   t|rt          |j                  }|D ]}|d         }|d         }d|cxk    r|k     rn nd|cxk    r|k     sn t          d| d	| d
          ||	v rQ||	v rMt          |t                    r||         n|}t!          j        | |	|         |	|         |j        |j                   |r|r|	                                D ]\  }
}t          |t                    r||
         n|}t+          |j        dz   t/          |j        dz                      }t!          j        | ||t2          |j                   t!          j        | ||j        |j        |j                   dS dS dS )a  Draws the landmarks and the connections on the image.

  Args:
    image: A three channel BGR image represented as numpy ndarray.
    landmark_list: A normalized landmark list proto message to be annotated on
      the image.
    connections: A list of landmark index tuples that specifies how landmarks to
      be connected in the drawing.
    landmark_drawing_spec: Either a DrawingSpec object or a mapping from hand
      landmarks to the DrawingSpecs that specifies the landmarks' drawing
      settings such as color, line thickness, and circle radius. If this
      argument is explicitly set to None, no landmarks will be drawn.
    connection_drawing_spec: Either a DrawingSpec object or a mapping from hand
      connections to the DrawingSpecs that specifies the connections' drawing
      settings such as color and line thickness. If this argument is explicitly
      set to None, no landmark connections will be drawn.
    is_drawing_landmarks: Whether to draw landmarks. If set false, skip drawing
      landmarks, only contours will be drawed.

  Raises:
    ValueError: If one of the followings:
      a) If the input image is not three channel BGR.
      b) If any connetions contain invalid landmark index.
  Nr   r9   
visibilitypresencer   r(   BLandmark index is out of range. Invalid connection from landmark # to landmark #.g333333?)r<   r=   r>   	enumeratelandmarkrG   r\   _VISIBILITY_THRESHOLDr]   _PRESENCE_THRESHOLDr2   rC   rD   len
isinstancer   rE   liner   r   itemsmaxr   r   rF   r   )r4   rV   rW   rX   rY   rZ   rM   rN   rO   idx_to_coordinatesidxrb   landmark_pxnum_landmarks
connection	start_idxend_idxdrawing_speccircle_border_radiuss                      r   draw_landmarksrs   w   s   H 
 
F
[^}$$
G
H
HH#k*j! !788 	, 	,mc8			<	(	( 		4	4	4			:	&	& 
5		0	0	028:xz3=zK KK , + ).//M! ) )
Q-i1g9,,,,},,,,,g1M1M1M1M1M1M1M1M O+4O ODKO O O P P 	P	(	(	(W8J-J-J>H#W?. ?. K.z::3J 	*95#G,l.@'	) 	) 	)
  =3 =.4466 
= 
=[3=
4* 4* E*3///D  !!;a!?!$\%?#%E!F!FH H	j%9;') ) ) 
j\%?#\%;= = = == = = =
= 
=r   )      ?rt   )        ru   g?rotationtranslationfocal_lengthprincipal_pointaxis_lengthaxis_drawing_specc                    | j         d         t          k    rt          d          | j         \  }}}	t          j        g dg dg dg dg          }
t          j        |||
j        z            j        |z   }|d         }|d         }|d	         }|\  }}|\  }}t          j        | |z  |d
z   z  |z   dd          }t          j        | |z  |d
z   z  |z   dd          }t          j        d|z   dz  |z            }t          j        d|z
  dz  |z            }|d         |d         f}|d         |d         f}|d         |d         f}|d         |d         f}t          j
        | ||t          |j                   t          j
        | ||t          |j                   t          j
        | ||t          |j                   dS )a  Draws the 3D axis on the image.

  Args:
    image: A three channel BGR image represented as numpy ndarray.
    rotation: Rotation matrix from object to camera coordinate frame.
    translation: Translation vector from object to camera coordinate frame.
    focal_length: camera focal length along x and y directions.
    principal_point: camera principal point in x and y.
    axis_length: length of the axis in the drawing.
    axis_drawing_spec: A DrawingSpec object that specifies the xyz axis drawing
      settings such as line thickness.

  Raises:
    ValueError: If one of the followings:
      a) If the input image is not three channel BGR.
  r   r9   r   )r(   r   r   )r   r(   r   )r   r   r(   ).r   ).r(   ).r   gh㈵>g      rt   r(   r   r   r   N)r<   r=   r>   npfloat32matmulTclipint32rE   arrowedLine	RED_COLORr   GREEN_COLOR
BLUE_COLOR)r4   rv   rw   rx   ry   rz   r{   rM   rN   rO   
axis_worldaxis_camrC   rD   zfxfypxpyx_ndcy_ndcx_imy_imoriginx_axisy_axisz_axiss                              r   	draw_axisr      s   . [^}$$
G
H
HH#k*j!z999iiiIIIFGG*Yxz|!;<<>L(v!v!v!&"b&"b
'2#'QX&+S"
5
5%
'2#'QX&+S"
5
5%	1u9#j0	1	1$	1u9#j0	1	1$GT!W&GT!W&GT!W&GT!W&/%4E4OPPP/%#-/ / //%#-/ / / / /r   c                 4    t          d | D                       S )Nc              3       K   | ]	}|d z  V  
dS )g     o@Nr   ).0vs     r   	<genexpr>z#_normalize_color.<locals>.<genexpr>   s&      ''Aq4x''''''r   )tupler3   s    r   _normalize_colorr      s    	'''''	'	''r      )r   r   
   	elevationazimuthc                 R   | sdS t          j        d           t          j        d          }|                    ||           i }t	          | j                  D ]\  }}	|	                    d          r|	j        t          k     s%|	                    d          r|	j	        t          k     rP|                    |	j         g|	j        g|	j         gt          |j        ddd	                   |j        
           |	j         |	j        |	j         f||<   |rt%          | j                  }
|D ]}|d         }|d         }d|cxk    r|
k     rn nd|cxk    r|
k     sn t'          d| d| d          ||v r||v r||         ||         g}|                    |d         d         |d         d         g|d         d         |d         d         g|d         d         |d         d         gt          |j        ddd	                   |j        
           t          j                     dS )a  Plot the landmarks and the connections in matplotlib 3d.

  Args:
    landmark_list: A normalized landmark list proto message to be plotted.
    connections: A list of landmark index tuples that specifies how landmarks to
      be connected.
    landmark_drawing_spec: A DrawingSpec object that specifies the landmarks'
      drawing settings such as color and line thickness.
    connection_drawing_spec: A DrawingSpec object that specifies the
      connections' drawing settings such as color and line thickness.
    elevation: The elevation from which to view the plot.
    azimuth: the azimuth angle to rotate the plot.

  Raises:
    ValueError: If any connection contains an invalid landmark index.
  N)r   r   )figsize3d)
projection)elevazimr\   r]   )xsyszsr   	linewidthr   r(   r^   r_   r`   r   )pltfigureaxes	view_initra   rb   rG   r\   rc   r]   rd   	scatter3Dr   rC   rD   r   r   r   re   r>   plot3Dshow)rV   rW   rX   rY   r   r   axplotted_landmarksrk   rb   rm   rn   ro   rp   landmark_pairs                  r   plot_landmarksr     s   0 
 
F*X
x4   ",,IG,,,, !788 D Dmc8			<	(	( 		4	4	4			:	&	& 
5		0	0	0LLZK=J<ZK=4:44R4@AA'1  3 3 3  (zk8:
{Cc 9.//M! 9 9
Q-i1g9,,,,},,,,,g1M1M1M1M1M1M1M1M O+4O ODKO O O P P 	P	'	'	'G7H,H,Hi(*;G*D
 			a #]1%5a%89a #]1%5a%89a #]1%5a%89"#:#@2#FGG-7 	 	9 	9 	9 (*****r   ))__doc__dataclassesr)   typingr   r   r   r   r   rE   matplotlib.pyplotpyplotr   numpyr}   mediapipe.framework.formatsr   r	   r
   rd   rc   r=   r   BLACK_COLORr   r   r   	dataclassr   r,   r   r2   ndarray	DetectionrU   NormalizedLandmarkListr-   rs   r   r   r   r   r   r   <module>r      s   ( '      8 8 8 8 8 8 8 8 8 8 8 8 8 8 



           5 5 5 5 5 5 4 4 4 4 4 4 9 9 9 9 9 9  	
        ',;>eCHo 56   , *59)E)E)E%0[]]	0F 0F:0F&0F '0F #	0F 0F 0F 0Fl 48 	)$$$ 	!%P= P=:P=6P= $uS#X/0P= $k73#3445	P= #WU38_k9::P= P= P= P= P=l 3=5?#&/:{}}1/ 1/RZ 1/
1/:1/ "%,/1/  %UE\2	1/
 !1/ "-1/ 1/ 1/ 1/h( ( (
 CG8C&!95 95 95:E+(A;7 ;7 ;7$&"$> >,"E > (eCHo)> ?>*5> -8	> ">  > > > > > >r   