cansik / mediapipe-silicon

Prebuilt Google MediaPipe packages for arm64.
MIT License
77 stars 4 forks source link

README could include conditional import instructions #9

Closed giorgiop closed 1 year ago

giorgiop commented 1 year ago

I have tried to import the package just as mediapipe on a machine with M1, but it didn't work. I'd like to support conditional import so that my project is agnostic to the architecture. I solved this way

import importlib
try:  # check if we are on Apple M1
    mp = importlib.import_module("mediapipe-silicon")
except ImportError:
    mp = importlib.import_module("mediapipe")

Can the authors confirm this is the intended behavior? If so (or else) I suggest to explain in the README

cansik commented 1 year ago

What do you mean by "platform agnostic"? Usually only one package is installed.

You can either install mediapipe or mediapipe-silicon (depending on your platform) and import mediapipe should work.

Please explain how you installed it and in which environment you are (python, os, conda or venv and so on).

giorgiop commented 1 year ago

By using two different python requirements.txt you can allow users to install a packag on either M1 and not M1 architectures (say, both unix). This is doable by following your conditional installation tip https://github.com/cansik/mediapipe-silicon#requirementstxt

Now, if I simpy do

import mediapipe

The M1 user who installed mediapipe-silicon will get an import error (tested). Am I doing something different from what you expect?

cansik commented 1 year ago

I can not replicate your behaviour. Tell me which environment (conda, venv python version and so on) you are using, otherwise I can not help you.

What you describe never happened to me and I did not get that feedback so far. You can have the same requirement.txt by using the following statement:

mediapipe; platform_system != "Darwin" or platform.machine != "arm64"
mediapipe-silicon; platform_system == "Darwin" and platform.machine == "arm64"

Now if you install this into a new virtual-env, you get either the mediapipe or mediapipe-silicon package installed (but both contain the mediapipe package). So import mediapipe works for both (silicon and non-silicon). Here the output of an example I just ran on my M1 Mac with python 3.9:

python --version                               
Python 3.9.12

python -m venv venv && source venv/bin/activate

(venv) cat requirements.txt                       
mediapipe; platform_system != "Darwin" or platform.machine != "arm64"
mediapipe-silicon; platform_system == "Darwin" and platform.machine == "arm64"
protobuf>=3.11,<4

(venv) pip install -r requirements.txt                       
Ignoring mediapipe: markers 'platform_system != "Darwin" or platform_machine != "arm64"' don't match your environment
Collecting mediapipe-silicon
  Using cached mediapipe_silicon-0.8.11-cp39-cp39-macosx_12_0_arm64.whl (62.1 MB)
Collecting protobuf<4,>=3.11
  Using cached protobuf-3.20.3-py2.py3-none-any.whl (162 kB)
Collecting matplotlib
  Using cached matplotlib-3.6.2-cp39-cp39-macosx_11_0_arm64.whl (7.2 MB)
Collecting numpy
  Using cached numpy-1.23.5-cp39-cp39-macosx_11_0_arm64.whl (13.4 MB)
Collecting absl-py
  Using cached absl_py-1.3.0-py3-none-any.whl (124 kB)
Collecting attrs>=19.1.0
  Using cached attrs-22.1.0-py2.py3-none-any.whl (58 kB)
Collecting opencv-contrib-python
  Using cached opencv_contrib_python-4.6.0.66-cp37-abi3-macosx_11_0_arm64.whl (38.9 MB)
Collecting kiwisolver>=1.0.1
  Using cached kiwisolver-1.4.4-cp39-cp39-macosx_11_0_arm64.whl (63 kB)
Collecting packaging>=20.0
  Using cached packaging-22.0-py3-none-any.whl (42 kB)
Collecting cycler>=0.10
  Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting contourpy>=1.0.1
  Using cached contourpy-1.0.6-cp39-cp39-macosx_11_0_arm64.whl (226 kB)
Collecting pyparsing>=2.2.1
  Using cached pyparsing-3.0.9-py3-none-any.whl (98 kB)
Collecting python-dateutil>=2.7
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting pillow>=6.2.0
  Using cached Pillow-9.3.0-cp39-cp39-macosx_11_0_arm64.whl (2.9 MB)
Collecting fonttools>=4.22.0
  Using cached fonttools-4.38.0-py3-none-any.whl (965 kB)
Collecting six>=1.5
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, pyparsing, protobuf, pillow, packaging, numpy, kiwisolver, fonttools, cycler, attrs, absl-py, python-dateutil, opencv-contrib-python, contourpy, matplotlib, mediapipe-silicon
Successfully installed absl-py-1.3.0 attrs-22.1.0 contourpy-1.0.6 cycler-0.11.0 fonttools-4.38.0 kiwisolver-1.4.4 matplotlib-3.6.2 mediapipe-silicon-0.8.11 numpy-1.23.5 opencv-contrib-python-4.6.0.66 packaging-22.0 pillow-9.3.0 protobuf-3.20.3 pyparsing-3.0.9 python-dateutil-2.8.2 six-1.16.0

(venv) python -c "import mediapipe as mp; print(mp.__version__)"
objc[87444]: Class CaptureDelegate is implemented in both mediapipe-test/venv/lib/python3.9/site-packages/mediapipe/.dylibs/libopencv_videoio.3.4.16.dylib (0x100c20860) and mediapipe-test/venv/lib/python3.9/site-packages/cv2/cv2.abi3.so (0x137586538). One of the two will be used. Which one is undefined.
objc[87444]: Class CVWindow is implemented in both mediapipe-test/venv/lib/python3.9/site-packages/mediapipe/.dylibs/libopencv_highgui.3.4.16.dylib (0x10091ca68) and mediapipe-test/venv/lib/python3.9/site-packages/cv2/cv2.abi3.so (0x137586588). One of the two will be used. Which one is undefined.
objc[87444]: Class CVView is implemented in both mediapipe-test/venv/lib/python3.9/site-packages/mediapipe/.dylibs/libopencv_highgui.3.4.16.dylib (0x10091ca90) and mediapipe-test/venv/lib/python3.9/site-packages/cv2/cv2.abi3.so (0x1375865b0). One of the two will be used. Which one is undefined.
objc[87444]: Class CVSlider is implemented in both mediapipe-test/venv/lib/python3.9/site-packages/mediapipe/.dylibs/libopencv_highgui.3.4.16.dylib (0x10091cab8) and mediapipe-test/venv/lib/python3.9/site-packages/cv2/cv2.abi3.so (0x1375865d8). One of the two will be used. Which one is undefined.
0.8.11

I can not understand how import mediapipe_silicon could even work, because the package (namespace) name inside the wheel is not mediapipe-silicon but mediapipe. That is why I do have the feeling that your environment is maybe mixing things up here.

Update: I just tested the same commands within a conda env and it worked as well.

giorgiop commented 1 year ago

Thanks for the explanation and the edits on the README! It works