Recognizing banned substances in the photo of a label despite OCR errors: deterministic, explainable fuzzy matching, on the phone
Find a substance in a misread text, on the phone, with no server, and know when to ask.
Date: 2022
In the VCScan app, agents scan a store’s products to spot those to withdraw from sale. For some, the barcode is not enough: the list of ingredients has to be read, in small print, on a curved, shiny bottle, hundreds of times a day.
Reading the text of a photo has become easy. The real challenge lies in everything surrounding it: OCR that may misread a letter, substance names spanning multiple words, a decision that determines whether a product is withdrawn from sale, non-chemist agents, and a cost that must remain near zero. I designed and built the algorithm that holds these constraints, in 2022, with no language model: it is deterministic, it explains itself word by word, and it runs on the phone.
The rule has three levels, from the cheapest to the dearest: the barcode answers for nearly every product, the photo for those left to check, and the agent only when the algorithm is in doubt.
The result
What was done
01
The framing
The photo straightened and cut down to the useful square before it is sent, for a surer reading and a lighter image.
Flutter · Dart
02
Reading the text
The words of the label and their position on the photo, from a text recognition service called directly from the app.
Google Cloud Vision
03
Finding the substances
An algorithm to find a substance several words long in a misread text, on the phone, and to explain every result.
Dart · Dice coefficient
04
The decision
A rule with two thresholds: the algorithm classifies by itself what it is sure of, and leaves the rest to the agent, photo in hand.
Flutter
05
The three levels
The barcode first, the photo next, the agent last: cost and time stay at their lowest, with no server to maintain.
Firebase
How it works
Scroll: the drawing follows.
01 / 05
Frame the label
When a product’s scan answers in orange, the agent turns the bottle over and frames the list of ingredients in a square on the screen. The app straightens the photo, then keeps only that square: the image sent is lighter, and the text on the rest of the packaging does not blur the reading.
02 / 05
Read the text of the photo
The app sends the square to a text recognition service, directly, with no server of its own in between. It receives the words in the order they are printed, each with its position on the photo. The reading is never perfect: on a curved bottle, an O becomes a zero, a comma sticks to a word.
03 / 05
Find a misread substance
Looking for the exact name would fail at the first wrong letter. So the algorithm puts the text and the list in the same form, with no capitals, accents or punctuation, then slides a window of one to seven words over the text, since a substance name is often several words long. Each run is compared with each substance by the pairs of letters they share: here, 24 pairs out of 27, a likeness of 0.91 despite the zero.
04 / 05
Decide alone, or ask
Above 0.8, the substance is kept. If every substance kept is above 0.97, the algorithm classifies the product for withdrawal without asking. In between, as here, the agent sees the photo, the words found framed where they are and the substance recognized, and decides. Every decision can be explained: a word, a substance, a score.
05 / 05
Spend as little as possible
We apply the fastest method first. The list of products is loaded on the phone, and the barcode answers at once, with no network, for nearly every product. The photo, the only step that is billed, is used only for the products to check, and the comparison runs on the phone. The back office updates the list of substances, with no new version of the app.
Specifications
- Date
- 2022
- Role
- Design and development of the algorithm and the app
- Client
- Confidential client
- Built with
- Dart · Google Cloud Vision