New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@camera.ui/camera-ui-wasm-motion

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@camera.ui/camera-ui-wasm-motion - npm Package Compare versions

Comparing version 0.0.11 to 0.0.12

4

dist/defaults.d.ts
export declare const DEFAULT_MOTION_AREA = 500;
export declare const DEFAULT_THRESHOLD = 20;
export declare const DEFAULT_THRESHOLD = 50;
export declare const DEFAULT_BLUR_RADIUS = 3;
export declare const DEFAULT_DILATION_SIZE = 15;
export declare const DEFAULT_DILATION_SIZE = 31;
export const DEFAULT_MOTION_AREA = 500;
export const DEFAULT_THRESHOLD = 20;
export const DEFAULT_THRESHOLD = 50;
export const DEFAULT_BLUR_RADIUS = 3;
export const DEFAULT_DILATION_SIZE = 15;
export const DEFAULT_DILATION_SIZE = 31;
//# sourceMappingURL=defaults.js.map

@@ -11,22 +11,12 @@ import type { CameraDevice, CameraStorage, LoggerService, PluginAPI } from '@camera.ui/types';

private restarting;
private wasmModule?;
private previousFramePtr;
private framePtr;
private boxesPtr;
private tempPtr;
private visitedPtr;
private queuePtr;
private memoryBuffer;
private initiated;
private wasmExports?;
constructor(cameraDevice: CameraDevice, api: PluginAPI, logger: LoggerService);
start(): Promise<void>;
private initiateWASM;
private setupMemory;
private startDetection;
close(): void;
restart(): Promise<void>;
private reset;
private startDetection;
private setupPreviousFrame;
private getMotionData;
private createCameraStorage;
private unpinSafe;
private collectSafe;
}
import { instantiate } from '@assemblyscript/loader';
import fs from 'node:fs';
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';

@@ -16,10 +16,4 @@ import { fileURLToPath } from 'node:url';

