Skip to content

Commit

Permalink
videoanalysis: fix broken concave polygon math, optimize for intersec…
Browse files Browse the repository at this point in the history
…t boolean rather than intersect polygon
  • Loading branch information
koush committed Jan 8, 2025
1 parent 892b978 commit f8a8ed4
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 196 deletions.
198 changes: 51 additions & 147 deletions plugins/objectdetector/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions plugins/objectdetector/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/objectdetector",
"version": "0.1.62",
"version": "0.1.63",
"description": "Scrypted Video Analysis Plugin. Installed alongside a detection service like OpenCV or TensorFlow.",
"author": "Scrypted",
"license": "Apache-2.0",
Expand Down Expand Up @@ -46,12 +46,9 @@
},
"dependencies": {
"@scrypted/common": "file:../../common",
"@scrypted/sdk": "file:../../sdk",
"polygon-clipping": "^0.15.7",
"semver": "^7.5.4"
"@scrypted/sdk": "file:../../sdk"
},
"devDependencies": {
"@types/node": "^20.11.0",
"@types/semver": "^7.5.6"
"@types/node": "^20.11.0"
}
}
13 changes: 5 additions & 8 deletions plugins/objectdetector/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import crypto from 'crypto';
import { AutoenableMixinProvider } from "../../../common/src/autoenable-mixin-provider";
import { SettingsMixinDeviceBase } from "../../../common/src/settings-mixin";
import { FFmpegVideoFrameGenerator } from './ffmpeg-videoframes';
import { fixLegacyClipPath, insidePolygon, normalizeBoxToClipPath, polygonOverlap } from './polygon';
import { fixLegacyClipPath, normalizeBox, polygonContainsBoundingBox, polygonIntersectsBoundingBox } from './polygon';
import { SMART_MOTIONSENSOR_PREFIX, SmartMotionSensor } from './smart-motionsensor';
import { SMART_OCCUPANCYSENSOR_PREFIX, SmartOccupancySensor } from './smart-occupancy-sensor';
import { getAllDevices, safeParseJson } from './util';
Expand Down Expand Up @@ -545,7 +545,7 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
if (!o.boundingBox)
continue;

const box = normalizeBoxToClipPath(o.boundingBox, detection.inputDimensions);
const box = normalizeBox(o.boundingBox, detection.inputDimensions);

let included: boolean;
// need a way to explicitly include package zone.
Expand All @@ -572,13 +572,10 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera

let match = false;
if (zoneInfo?.type === 'Contain') {
match = insidePolygon(box[0] as Point, zoneValue) &&
insidePolygon(box[1], zoneValue) &&
insidePolygon(box[2], zoneValue) &&
insidePolygon(box[3], zoneValue);
match = polygonContainsBoundingBox(zoneValue, box);
}
else {
match = polygonOverlap(box, zoneValue);
match = polygonIntersectsBoundingBox(zoneValue, box);
}

const classes = zoneInfo?.classes?.length ? zoneInfo?.classes : this.model?.classes || [];
Expand All @@ -604,7 +601,7 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
// prevents errant motion from the on screen time changing every second.
if (this.hasMotionType && included === undefined) {
const defaultInclusionZone: ClipPath = [[0, .1], [1, .1], [1, .9], [0, .9]];
included = polygonOverlap(box, defaultInclusionZone);
included = polygonIntersectsBoundingBox(defaultInclusionZone, box);
}

// if there are inclusion zones and this object
Expand Down
Loading

0 comments on commit f8a8ed4

Please sign in to comment.