简体   繁体   English

在特定时间后在p5.js中从视频捕获照片

[英]Capture photos from video after specific time in p5.js

var video;
var snapshots = [];
var readyCheck = false;
var button;

function setup() {
    createCanvas(800, 600);
    background(0);
    video = createCapture(VIDEO, ready);
    video.size(200, 150);
}

function ready() {
    readyCheck = true;
    console.log('work');
}

function draw() {
    var w = 200;
    var h = 150;
    var x = 0;
    var y = 0;

    if (readyCheck) {
        for (var i = 0; i < 100; i++) {
            // use setTimeout() to wait for 2 seconds
            setTimeout(function() {
                snapshots[i] = video.get();
                image(snapshots[i],x, y);
                x += w;
                if (x >= width) {
                    x = 0;
                    y += h;
                }
            }, 2000);
        }
    }
}

my purpose is taking pictures from the webcam after specific time. 我的目的是在特定时间后从网络摄像头拍照。 So I use the setTimeout() in JS. 所以我在JS中使用setTimeout() I expect pictures will appear on the canvas every 2 seconds in a row. 我希望图片每2秒连续出现在画布上一次。

when entering the for part, the code will wait 2 seconds and capture the image from webcam and display it. 输入for零件时,代码将等待2秒钟,并从网络摄像头捕获图像并显示它。

but my situation is that all the picture appear on the canvas at the same time. 但我的情况是所有图片都同时出现在画布上。

You need to take a step back and understand how the draw() function and the setTimeout() functions work. 您需要退后一步,了解draw()函数和setTimeout()函数如何工作。

  • The draw() function is automatically called 60 times per second. 每秒自动调用draw()函数60次。 You can adjust this by calling the frameRate() function or the noLoop() function. 您可以通过调用frameRate()函数或noLoop()函数来进行调整。 More info is available in the reference . 参考中提供了更多信息。

  • The setTimeout() function sets up a callback function that is automatically called after some duration, in your case 2 seconds. setTimeout()函数设置一个回调函数 ,该函数会在一段时间(在您的情况下为2秒)后自动调用。

So, what your code is doing is setting up 100 callback functions that will all fire in 2 seconds- and it's doing this 60 times per second! 因此,您的代码正在做的是设置100个回调函数,这些函数将在2秒内全部触发-并且每秒执行60次! So in 1 second, you'll have 6000 functions that will start firing 2 seconds later! 因此,在1秒内,您将拥有6000个功能,这些功能将在2秒后开始触发! This is almost definitely not what you want. 这几乎绝对不是您想要的。

P5.js already has its own timing mechanism in the draw() function that's called 60 times per second, so it seems a little weird to use the setTimeout() function inside P5.js code. P5.js在draw()函数中已经具有自己的计时机制,该机制称为每秒60次,因此在P5.js代码中使用setTimeout()函数似乎有点怪异。 Instead, you should probably set up your own timing using the frameCount variable or the millis() function. 相反,您可能应该使用frameCount变量或millis()函数来设置自己的计时。

Here's an example that shows a random color every second: 这是一个每秒显示随机颜色的示例:

 function setup() { createCanvas(200, 200); } function draw() { if(frameCount % 60 == 0){ background(random(256), random(256), random(256)); } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script> 

This code uses the frameCount variable and the % modulus operator to check whether 60 frames have passed, and if so, it sets the background to a random color. 该代码使用frameCount变量和% 模数运算符来检查是否已通过60帧,如果已通过,则将背景设置为随机颜色。 You'll want to do something similar. 您将想要做类似的事情。

Like I said above, more info about all of this can be found in the reference . 就像我上面所说的,有关所有这些的更多信息可以在参考资料中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM