xxxxxxxxxx
207
let mic;
let facemesh;
let video;
let predictions = [];
let canvas_w = 800;
let canvas_h = 600;
let video_w = 640;
let video_h = 480;
let margin_left = 50;
let margin_top = 50;
let video_w2 = 480;
let video_h2 = 480;
let margin_left2 = 130;
let margin_top2 = 50;
let margin = 50;
//pictures
let catimg;
let openmouth;
let closemouth;
function preload() {
catimg = loadImage('assets/popcat.gif');
openmouth = loadImage('assets/open.png');
closemouth = loadImage('assets/shutup.png');
}
function setup() {
let cnv = createCanvas(canvas_w, canvas_h);
video = createCapture(VIDEO);
video.size(video_w, video_h);
facemesh = ml5.facemesh(video, modelReady);
// This sets up an event that fills the global variable "predictions"
// with an array every time new predictions are made
facemesh.on("predict", (results) => {
predictions = results;
});
// Hide the video element, and just show the canvas
video.hide();
//microphone
mic=new p5.AudioIn();
mic.start();
cnv.mousePressed(userStartAudio);
background("rgb(0,0,0)");
}
function modelReady() {
console.log("Model ready!");
}
function draw() {
textSize(32);
text('Press 1 or 2!!!',0, 0);
fill("#FFFFFF");
//状态1:-------------------------------------------------
drawBackground_1();
image(video, margin_left, margin_top, video_w, video_h);
drawForeground_1();
//脸上的点变成食物emoji
drawKeypoints_food();
//状态2:-------------------------------------------------
if(key=="1"){
drawBackground_2();
image(video, margin_left, margin_top, video_w, video_h);
//小猫在脸上随着声音变化
drawMicLevel();
}
}
// Miclevel猫猫变大
function drawMicLevel() {
push();
micLevel = mic.getLevel();
noStroke();
pop();
for (let i = 0; i < predictions.length; i += 1) {
const keypoints = predictions[i].scaledMesh;
// Draw facial keypoints.
for (let j = 0; j < keypoints.length; j += 1) {
const [x, y] = keypoints[j];
push();
translate(margin_left, margin_top);
let catsize = map(micLevel,0,0.8,0,1000);
if (catsize<130){
image(closemouth,x,y,catsize/2,catsize/2);
}
else{
image(openmouth,x,y,catsize/2,catsize/2);
}
pop();
}
}
}
//脸上的点变成食物
function drawKeypoints_food() {
for (let i = 0; i < predictions.length; i += 1) {
const keypoints = predictions[i].scaledMesh;
// Draw facial keypoints.
for (let j = 0; j < keypoints.length; j += 1) {
const [x, y] = keypoints[j];
push();
translate(margin_left, margin_top);
//let food= text("🐟","🐠","🐡")
textSize(20);
text("🐟",x,y);
pop();
image(catimg,mouseX-90,mouseY-90,86.4*1.5,71.8*1.5)
}
}
}
//以下函数无需修改
//星星旋转的前景函数
function drawForeground_1() {
push();
//circle(20, 20, random(width));
translate(width/2, height/2);
rotate(map(mouseX, 0, width, 0, 360) / 3.0);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 5);
rotate(map(mouseY, 0, width, 0, 180) / 3.5);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 6);
translate(width / 2, height / 2);1
rotate(map(mouseY, 0, width, 0, 360) / 4.0);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 7);
rotate(map(mouseX, 0, width, 0, 180) / 4.5);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 8);
translate(width / 2, height / 2);
rotate(map(mouseX, 0, width, 0, 360) / 5.0);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 9);
pop();
}
//background1 black&white star background
function drawBackground_1() {
background("rgb(0,0,0)");
rotate(map(mouseX, 0, width, 0, 360) / 0);
star(0, 0, map(mouseX, 0, width, 0, 400), map(mouseY, 0, width, 0, 600), 9);
star(800, 0, map(mouseX, 0, width, 0, 200), map(mouseY, 0, width, 0, 400), 8);
star(0, 600, map(mouseX, 0, width, 0, 200), map(mouseY, 0, width, 0, 400), 9);
star(800, 600, map(mouseX, 0, width, 0, 300), map(mouseY, 0, width, 0, 400), 9);
}
//background2
function drawBackground_2() {
background("#E91E63");
push();
translate(width / 2, height / 2);
rotate(map(mouseX, 0, width, 0, 360) / 1.0);
let e = color(map(mouseY, 0, width, 5, 144),5,253);
fill(e);
star(0, 0, map(mouseX, 0, width, 0, 400), map(mouseY, 0, width, 0, 600), 9);
star(800, 0, map(mouseX, 0, width, 0, 200), map(mouseY, 0, width, 0, 400), 8);
star(0, 600, map(mouseX, 0, width, 0, 200), map(mouseY, 0, width, 0, 400), 9);
star(800, 600, map(mouseX, 0, width, 0, 300), map(mouseY, 0, width, 0, 400), 9);
//rect(random(width), 0, map(mouseX, 0, width, 10, 100), height);
pop();
}
//星星函数
function star(x, y, radius1, radius2, npoints) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius2;
let sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}