
    Wpf-                        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j*                  Z+dZ, e-e+j.        e+j/        fe+j.        e+j0        fe+j.        e+j1        fe+j/        e+j2        fe+j/        e+j3        fe+j0        e+j2        fe+j0        e+j4        fe+j2        e+j5        fe+j1        e+j3        fe+j1        e+j4        fe+j3        e+j5        fe+j4        e+j5        fg          Z6 ej7        d            G d! d"e8                      Z9 ej7        dd#           G d$ d%e9                      Z: ej7        dd#           G d& d'e9                      Z; ej7        dd#           G d( d)e9                      Z< ej7        dd#           G d* d+e9                      Z= e:             e;             e=             e<            d,Z>d-e?fd.Z@d/e?d0e9fd1ZA ej7        d            G d2 d3e8                      ZB G d4 d5e'          ZCdS )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                   6    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )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     d/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/mediapipe/python/solutions/objectron.pyr    r    3   sG          &-..///r7   r    z2mediapipe/modules/objectron/objectron_cpu.binarypbT)auto_attribsc                   $    e Zd ZU eed<   eed<   dS )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<   dS )	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<   dS )
ChairModelz<mediapipe/modules/objectron/object_detection_3d_chair.tfliter<   Chairr=   NrD   r6   r7   r8   rF   rF   l   s:         8*c 9 9 9*cr7   rF   c                   ,    e Zd ZU dZeed<   dZeed<   dS )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<   dS )CupModelz:mediapipe/modules/objectron/object_detection_3d_cup.tfliter<   zCoffee cup, Mugr=   NrD   r6   r7   r8   rL   rL   z   s:         6*c 7 7 7%*c%%%%%r7   rL   )ShoerG   CuprJ   objectron_modelc                 V    t          j        d           t          j        |            dS )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      s5     #V   #O44444r7   namereturnc                     | 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      sE    	
GGG
H
HH T!2!=>>>	T	r7   c                   t    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<   dS )ObjectronOutputslandmarks_2dlandmarks_3drotationtranslationscaleN)	r)   r*   r+   r   NormalizedLandmarkListr?   LandmarkListnpndarrayr6   r7   r8   rZ   rZ      sZ         3333))))Jz	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.
  Fr%         ?Gz?rM         ?ri           rk   Nstatic_image_modemax_num_objectsmin_detection_confidencemin_tracking_confidence
model_namefocal_lengthprincipal_point
image_sizec	                 &   |\  }	}
|\  }}|2|d         dz  }|d         dz  }|	|z  }	|
|z  }
||z
   |z  }||z
   |z  }t          |          }t                                          t          |j        |j        || d|||	|
||ddg           dS )	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_labelsrm   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=   )selfrl   rm   rn   ro   rp   rq   rr   rs   fxfypxpy
half_widthhalf_heightmodel	__class__s                   r8   r~   zObjectron.__init__   s    L FBFBa=3&jqMC'k
?bb:o+b;+-b j))E	GG-','7#..&7"7	
 
 ) (%'%'/1/1
 
  $$1  & & & & &r7   imagerT   c                     t                                          d|i          }|j        r |                     |j                  |_        n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)r}   processrx   _convert_format)r   r   resultsr   s      r8   r   zObjectron.process   sU      ggoo'5)9o::G &!%!5!5g6N!O!Og!%gNr7   inputsc           
      d   t                      }|j        D ]}t          j        t          j        |j                  d          }t          j        |j                  }t          j        |j                  }t          j	                    }t          j
                    }|j        D ]b}	|	j        }
|j                            |
j        |
j                   |	j        }|j                            |j        |j        |j                   c|                    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   )r   r   new_outputs
annotationr]   r^   r_   r[   r\   keypointr   r   s               r8   r   zObjectron._convert_format  s3    &&K( O O
BHZ%8996BBhHZ344khz'((e!8::l!.00l * L L($!!HJ(*!===$!!HJ(*
!KKKK ),*2KuN N N O O O Or7   )Fr%   rf   rg   rM   rh   rj   N)r)   r*   r+   r,   boolintfloatr>   r   r   r~   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&
 J& #5%<0J& !&eUl 3J& $E#s(O4J& J& J& J& J& J&X2: *      .!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    r   	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      sN      4 4 4 4 4 4 4 4 4 4 4 4      K J J J J J : : : : : : B B B B B B G G G G G G A A A A A A M M M M M M I I I I I I L L L L L L C C C C C C A A A A A A M M M M M M P P P P P P I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I B B B B B B 4 4 4 4 4 4 G G G G G G [ [ [ [ [ [ a a a a a a 7 7 7 7 7 7 5 5 5 5 5 5    $,   6 K )!;#@A!;#<=!;#@A"K$>?"K$BC :; :;!<="K$BC"K$>?#[%@A!<=    T    V   
 T$'''       (' T$'''       (' T$'''    .   (' T$'''& & & & &~ & & ('& IKKZ\\8::kmm	 5C 5 5 5 5C N     T    v            r7   