mediapipe .GestureRecognizer() throwing AttributeError

92 views Asked by At

I am trying to following mediapipe's tutorial on a simple gesture recogniser for static images (seen here), however when I run a simple version I am getting "AttributeError: 'FieldDescriptor' object has no attribute '_default_constructor' ".

Here is the entire message:

Traceback (most recent call last):
  File "/Users/catlett/Desktop/PycharmProjects/pythonProject/main.py", line 33, in <module>
    recogniser = vision.GestureRecognizer.create_from_options(options)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/mediapipe/tasks/python/vision/gesture_recognizer.py", line 341, in create_from_options
    task_info.generate_graph_config(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/mediapipe/tasks/python/core/task_info.py", line 97, in generate_graph_config
    node_config.options.CopyFrom(task_subgraph_options)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/google/protobuf/message.py", line 129, in CopyFrom
    self.MergeFrom(other_msg)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/google/protobuf/internal/python_message.py", line 1334, in MergeFrom
    field_value = field._default_constructor(self)
AttributeError: 'FieldDescriptor' object has no attribute '_default_constructor'

Process finished with exit code 1

And here is my code:

import mediapipe as mp
import cv2
import math

from mediapipe.tasks import python
from mediapipe.tasks.python import vision

if __name__ == '__main__':
    print("running...")

    DESIRED_HEIGHT = 400
    DESIRED_WIDTH = 400

    def resize_and_show(image):
        h, w = image.shape[:2]
        if h < w:
            img = cv2.resize(image, (DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH))))
        else:
            img = cv2.resize(image, (math.floor(w / (h / DESIRED_HEIGHT)), DESIRED_HEIGHT))
        print("showing...")
        cv2.imshow("very_good", img)
        cv2.waitKey()

    image_file_path = '/Users/user/Desktop/Photo on 06-03-2024 at 16.31.jpg'
    resize_and_show(cv2.imread(image_file_path))

    model_path = '/Users/user/Downloads/gesture_recognizer.task'

    base_options = python.BaseOptions(model_asset_path=model_path)
    options = vision.GestureRecognizerOptions(base_options=base_options)
    recogniser = vision.GestureRecognizer.create_from_options(options)

    images = []
    results = []

    image = mp.Image.create_from_file(image_file_path)

    recognition_result = recogniser.recognize(image)

    images.append(image)
    top_gesture = recognition_result.gestures[0][0]
    hand_landmarks = recognition_result.hand_landmarks
    results.append((top_gesture, hand_landmarks))

    print(results)

I have tried changing versions of mediapipe and protobuf thinking it was a version compatibility issue (this is like my 3rd attempt at using mediapipe and I've had dozens of version issues). But I wasn't able to get it working.

I'm running Python3.10 and mediapipe=0.10.10

Does anyone know what I'm doing wrong or have an idea to circumvent this?

Answer: Downgrading to mediapipe=0.10.9 solved the issue

0

There are 0 answers