
    Wpf:K                     6   d Z ddlZddlmZ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 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' ej(        Z)ej*        Z+ej,        Z-e'j.        Z/e%j0        Z1ej2        Z3dZ4dZ5dZ6dZ7dZ8dZ9dZ:dZ;dZ<dZ=dZ>dZ?d Z@d!ZAd"ZBd#ZCejD         G d$ d%                      ZEd&eeFejG        f         d'eEfd(ZHejD         G d) d*                      ZI G d+ d,e#jJ                  ZKdS )-z"MediaPipe gesture recognizer task.    N)CallableMappingOptionalList)classification_pb2)landmark_pb2)packet_creator)packet_getter)image)packet)$gesture_recognizer_graph_options_pb2)category)landmark)classifier_optionsbase_options)	task_info)doc_controls)base_vision_task_api)image_processing_options)vision_task_running_modeimage_in	image_outIMAGEnorm_rect_in	NORM_RECThand_gesturesHAND_GESTURES
handedness
HANDEDNESS	landmarks	LANDMARKSworld_landmarksWORLD_LANDMARKSz@mediapipe.tasks.vision.gesture_recognizer.GestureRecognizerGraphi  c                       e Zd ZU dZeeej                          ed<   eeej                          ed<   eeej	                          ed<   eeej
                          ed<   dS )GestureRecognizerResulta0  The gesture recognition result from GestureRecognizer, where each vector element represents a single hand detected in the image.

  Attributes:
    gestures: Recognized hand gestures of detected hands. Note that the index of
      the gesture is always -1, because the raw indices from multiple gesture
      classifiers cannot consolidate to a meaningful index.
    handedness: Classification of handedness.
    hand_landmarks: Detected hand landmarks in normalized image coordinates.
    hand_world_landmarks: Detected hand landmarks in world coordinates.
  gesturesr   hand_landmarkshand_world_landmarksN)__name__
__module____qualname____doc__r   category_moduleCategory__annotations__landmark_moduleNormalizedLandmarkLandmark     p/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/mediapipe/tasks/python/vision/gesture_recognizer.pyr'   r'   A   s         	 	 o./00004012222tO>?@@@@T/":;<<<<<<r6   r'   output_packetsreturnc           
      b   t          j        | t                             }t          j        | t                             }t          j        | t                             }t          j        | t
                             }g }|D ]}g }t          j                    }|                    |           |j	        D ]A}	|
                    t          j        t          |	j        |	j        |	j                             B|
                    |           g }
|D ]}g }t          j                    }|                    |           |j	        D ]A}|
                    t          j        |j        |j        |j        |j                             B|

                    |           g }|D ]}}t%          j                    }|                    |           g }|j        D ]4}|
                    t*          j                            |                     5|
                    |           ~g }|D ]}}t%          j                    }|                    |           g }|j        D ]4}|
                    t*          j                            |                     5|
                    |           ~t5          ||
||          S )z;Constructs a `GestureRecognizerResult` from output packets.)indexscoredisplay_namecategory_name)r
   get_proto_list_HAND_GESTURE_STREAM_NAME_HANDEDNESS_STREAM_NAME_HAND_LANDMARKS_STREAM_NAME!_HAND_WORLD_LANDMARKS_STREAM_NAMEr   ClassificationList	MergeFromclassificationappendr/   r0   _GESTURE_DEFAULT_INDEXr<   r=   labelr;   r   NormalizedLandmarkListr   r2   r3   create_from_pb2LandmarkListr4   r'   )r8   gestures_proto_listhandedness_proto_listhand_landmarks_proto_listhand_world_landmarks_proto_listgesture_resultsprotogesture_categoriesgesture_classificationsgesturehandedness_resultshandedness_categorieshandedness_classificationsr   hand_landmarks_resultsr)   hand_landmarks_listhand_landmarkhand_world_landmarks_resultsr*   hand_world_landmarks_listhand_world_landmarks                         r7   _build_recognition_resultr_   T   s5    &4./  (6,-  ,:01  %2$@67% %! /" / /e0CEE%%e,,,*9  

"*M"/#M	      -....$ 5 5e!3!F!H!H((///0?  
""

"$$%2&,	      34444( 7 7e!8::NU###'0    

,
<
<]
K
K    !!"56666!#. C Ce'466""5))) "3<  &&

