我有一個激活影像并使其保持激活狀態的代碼,但如果滿足引數,我想更改為其他影像。如果我說某個詞,我想激活一件事并停用另一件事
我的代碼:https ://editor.p5js.org/perikleousd910/sketches/OI1glzfWS
let video;
let poseNet;
let pose;
let classifier;
let label = "listening";
let soundModelURL = "https://teachablemachine.withgoogle.com/models/xe3C8s4Co/model.json";
var flag = false;
var flag2 = false;
var flag3 = false;
let div = createDiv("div");
//var song = new Audio('sound/Itachi.mp3');
function preload() {
img1 = loadImage("pics/Sharingan_Triple.png");
img2 = loadImage("pics/Untitled-5.jpg");
img3 = loadImage("pics/orochimaruright.png");
img4 = loadImage("pics/orochimaruleft.png");
//song = loadSound('sound/Itachi.mp3');
classifier = ml5.soundClassifier(soundModelURL);
}
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on("pose", gotPoses);
classifier.classify(gotResult);
}
function gotPoses(poses) {
if (poses.length > 0) {
pose = poses[0].pose;
}
}
function modelLoaded() {
console.log("poseNet ready");
}
function draw() {
image(video, 0, 0);
if (flag === true || (pose && label == "HowMuchCanYouSee")) {
flag = true;
let eyeR = pose.rightEye;
let eyeL = pose.leftEye;
let d = dist(eyeR.x, eyeR.y, eyeL.x, eyeL.y);
//sharingan complete
image(img1, pose.leftEye.x - d / 2, pose.leftEye.y - d / 2, d, d);
image(img1, pose.rightEye.x - d / 2, pose.rightEye.y - d / 2, d, d);
} else if (flag2 === true || (pose && label == "Orochimaru")) {
flag2 = true;
let eyeR = pose.rightEye;
let eyeL = pose.leftEye;
let d = dist(eyeR.x, eyeR.y, eyeL.x, eyeL.y);
image(img4, pose.leftEye.x - d / 1.53, pose.leftEye.y - d / 1.5, (d * 3) / 2, (d * 3) / 2);
image(img3, pose.rightEye.x - d / 1.2, pose.rightEye.y - d / 1.5, (d * 3) / 2, (d * 3) / 2);
}
fill(255, 0, 0);
textSize(24);
textAlign(CENTER, CENTER);
text(label, 118, 15);
}
function gotResult(error, results) {
if (error) {
console.error(error);
return;
}
label = results[0].label;
}
uj5u.com熱心網友回復:
如果我對您的理解正確,您可能想要這樣的東西;shownImageSet如果沒有姿勢和標簽,則全域變數“保持”到最后一個值。
不需要flag/flag2變數。
// ... initialization code elided ...
let shownImageSet = 0;
function drawImages() {
// Select image set based on pose label
if (pose) {
if (label == "HowMuchCanYouSee") {
shownImageSet = 1;
}
if (label == "Orochimaru") {
shownImageSet = 2;
}
}
if (shownImageSet) {
// Need to show something?
const eyeR = pose.rightEye;
const eyeL = pose.leftEye;
const d = dist(eyeR.x, eyeR.y, eyeL.x, eyeL.y);
if (shownImageSet == 1) {
image(img1, eyeL.x - d / 2, eyeL.y - d / 2, d, d);
image(img1, eyeR.x - d / 2, eyeR.y - d / 2, d, d);
} else if (shownImageSet == 2) {
image(img4, eyeL.x - d / 1.53, eyeL.y - d / 1.5, (d * 3) / 2, (d * 3) / 2);
image(img3, eyeR.x - d / 1.2, eyeR.y - d / 1.5, (d * 3) / 2, (d * 3) / 2);
}
}
}
function draw() {
image(video, 0, 0);
drawImages();
fill(255, 0, 0);
textSize(24);
textAlign(CENTER, CENTER);
text(label, 118, 15);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457669.html
標籤:javascript if 语句 p5.js
