Переместите объект между двумя основными точками ml5js poseNet, используя p5js - PullRequest
1 голос
/ 27 марта 2019

Я пытаюсь заставить круг перемещаться между двумя точками тела - между левым и правым плечом. Я начал с этого кода, чтобы сделать объект после некоторой позиции:

var rectLocation;
var x = 50;
var y = 50;

function setup() {
  createCanvas(640,400);
  rectLocation = createVector(width/2,height/2);
}

function draw() {
    background(255);
  
    var target = createVector(x,y);
    var distance = target.dist(rectLocation);
    var mappedDistance = map(distance, 100, 0, 1.5, 0.5);
  
    target.sub(rectLocation);
    target.normalize();
    target.mult(mappedDistance);  
    rectLocation.add(target);
  
    text('hello',rectLocation.x, rectLocation.y);
  
    x = x + 1 ;
  
    if (x > width) {
      x = 50;
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>

Так что с poseNet я преобразовал значения x и y каждой ключевой точки в p5 Векторы и сделал так, чтобы одно местоположение следовало за другим, но, похоже, оно не двигается, и я не могу понять, почему. Эскиз: https://editor.p5js.org/sebyakin/sketches/LQ7gqwGZH и мой код здесь:

let video;
let poseNet;
let noseX = 0;
let noseY = 0;
let eyelX = 0;
let eyelY = 0;
let shoulderX = 0;
let shoulderY = 0;
let firstLocation;
let target;

function setup() {
  createCanvas(640, 480);
  video = createCapture(VIDEO);
  video.hide();
  
  poseNet = ml5.poseNet(video, modelReady);
  poseNet.on('pose', gotPoses);
  // creating empty vectors
  firstLocation = createVector(0,0);
  target = createVector(0,0);
}


function gotPoses(poses) {
  if (poses.length > 0) {
    let nX = poses[0].pose.keypoints[0].position.x;
    let nY = poses[0].pose.keypoints[0].position.y;
    let eX = poses[0].pose.keypoints[1].position.x;
    let eY = poses[0].pose.keypoints[1].position.y;
    let sX = poses[0].pose.keypoints[5].position.x;
    let sY = poses[0].pose.keypoints[5].position.y;
    noseX = nX;
    noseY = nY;
    eyelX = eX;
    eyelY = eY;
    shoulderX = sX;
    shoulderY = sY;
  }
}


function modelReady() {
  console.log('model ready');
}


function draw() {
  image(video, 0, 0);
  fill(0,0,255);
  
  // set vectors to keypoints values 
  firstLocation.set(noseX, noseY);
  target.set(shoulderX, shoulderY)
  
  // make a circle follow target loaction 
  let distance = target.dist(firstLocation);
  let mappedDistance = map(distance, 100, 0, 1.5, 0.5);
  target.sub(firstLocation);
  // target.normalize();
  // target.mult(mappedDistance);  
  firstLocation.add(target);
  
  ellipse(firstLocation.x, firstLocation.y, 50);
  console.log(target.x);
}

Где я ошибся?

...