google-ai-edge / mediapipe

Cross-platform, customizable ML solutions for live and streaming media.
https://ai.google.dev/edge/mediapipe
Apache License 2.0
27.39k stars 5.15k forks source link

BaseOptions() concatenates model_asset_path with path of venv/libsite-packages #5343

Open vollkornholztuer opened 6 months ago

vollkornholztuer commented 6 months ago

Have I written custom code (as opposed to using a stock example script provided in MediaPipe)

Yes

OS Platform and Distribution

Windows 11

Mobile device if the issue happens on mobile device

No response

Browser and version if the issue happens on browser

No response

Programming Language and version

Python 3.11.7

MediaPipe version

0.10.11

Bazel version

No response

Solution

GestureRecognizerOptions(base_options=BaseOptions(model_asset="insert/path/here/gesture_recognizer.task)

Android Studio, NDK, SDK versions (if issue is related to building in Android environment)

No response

Xcode & Tulsi version (if issue is related to building for iOS)

No response

Describe the actual behavior

Base option concatenates model_asset_path with path ov .venv/site-packages for no apparent reason and crashes while creating the model. I get RuntimeError errnr=22 (see logs)

Describe the expected behaviour

The mode_asset_pash shoudln't be concatenated with anything and the webcam with gesture recognition should start.

Standalone code/steps you may have used to try to get what you need

Standalone code:

import cv2
import mediapipe as mp
import os

mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
cap = cv2.VideoCapture(0)

BaseOptions = mp.tasks.BaseOptions
GestureRecognizer = mp.tasks.vision.GestureRecognizer
GestureRecognizerOptions = mp.tasks.vision.GestureRecognizerOptions
GestureRecognizerResult = mp.tasks.vision.GestureRecognizerResult
VisionRunningMode = mp.tasks.vision.RunningMode

gestureRecognizerModelPath = "mpModels/gesture_recognizer.task"
print(gestureRecognizerModelPath)

def print_result(result: GestureRecognizerResult, output_image: mp.Image, timestamp_ms: int):
    print(result.gestures)

options = GestureRecognizerOptions(
    base_options=BaseOptions(model_asset_path=gestureRecognizerModelPath),
    running_mode=VisionRunningMode.LIVE_STREAM,
    result_callback=print_result)

timestamp = 0
with GestureRecognizer.create_from_options(options) as recognizer: # EXCEPTION HAPPENS HERE
    # Recognizer is initialized. Use it here

    while cap.isOpened():
        # Capture frame-by-frame
        ret, frame = cap.read()

        if not ret:
            print("Ignoring empty camera frame.")
            break

        timestamp += 1
        mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
        # Send live image data to perform gesture recognition
        # The results are accessible via the `result_callback` provided in
        # the `GestureRecognizerOptions` object.
        # The gesture recognizer must be created with the live stream mode.
        recognizer.recognize_async(mp_image, timestamp)

        if cv2.waitKey(5) & 0xFF == 27:
            break

cap.release()
cv2.destroyAllWindows()

### Other info / Complete Logs

```shell
console log

Traceback (most recent call last):
  File "c:\Users\mcret\Documents\+ HS\6. VC - SS 2024\IntProa\Projekt\BabsProjekt311\gestureRecognition.py", line 27, in <module>
    with GestureRecognizer.create_from_options(options) as recognizer:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mcret\Documents\+ HS\6. VC - SS 2024\IntProa\Projekt\BabsProjekt311\.venv\Lib\site-packages\mediapipe\tasks\python\vision\gesture_recognizer.py", line 340, in create_from_options
    return cls(
           ^^^^
  File "C:\Users\mcret\Documents\+ HS\6. VC - SS 2024\IntProa\Projekt\BabsProjekt311\.venv\Lib\site-packages\mediapipe\tasks\python\vision\core\base_vision_task_api.py", line 70, in __init__
    self._runner = _TaskRunner.create(graph_config, packet_callback)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Unable to open file at C:\Users\mcret\Documents\+ HS\6. VC - SS 2024\IntProa\Projekt\BabsProjekt311\.venv\Lib\site-packages/C:\Users\mcret\Documents\+ HS\6. VC - SS 2024\IntProa\Projekt\BabsProjekt311\mpModels\gesture_recognizer.task, errno=22
kuaashish commented 6 months ago

Hi @vollkornholztuer,

There appears to be a problem loading the model in Windows when using the "model_asset_path" in the Base Option.

To address this, could you please try loading it using "model_asset_buffer" as demonstrated below? Kindly let us know if this resolves the issue.

base_options = BaseOptions(model_asset_buffer=open(gestureRecognizerModelPath, "rb").read())

For additional clarity, we suggest following this thread.

Thank you!!

danielkonecny commented 6 months ago

I am not an OP, but the solution that @kuaashish provided worked for me.

kuaashish commented 6 months ago

Hi @danielkonecny,

Thank you for the confirmation, @vollkornholztuer. Could you please update the status if this solution is working for you?