mo49
8/23/2019 - 5:58 PM

00_ClmtrackrSmaple.md

// available:
// 鼻 noseX, noseY
// 口 mouthX, mouthY, mouthW, mouthH
// 眼 eyeLX, eyeLY, eyeRX, eyeRY

let f = 0;

function setup() {
  createCanvas(640, 480);
  setupTracking();
}

function draw() {
  image(video, 0, 0, width, height);
  drawTracking();
  
  // eye
  let distance = eyeRX - eyeLX;
  let l_offset = (eyeLH/15 > 0.8) ? eyeLH/15 : 0.2;
  let r_offset = (eyeRH/15 > 0.8) ? eyeRH/15 : 0.2;
  
  ellipse(eyeLX, eyeLY, distance/2, distance * l_offset);
  ellipse(eyeRX, eyeRY, distance/2, distance * r_offset);
  line(eyeLX, eyeLY, eyeRX, eyeRY);

  // mouth
  rect(mouthX - mouthW/2, mouthY - mouthH/2, mouthW, mouthH*1.2);
  
  if(mouthH > 30){
    for(let i=0; i < 300; i++){
      ellipse((mouthX) + (sin(i+f) * i * 0.5), (mouthY) + (cos(i+f) * i * 0.5), 5, 5);
    }
  }
  f++;
}
let video;
let tracker

let noseX, noseY;
let mouthX, mouthY, mouthW, mouthH;
let eyeLX, eyeLY, eyeLH, eyeRX, eyeRY, eyeRH;

let updateTracking = function () {
  let positions = tracker.getCurrentPosition();

  if (positions.length > 0) {
    noseX = positions[62][0];
    noseY = positions[62][1];
    mouthX = (positions[44][0] + positions[50][0]) / 2;
    mouthY = (positions[47][1] + positions[53][1]) / 2;
    mouthW = (positions[50][0] - positions[44][0]);
    mouthH = (positions[53][1] - positions[47][1]);
    eyeLX = positions[27][0];
    eyeLY = positions[27][1];
    eyeLH = positions[31][1] - positions[29][1];
    eyeRX = positions[32][0];
    eyeRY = positions[32][1];
    eyeRH = positions[26][1] - positions[24][1];
    faceW = (positions[14][0] - positions[0][0]);
  }
}

function setupTracking() {
  let w = width,
    h = height;
  
  video = createCapture({
    audio: false,
    video: {
      width: w,
      height: h
    }
  }, function() {
    console.log('video ready.')
  });
  video.elt.setAttribute('playsinline', '');
  video.size(w, h);
  video.hide();

  tracker = new clm.tracker();
  tracker.init();
  tracker.start(video.elt);
  
  registerMethod('pre', updateTracking);
}

function drawTracking() {
  let positions = tracker.getCurrentPosition();

  colorMode(HSB);
  
  noFill();
  stroke(255);
  beginShape();
  for (let i = 0; i < positions.length; i++) {
    vertex(positions[i][0], positions[i][1]);
  }
  endShape();

  noStroke();
  for (let i = 0; i < positions.length; i++) {
    fill(map(i, 0, positions.length, 0, 360), 50, 100);
    ellipse(positions[i][0], positions[i][1], 4, 4);
    text(i, positions[i][0], positions[i][1]);
  }
  
  colorMode(RGB);
}