mediapipe
mediapipe copied to clipboard
[Python] NamedTuple SolutionOutputs is not actually a NamedTuple
System information (Please provide as much relevant information as possible)
- Have I written custom code (as opposed to using a stock example script provided in MediaPipe): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04, Android 11, iOS 14.4): NA
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: NA
- Browser and version (e.g. Google Chrome, Safari) if the issue happens on browser: NA
- Programming Language and version ( e.g. C++, Python, Java): Python
- MediaPipe version: 0.8.9.1
- Bazel version (if compiling from source): NA
- Solution ( e.g. FaceMesh, Pose, Holistic ): Pose
- Android Studio, NDK, SDK versions (if issue is related to building in Android environment): NA
- Xcode & Tulsi version (if issue is related to building for iOS): NA
Describe the current behavior:
When calling _asdict() on SolutionOutputs (given it is a NamedTuple), it says TypeError: _asdict() missing 1 required positional argument: 'self'. SolutionOutputs obtained by running pose.process(image).
Describe the expected behavior:
Calling _asdict() should successfully convert the results object into a dictionary.
Standalone code to reproduce the issue: Provide a reproducible test case that is the bare minimum necessary to replicate the problem. If possible, please share a link to Colab/repo link /any notebook:
import cv2
import mediapipe as mp
mp_pose = mp.solutions.pose
with mp_pose.Pose(
static_image_mode=True,
model_complexity=2,
enable_segmentation=True,
min_detection_confidence=0.5) as pose:
image = cv2.imread('test.png')
image_height, image_width, _ = image.shape
# Convert the BGR image to RGB before processing.
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
if not results.pose_landmarks:
continue
print(results._asdict())
Other info / Complete Logs :
Looking into the source code (mediapipe/python/solution_base.py), I noticed that a new NamedTuple factory is being created each time during process(), and then the results are added to the newly created classs via setattr(). This isn't the correct way to use a NamedTuple. The correct way is:
solution_outputs = collection.namedtuple('SolutionOutputs', keys)
results = solution_outputs(pose_landmarks=..., ...)
Hi @Interpause , Could you please elaborate w.r.t above issue to investigate further.
My specific use case is serializing the results produced by results = pose.process(image). Given Mediapipe Python says that results should be a collection.namedtuple, I call _asdict() on it to convert it to a dictionary (see Python docs: https://docs.python.org/3/library/collections.html#collections.somenamedtuple._asdict). This is so that I can iterate over the protobuf messages present in the tuple and convert them via SerializeToString().
However, it throws TypeError: _asdict() missing 1 required positional argument: 'self'. When I took a look at Mediapipe's Python source code, I noticed that namedtuples were being used wrong. Hence the aforementioned issue. See Other info / Complete Logs for how I think it can be fixed.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.
Closing as stale. Please reopen if you'd like to work on this further.
Closing as stale. Please reopen if you'd like to work on this further.
I created a possible fix in this pull request #4024
Hello @Interpause, We are upgrading the MediaPipe Legacy Solutions to new MediaPipe solutions However, the libraries, documentation, and source code for all the MediapPipe Legacy Solutions will continue to be available in our GitHub repository and through library distribution services, such as Maven and NPM.
You can continue to use those legacy solutions in your applications if you choose. Though, we would request you to check new MediaPipe solutions which can help you more easily build and customize ML solutions for your applications. These new solutions will provide a superset of capabilities available in the legacy solutions. Thank you
This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.