Skip to content

Commit

Permalink
common: shuffle some polygon code
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Dec 7, 2023
1 parent c95248f commit cd298f7
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 2 additions & 14 deletions plugins/objectdetector/src/main.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import { AutoenableMixinProvider } from "../../../common/src/autoenable-mixin-pr
import { SettingsMixinDeviceBase } from "../../../common/src/settings-mixin";
import { FFmpegVideoFrameGenerator } from './ffmpeg-videoframes';
import { getMaxConcurrentObjectDetectionSessions } from './performance-profile';
import { insidePolygon, polygonOverlap } from './polygon';
import { insidePolygon, normalizeBox, polygonOverlap } from './polygon';
import { serverSupportsMixinEventMasking } from './server-version';
import { SMART_MOTIONSENSOR_PREFIX, SmartMotionSensor, createObjectDetectorStorageSetting } from './smart-motionsensor';
import { getAllDevices, safeParseJson } from './util';
@@ -461,18 +461,6 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
}
}

normalizeBox(boundingBox: [number, number, number, number], inputDimensions: [number, number]): [Point, Point, Point, Point] {
let [x, y, width, height] = boundingBox;
let x2 = x + width;
let y2 = y + height;
// the zones are point paths in percentage format
x = x * 100 / inputDimensions[0];
y = y * 100 / inputDimensions[1];
x2 = x2 * 100 / inputDimensions[0];
y2 = y2 * 100 / inputDimensions[1];
return [[x, y], [x2, y], [x2, y2], [x, y2]];
}

applyZones(detection: ObjectsDetected) {
// determine zones of the objects, if configured.
if (!detection.detections)
@@ -483,7 +471,7 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
continue;

o.zones = []
const box = this.normalizeBox(o.boundingBox, detection.inputDimensions);
const box = normalizeBox(o.boundingBox, detection.inputDimensions);

let included: boolean;
for (const [zone, zoneValue] of Object.entries(this.zones)) {
12 changes: 12 additions & 0 deletions plugins/objectdetector/src/polygon.ts
Original file line number Diff line number Diff line change
@@ -13,3 +13,15 @@ export function insidePolygon(point: Point, polygon: Point[]) {
const intersect = polygonClipping.intersection([polygon], [[point, [point[0] + 1, point[1]], [point[0] + 1, point[1] + 1]]]);
return !!intersect.length;
}

export function normalizeBox(boundingBox: [number, number, number, number], inputDimensions: [number, number]): [Point, Point, Point, Point] {
let [x, y, width, height] = boundingBox;
let x2 = x + width;
let y2 = y + height;
// the zones are point paths in percentage format
x = x * 100 / inputDimensions[0];
y = y * 100 / inputDimensions[1];
x2 = x2 * 100 / inputDimensions[0];
y2 = y2 * 100 / inputDimensions[1];
return [[x, y], [x2, y], [x2, y2], [x, y2]];
}

0 comments on commit cd298f7

Please sign in to comment.