restarting = false;
wasmModule;
previousFramePtr = null;
framePtr = 0;
boxesPtr = 0;
tempPtr = 0;
visitedPtr = 0;
queuePtr = 0;
memoryBuffer;
initiated = false;
wasmExports;
constructor(cameraDevice, api, logger) {

@@ -44,18 +38,3 @@ this.api = api;

this.logger.log(`Starting motion detection for camera ${this.cameraDevice.name}`);
if (!this.wasmModule) {
const wasmPath = resolve(__dirname, './wasm/build/detector.wasm');
this.wasmModule = await instantiate(fs.readFileSync(wasmPath), {
console: {
log: (messagePtr) => {
this.logger.log(this.wasmModule.exports.__getString(messagePtr));
},
},
env: {
abort: (msg, file, line, column) => {
this.logger.warn(`WASM abort message: ${this.wasmModule.exports.__getString(msg)} at ${this.wasmModule.exports.__getString(file)}:${line}:${column}`);
},
},
});
this.setupMemory();
}
await this.initiateWASM();
this.startDetection().catch((error) => {

@@ -67,57 +46,36 @@ this.logger.error(this.cameraDevice.name, 'Error generating frames', error);

}
async initiateWASM() {
if (this.initiated) {
return;
}
this.initiated = true;
const wasmModule = await instantiate(readFileSync(resolve(__dirname, './wasm/build/detector.wasm')), {
console: {
log: (messagePtr) => {
console.log(this.wasmExports.__getString(messagePtr));
},
},
env: {
abort: (msgPtr, filePtr, line, column) => {
const msg = this.wasmExports.__getString(msgPtr);
const file = this.wasmExports.__getString(filePtr);
console.error(`Abort called at ${file}:${line}:${column}`, msg);
},
},
});
this.wasmExports = wasmModule.exports;
this.setupMemory();
}
setupMemory() {
if (!this.wasmModule) {
if (!this.wasmExports) {
throw new Error('Wasm not initiated');
}
this.memoryBuffer = new Uint8Array(this.wasmModule.exports.memory.buffer);
const width = this.cameraDevice.frameWorkerSettings.resolution;
const maxFrameLength = width * width;
this.tempPtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__new(maxFrameLength, this.wasmModule.exports.Uint8Array_ID.value));
this.visitedPtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__new(maxFrameLength, this.wasmModule.exports.Uint8Array_ID.value));
this.queuePtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__new(maxFrameLength * 4, this.wasmModule.exports.Uint8Array_ID.value));
// this.boxesPtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__new(maxFrameLength * 4, this.wasmModule.exports.Uint8Array_ID.value));
const [width, height] = this.cameraDevice.frameWorkerSettings.resolution.split('x').map((v) => parseInt(v, 10));
this.wasmExports.initialize(width, height);
}
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: [] });
async startDetection() {
if (!this.wasmExports) {
throw new Error('Wasm not initiated');
}
}
async restart() {
if (this.restarting) {
return;
}
this.restarting = true;
this.logger.log(`Restarting motion detection for camera ${this.cameraDevice.name}`);
this.close();
this.reset();
await new Promise((resolve) => setTimeout(resolve, 1500));
this.wasmModule = undefined;
if (this.cameraDevice.connected) {
await this.start();
}
this.restarting = false;
}
reset() {
// Stellen Sie sicher, dass alle Pointer freigegeben werden
this.unpinSafe(this.previousFramePtr);
this.unpinSafe(this.tempPtr);
this.unpinSafe(this.visitedPtr);
this.unpinSafe(this.queuePtr);
this.unpinSafe(this.boxesPtr);
this.unpinSafe(this.framePtr);
// Setzen Sie alle Pointer auf null zurück
this.previousFramePtr = null;
this.framePtr = 0;
this.boxesPtr = 0;
this.tempPtr = 0;
this.visitedPtr = 0;
this.queuePtr = 0;
this.collectSafe();
// Setzen Sie den memoryBuffer zurück
this.memoryBuffer = new Uint8Array();
}
async startDetection() {
const { detectMotion, getNumBoxes, __newArray, __getInt32Array, __pin, __unpin, __collect, Uint8Array_ID } = this.wasmExports;
for await (const frame of this.cameraDevice.getFrames()) {

@@ -139,9 +97,16 @@ if (this.closed || this.restarting) {

}
const inputPtr = __pin(__newArray(Uint8Array_ID.value, frameImage.image));
const resultPtr = detectMotion(inputPtr, threshold, blurRadius, dilationSize, motionArea);
const numBoxes = getNumBoxes();
const boxesArray = __getInt32Array(resultPtr);
const detections = [];
const dets = this.getMotionData(frameImage.image, frameImage.info.width, frameImage.info.height, motionArea, threshold, blurRadius, dilationSize);
for (const det of dets) {
for (let i = 0; i < numBoxes; i++) {
const x = boxesArray[i * 4];
const y = boxesArray[i * 4 + 1];
const w = boxesArray[i * 4 + 2];
const h = boxesArray[i * 4 + 3];
detections.push({
label: 'motion',
confidence: 1,
boundingBox: [det[0], det[1], det[2], det[3]],
boundingBox: [x, y, x + w, y + h],
inputWidth: frameImage.info.width,

@@ -153,52 +118,31 @@ inputHeight: frameImage.info.height,

}
__unpin(inputPtr);
__collect();
await this.cameraDevice.updateState('motion', { detections: detections }, frame);
}
}
setupPreviousFrame(frame, width, height, blurRadius) {
if (this.previousFramePtr !== null) {
this.unpinSafe(this.previousFramePtr);
this.collectSafe();
this.previousFramePtr = null;
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: [],
});
}
const frameLength = width * height;
const framePtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__newArrayBuffer(frame));
this.wasmModule.exports.stackBlur(framePtr, this.tempPtr, width, height, blurRadius);
this.previousFramePtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__newArrayBuffer(new Uint8Array(this.memoryBuffer.buffer, framePtr, frameLength)));
this.unpinSafe(framePtr);
}
getMotionData(frame, width, height, motionArea, threshold, blurRadius, dilationSize) {
if (!this.wasmModule) {
throw new Error('Wasm not initiated');
async restart() {
if (this.restarting) {
return;
}
try {
if (this.previousFramePtr === null) {
this.setupPreviousFrame(frame, width, height, blurRadius);
return [];
}
const frameLength = width * height;
this.framePtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__newArrayBuffer(frame));
this.boxesPtr = this.wasmModule.exports.__pin(this.wasmModule.exports.__new(frameLength, this.wasmModule.exports.Uint8Array_ID.value));
this.wasmModule.exports.stackBlur(this.framePtr, this.tempPtr, width, height, blurRadius);
this.wasmModule.exports.createMotionMask(this.previousFramePtr, this.framePtr, width, height, threshold);
this.wasmModule.exports.dilate(this.framePtr, this.tempPtr, width, height, dilationSize);
const numBoxes = this.wasmModule.exports.findBoundingBoxes(this.framePtr, this.visitedPtr, this.queuePtr, this.boxesPtr, width, height, motionArea);
const boxes = [];
const boxesArray = new Int32Array(this.wasmModule.exports.memory.buffer, this.boxesPtr, numBoxes * 4);
for (let i = 0; i < numBoxes; i++) {
const x = boxesArray[i * 4];
const y = boxesArray[i * 4 + 1];
const w = boxesArray[i * 4 + 2];
const h = boxesArray[i * 4 + 3];
boxes.push([x, y, x + w, y + h]);
}
this.unpinSafe(this.framePtr);
this.unpinSafe(this.boxesPtr);
this.collectSafe();
this.setupPreviousFrame(frame, width, height, blurRadius);
return boxes;
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();
}
catch (error) {
this.logger.error(this.cameraDevice.name, 'Error generating frames', error);
return [];
}
this.restarting = false;
}

@@ -274,21 +218,3 @@ createCameraStorage() {

}
unpinSafe(ptr) {
if (ptr !== null) {
try {
this.wasmModule.exports.__unpin(ptr);
}
catch {
//
}
}
}
collectSafe() {
try {
this.wasmModule.exports.__collect();
}
catch {
//
}
}
}
//# sourceMappingURL=detector.js.map

