← All projects
CodeWeb

Teaching MediaPipe a hammer

Year2023
Teaching MediaPipe a hammer

An R&D project carried out at the Bright agency: teaching MediaPipe to recognise an object it doesn’t know. The test object was a hammer, which is not among the base model’s classes. The result is a pipeline that goes from the real object to a lightweight model able to detect the hammer in real time in a smartphone browser, all in three hours.

Making the data instead of collecting it

To teach a model a new object, you have to show it hundreds of annotated images. Photographing them and drawing a box on each by hand takes days. We chose to make those images instead.

It all starts from a real hammer, which we modelled in 3D. The 3D model becomes an endless source of images: it can be shown from every angle, as many times as needed.

Annotations that make themselves

On every generated image, OpenCV detects the object’s outline and derives its bounding box. Those boxes feed the metadata files that training needs. Nobody draws a single rectangle by hand.

The full pipeline, from real object to deployed model.
The full pipeline, from real object to deployed model.

The psychedelic background

This is the project’s trick. Each hammer is placed on a saturated, random background. The model then cannot rely on the setting or the lighting: it learns the shape of the object, and only that.

I later found out that NVIDIA works the same way to train the machines in its warehouses. The principle has a name, domain randomization: making the environment so variable that only the object stays constant.

Transfer learning

Rather than starting from scratch, we took a lightweight, pre-trained MediaPipe model and retrained it on the new data with PyTorch. The model keeps what it can already see and only learns what it is missing.

Real time, in the browser

The result is a frugal model, deployed on a mobile website. It detects the hammer in real time in a smartphone browser, with no app to install.

From generating the dataset to deployment, training included, the whole pipeline fits in three hours. The hammer was only a test object: the same chain works for any object that can be modelled.

Technical specs

ContextR&D, Bright agency, 2023
Test objectA hammer, not among the base model’s classes
DataSynthetic images generated from a 3D model, on random backgrounds
AnnotationAutomatic: bounding boxes extracted with OpenCV
TrainingTransfer learning on a lightweight MediaPipe model, with PyTorch
TargetMobile browser, real-time detection
Pipeline timeAbout 3 hours, from dataset to deployment