xxxxxxxxxx
224
/*
libmario is derived from https://www.youtube.com/watch?v=Z57hx4ey5RY
by ArtCode_ https://www.youtube.com/channel/UCYTP0Gbjq9xHyssfxBrLSGg
His naming style is written in Italian.
*/
let faceapi;
let detections = [];
let w = 1920 / 10;
let h = 1080 / 10;
// Video
let video;
let frame_count = 0;
function preload() {}
function setup() {
createCanvas(705, 607.5);
// Creat the video and start face tracking
video = createCapture(VIDEO);
video.hide();
video.size(w, h);
// Only need landmarks for this example
const faceOptions = {
withLandmarks: false,
withExpressions: true,
withDescriptors: false,
minConfidence: 0.5,
withTinyNet: true,
};
faceapi = ml5.faceApi(video, faceOptions, faceReady);
frameRate(30);
textFont("monospace");
angleMode(DEGREES);
rectMode(CENTER);
imageMode(CENTER);
setupMario();
}
// Start detecting faces
function faceReady() {
faceapi.detect(gotFaces);
}
// Got faces
let t0;
function gotFaces(error, result) {
if (error) {
console.log(error);
return;
}
detections = result;
if (detections.length > 0) {
// {id:"happy", score:0.001},
// {id:"sad", score:0.12934854}
let e_array = [];
for (let i = 0; i < detections.length; i++) {
let e = detections[i].expressions;
for (let key in e) {
e_array.push({
id: key,
score: e[key],
});
}
}
//console.log(e_array);
// Score をキーにして連想配列をソートします.
e_array.sort(function (a, b) {
if (a.score < b.score) return 1;
if (a.score > b.score) return -1;
return 0;
});
// console.log(e_array);
if (e_array[0].id == "happy") {
jatekos.jobbra = true;
loves.i = "j";
} else {
jatekos.jobbra = false;
}
if (e_array[0].id == "surprised") {
if (jatekos.zuhanas == false) {
jatekos.ugras = true;
jatekos.zuhanas = true;
}
} else {
jatekos.zuhanas = false;
}
if ((e_array[0].id == "sad")) {
if (jatekos.guggolas == false) {
jatekos.balra = true;
loves.i = "b";
}
}
else{
jatekos.balra = false;
}
for (let i = 0; i < detections.length; i++) {
let expressions = detections[i].expressions;
for (let key in expressions) {
if (key == "neutral") {
select("#neutral").value(parseInt(expressions[key] * 100));
} else if (key == "happy") {
select("#happy").value(parseInt(expressions[key] * 100));
} else if (key == "sad") {
select("#sad").value(parseInt(expressions[key] * 100));
} else if (key == "angry") {
select("#angry").value(parseInt(expressions[key] * 100));
} else if (key == "fearful") {
select("#fearful").value(parseInt(expressions[key] * 100));
} else if (key == "disgusted") {
select("#disgusted").value(parseInt(expressions[key] * 100));
} else if (key == "surprised") {
select("#surprised").value(parseInt(expressions[key] * 100));
}
}
}
}
faceapi.detect(gotFaces);
}
function draw() {
frame_count++;
drawMario();
imageMode(CORNER);
image(video, 0, 0, w, h);
imageMode(CENTER);
textSize(24);
// フレーム数を描画
text(frame_count, 590, 100);
// Just look at the first face and draw all the points
// if (detections.length > 0) {
// for (let i = 0; i < detections.length; i++) {
// let points = detections[i].landmarks.positions;
// for (let j = 0; j < points.length; j++) {
// stroke(161, 95, 251);
// strokeWeight(4);
// point(points[j].x, points[j].y);
// }
// }
// }
}
function keyPressed() {
if (palya_vege.mt == false) {
// move to left
if (key == "a" && jatekos.guggolas == false) {
jatekos.balra = true;
loves.i = "b";
}
// move to right
if (key == "d" && jatekos.guggolas == false) {
jatekos.jobbra = true;
loves.i = "j";
}
// crouch
if (key == "s") {
jatekos.guggolas = true;
}
// jump
if (key == "w") {
if (jatekos.zuhanas == false) {
jatekos.ugras = true;
}
}
// Fire ball!!!!
if (key == "f") {
loves.e = true;
}
}
// スペースキーを押したらフレームカウントをリセット
if (key == " ") {
frame_count = 0;
}
}
function keyReleased() {
if (key == "a") {
jatekos.balra = false;
}
if (key == "d") {
jatekos.jobbra = false;
}
if (key == "s") {
jatekos.guggolas = false;
}
if (key == "f") {
loves.e = false;
}
key = "";
}
function mousePressed() {
// jatekos.x = mouseX;
// jatekos.y = mouseY;
// print(mouseX);
// print(mouseY);
}