@camera.ui/camera-ui-wasm-motion
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -1,4 +0,4 @@ | ||
export declare const DEFAULT_MOTION_AREA = 500; | ||
export declare const DEFAULT_MOTION_AREA = 400; | ||
export declare const DEFAULT_THRESHOLD = 50; | ||
export declare const DEFAULT_BLUR_RADIUS = 3; | ||
export declare const DEFAULT_DILATION_SIZE = 31; | ||
export declare const DEFAULT_BLUR_RADIUS = 5; | ||
export declare const DEFAULT_DILATION_SIZE = 5; |
@@ -1,5 +0,5 @@ | ||
export const DEFAULT_MOTION_AREA = 500; | ||
export const DEFAULT_MOTION_AREA = 400; | ||
export const DEFAULT_THRESHOLD = 50; | ||
export const DEFAULT_BLUR_RADIUS = 3; | ||
export const DEFAULT_DILATION_SIZE = 31; | ||
export const DEFAULT_BLUR_RADIUS = 5; | ||
export const DEFAULT_DILATION_SIZE = 5; | ||
//# sourceMappingURL=defaults.js.map |
@@ -1,2 +0,2 @@ | ||
import type { CameraDevice, CameraStorage, LoggerService, PluginAPI } from '@camera.ui/types'; | ||
import type { CameraDevice, CameraStorage, PluginAPI } from '@camera.ui/types'; | ||
import type { CameraStorageValues } from './types.js'; | ||
@@ -7,16 +7,17 @@ export declare class CameraDetector { | ||
private cameraDevice; | ||
private logger; | ||
private cameraLogger; | ||
private started; | ||
private closed; | ||
private restarting; | ||
private initiated; | ||
private wasmExports?; | ||
constructor(cameraDevice: CameraDevice, api: PluginAPI, logger: LoggerService); | ||
constructor(cameraDevice: CameraDevice, api: PluginAPI); | ||
start(): Promise<void>; | ||
close(): void; | ||
private initiateWASM; | ||
private setupMemory; | ||
private startDetection; | ||
close(): void; | ||
restart(): Promise<void>; | ||
private restart; | ||
private detect; | ||
private defaultSettings; | ||
private createCameraStorage; | ||
} |
@@ -12,12 +12,11 @@ import { instantiate } from '@assemblyscript/loader'; | ||
cameraDevice; | ||
logger; | ||
cameraLogger; | ||
started = false; | ||
closed = false; | ||
restarting = false; | ||
initiated = false; | ||
wasmExports; | ||
constructor(cameraDevice, api, logger) { | ||
constructor(cameraDevice, api) { | ||
this.api = api; | ||
this.cameraDevice = cameraDevice; | ||
this.logger = logger; | ||
this.cameraLogger = cameraDevice.logger; | ||
this.cameraStorage = this.createCameraStorage(); | ||
@@ -37,15 +36,19 @@ this.cameraDevice.onConnected.subscribe((connected) => { | ||
this.closed = false; | ||
this.logger.log(`Starting motion detection for camera ${this.cameraDevice.name}`); | ||
this.cameraLogger.log(this.cameraDevice.name, 'Starting motion detection'); | ||
await this.initiateWASM(); | ||
this.startDetection().catch((error) => { | ||
this.logger.error(this.cameraDevice.name, 'Error generating frames', error); | ||
this.restart(); | ||
}); | ||
await this.startDetection(); | ||
} | ||
} | ||
close() { | ||
if (this.started && !this.closed) { | ||
this.started = false; | ||
this.closed = true; | ||
this.cameraLogger.log(this.cameraDevice.name, 'Stopping motion detection'); | ||
// reset wasm? | ||
} | ||
} | ||
async initiateWASM() { | ||
if (this.initiated) { | ||
if (this.wasmExports) { | ||
return; | ||
} | ||
this.initiated = true; | ||
const wasmModule = await instantiate(readFileSync(resolve(__dirname, './wasm/build/detector.wasm')), { | ||
@@ -76,2 +79,26 @@ console: { | ||
async startDetection() { | ||
try { | ||
await this.detect(); | ||
} | ||
catch (error) { | ||
this.cameraLogger.error(this.cameraDevice.name, 'Error generating frames', error); | ||
await this.restart(); | ||
} | ||
} | ||
async restart() { | ||
if (this.restarting || this.closed) { | ||
return; | ||
} | ||
this.restarting = true; | ||
this.cameraLogger.log(this.cameraDevice.name, 'Restarting motion detection in 2s...'); | ||
await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
this.restarting = false; | ||
if (this.cameraDevice.connected && !this.closed) { | ||
await this.startDetection(); | ||
} | ||
else { | ||
this.cameraLogger.log(this.cameraDevice.name, 'Camera not connected, not restarting'); | ||
} | ||
} | ||
async detect() { | ||
if (!this.wasmExports) { | ||
@@ -93,2 +120,6 @@ throw new Error('Wasm not initiated'); | ||
}, | ||
resize: { | ||
width: 640, | ||
height: 0, | ||
}, | ||
}); | ||
@@ -123,26 +154,15 @@ if (this.closed || this.restarting) { | ||
} | ||
close() { | ||
if (this.started && !this.closed) { | ||
this.started = false; | ||
this.closed = true; | ||
this.logger.log(`Stopping motion detection for camera ${this.cameraDevice.name}`); | ||
this.cameraDevice.updateState('motion', { | ||
state: false, | ||
detections: [], | ||
}); | ||
defaultSettings() { | ||
if (this.cameraStorage.values.area !== DEFAULT_MOTION_AREA) { | ||
this.cameraStorage.setValue('area', DEFAULT_MOTION_AREA); | ||
} | ||
} | ||
async restart() { | ||
if (this.restarting) { | ||
return; | ||
if (this.cameraStorage.values.threshold !== DEFAULT_THRESHOLD) { | ||
this.cameraStorage.setValue('threshold', DEFAULT_THRESHOLD); | ||
} | ||
this.restarting = true; | ||
this.logger.log(`Restarting motion detection for camera ${this.cameraDevice.name}`); | ||
this.close(); | ||
await new Promise((resolve) => setTimeout(resolve, 1500)); | ||
this.initiated = false; | ||
if (this.cameraDevice.connected) { | ||
await this.start(); | ||
if (this.cameraStorage.values.blurRadius !== DEFAULT_BLUR_RADIUS) { | ||
this.cameraStorage.setValue('blurRadius', DEFAULT_BLUR_RADIUS); | ||
} | ||
this.restarting = false; | ||
if (this.cameraStorage.values.dilationSize !== DEFAULT_DILATION_SIZE) { | ||
this.cameraStorage.setValue('dilationSize', DEFAULT_DILATION_SIZE); | ||
} | ||
} | ||
@@ -163,4 +183,3 @@ createCameraStorage() { | ||
onSet: async (newValue, oldValue) => { | ||
this.logger.log(this.cameraDevice.name, `Motion area changed from ${oldValue} to ${newValue}`); | ||
this.restart(); | ||
this.cameraLogger.log(this.cameraDevice.name, `Motion area changed from ${oldValue} to ${newValue}`); | ||
}, | ||
@@ -180,4 +199,3 @@ }, | ||
onSet: async (newValue, oldValue) => { | ||
this.logger.log(this.cameraDevice.name, `Motion threshold changed from ${oldValue} to ${newValue}`); | ||
this.restart(); | ||
this.cameraLogger.log(this.cameraDevice.name, `Motion threshold changed from ${oldValue} to ${newValue}`); | ||
}, | ||
@@ -197,4 +215,3 @@ }, | ||
onSet: async (newValue, oldValue) => { | ||
this.logger.log(this.cameraDevice.name, `Motion blur radius changed from ${oldValue} to ${newValue}`); | ||
this.restart(); | ||
this.cameraLogger.log(this.cameraDevice.name, `Motion blur radius changed from ${oldValue} to ${newValue}`); | ||
}, | ||
@@ -214,6 +231,14 @@ }, | ||
onSet: async (newValue, oldValue) => { | ||
this.logger.log(this.cameraDevice.name, `Motion dilation size changed from ${oldValue} to ${newValue}`); | ||
this.restart(); | ||
this.cameraLogger.log(this.cameraDevice.name, `Motion dilation size changed from ${oldValue} to ${newValue}`); | ||
}, | ||
}, | ||
reset: { | ||
type: 'button', | ||
title: 'Default Settings', | ||
key: 'default', | ||
description: 'Reset motion detection settings to default', | ||
onSet: async () => { | ||
this.defaultSettings(); | ||
}, | ||
}, | ||
}); | ||
@@ -220,0 +245,0 @@ return cameraStorage; |
@@ -49,3 +49,3 @@ import { CameraDetector } from './detector.js'; | ||
createDetector(cameraDevice) { | ||
const detector = new CameraDetector(cameraDevice, this.api, this.logger); | ||
const detector = new CameraDetector(cameraDevice, this.api); | ||
this.detectors.set(cameraDevice.id, detector); | ||
@@ -52,0 +52,0 @@ } |
{ | ||
"displayName": "WASM Motion", | ||
"name": "@camera.ui/camera-ui-wasm-motion", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "camera.ui wasm motion detection plugin", | ||
@@ -18,10 +18,14 @@ "author": "seydx (https://github.com/seydx/camera.ui)", | ||
}, | ||
"dependencies": { | ||
"@assemblyscript/loader": "^0.27.30", | ||
"as-wasi": "^0.6.0" | ||
}, | ||
"devDependencies": { | ||
"@camera.ui/types": "^0.0.76", | ||
"@camera.ui/types": "^0.0.77", | ||
"@rushstack/eslint-patch": "^1.10.4", | ||
"@types/fs-extra": "^11.0.4", | ||
"@types/node": "^22.7.5", | ||
"@types/node": "^22.7.6", | ||
"@types/ws": "^8.5.12", | ||
"@typescript-eslint/eslint-plugin": "^8.8.1", | ||
"@typescript-eslint/parser": "^8.8.1", | ||
"@typescript-eslint/eslint-plugin": "^8.10.0", | ||
"@typescript-eslint/parser": "^8.10.0", | ||
"assemblyscript": "^0.27.30", | ||
@@ -46,3 +50,3 @@ "assemblyscript-prettier": "^3.0.1", | ||
"engines": { | ||
"camera.ui": ">=0.0.34-alpha.1", | ||
"camera.ui": ">=0.0.34-alpha.2", | ||
"node": ">=20.17.0" | ||
@@ -64,8 +68,7 @@ }, | ||
"extension": "motionDetection", | ||
"dependencies": [] | ||
}, | ||
"dependencies": { | ||
"@assemblyscript/loader": "^0.27.30", | ||
"as-wasi": "^0.6.0" | ||
"dependencies": [], | ||
"options": { | ||
"extendedMotionDetection": false | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
49342
607