From 233d56a36e09b6c408131ce64461e449021b8811 Mon Sep 17 00:00:00 2001 From: David Jakowenko Date: Mon, 6 Sep 2021 22:09:28 -0400 Subject: [PATCH] feat: ability to include base64 encoded string in API results and MQTT messages (#52) --- README.md | 1 + api/src/constants/defaults.js | 1 + api/src/util/process.util.js | 14 ++++++++++++-- api/src/util/recognize.util.js | 3 +++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5cb9b936..eb36f54c 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,7 @@ detectors: | objects.face.min_area_match | `10000` | Minimum area in pixels to consider a result a match | | save.matches | `true` | Save match images | | save.unknown | `true` | Save unknown images | +| save.base64 | `false` | Include Base64 encoded string in API results and MQTT messages. Options include: `true`, `false`, or `box`. | | purge.matches | `168` | Hours to keep match images until they are deleted | | purge.unknown | `8` | Hours to keep unknown images until they are deleted | | frigate.url | | Base URL for Frigate | diff --git a/api/src/constants/defaults.js b/api/src/constants/defaults.js index 49f7889b..675e8ee1 100644 --- a/api/src/constants/defaults.js +++ b/api/src/constants/defaults.js @@ -10,6 +10,7 @@ module.exports = { save: { matches: true, unknown: true, + base64: false, }, objects: { face: { min_area_match: 10000 }, diff --git a/api/src/util/process.util.js b/api/src/util/process.util.js index 0d6f692f..c4bc0b84 100644 --- a/api/src/util/process.util.js +++ b/api/src/util/process.util.js @@ -7,7 +7,7 @@ const database = require('./db.util'); const mask = require('./mask.image.util'); const sleep = require('./sleep.util'); const { recognize, normalize } = require('./detectors/actions'); -const { FRIGATE, STORAGE, SAVE } = require('../constants'); +const { SERVER, FRIGATE, STORAGE, SAVE } = require('../constants'); const DETECTORS = require('../constants/config').detectors(); module.exports.polling = async (event, { retries, id, type, url, breakMatch, MATCH_IDS }) => { @@ -53,8 +53,18 @@ module.exports.polling = async (event, { retries, id, type, url, breakMatch, MAT .length; const totalFaces = results.flatMap((obj) => obj.results.filter((item) => item)).length; - if (foundMatch || (SAVE.UNKNOWN && totalFaces > 0)) + if (foundMatch || (SAVE.UNKNOWN && totalFaces > 0)) { await this.save(event, results, filename, maskBuffer?.visible ? tmp.mask : tmp.source); + if (SAVE.BASE64 === true || SAVE.BASE64 === 'box') { + const base64 = + SAVE.BASE64 === true + ? stream + : await this.stream( + `http://0.0.0.0:${SERVER.PORT}/api/storage/matches/${filename}?box=true` + ); + results.forEach((result) => (result.base64 = base64.toString('base64'))); + } + } allResults.push(...results); diff --git a/api/src/util/recognize.util.js b/api/src/util/recognize.util.js index 8f44e592..18c28b0c 100644 --- a/api/src/util/recognize.util.js +++ b/api/src/util/recognize.util.js @@ -19,6 +19,7 @@ module.exports.normalize = (results = []) => { duration: attempt.duration, detector: attempt.detector, filename: attempt.filename, + base64: attempt.base64, }; } }); @@ -34,6 +35,7 @@ module.exports.normalize = (results = []) => { duration: attempt.duration, detector: attempt.detector, filename: attempt.filename, + base64: attempt.base64, }; } }); @@ -49,6 +51,7 @@ module.exports.normalize = (results = []) => { duration: attempt.duration, detector: attempt.detector, filename: attempt.filename, + base64: attempt.base64, }; } });