Skip to content

Commit

Permalink
videoanalysis: generator cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Mar 16, 2023
1 parent 00cf987 commit 8bbc3d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
4 changes: 2 additions & 2 deletions plugins/objectdetector/package-lock.json

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

2 changes: 1 addition & 1 deletion plugins/objectdetector/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/objectdetector",
"version": "0.0.104",
"version": "0.0.105",
"description": "Scrypted Video Analysis Plugin. Installed alongside a detection service like OpenCV or TensorFlow.",
"author": "Scrypted",
"license": "Apache-2.0",
Expand Down
55 changes: 31 additions & 24 deletions plugins/objectdetector/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,32 +487,39 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
this.analyzeStop = Date.now() + this.getDetectionDuration();

const newPipeline = this.newPipeline;
let generator: AsyncGenerator<VideoFrame & MediaObject>;
let generator : () => Promise<AsyncGenerator<VideoFrame & MediaObject>>;
if (newPipeline === 'Snapshot') {
const self = this;
generator = (async function* gen() {
while (true) {
const now = Date.now();
const sleeper = async () => {
const diff = now + 1100 - Date.now();
if (diff > 0)
await sleep(diff);
};
let image: MediaObject & VideoFrame;
try {
const mo = await self.cameraDevice.takePicture({
reason: 'event',
});
image = await sdk.mediaManager.convertMediaObject(mo, ScryptedMimeTypes.Image);
}
catch (e) {
self.console.error('Video analysis snapshot failed. Will retry in a moment.');
generator = async () => (async function* gen() {
try {
while (self.detectorRunning) {
const now = Date.now();
const sleeper = async () => {
const diff = now + 1100 - Date.now();
if (diff > 0)
await sleep(diff);
};
let image: MediaObject & VideoFrame;
try {
const mo = await self.cameraDevice.takePicture({
reason: 'event',
});
image = await sdk.mediaManager.convertMediaObject(mo, ScryptedMimeTypes.Image);
}
catch (e) {
self.console.error('Video analysis snapshot failed. Will retry in a moment.');
await sleeper();
continue;
}

// self.console.log('yield')
yield image;
// self.console.log('done yield')
await sleeper();
continue;
}

yield image;
await sleeper();
}
finally {
self.console.log('Snapshot generation finished.');
}
})();
}
Expand All @@ -526,14 +533,14 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
audio: null,
});

generator = await videoFrameGenerator.generateVideoFrames(stream);
generator = async () => videoFrameGenerator.generateVideoFrames(stream);
}

try {
const start = Date.now();
let detections = 0;
for await (const detected
of await this.objectDetection.generateObjectDetections(generator, {
of await this.objectDetection.generateObjectDetections(await generator(), {
settings: this.getCurrentSettings(),
})) {
if (!this.detectorRunning) {
Expand Down

0 comments on commit 8bbc3d5

Please sign in to comment.