opencv: startup and logging

This commit is contained in:
Koushik Dutta
2021-10-02 19:53:53 -07:00
parent 198a97a874
commit e6547c6e4a
3 changed files with 29 additions and 6 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/prebuffer-mixin",
"version": "0.1.48",
"version": "0.0.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/prebuffer-mixin",
"version": "0.1.48",
"version": "0.0.2",
"license": "Apache-2.0",
"dependencies": {
"@koush/opencv4nodejs": "^5.6.6",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/opencv",
"version": "0.0.1",
"version": "0.0.2",
"description": "Motion and Object Detection for VideoCameras.",
"author": "Scrypted",
"license": "Apache-2.0",

View File

@@ -17,6 +17,7 @@ const defaultArea = 2000;
class OpenCVMixin extends SettingsMixinDeviceBase<VideoCamera> implements MotionSensor, Settings {
area: number;
released = false;
constructor(mixinDevice: VideoCamera & Settings, mixinDeviceInterfaces: ScryptedInterface[], mixinDeviceState: { [key: string]: any }, providerNativeId: string) {
super(mixinDevice, mixinDeviceState, {
@@ -27,11 +28,23 @@ class OpenCVMixin extends SettingsMixinDeviceBase<VideoCamera> implements Motion
});
this.area = parseInt(localStorage.getItem('area')) || defaultArea;
if (this.mixinDevice.providedInterfaces.includes(ScryptedInterface.MotionSensor)) {
if (this.providedInterfaces.includes(ScryptedInterface.MotionSensor)) {
log.a(`${this.name} has a built in MotionSensor. OpenCV motion processing cancelled. Pleaes disable this extension.`);
return;
}
this.start();
// to prevent noisy startup/reload/shutdown, delay the prebuffer starting.
console.log(this.name, 'session starting in 10 seconds');
setTimeout(async () => {
try {
await this.start();
console.log(this.name, 'shut down gracefully');
}
catch (e) {
console.error(this.name, 'session unexpectedly terminated, restarting in 10 seconds');
await new Promise(resolve => setTimeout(resolve, 10000));
}
}, 10000);
}
async start() {
@@ -53,7 +66,7 @@ class OpenCVMixin extends SettingsMixinDeviceBase<VideoCamera> implements Motion
setTimeout(() => this.motionDetected = false, 10000);
}
this.motionDetected = false;
while (true) {
while (!this.released) {
let mat = await cap.readAsync();
if (this.motionDetected) {
@@ -118,12 +131,22 @@ class OpenCVMixin extends SettingsMixinDeviceBase<VideoCamera> implements Motion
}
release() {
this.released = true;
}
}
class OpenCVProvider extends AutoenableMixinProvider implements MixinProvider {
constructor(nativeId?: string) {
super(nativeId);
// trigger opencv.
for (const id of Object.keys(systemManager.getSystemState())) {
const device = systemManager.getDeviceById<VideoCamera>(id);
if (!device.mixins?.includes(this.id))
continue;
device.getVideoStreamOptions();
}
}
async canMixin(type: ScryptedDeviceType, interfaces: string[]): Promise<string[]> {