@@ -16,10 +16,31 @@ declare namespace __AdaptedExports {

/**
* assembly/index/resetStackPool
* assembly/index/initialize
* @param w `i32`
* @param h `i32`
*/
export function initialize(w: number, h: number): void;
/**
* assembly/index/detectMotion
* @param inputFrame `~lib/typedarray/Uint8Array`
* @param threshold `i32`
* @param radius `i32`
* @param dilationSize `i32`
* @param minArea `i32`
* @returns `~lib/typedarray/Int32Array`
*/
export function resetStackPool(radius: number): void;
export function detectMotion(inputFrame: Uint8Array, threshold: number, radius: number, dilationSize: number, minArea: number): Int32Array;
/**
* assembly/index/stackBlur
* assembly/index/getNumBoxes
* @returns `i32`
*/
export function getNumBoxes(): number;
/** assembly/index/Uint8Array_ID */
export const Uint8Array_ID: {
/** @type `u32` */
get value(): number
};
/**
* assembly/test/blurFrameSIMD
* @param framePtr `usize`
* @param tempPtr `usize`
* @param tempFramePtr `usize`
* @param width `i32`

@@ -29,38 +50,83 @@ * @param height `i32`

*/
export function stackBlur(framePtr: number, tempPtr: number, width: number, height: number, radius: number): void;
export function blurFrameSIMD(framePtr: number, tempFramePtr: number, width: number, height: number, radius: number): void;
/**
* assembly/index/createMotionMask
* @param frame1Ptr `usize`
* @param frame2Ptr `usize`
* assembly/test/detectMotionSIMD
* @param prevFramePtr `usize`
* @param currentFramePtr `usize`
* @param width `i32`
* @param height `i32`
* @param threshold `i32`
* @param motionMaskPtr `usize`
*/
export function createMotionMask(frame1Ptr: number, frame2Ptr: number, width: number, height: number, threshold: number): void;
export function detectMotionSIMD(prevFramePtr: number, currentFramePtr: number, width: number, height: number, threshold: number, motionMaskPtr: number): void;
/**
* assembly/index/dilate
* @param framePtr `usize`
* @param tempPtr `usize`
* assembly/test/dilateMotionMask
* @param motionMaskPtr `usize`
* @param tempMaskPtr `usize`
* @param width `i32`
* @param height `i32`
* @param size `i32`
* @param dilationSize `i32`
*/
export function dilate(framePtr: number, tempPtr: number, width: number, height: number, size: number): void;
export function dilateMotionMask(motionMaskPtr: number, tempMaskPtr: number, width: number, height: number, dilationSize: number): void;
/**
* assembly/index/findBoundingBoxes
* @param framePtr `usize`
* assembly/test/calculateBoundingBoxes
* @param motionMaskPtr `usize`
* @param visitedPtr `usize`
* @param queuePtr `usize`
* @param boxesPtr `usize`
* @param width `i32`
* @param height `i32`
* @param minArea `i32`
* @param motionArea `i32`
* @param boundingBoxesPtr `usize`
* @param stackXPtr `usize`
* @param stackYPtr `usize`
* @returns `i32`
*/
export function findBoundingBoxes(framePtr: number, visitedPtr: number, queuePtr: number, boxesPtr: number, width: number, height: number, minArea: number): number;
export function calculateBoundingBoxes(motionMaskPtr: number, visitedPtr: number, width: number, height: number, motionArea: number, boundingBoxesPtr: number, stackXPtr: number, stackYPtr: number): number;
/**
* assembly/index/detectMotion
* assembly/test/calculateBoundingBoxesSIMD
* @param motionMaskPtr `usize`
* @param visitedPtr `usize`
* @param width `i32`
* @param height `i32`
* @param motionArea `i32`
* @param boundingBoxesPtr `usize`
* @param stackXPtr `usize`
* @param stackYPtr `usize`
* @returns `i32`
*/
export function calculateBoundingBoxesSIMD(motionMaskPtr: number, visitedPtr: number, width: number, height: number, motionArea: number, boundingBoxesPtr: number, stackXPtr: number, stackYPtr: number): number;
/**
* assembly/test2/detectMotion
* @param prevFramePtr `usize`
* @param curFramePtr `usize`
* @param blurFrame1Ptr `usize`
* @param blurFrame2Ptr `usize`
* @param motionMaskPtr `usize`
* @param dilateMaskPtr `usize`
* @param boundingBoxesPtr `usize`
* @param width `i32`
* @param height `i32`
* @param blurRadius `i32`
* @param threshold `i32`
* @param dilationSize `i32`
* @param motionArea `i32`
* @returns `i32`
*/
export function detectMotion(prevFramePtr: number, curFramePtr: number, blurFrame1Ptr: number, blurFrame2Ptr: number, motionMaskPtr: number, dilateMaskPtr: number, boundingBoxesPtr: number, width: number, height: number, blurRadius: number, threshold: number, dilationSize: number, motionArea: number): number;
/**
* assembly/test3/blurFrame
* @param framePtr `usize`
* @param blurPtr `usize`
* @param width `i32`
* @param height `i32`
* @param radius `i32`
*/
export function blurFrame(framePtr: number, blurPtr: number, width: number, height: number, radius: number): void;
/**
* assembly/test3/detectMotion
* @param frame1Ptr `usize`
* @param frame2Ptr `usize`
* @param tempPtr `usize`
* @param blurredFrame1Ptr `usize`
* @param blurredFrame2Ptr `usize`
* @param maskPtr `usize`
* @param dilatePtr `usize`
* @param visitedPtr `usize`

@@ -77,13 +143,3 @@ * @param queuePtr `usize`

*/
export function detectMotion(frame1Ptr: number, frame2Ptr: number, tempPtr: number, visitedPtr: number, queuePtr: number, boxesPtr: number, width: number, height: number, blurRadius: number, dilationSize: number, motionThreshold: number, motionArea: number): number;
/** assembly/index/Uint8Array_ID */
export const Uint8Array_ID: {
/** @type `u32` */
get value(): number
};
/** assembly/index/Int32Array_ID */
export const Int32Array_ID: {
/** @type `u32` */
get value(): number
};
export function detectMotion(frame1Ptr: number, frame2Ptr: number, blurredFrame1Ptr: number, blurredFrame2Ptr: number, maskPtr: number, dilatePtr: number, visitedPtr: number, queuePtr: number, boxesPtr: number, width: number, height: number, blurRadius: number, dilationSize: number, motionThreshold: number, motionArea: number): number;
}