"
2
23F
G
G    !''(ABBBB	 "	
 
 r6   c                   &   e Zd ZU dZeed<   ej        Zeed<   dZ	e
ed<   dZeed<   dZeed<   dZeed	<    ej        e
          Zeed<    ej        e
          Zeed<   dZeeeej        e
gdf                  ed<   ej        defd            ZdS )GestureRecognizerOptionsa  Options for the gesture recognizer task.

  Attributes:
    base_options: Base options for the hand gesture recognizer task.
    running_mode: The running mode of the task. Default to the image mode.
      Gesture recognizer task has three running modes: 1) The image mode for
      recognizing hand gestures on single image inputs. 2) The video mode for
      recognizing hand gestures on the decoded frames of a video. 3) The live
      stream mode for recognizing hand gestures on a live stream of input data,
      such as from camera.
    num_hands: The maximum number of hands can be detected by the recognizer.
    min_hand_detection_confidence: The minimum confidence score for the hand
      detection to be considered successful.
    min_hand_presence_confidence: The minimum confidence score of hand presence
      score in the hand landmark detection.
    min_tracking_confidence: The minimum confidence score for the hand tracking
      to be considered successful.
    canned_gesture_classifier_options: Options for configuring the canned
      gestures classifier, such as score threshold, allow list and deny list of
      gestures. The categories for canned gesture classifiers are: ["None",
      "Closed_Fist", "Open_Palm", "Pointing_Up", "Thumb_Down", "Thumb_Up",
      "Victory", "ILoveYou"]. Note this option is subject to change.
    custom_gesture_classifier_options: Options for configuring the custom
      gestures classifier, such as score threshold, allow list and deny list of
      gestures. Note this option is subject to change.
    result_callback: The user-defined result callback for processing live stream
      data. The result callback should only be specified when the running mode
      is set to the live stream mode.
  r   running_mode   	num_handsg      ?min_hand_detection_confidencemin_hand_presence_confidencemin_tracking_confidence)default_factory!canned_gesture_classifier_options!custom_gesture_classifier_optionsNresult_callbackr9   c                    | j                                         }| j        t          j        k    rdnd|_        t          |          }|j        }| j        |_        | j	        |j
        _	        | j        |j
        _        | j        |j        _        |j        }|j        j                            | j                                                   |j        j                            | j                                                   |S )z6Generates an GestureRecognizerOptions protobuf object.FTr   )r   to_pb2rb   _RunningModer   use_stream_mode#_GestureRecognizerGraphOptionsProtohand_landmarker_graph_optionsrg   rd   hand_detector_graph_optionsre   min_detection_confidencerf   %hand_landmarks_detector_graph_options%hand_gesture_recognizer_graph_options'canned_gesture_classifier_graph_optionsr   CopyFromri   'custom_gesture_classifier_graph_optionsrj   )selfbase_options_proto gesture_recognizer_options_protohand_landmarker_options_proto%hand_gesture_recognizer_options_protos        r7   rm   zGestureRecognizerOptions.to_pb2   s!    *1133"l&888d &
 (K'( ( ($
 	)F " 	$ "9 	 "=G 	* "=V 	) "G` 	)N * *Qdmm.5577   *Qdmm.5577   ,+r6   )r+   r,   r-   r.   _BaseOptionsr1   rn   r   rb   rd   intre   floatrf   rg   dataclassesfield_ClassifierOptionsri   rj   rk   r   r   r'   image_moduleImager   do_not_generate_docsrp   rm   r5   r6   r7   ra   ra      sI         < +1,111)S),,,,(++++#&5&&&:K+:K(; ; ;#%7    ;L+:K(; ; ;#%7   
  8');SA4GH    $',9 ', ', ', %$', ', ',r6   ra   c            	           e Zd ZdZededd fd            Zededd fd            Z	 dde	j
        d	ee         defd
Z	 dde	j
        ded	ee         defdZ	 dde	j
        ded	ee         ddfdZdS )GestureRecognizerz2Class that performs gesture recognition on images.
