diff --git a/plugins/objectdetector/package-lock.json b/plugins/objectdetector/package-lock.json index 6666d048d..bd822523d 100644 --- a/plugins/objectdetector/package-lock.json +++ b/plugins/objectdetector/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/objectdetector", - "version": "0.0.46", + "version": "0.0.47", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/objectdetector", - "version": "0.0.46", + "version": "0.0.47", "license": "Apache-2.0", "dependencies": { "@scrypted/common": "file:../../common", diff --git a/plugins/objectdetector/package.json b/plugins/objectdetector/package.json index 1db66b715..58968457e 100644 --- a/plugins/objectdetector/package.json +++ b/plugins/objectdetector/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/objectdetector", - "version": "0.0.46", + "version": "0.0.47", "description": "Scrypted Video Analysis Plugin. Installed alongside a detection service like OpenCV or TensorFlow.", "author": "Scrypted", "license": "Apache-2.0", diff --git a/plugins/objectdetector/src/denoise.ts b/plugins/objectdetector/src/denoise.ts index 8c55f4d58..dcf64dadf 100644 --- a/plugins/objectdetector/src/denoise.ts +++ b/plugins/objectdetector/src/denoise.ts @@ -2,7 +2,7 @@ export class DenoisedDetectionEntry { id: string; name: string; detection: T; - timeout?: NodeJS.Timeout; + lastSeen: number; } export interface DenoisedDetectionOptions { @@ -15,6 +15,7 @@ export function denoiseDetections(previousDetections: DenoisedDetectionEntry< currentDetections: DenoisedDetectionEntry[], options?: DenoisedDetectionOptions ) { + const now = Date.now(); const newAndExisting: DenoisedDetectionEntry[] = []; for (const cd of currentDetections) { let index = -1; @@ -28,24 +29,20 @@ export function denoiseDetections(previousDetections: DenoisedDetectionEntry< } else { const [found] = previousDetections.splice(index, 1); - if (found.timeout) { - clearTimeout(found.timeout); - found.timeout = undefined; - } + found.lastSeen = now; newAndExisting.push(cd); } } + const purgeTime = options?.timeout || 10000; // anything remaining in previousDetections at this point has possibly left the scene. - for (const cd of previousDetections) { - if (!cd.timeout) { - cd.timeout = setTimeout(() => { - const index = previousDetections.findIndex(check => check === cd); - if (index !== -1) - previousDetections.splice(index, 1); - options?.removed?.(cd); - }, options?.timeout || 10000); - } + for (const cd of previousDetections.slice()) { + if (now - cd.lastSeen < purgeTime) + continue; + const index = previousDetections.findIndex(check => check === cd); + if (index !== -1) + previousDetections.splice(index, 1); + options?.removed?.(cd); } // add all the detections that are pending removal diff --git a/plugins/objectdetector/src/main.ts b/plugins/objectdetector/src/main.ts index 3993850b4..c2ac30289 100644 --- a/plugins/objectdetector/src/main.ts +++ b/plugins/objectdetector/src/main.ts @@ -220,6 +220,8 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase !o.boundingBox || o?.zones?.length); + if (this.hasMotionType) { const found = detection.detections?.find(d => d.className === 'motion'); if (found) { @@ -263,35 +293,8 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase !o.boundingBox || o?.zones?.length); - } + if (!this.hasMotionType || this.motionAsObjects) this.onDeviceEvent(ScryptedInterface.ObjectDetector, detection); - } } async extendedObjectDetect() { @@ -324,6 +327,7 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase found.push(d),