Preliminary 'flip' variable, but not really compatible with faceApi points, so disabled by default

This commit is contained in:
Ruben van de Ven 2020-10-25 15:26:11 +01:00
parent 7acb164f93
commit 4f0cb957a2
2 changed files with 48 additions and 22 deletions

View File

@ -46,6 +46,7 @@ function drawLandmarkOverlay(detection) {
text(nr, p.x, p.y); text(nr, p.x, p.y);
} }
noFill();
} }
@ -150,6 +151,7 @@ var lastFrame;
var frameToDetect; var frameToDetect;
var detections = []; var detections = [];
var factor_x, factor_y; var factor_x, factor_y;
var flip = false; // mirror mode, disabled, because it's tricky to enable with faceApi
// function pause() { // function pause() {
@ -271,11 +273,11 @@ function modelReady() {
faceapi.detect(gotResults); faceapi.detect(gotResults);
} }
var handleResults = function(){ // var handleResults = function(){
// background(parseInt(Math.random()*255),parseInt(Math.random()*255),parseInt(Math.random()*255)); // // background(parseInt(Math.random()*255),parseInt(Math.random()*255),parseInt(Math.random()*255));
background((millis()/100)%255,0,0); // background((millis()/100)%255,0,0);
image(video, -width/2 + 10, -height/2 + 10, width - 20, height -20); // image(video, -width/2 + 10, -height/2 + 10, width - 20, height -20);
}; // };
gotResults = function(err, result) { gotResults = function(err, result) {
@ -287,8 +289,17 @@ gotResults = function(err, result) {
// store data for async draw function // store data for async draw function
// TODO results to more compatible format // TODO results to more compatible format
lastFrame.image(frameToDetect, 0,0, video.width, video.height); // translate(width,0); // move to far corner
detections = parseDetectionResults(result); if (flip) {
lastFrame.push();
lastFrame.scale(-1.0,1.0); // flip x-axis backwards
lastFrame.image(frameToDetect, -lastFrame.width, 0, lastFrame.width, lastFrame.height);
lastFrame.pop();
} else {
lastFrame.image(frameToDetect, 0, 0, lastFrame.width, lastFrame.height);
}
detections = parseDetectionResults(result, flip, width);
// size of video becomes known only after camera approval // size of video becomes known only after camera approval
if(lastFrame.width != video.width || lastFrame.height != video.height){ if(lastFrame.width != video.width || lastFrame.height != video.height){
@ -432,22 +443,27 @@ function getBoundingBox(){
} }
} }
function parseDetectionResults(results) { function parseDetectionResults(results, flip, frameWidth) {
let detections = []; let detections = [];
for(let result of results) { for(let result of results) {
const landmarks = result.landmarks._positions.map((pos) => parseCoordinate(pos)); const landmarks = result.landmarks._positions.map((pos) => parseCoordinate(pos, flip, frameWidth));
let x = result.alignedRect._box._x * factor_x;
if(flip) {
x *= -1;
x += frameWidth;
}
let detection = { let detection = {
'points': landmarks, 'points': landmarks,
// TODO: rotation // TODO: rotation
'parts': {}, 'parts': {},
x: result.alignedRect._box._x * factor_x, x: x,
y: result.alignedRect._box._y * factor_y, y: result.alignedRect._box._y * factor_y,
width: result.alignedRect._box._width * factor_x, width: result.alignedRect._box._width * factor_x,
height: result.alignedRect._box._height * factor_y, height: result.alignedRect._box._height * factor_y,
} }
for(let idx in result.parts) { // for(let idx in result.parts) {
detection.parts[idx] = result.parts[idx].map((pos) => parseCoordinate(pos)); // detection.parts[idx] = result.parts[idx].map((pos) => parseCoordinate(pos));
} // }
detection['center'] = { detection['center'] = {
x: detection.x + detection.width / 2, x: detection.x + detection.width / 2,
y: detection.y + detection.height / 2, y: detection.y + detection.height / 2,
@ -463,33 +479,39 @@ function parseDetectionResults(results) {
* We convert this to the canvas's coordinates * We convert this to the canvas's coordinates
* @param Object {_x: , _y: } * @param Object {_x: , _y: }
*/ */
function parseCoordinate(position) { function parseCoordinate(position, flip, frameWidth) {
let x = position._x * factor_x;
if (flip) {
x *= -1;
x += frameWidth;
}
return { return {
x: position._x * factor_x, x: x,
y: position._y * factor_y, y: position._y * factor_y,
} }
} }
function transformDetection(original) { function transformDetection(original) {
let b = original.points[36]; // outer point on left eye const b = original.points[36]; // outer point on left eye
let a = original.points[45]; // outer point on right eye const a = original.points[45]; // outer point on right eye
let angle = atan2(a.y - b.y, a.x - b.x);
const angle = atan2(a.y - b.y, a.x - b.x);
// let cx =a.x/2 + b.x/2 // let cx =a.x/2 + b.x/2
// let cy = a.y/2 + b.y/2 // let cy = a.y/2 + b.y/2
let cx = original.x const cx = original.x
let cy = original.y const cy = original.y
let detection = { let detection = {
'points': original.points.map(p => transformPoint(p, cx, cy, angle)), 'points': original.points.map(p => transformPoint(p, cx, cy, angle)),
'origin': {x:cx, y:cy}, 'origin': {x:cx, y:cy},
'angle': angle, 'angle': angle,
original: original 'original': original
} }
let bbox = getBoundingBox(detection.points); const bbox = getBoundingBox(detection.points);
padding_x = bbox.width * .1; padding_x = bbox.width * .1;
padding_y = bbox.height * .1; padding_y = bbox.height * .1;

View File

@ -27,6 +27,10 @@ detection.width
detection.height detection.height
: Hoogte van de bounding box van het gezicht. : Hoogte van de bounding box van het gezicht.
detection.angle
: Hoeveel het gezicht gedraaid is (oftewel de 'roll').
<!-- detection.center.x & detection.center.y <!-- detection.center.x & detection.center.y
: Het midden van de bounding box --> : Het midden van de bounding box -->