
    uki                        d Z ddlZddlmZmZmZ ddlmZ ddlmZ ddl	m
Z ddl	mZ ddl	mZ dd	lmZ ej$                  Zej(                  Zej,                  Zej0                   G d
 d             Zej0                   G d d             Zy)zDetections data class.    N)AnyListOptional)detection_pb2)location_data_pb2)bounding_box)category)keypoint)doc_controlsc                       e Zd ZU dZej
                  ed<   eej                     ed<   dZ
eeej                        ed<   ej                  defd       Zeej                  dedd fd	              Zd
edefdZy)	DetectionzRepresents one detected object in the object detector's results.

  Attributes:
    bounding_box: A BoundingBox object.
    categories: A list of Category objects.
    keypoints: A list of NormalizedKeypoint objects.
  r   
categoriesN	keypointsreturnc                 ^   g }g }g }g }g }| j                   D ]  }|j                  |j                         |j                  r|j                  |j                         |j                  r|j                  |j                         |j
                  sy|j                  |j
                          | j                  r| j                  D ]  }t        j                         }|j                  r|j                  |_	        |j                  r|j                  |_
        |j                  r|j                  |_        |j                  r|j                  |_        |j                  |        t        ||||t        t        j                  j                  | j                   j#                         |            S )z&Generates a Detection protobuf object.)formatr   relative_keypoints)labellabel_idscoredisplay_namelocation_data)r   appendr   indexcategory_namer   r   _LocationDataProtoRelativeKeypointxyr   keypoint_label_DetectionProtoFormatBOUNDING_BOXr   to_pb2)	selflabels	label_idsscoresdisplay_namesr   r	   r
   relative_keypoint_protos	            r/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/mediapipe/tasks/python/components/containers/detections.pyr$   zDetection.to_pb2-   sc    FIFMOO 4mmHNN#	(			h,,-			X2234 ~~nn 
;("4"E"E"G::&.jj
!
#::&.jj
!
#>>3;>>
!
0>>*2..
!
'!!"9:
; "(%,,99**1131

 
    pb2_objc                    g }g }t        |j                        D ]  \  }}|j                  t        j                  ||t        |j                        k  r|j                  |   nd|t        |j                        k  r|j                  |   nd|t        |j                        k  r|j                  |   nd              |j                  j                  rwt        |j                  j                        D ]U  \  }}|j                  t        j                  |j                  |j                  |j                  |j                               W t!        t"        j$                  j'                  |j                  j(                        ||      S )z<Creates a `Detection` object from the given protobuf object.N)r   r   r   r   )r   r   r   r   )r   r   r   )	enumerater   r   category_moduleCategorylenr   r   r   r   r   keypoint_moduleNormalizedKeypointr   r   r    r   bounding_box_moduleBoundingBoxcreate_from_pb2r   )clsr-   r   r   idxr   elems          r+   r7   zDetection.create_from_pb2X   s`    JI. 
U

"
"s7++,, $$S)s7==)) $MM#.s7//00 #//4  // !6!6!I!IJ 
)#t..&&&&))jj		

 (44DD!!..
  r,   otherc                 ~    t        |t              sy| j                         j                  |j                               S zChecks if this object is equal to the given object.

    Args:
      other: The object to be compared with.

    Returns:
      True if the objects are equal.
    F)
isinstancer   r$   __eq__r%   r;   s     r+   r?   zDetection.__eq__   s.     eY';;=//r,   )__name__
__module____qualname____doc__r5   r6   __annotations__r   r0   r1   r   r   r3   r4   r   do_not_generate_docsr!   r$   classmethodr7   r   boolr?    r,   r+   r   r      s     $///?++,,BF)Xd?==>?F$$(o ( %(T $$&O & & % &P0# 0$ 0r,   r   c                       e Zd ZU dZee   ed<   ej                  de	fd       Z
eej                  de	dd fd              ZdedefdZy	)
DetectionResultziRepresents the list of detected objects.

  Attributes:
    detections: A list of `Detection` objects.
  
detectionsr   c                 p    t        | j                  D cg c]  }|j                          c}      S c c}w )z*Generates a DetectionList protobuf object.)	detection)_DetectionListProtorL   r$   )r%   rN   s     r+   r$   zDetectionResult.to_pb2   s1     7;G)9##%GI IGs   3r-   c                 z    t        |j                  D cg c]  }t        j                  |       c}      S c c}w )zBCreates a `DetectionResult` object from the given protobuf object.)rL   )rK   rN   r   r7   )r8   r-   rN   s      r+   r7   zDetectionResult.create_from_pb2   s:     >E>O>O'1:	!!),'   's   8r;   c                 ~    t        |t              sy| j                         j                  |j                               S r=   )r>   rK   r$   r?   r@   s     r+   r?   zDetectionResult.__eq__   s.     e_-;;=//r,   N)rA   rB   rC   rD   r   r   rE   r   rF   rO   r$   rG   r7   r   rH   r?   rI   r,   r+   rK   rK      s{     9o$$I) I %I
 $$$7 <M  % 0# 0$ 0r,   rK   )rD   dataclassestypingr   r   r   mediapipe.framework.formatsr   r   ,mediapipe.tasks.python.components.containersr   r5   r	   r0   r
   r3   1mediapipe.tasks.python.core.optional_dependenciesr   DetectionListrO   r   r!   LocationDatar   	dataclassrK   rI   r,   r+   <module>rZ      s      & & 5 9 \ T T J#11 ))&33  n0 n0 n0b #0 #0 #0r,   