I want to detect the total amount from Google Firebase Vision kit.
What I did ?
I have all the text from the vision recognizer but unable to find a perfect algor
ML Kit is quite good at detecting text in an image, and extracting it from there. But it doesn't have any built-in "total amount" detection.
We needed this same functionality, for a talk we did at Google I/O building an expense tracker, and it turned out to be surprisingly tricky. We ended up using this very simple function, which finds the maximum number in the detected text:
exports.findTotal = function findTotal(detections) {
const regex = '^[$]?\s*(\\d+[\\.,]\\d{2})$';
const amounts = detections
.filter(text => text.description.match(regex))
.map(text => text.description.match(regex)[1])
.map(text => text.replace(',', '.'))
.map(text => Number(text))
.concat([0.0]);
return Math.max.apply(null, amounts);
}
Note though that, even in our limited testing, this didn't always work great. So your mileage may vary.
Full code for the project is on Github: https://github.com/puf/zero-to-app-expenses.