@@ -90,0 +146,0 @@ /** Instantiates the compiled WebAssembly module with the given imports. */

@@ -33,2 +33,7 @@ export async function instantiate(module, imports = {}) {

},
detectMotion(inputFrame, threshold, radius, dilationSize, minArea) {
// assembly/index/detectMotion(~lib/typedarray/Uint8Array, i32, i32, i32, i32) => ~lib/typedarray/Int32Array
inputFrame = __lowerTypedArray(Uint8Array, 4, 0, inputFrame) || __notnull();
return __liftTypedArray(Int32Array, exports.detectMotion(inputFrame, threshold, radius, dilationSize, minArea) >>> 0);
},
Uint8Array_ID: {

@@ -41,9 +46,2 @@ // assembly/index/Uint8Array_ID: u32

},
Int32Array_ID: {
// assembly/index/Int32Array_ID: u32
valueOf() { return this.value; },
get value() {
return exports.Int32Array_ID.value >>> 0;
}
},
}, exports);

@@ -70,6 +68,44 @@ function __liftString(pointer) {

}
function __liftTypedArray(constructor, pointer) {
if (!pointer) return null;
return new constructor(
memory.buffer,
__getU32(pointer + 4),
__dataview.getUint32(pointer + 8, true) / constructor.BYTES_PER_ELEMENT
).slice();
}
function __lowerTypedArray(constructor, id, align, values) {
if (values == null) return 0;
const
length = values.length,
buffer = exports.__pin(exports.__new(length << align, 1)) >>> 0,
header = exports.__new(12, id) >>> 0;
__setU32(header + 0, buffer);
__dataview.setUint32(header + 4, buffer, true);
__dataview.setUint32(header + 8, length << align, true);
new constructor(memory.buffer, buffer, length).set(values);
exports.__unpin(buffer);
return header;
}
function __notnull() {
throw TypeError("value must not be null");
}
let __dataview = new DataView(memory.buffer);
function __setU32(pointer, value) {
try {
__dataview.setUint32(pointer, value, true);
} catch {
__dataview = new DataView(memory.buffer);
__dataview.setUint32(pointer, value, true);
}
}
function __getU32(pointer) {
try {
return __dataview.getUint32(pointer, true);
} catch {
__dataview = new DataView(memory.buffer);
return __dataview.getUint32(pointer, true);
}
}
return adaptedExports;
}
{
"displayName": "WASM Motion",
"name": "@camera.ui/camera-ui-wasm-motion",
"version": "0.0.11",
"version": "0.0.12",
"description": "camera.ui wasm motion detection plugin",

@@ -19,12 +19,12 @@ "author": "seydx (https://github.com/seydx/camera.ui)",

"devDependencies": {
"@camera.ui/types": "^0.0.71",
"@camera.ui/types": "^0.0.76",
"@rushstack/eslint-patch": "^1.10.4",
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.5.4",
"@types/node": "^22.7.5",
"@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^8.5.0",
"@typescript-eslint/parser": "^8.5.0",
"assemblyscript": "^0.27.29",
"@typescript-eslint/eslint-plugin": "^8.8.1",
"@typescript-eslint/parser": "^8.8.1",
"assemblyscript": "^0.27.30",
"assemblyscript-prettier": "^3.0.1",
"concurrently": "^9.0.0",
"concurrently": "^9.0.1",
"copyfiles": "^2.4.1",

@@ -39,3 +39,3 @@ "eslint": "8.57.0",

"sharp": "^0.33.5",
"typescript": "^5.6.2",
"typescript": "^5.6.3",
"updates": "^16.4.0"

@@ -47,3 +47,3 @@ },

"engines": {
"camera.ui": ">=0.0.31-alpha.1",
"camera.ui": ">=0.0.34-alpha.1",
"node": ">=20.17.0"

@@ -68,5 +68,5 @@ },

"dependencies": {
"@assemblyscript/loader": "^0.27.29",
"@assemblyscript/loader": "^0.27.30",
"as-wasi": "^0.6.0"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc