Search code examples
pythonpython-3.xmediapipe

Extract the gesture from GestureRecognizerResult


In the mediapipe library, there is a task called GestureRecognizer which can recognize certain hand gestures. There is also a task called GestureRecognizerResult which consists of the results from the GestureRecognizer. GestureRecognizerResult has an attribute called gesture, which when printed shows the following output

> print(getattr(GestureRecognizerResult, 'gestures'))
#[[Category(index=-1, score=0.8142859935760498, display_name='', category_name='Open_Palm')]]

I actually want just the category_name to be printed, how can I do that?

Thanks in advance.


Solution

  • According to the API documentation, GestureRecognizerResult has the following attributes:

    mp.tasks.vision.GestureRecognizerResult(
        gestures: List[List[category_module.Category]],
        handedness: List[List[category_module.Category]],
        hand_landmarks: List[List[landmark_module.NormalizedLandmark]],
        hand_world_landmarks: List[List[landmark_module.Landmark]]
    )
    

    The gestures attribute is a List of gestures, each with a List of categories, so you can access those categories using the following:

    for gesture in recognition_result.gestures:
        print([category.category_name for category in gesture])