model_pathr9   c                     t          |          }t          |t          j                  }|                     |          S )ai  Creates an `GestureRecognizer` object from a TensorFlow Lite model and the default `GestureRecognizerOptions`.

    Note that the created `GestureRecognizer` instance is in image mode, for
    recognizing hand gestures on single image inputs.

    Args:
      model_path: Path to the model.

    Returns:
      `GestureRecognizer` object that's created from the model file and the
      default `GestureRecognizerOptions`.

    Raises:
      ValueError: If failed to create `GestureRecognizer` object from the
        provided file such as invalid file path.
      RuntimeError: If other types of error occurred.
    )model_asset_path)r   rb   )r~   ra   rn   r   create_from_options)clsr   r   optionss       r7   create_from_model_pathz(GestureRecognizer.create_from_model_path  sF    &  <<<L&!0B  G ""7+++r6   r   c                    dt           t          t          j        f         ffd}t	          t
          d                    t          t          g          d                    t          t          g          gd                    t          t          g          d                    t          t          g          d                    t          t           g          d                    t"          t$          g          d                    t          t&          g          g          } | |                    j        t,          j        k              j        j        r|nd          S )a  Creates the `GestureRecognizer` object from gesture recognizer options.

    Args:
      options: Options for the gesture recognizer task.

    Returns:
      `GestureRecognizer` object that's created from `options`.

    Raises:
      ValueError: If failed to create `GestureRecognizer` object from
        `GestureRecognizerOptions` such as missing the model.
      RuntimeError: If other types of error occurred.
    r8   c                    | t                                                    rd S t          j        | t                              }| t                                                   rH| t                   }                    t          g g g g           ||j        j        t          z             d S t          |           }| t                   j        }                    |||j        t          z             d S N)_IMAGE_OUT_STREAM_NAMEis_emptyr
   	get_imager@   rk   r'   	timestampvalue_MICRO_SECONDS_PER_MILLISECONDr_   )r8   r   empty_packetgesture_recognizer_resultr   r   s        r7   packets_callbackz?GestureRecognizer.create_from_options.<locals>.packets_callback,  s    	.	/	8	8	:	: %n5K&LMMe	1	2	;	;	=	= %&?@#BB33"(,JJ	
 	
 	

 	";N"K"K !:;Ei
#

/;
;    r6   :)
task_graphinput_streamsoutput_streamstask_options)enable_flow_limitingN)r   strpacket_modulePacket	_TaskInfo_TASK_GRAPH_NAMEjoin
_IMAGE_TAG_IMAGE_IN_STREAM_NAME_NORM_RECT_TAG_NORM_RECT_STREAM_NAME_HAND_GESTURE_TAGr@   _HANDEDNESS_TAGrA   _HAND_LANDMARKS_TAGrB   _HAND_WORLD_LANDMARKS_TAGrC   r   generate_graph_configrb   rn   LIVE_STREAMrk   )r   r   r   r   s    `  r7   r   z%GestureRecognizer.create_from_options  sS   $m6J1J)K      . #HHj"7899HHn&<=>>

 HH')BCDDHHo'>?@@HH)+FGHHHH*,MN  HHj"89::
   I" 3''!(!5'"( 	( 	
 	
 	#3=  r6   Nr   r   c           	      d   |                      ||d          }|                     t          t          j        |          t
          t          j        |                                          i          }|t                   	                                rt          g g g g           S t          |          S )aa  Performs hand gesture recognition on the given image.

    Only use this method when the GestureRecognizer is created with the image
    running mode.

    The image can be of any size with format RGB or RGBA.
    TODO: Describes how the input image will be preprocessed after the yuv
    support is implemented.

    Args:
      image: MediaPipe Image.
      image_processing_options: Options for image processing.

    Returns:
      The hand gesture recognition results.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If gesture recognition failed to run.
    Froi_allowed)convert_to_normalized_rect_process_image_datar   r	   create_imager   create_protorm   r@   r   r'   r_   )ry   r   r   normalized_rectr8   s        r7   	recognizezGestureRecognizer.recognize]  s    2 55 %U 6  O --~:5AA ;""$$!
 !
/  N /099;; 5$RR444$^444r6   timestamp_msc           	         |                      ||d          }|                     t          t          j        |                              |t          z            t          t          j        |	                                                              |t          z            i          }|t                                                   rt          g g g g           S t          |          S )a6  Performs gesture recognition on the provided video frame.

    Only use this method when the GestureRecognizer is created with the video
    running mode.

    Only use this method when the GestureRecognizer is created with the video
    running mode. It's required to provide the video frame's timestamp (in
    milliseconds) along with the video frame. The input timestamps should be
    monotonically increasing for adjacent calls of this method.

    Args:
      image: MediaPipe Image.
      timestamp_ms: The timestamp of the input video frame in milliseconds.
      image_processing_options: Options for image processing.

    Returns:
      The hand gesture recognition results.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If gesture recognition failed to run.
    Fr   )r   _process_video_datar   r	   r   atr   r   r   rm   r@   r   r'   r_   )ry   r   r   r   r   r8   s         r7   recognize_for_videoz%GestureRecognizer.recognize_for_video  s    8 55 %U 6  O --~:5AADD99 
  
 	 ;""$$!
 !

"\::
;
;/  N /099;; 5$RR444$^444r6   c           	      T   |                      ||d          }|                     t          t          j        |                              |t          z            t          t          j        |	                                                              |t          z            i           dS )av  Sends live image data to perform gesture recognition.

    The results will be available via the "result_callback" provided in the
    GestureRecognizerOptions. Only use this method when the GestureRecognizer
    is created with the live stream running mode.

    Only use this method when the GestureRecognizer is created with the live
    stream running mode. The input timestamps should be monotonically increasing
    for adjacent calls of this method. This method will return immediately after
    the input image is accepted. The results will be available via the
    `result_callback` provided in the `GestureRecognizerOptions`. The
    `recognize_async` method is designed to process live stream data such as
    camera input. To lower the overall latency, gesture recognizer may drop the
    input images if needed. In other words, it's not guaranteed to have output
    per input image.

    The `result_callback` provides:
      - The hand gesture recognition results.
      - The input image that the gesture recognizer runs on.
      - The input timestamp in milliseconds.

    Args:
      image: MediaPipe Image.
      timestamp_ms: The timestamp of the input image in milliseconds.
      image_processing_options: Options for image processing.

    Raises:
      ValueError: If the current input timestamp is smaller than what the
      gesture recognizer has already processed.
    Fr   N)
r   _send_live_stream_datar   r	   r   r   r   r   r   rm   )ry   r   r   r   r   s        r7   recognize_asyncz!GestureRecognizer.recognize_async  s    H 55 %U 6  O 	~:5AADD99 
  
 	 ;""$$!
 !

"\::
;
;!     r6   r   )r+   r,   r-   r.   classmethodr   r   ra   r   r   r   r   _ImageProcessingOptionsr'   r   r   r   r   r5   r6   r7   r   r      sc       ::,c ,6I , , , ;,0 @,@@ @ @ ;@J EI&5 &5&5 !))@ A&5 	&5 &5 &5 &5X EI	+5 +5+5 +5 !))@ A	+5
 +5 +5 +5 +5b EI	. .. . !))@ A	.
 . . . . . .r6   r   )Lr.   r   typingr   r   r   r   mediapipe.framework.formatsr   r   mediapipe.pythonr	   r
   $mediapipe.python._framework_bindingsr   r   r   r   2mediapipe.tasks.cc.vision.gesture_recognizer.protor   ,mediapipe.tasks.python.components.containersr   r/   r   r2   ,mediapipe.tasks.python.components.processorsr   mediapipe.tasks.python.corer   base_options_moduler   task_info_module1mediapipe.tasks.python.core.optional_dependenciesr   "mediapipe.tasks.python.vision.corer   r   image_processing_options_moduler   running_mode_moduleBaseOptionsr~   GestureRecognizerGraphOptionsrp   ClassifierOptionsr   VisionTaskRunningModern   ImageProcessingOptionsr   TaskInfor   r   r   r   r   r   r@   r   rA   r   rB   r   rC   r   r   r   rH   	dataclassr'   r   r   r_   ra   BaseVisionTaskApir   r5   r6   r7   <module>r      s   ) (     4 4 4 4 4 4 4 4 4 4 4 4 : : : : : : 4 4 4 4 4 4 + + + + + + * * * * * * F F F F F F H H H H H H c c c c c c T T T T T T T T T T T T K K K K K K K K K K K K E E E E E E J J J J J J C C C C C C j j j j j j ^ ^ ^ ^ ^ ^".(F $ (9 "89P %	" $ 
' + # & ) ! $5 !- F  "&   = = = = = = = =$LC!556LL L L L^ W, W, W, W, W, W, W, W,tb b b b b,> b b b b br6   