xxxxxxxxxx
243
import {
//HandLandmarker,
PoseLandmarker,
FilesetResolver
} from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0";
//let handLandmarker;
let poseLandmarker;
let video;
let img = null;
let vw, vh;
let person = false;
let circle_radius = 20;
async function setup() {
createCanvas(1280, 720);
let constraints = {
video: {
mandatory: {
minWidth: 1280,
minHeight: 720
},
optional: [{ maxFrameRate: 25 }]
},
audio: false
};
video = createCapture(constraints);
video.hide();
video.id("webcam");
print(video);
//handLandmarker = await initHandLandmarker();
poseLandmarker = await createPoseLandmarker();
}
function draw() {
//scale(0.5);
background(0);
//tint(255, 50);
image(video, 0, 0, width, height);
if (poseLandmarker != null) {
let poseLandmarkerResult = poseLandmarker.detectForVideo(video.canvas, millis());
if (poseLandmarkerResult.landmarks.length > 0) {
if (person == false) {
print("person detected");
circle_radius = 20;
}
person = true;
if (circle_radius < 270)
circle_radius += 1;
noFill();
stroke(0);
drawBodySkeleton(poseLandmarkerResult.landmarks[0]);
let nose = poseLandmarkerResult.landmarks[0][0];
let mouth = poseLandmarkerResult.landmarks[0][10];
fill(128,150);
stroke(0);
strokeWeight(5);
ellipse(nose.x*vw, nose.y*vh-(mouth.y*vh-nose.y*vh)*5-circle_radius/2, circle_radius*1.7, circle_radius*1.7);
strokeWeight(1+circle_radius/100);
noFill();
arc(nose.x*vw, nose.y*vh-(mouth.y*vh-nose.y*vh)*5-circle_radius/2, circle_radius*0.75*1.84, circle_radius*0.75*1.84, -0.8, 0);
arc(nose.x*vw, nose.y*vh-(mouth.y*vh-nose.y*vh)*5-circle_radius/2, circle_radius*0.75*1.8, circle_radius*0.75*1.8, -0.8, 0);
arc(nose.x*vw, nose.y*vh-(mouth.y*vh-nose.y*vh)*5-circle_radius/2, circle_radius*0.75*1.82, circle_radius*0.75*1.82, -0.8, 0);
fill(0);
textSize(circle_radius/4*1.4);
textAlign(CENTER, CENTER);
text(int(circle_radius) + " kg", nose.x*vw, nose.y*vh-(mouth.y*vh-nose.y*vh)*5-circle_radius/2);
}
else {
person = false;
}
}
}
function drawBodySkeleton(pose) {
vw = video.width;
vh = video.height;
// right head
line(pose[0].x*vw, pose[0].y*vh, pose[1].x*vw, pose[1].y*vh);
line(pose[1].x*vw, pose[1].y*vh, pose[2].x*vw, pose[2].y*vh);
line(pose[2].x*vw, pose[2].y*vh, pose[3].x*vw, pose[3].y*vh);
line(pose[3].x*vw, pose[3].y*vh, pose[7].x*vw, pose[7].y*vh);
// left head
line(pose[0].x*vw, pose[0].y*vh, pose[4].x*vw, pose[4].y*vh);
line(pose[4].x*vw, pose[4].y*vh, pose[5].x*vw, pose[5].y*vh);
line(pose[5].x*vw, pose[5].y*vh, pose[6].x*vw, pose[6].y*vh);
line(pose[6].x*vw, pose[6].y*vh, pose[8].x*vw, pose[8].y*vh);
// mouth
//line(pose[9].x*vw, pose[9].y*vh, pose[10].x*vw, pose[10].y*vh);
// toso
line(pose[11].x*vw, pose[11].y*vh, pose[12].x*vw, pose[12].y*vh);
line(pose[12].x*vw, pose[12].y*vh, pose[24].x*vw, pose[24].y*vh);
line(pose[24].x*vw, pose[24].y*vh, pose[23].x*vw, pose[23].y*vh);
line(pose[23].x*vw, pose[23].y*vh, pose[11].x*vw, pose[11].y*vh);
// right arm
line(pose[11].x*vw, pose[11].y*vh, pose[13].x*vw, pose[13].y*vh);
line(pose[13].x*vw, pose[13].y*vh, pose[15].x*vw, pose[15].y*vh);
line(pose[15].x*vw, pose[15].y*vh, pose[19].x*vw, pose[19].y*vh);
// ...
// left arm
line(pose[12].x*vw, pose[12].y*vh, pose[14].x*vw, pose[14].y*vh);
line(pose[14].x*vw, pose[14].y*vh, pose[16].x*vw, pose[16].y*vh);
line(pose[16].x*vw, pose[16].y*vh, pose[20].x*vw, pose[20].y*vh);
// ...
// right foot
line(pose[23].x*vw, pose[23].y*vh, pose[25].x*vw, pose[25].y*vh);
line(pose[25].x*vw, pose[25].y*vh, pose[27].x*vw, pose[27].y*vh);
line(pose[27].x*vw, pose[27].y*vh, pose[29].x*vw, pose[29].y*vh);
line(pose[29].x*vw, pose[29].y*vh, pose[31].x*vw, pose[31].y*vh);
// left foot
line(pose[24].x*vw, pose[24].y*vh, pose[26].x*vw, pose[26].y*vh);
line(pose[26].x*vw, pose[26].y*vh, pose[28].x*vw, pose[28].y*vh);
line(pose[28].x*vw, pose[28].y*vh, pose[30].x*vw, pose[30].y*vh);
line(pose[30].x*vw, pose[30].y*vh, pose[32].x*vw, pose[32].y*vh);
}
function drawHandSkeleton(hand) {
vw = video.width;
vh = video.height;
// thumb
line(hand[0].x*vw, hand[0].y*vh, hand[1].x*vw, hand[1].y*vh);
line(hand[1].x*vw, hand[1].y*vh, hand[2].x*vw, hand[2].y*vh);
line(hand[2].x*vw, hand[2].y*vh, hand[3].x*vw, hand[3].y*vh);
line(hand[3].x*vw, hand[3].y*vh, hand[4].x*vw, hand[4].y*vh);
// index
line(hand[0].x*vw, hand[0].y*vh, hand[5].x*vw, hand[5].y*vh);
line(hand[5].x*vw, hand[5].y*vh, hand[6].x*vw, hand[6].y*vh);
line(hand[6].x*vw, hand[6].y*vh, hand[7].x*vw, hand[7].y*vh);
line(hand[7].x*vw, hand[7].y*vh, hand[8].x*vw, hand[8].y*vh);
// middle
line(hand[0].x*vw, hand[0].y*vh, hand[9].x*vw, hand[9].y*vh);
line(hand[9].x*vw, hand[9].y*vh, hand[10].x*vw, hand[10].y*vh);
line(hand[10].x*vw, hand[10].y*vh, hand[11].x*vw, hand[11].y*vh);
line(hand[11].x*vw, hand[11].y*vh, hand[12].x*vw, hand[12].y*vh);
// ring
line(hand[0].x*vw, hand[0].y*vh, hand[13].x*vw, hand[13].y*vh);
line(hand[13].x*vw, hand[13].y*vh, hand[14].x*vw, hand[14].y*vh);
line(hand[14].x*vw, hand[14].y*vh, hand[15].x*vw, hand[15].y*vh);
line(hand[15].x*vw, hand[15].y*vh, hand[16].x*vw, hand[16].y*vh);
// pinky
line(hand[0].x*vw, hand[0].y*vh, hand[17].x*vw, hand[17].y*vh);
line(hand[17].x*vw, hand[17].y*vh, hand[18].x*vw, hand[18].y*vh);
line(hand[18].x*vw, hand[18].y*vh, hand[19].x*vw, hand[19].y*vh);
line(hand[19].x*vw, hand[19].y*vh, hand[20].x*vw, hand[20].y*vh);
// knuckles
line(hand[5].x*vw, hand[5].y*vh, hand[9].x*vw, hand[9].y*vh);
line(hand[9].x*vw, hand[9].y*vh, hand[13].x*vw, hand[13].y*vh);
line(hand[13].x*vw, hand[13].y*vh, hand[17].x*vw, hand[17].y*vh);
for (let lm=0; lm<hand.length; lm++) {
ellipse(hand[lm].x*video.width, hand[lm].y*video.height, hand[lm].z*100, hand[lm].z*100);
}
}
async function createPoseLandmarker () {
print("getting poseLandmarker");
const vision = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm"
);
print("creating poseLandmarker");
const poseLandmarker_ = await PoseLandmarker.createFromOptions(vision, {
baseOptions: {
modelAssetPath: "https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_lite/float16/1/pose_landmarker_lite.task",
delegate: "GPU"
},
runningMode: "VIDEO",
outputSegmentationMasks: "False",
numPoses: 1
});
print("returning poseLandmarker");
return poseLandmarker_;
};
/*
async function initHandLandmarker() {
const vision = await FilesetResolver.forVisionTasks(
// path/to/wasm/root
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm"
);
const handLandmarker_ = await HandLandmarker.createFromOptions(
vision,
{
baseOptions: {
modelAssetPath: "https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task"
},
numHands: 2
});
return handLandmarker_;
}
*/
function createImageFromBitmap(bitmap) {
const pImg = new p5.Image(1, 1, this);
pImg.width = pImg.canvas.width = bitmap.width;
pImg.height = pImg.canvas.height = bitmap.height;
// Draw the image into the backing canvas of the p5.Image
pImg.drawingContext.drawImage(bitmap, 0, 0);
pImg.modified = true;
return pImg;
}
window.setup = setup;
window.draw = draw;
window.preload = preload;
//window.keyPressed = keyPressed;