Skip to content

Instantly share code, notes, and snippets.

@khanhlvg
Last active June 8, 2024 23:21
Show Gist options
  • Save khanhlvg/bbeb5e4ccfca6cbcf18508a44f5964be to your computer and use it in GitHub Desktop.
Save khanhlvg/bbeb5e4ccfca6cbcf18508a44f5964be to your computer and use it in GitHub Desktop.
## Sep 2023 update: We've released official support for MediaPipe on Raspberry Pi.
## It provides many more features to what's available in TFLite Task Library. The guide
## below has been updated to use MediaPipe instead of TFLite Task Library.
## Check out this blog post to learn more:
## https://developers.googleblog.com/2023/08/mediapipe-for-raspberry-pi-and-ios.html
# Show your Raspberry Pi OS version.
cat /etc/os-release
# Update packages on your Raspberry Pi OS.
sudo apt-get update
# Check your Python version. You should have Python 3.8 or later.
python3 --version
# Install virtualenv and upgrade pip.
python3 -m pip install --user --upgrade pip
python3 -m pip install --user virtualenv
# Create a Python virtual environment for the MediaPipe samples (optional but strongly recommended)
python3 -m venv ~/mp
# Run this command whenever you open a new Terminal window/tab to activate the environment.
source ~/mp/bin/activate
# Clone the MediaPipe samples repository with the MediaPipe Raspberry Pi samples.
git clone https://github.com/googlesamples/mediapipe.git
cd mediapipe/examples/object_detection/raspberry_pi
# Install dependencies required by the sample
sh setup.sh
# Run the object detection sample
# **IMPORTANT**: If you SSH to the Pi, make sure that:
# 1. There is a display connected to the Pi.
# 2. Run `export DISPLAY=:0` before proceed to make the object_detection window appear on the display.
python detect.py
####
# If you see an error running the sample:
# ImportError: libcblas.so.3: cannot open shared object file: No such file or directory
# you can fix it by installing an OpenCV dependency that is missing on your Raspberry Pi.
sudo apt-get install libatlas-base-dev
@gbin0301
Copy link

ImportError: /home/qvinh0301/.local/lib/python3.9/site-packages/cv2/cv2.abi3.so: undefined symbol: __atomic_store_8
Screenshot (349)
I got this error when run the command, Please help me !! thank you

@dannyhung1
Copy link

@khanhlvg Hi, I am all new to this so please help, when I tried to run the following:
python3 -m pip install --user --upgrade pip
python3 -m pip install --user virtualenv

I got this error:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.

If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.

For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.

If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.

For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

@ldiangelis
Copy link

@dannyhung1 the latest versions of Raspberry Pi OS only allow pip packages to be installed in virtual environments. As such, you have to install the virtual environment package via apt.

You can search for it to see if this has already been installed on your system: apt list --installed | grep python3-venv
If it is not installed run: sudo apt install python3-venv

Once installed, you can create your venv.

Details about the change as well as commands can be found on the Raspberry Pi website (Python on Raspberry Pi section). I am also including a link to the Adafruit tutorial that has a few additional commands that may be useful.
Python on Raspberry Pi: https://www.raspberrypi.com/documentation/computers/os.html#python-on-raspberry-pi
Python Virtual Environment Usage on Raspberry Pi: https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi/overview

@dannyhung1
Copy link

@ldiangelis Thank you so much. I was able to create the virtual environment successfully. But I tried to run the program in the virtual environment, I got the following error. I checked my Raspberry Pi camera using the "rpicam-hello" command and it looks ok. Any idea?

(mp) dhung@raspberrypi:/mediapipe/examples/object_detection/raspberry_pi $ python detect.py
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
[ WARN:0@11.985] global cap_v4l.cpp:1119 tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
ERROR: Unable to read from webcam. Please verify your webcam settings.
(mp) dhung@raspberrypi:
/mediapipe/examples/object_detection/raspberry_pi $ rpicam-hello
[82:20:14.238467034] [20237] INFO Camera camera_manager.cpp:284 libcamera v0.1.0+118-563cd78e
[82:20:14.294448017] [20240] WARN RPiSdn sdn.cpp:39 Using legacy SDN tuning - please consider moving SDN inside rpi.denoise
[82:20:14.297229511] [20240] INFO RPI vc4.cpp:444 Registered camera /base/soc/i2c0mux/i2c@1/imx708@1a to Unicam device /dev/media2 and ISP device /dev/media1
[82:20:14.297329416] [20240] INFO RPI pipeline_base.cpp:1142 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
Made X/EGL preview window
Mode selection for 2304:1296:12:P
SRGGB10_CSI2P,1536x864/0 - Score: 3400
SRGGB10_CSI2P,2304x1296/0 - Score: 1000
SRGGB10_CSI2P,4608x2592/0 - Score: 1900
Stream configuration adjusted
[82:20:14.788965979] [20237] INFO Camera camera.cpp:1183 configuring streams: (0) 2304x1296-YUV420 (1) 2304x1296-SBGGR10_CSI2P
[82:20:14.789691463] [20240] INFO RPI vc4.cpp:608 Sensor: /base/soc/i2c0mux/i2c@1/imx708@1a - Selected sensor format: 2304x1296-SBGGR10_1X10 - Selected unicam format: 2304x1296-pBAA
(mp) dhung@raspberrypi:~/mediapipe/examples/object_detection/raspberry_pi $

@ldiangelis
Copy link

@dannyhung1 here are a couple of basic methods to try. If the method does not work, make sure to revert any changes before moving to the next step.

  1. Try running: sudo python detect,py
  2. Pass the argument in the terminal when calling the script with: python detect.py --device 1
    • The default device is 0, but always good to test with another device number
  3. Replace cap.read() with cap.open() in the detect.py script
  4. Modify the config file to include start_x=1 and comment out camera_auto_detect=1
    • There probably is no additional support for the legacy camera stack so this may not work.
    • If this does work it is likely an issue with trying to use the V4L2 driver and the camera module with the script.

If none of these work, there are additional steps that you can try. Always good to start simple!

@dannyhung1
Copy link

@ldiangelis Thank you for your advice. I have tried all the steps that you suggested but unfortunately none of them works. Would appreciate further suggestions.

@Leadwood
Copy link

Hi! I've had an error when running python detect.py "No module Cv2 found" is it because I'm running the latest version of the OS (bookworm)? I changed the python version to 3.7.3 tho

@dannyhung1
Copy link

@khanhlvg Thanks for the help, detect.py is working fine now. But when i tried to use the model maker in colab notebook, i ran into errors. Any suggestions?

Preparing metadata (setup.py) ... done
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 106.6/106.6 kB 14.0 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.6/43.6 kB 5.7 MB/s eta 0:00:00
Preparing metadata (setup.py) ... done
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.2/5.2 MB 99.4 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 83.5 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 80.3/80.3 kB 11.8 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.8/2.8 MB 97.7 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 84.6/84.6 kB 10.7 MB/s eta 0:00:00
ERROR: Cannot install tflite-model-maker==0.1.2, tflite-model-maker==0.2.0, tflite-model-maker==0.2.1, tflite-model-maker==0.2.2, tflite-model-maker==0.2.3, tflite-model-maker==0.2.4, tflite-model-maker==0.2.5, tflite-model-maker==0.3.3, tflite-model-maker==0.3.4, tflite-model-maker==0.4.0, tflite-model-maker==0.4.1, tflite-model-maker==0.4.2 and tflite-model-maker==0.4.3 because these package versions have conflicting dependencies.
ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment