
    uki-                        d Z ddl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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 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 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! 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)  G d dejT                        Z+dZ, e-e+j\                  e+j^                  fe+j\                  e+j`                  fe+j\                  e+jb                  fe+j^                  e+jd                  fe+j^                  e+jf                  fe+j`                  e+jd                  fe+j`                  e+jh                  fe+jd                  e+jj                  fe+jb                  e+jf                  fe+jb                  e+jh                  fe+jf                  e+jj                  fe+jh                  e+jj                  fg      Z6 ejn                  d        G d! d"e8             Z9 ejn                  dd#       G d$ d%e9             Z: ejn                  dd#       G d& d'e9             Z; ejn                  dd#       G d( d)e9             Z< ejn                  dd#       G d* d+e9             Z= e:        e;        e=        e<       d,Z>d-e?fd.Z@d/e?d0e9fd1ZA ejn                  d        G d2 d3e8             ZB G d4 d5e'      ZCy)6zMediaPipe Objectron.    N)ListTuple
NamedTupleOptional)#constant_side_packet_calculator_pb2)gate_calculator_pb2)split_vector_calculator_pb2)image_to_tensor_calculator_pb2)inference_calculator_pb2)$tensors_to_detections_calculator_pb2) tensors_to_floats_calculator_pb2)#tensors_to_landmarks_calculator_pb2)ssd_anchors_calculator_pb2)association_calculator_pb2)&collection_has_min_size_calculator_pb2))detection_label_id_to_text_calculator_pb2)"detections_to_rects_calculator_pb2)"landmark_projection_calculator_pb2)"local_file_contents_calculator_pb2)"non_max_suppression_calculator_pb2)"rect_transformation_calculator_pb2)thresholding_calculator_pb2)landmark_pb2)annotation_data_pb2)'frame_annotation_to_rect_calculator_pb2)-lift_2d_frame_annotation_to_3d_calculator_pb2)SolutionBase)download_utilsc                   4    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
Zy)BoxLandmarkzThe 9 3D box landmarks.r                           N)__name__
__module____qualname____doc__CENTERBACK_BOTTOM_LEFTFRONT_BOTTOM_LEFTBACK_TOP_LEFTFRONT_TOP_LEFTBACK_BOTTOM_RIGHTFRONT_BOTTOM_RIGHTBACK_TOP_RIGHTFRONT_TOP_RIGHT     _/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/mediapipe/python/solutions/objectron.pyr    r    3   s8      &-../r7   r    z2mediapipe/modules/objectron/objectron_cpu.binarypbT)auto_attribsc                   "    e Zd ZU eed<   eed<   y)ObjectronModel
model_path
label_nameN)r)   r*   r+   str__annotations__r6   r7   r8   r;   r;   _   s    //r7   r;   )r9   frozenc                   *    e Zd ZU dZeed<   dZeed<   y)	ShoeModelz?mediapipe/modules/objectron/object_detection_3d_sneakers.tfliter<   Footwearr=   Nr)   r*   r+   r<   r>   r?   r=   r6   r7   r8   rB   rB   e   s    ;*c <*cr7   rB   c                   *    e Zd ZU dZeed<   dZeed<   y)
ChairModelz<mediapipe/modules/objectron/object_detection_3d_chair.tfliter<   Chairr=   NrD   r6   r7   r8   rF   rF   l   s    8*c 9*cr7   rF   c                   *    e Zd ZU dZeed<   dZeed<   y)CameraModelz=mediapipe/modules/objectron/object_detection_3d_camera.tfliter<   Camerar=   NrD   r6   r7   r8   rI   rI   s   s    9*c :*cr7   rI   c                   *    e Zd ZU dZeed<   dZeed<   y)CupModelz:mediapipe/modules/objectron/object_detection_3d_cup.tfliter<   zCoffee cup, Mugr=   NrD   r6   r7   r8   rL   rL   z   s    6*c 7%*c%r7   rL   )ShoerG   CuprJ   objectron_modelc                 X    t        j                  d       t        j                  |        y)zaDownloads the objectron models from the MediaPipe Github repo if they don't exist in the package.zNmediapipe/modules/objectron/object_detection_ssd_mobilenetv2_oidv4_fp16.tfliteN)r   download_oss_model)rO   s    r8   _download_oss_objectron_modelsrR      s%     ##V ##O4r7   namereturnc                 x    | t         vrt        |  d      t        t         |    j                         t         |    S )Nz) is not a valid model name for Objectron.)_MODEL_DICT
ValueErrorrR   r<   )rS   s    r8   get_model_by_namerX      s;    	
vFG
HH T!2!=!=>	T	r7   c                       e Zd ZU ej                  ed<   ej                  ed<   ej                  ed<   ej                  ed<   ej                  ed<   y)ObjectronOutputslandmarks_2dlandmarks_3drotationtranslationscaleN)	r)   r*   r+   r   NormalizedLandmarkListr?   LandmarkListnpndarrayr6   r7   r8   rZ   rZ      s8    333)))JJzz	r7   rZ   c                        e Zd ZdZ	 	 	 	 	 	 	 	 ddedededededeeef   deeef   d	e	eeef      f fd
Z
dej                  def fdZdej                   dee   fdZ xZS )	ObjectronzMediaPipe Objectron.

  MediaPipe Objectron processes an RGB image and returns the 3D box landmarks
  and 2D rectangular bounding box of each detected object.
  static_image_modemax_num_objectsmin_detection_confidencemin_tracking_confidence
model_namefocal_lengthprincipal_point
image_sizec	                     |\  }	}
|\  }}|,|d   dz  }|d   dz  }|	|z  }	|
|z  }
||z
   |z  }||z
   |z  }t        |      }t        | 	  t        |j                  |j
                  || d|||	|
||ddg       y)	a  Initializes a MediaPipe Objectron class.

    Args:
      static_image_mode: Whether to treat the input images as a batch of static
        and possibly unrelated images, or a video stream.
      max_num_objects: Maximum number of objects to detect.
      min_detection_confidence: Minimum confidence value ([0.0, 1.0]) for object
        detection to be considered successful.
      min_tracking_confidence: Minimum confidence value ([0.0, 1.0]) for the
        box landmarks to be considered tracked successfully.
      model_name: Name of model to use for predicting box landmarks, currently
        support {'Shoe', 'Chair', 'Cup', 'Camera'}.
      focal_length: Camera focal length `(fx, fy)`, by default is defined in NDC
        space. To use focal length (fx_pixel, fy_pixel) in pixel space, users
        should provide image_size = (image_width, image_height) to enable
        conversions inside the API.
      principal_point: Camera principal point (px, py), by default is defined in
        NDC space. To use principal point (px_pixel, py_pixel) in pixel space,
        users should provide image_size = (image_width, image_height) to enable
        conversions inside the API.
      image_size (Optional): size (image_width, image_height) of the input image
        , ONLY needed when use focal_length and principal_point in pixel space.

    Raises:
      ConnectionError: If the objectron open source model can't be downloaded
        from the MediaPipe Github repo.
    Nr   g       @r!   )box_landmark_model_pathallowed_labelsrg   use_prev_landmarks)zLobjectdetectionoidv4subgraph__TensorsToDetectionsCalculator.min_score_threshz5boxlandmarksubgraph__ThresholdingCalculator.thresholdz6Lift2DFrameAnnotationTo3DCalculator.normalized_focal_xz6Lift2DFrameAnnotationTo3DCalculator.normalized_focal_yz@Lift2DFrameAnnotationTo3DCalculator.normalized_principal_point_xz@Lift2DFrameAnnotationTo3DCalculator.normalized_principal_point_ydetected_objects)binary_graph_pathside_inputscalculator_paramsoutputs)rX   super__init___BINARYPB_FILE_PATHr<   r=   )selfrf   rg   rh   ri   rj   rk   rl   rm   fxfypxpy
half_widthhalf_heightmodel	__class__s                   r8   rx   zObjectron.__init__   s    L FBFBa=3&jqMC'k
?bb:o+b;+-b j)E	G-','7'7#...&7"7	
 ) (%'%'/1/1
  $$1  &r7   imagerT   c                     t         |   d|i      }|j                  r"| j                  |j                        |_        |S d|_        |S )a
  Processes an RGB image and returns the box landmarks and rectangular bounding box of each detected object.

    Args:
      image: An RGB image represented as a numpy ndarray.

    Raises:
      RuntimeError: If the underlying graph throws any error.
      ValueError: If the input image is not three channel RGB.

    Returns:
      A NamedTuple object with a "detected_objects" field that contains a list
      of detected 3D bounding boxes. Each detected box is represented as an
      "ObjectronOutputs" instance.
    r   )
input_dataN)rw   processrr   _convert_format)rz   r   resultsr   s      r8   r   zObjectron.process   sR      go'5)9o:G!%!5!5g6N6N!Og N "&gNr7   inputsc           
         t               }|j                  D ]Q  }t        j                  t        j                  |j
                        d      }t        j                  |j                        }t        j                  |j                        }t        j                         }t        j                         }|j                  D ]  }	|	j                  }
|j                  j                  |
j                  |
j                          |	j"                  }|j                  j                  |j                  |j                   |j$                          |j'                  t)        |||||             T |S )N)r#   r#   )xy)r   r   z)r_   )listannotationsrb   reshapearrayr]   r^   r_   r   r`   ra   	keypointspoint_2dlandmarkaddr   r   point_3dr   appendrZ   )rz   r   new_outputs
annotationr]   r^   r_   r[   r\   keypointr   r   s               r8   r   zObjectron._convert_format  s'    &K(( O
BHHZ%8%896BhHHZ334khhz''(e!88:l!..0l ** L($$!!HJJ(**!=$$!!HJJ(**

!K	L ),*2KuN OO" r7   )Fr%   g      ?gGz?rM   )      ?r   )        r   N)r)   r*   r+   r,   boolintfloatr>   r   r   rx   rb   rc   r   r   r   FrameAnnotationr   rZ   r   __classcell__)r   s   @r8   re   re      s     */&'1404!'3=6@7;J&"&J& #J& */J& ).	J&
 J& #5%<0J& !&eUl 3J& $E#s(O4J&X2:: * .!116:;K6Lr7   re   )Dr,   enumtypingr   r   r   r   attrnumpyrb   mediapipe.calculators.corer   r   r	   mediapipe.calculators.tensorr
   r   r   r   r   mediapipe.calculators.tfliter   mediapipe.calculators.utilr   r   r   r   r   r   r   r   r   mediapipe.framework.formatsr   'mediapipe.modules.objectron.calculatorsr   r   r   mediapipe.python.solution_baser   mediapipe.python.solutionsr   IntEnumr    ry   	frozensetr.   r/   r0   r2   r1   r3   r4   r5   BOX_CONNECTIONSsobjectr;   rB   rF   rI   rL   rV   r>   rR   rX   rZ   re   r6   r7   r8   <module>r      s     4 4   K : B G A M I L C A M P I I I I I B 4 G [ a 7 5$,, 6 K !!;#@#@A!!;#<#<=!!;#@#@A""K$>$>?""K$B$BC : :; : :;!<!<=""K$B$BC""K$>$>?##[%@%@A!<!<=   TV  
 T$'  ( T$'  ( T$'.  ( T$'&~ & (& K\:m	5C 5C N  Tv   r7   