xxxxxxxxxx
72
// duplicated from https://editor.p5js.org/kylemcdonald/sketches/H1OoUd9h7
// Copyright (c) 2018 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
PoseNet example using p5.js
=== */
let video;
let poseNet;
let poses = [];
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(width, height);
// Create a new poseNet method with a single detection
poseNet = ml5.poseNet(video, modelReady);
// This sets up an event that fills the global variable "poses"
// with an array every time new poses are detected
poseNet.on('pose', function(results) {
poses = results;
});
// Hide the video element, and just show the canvas
video.hide();
}
function preload(){
imgWingL = loadImage("https://i.imgur.com/hovd2m2.gif");
imgWingR = loadImage("https://i.imgur.com/N6ak1S1.gif");
}
function modelReady() {
select('#status').html('Model Loaded');
}
function draw() {
image(video, 0, 0, width, height);
// We can call both functions to draw all keypoints and the skeletons
drawWings();
}
function drawWings() {
// 繪製肩膀圖片
for (let i = 0; i < poses.length; i++) {
let pose = poses[i].pose;
let leftShoulder = pose.keypoints[5];
let rightShoulder = pose.keypoints[6];
if (leftShoulder.score > 0.2) {
push();
translate(-80, -300);
image(imgWingL, leftShoulder.position.x, leftShoulder.position.y, 400, 500);
pop();
}
if (rightShoulder.score > 0.2) {
push();
translate(-300, -300);
image(imgWingR, rightShoulder.position.x, rightShoulder.position.y, 400, 500);
pop();
}
}
}