I'm attempting to create a networked program that draws using a specific part of the body using P5.js, and Posenet within ML5.js. I've successfully created the networked model which uses a live camera feed using createCapture(VIDEO) in setup as seen below
function setup() {
socket = io();
cnv = createCanvas(windowWidth,windowHeight);
capture = createCapture(VIDEO);
background(255);
PoseZero.init();
local = new Agent();
socket.emit('game-start', local.data)
socket.on('heartbeat', function(data){
world = data;
})
}
var localPreviousPose = {};
var remotePreviousPose = {};
function draw() {
image(capture, 0, 0, 220, 140);
(full code could be seen at https://glitch.com/edit/#!/makeyourmark-final)
However, for easier testing, I wanted to use a local video for the local side, I tried implementing it this way:
function setup() {
socket = io();
cnv = createCanvas(windowWidth, windowHeight);
capture = createVideo(
[
"https://cdn.glitch.com/e36b62ca-cf57-47f9-a16d-caa525a0d130%2FScreen%20Recording%202021-05-25%20at%2013.06.25.MOV?v=1621952189772"
],
onVideoLoad
);
background(255);
PoseZero.init();
local = new Agent();
socket.emit("game-start", local.data);
socket.on("heartbeat", function(data) {
world = data;
});
}
var localPreviousPose = {};
var remotePreviousPose = {};
function onVideoLoad() {
// The media will play as soon as it is loaded.
capture.autoplay();
capture.loop();
}
function draw() {
image(capture, 0, 0, 220, 140);
(full code could be seen at https://glitch.com/edit/#!/makeyourmark-video)
The error seems to be unrelated (e.g "Uncaught TypeError: Cannot read property 'autoplay' of undefined" when it is already autoplaying)
Is there any way I could fix this? Any insight/solution is appreciated.
Thank you!
There were are a couple of issues with your code, none of which were evident in what you included in your post:
.bind(this)
on callback function in pose.jsIn your init function on the class declared in pose.js you pass the onVideoLoad function as a callback to createVideo. However the onVideoLoad function references
this
. Any time you are going to usethis
from a function you pass as a callback, you need to call.bind(this)
:i
isn't declaredIn your draw() function in client.js you make several references to
world[i].data.pose
but you haven't declaredi
.pop()
without matching call topush()
In pose.js you have to following code:
Calling
loop()
as soon as your video is loaded is not going to work in all cases. Instead you should have some prompt for the users to click on to initiate playback.Working Remix
Here is remix that i think is "working" although I'm not 100% about your expected behavior: https://glitch.com/edit/#!/stackoverflow-67694586. I made a few changes which might not be what you want, such as making it create a single video element and passing that to PoseZero.init (instead of PoseZero having it's own video), and hiding the video element, since you're drawing it to the canvas anyway.
Conclusions
I think you should heed to following advice:
/* globals ... */
comments to your javascript files (full example below) that use p5.js so that glitch won't show errors where you are using p5.js globals without declaring them. That way where you do see errors they will be meaningful.Here is a sample globals comment for p5.js (may not be exhaustive):