Distance sensors are an excellent tool for a variety of Raspberry Pi projects ranging from things like robotic navigation to this automatic soap dispenser. But what if you aren’t able to get ahold of one? Thankfully, the Pi community is incredibly robust. The makers at Freedom Tech have developed a workaround solution that uses a webcam with OpenCV to calculate the distance of a given user's hand from the camera.
This project relies heavily on OpenCV and uses our favorite SBC—the Raspberry Pi. With the help of a webcam, the Pi is able to check images for whether or not a user's hand is visible. If it is, the Pi then determines the size of the hand and estimates the distance of said hand from the camera module before displaying the estimation through a video overlay.
If you haven’t heard of Freedom Tech before, you’re missing out on a huge catalog of Raspberry Pi projects. A quick look at the official Freedom Tech YouTube channel shows an extensive history of OpenCV-based Pi projects that use webcams and image recognition for an assortment of use cases. Previous developments include a virtual OpenCV keyboard, a parking space detector, and an attendance system that uses facial recognition to identify users.
FreedomTech has opted to use a Raspberry Pi 4 for this project which is arguably one of the better options to use when implementing any kind of machine learning project. That said, you could get away with using a Raspberry Pi 3 or even a CM4 module to complete the same task. A standard webcam is used for image capturing but there’s no reason users couldn’t opt for a camera module in its stead.
The meat and potatoes of this project lies in the code, and to our benefit, this project is open source. Everything you need to recreate this project is available in the tutorial video while all of the code used is provided at GitHub. The software is built around OpenCV, Tensorflow-Lite and MediaPipe. Everything is tied together by means of a custom Python script.
Stay on the Cutting Edge
Join the experts who read Tom's Hardware for the inside track on enthusiast PC tech news — and have for over 25 years. We'll send breaking news and in-depth reviews of CPUs, GPUs, AI, maker hardware and more straight to your inbox.
Ash Hill is a Freelance News and Features Writer at Tom's Hardware US. She manages the Pi projects of the month and much of our daily Raspberry Pi reporting.