カウントアップの方法
私は筋力トレーニングの回数カウントアプリを作っています。
index.html
<html>
<head>
<meta charset="UTF-8">
<title>PoseNet example using p5.js</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script>
<script src="https://unpkg.com/ml5@0.3.1/dist/ml5.min.js" type="text/javascript"></script>
</head>
<body>
<h1>PoseNet example using p5.js</h1>
<p id='status'>Loading model...</p>
<h1>回数</h1>
<div id="disp_count">0</div>
<script>
let video;
let poseNet;
let poses = [];
let count_value = 0;
let count_disp = document.getElementById("disp_count");
function setup() {
console.log('セットアップ');
createCanvas(740, 600);
video = createCapture(VIDEO);
video.size(width, height);
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', function(results) {
poses = results;
});
video.hide();
}
function modelReady() {
select('#status').html('Model Loaded');
}
function count() {
}
function draw() {
if (poses.length > 0) {
let pose = poses[0].pose;
let keypoint = pose.keypoints[0];
// console.log('部位名:' + keypoint.part);
for (let i = 0; i < poses.length; i++) {
// poseが持つ情報を出力
let pose = poses[i].pose;
// console.log('全体の精度' + pose.score);
// console.log('nose' + pose.nose.x);
// console.log('nose' + pose.nose.y);
if (pose.score <= 0.5){
console.log("ok");
if (pose.nose.x <= 370.0) {
console.log('---------');
count_value += 1;
console.log(count_value);
count_disp.innerHTML = count_value;
}
}
}
for (let i = 0; i < poses.length; i++) {
let skeleton = poses[i].skeleton;
}
image(video, 0, 0, width, height);
drawSkeleton();
drawKeypoints();
}
fill(255, 0, 0);
rect(370, 300, 2, 500);
}
// A function to draw ellipses over the detected keypoints
function drawKeypoints() {
// Loop through all the poses detected
for (let i = 0; i < poses.length; i++) {
// For each pose detected, loop through all the keypoints
let pose = poses[i].pose;
for (let j = 0; j < pose.keypoints.length; j++) {
// A keypoint is an object describing a body part (like rightArm or leftShoulder)
let keypoint = pose.keypoints[j];
// Only draw an ellipse is the pose probability is bigger than 0.2
if (keypoint.score > 0.2) {
fill(color(0, 0, 255));
stroke(20);
// noStroke();
ellipse(keypoint.position.x, keypoint.position.y, 10, 10);
}
}
}
}
// A function to draw the skeletons
function drawSkeleton() {
// Loop through all the skeletons detected
for (let i = 0; i < poses.length; i++) {
let skeleton = poses[i].skeleton;
// For every skeleton, loop through all body connections
for (let j = 0; j < skeleton.length; j++) {
let partA = skeleton[j][0];
let partB = skeleton[j][1];
stroke('rgb(0,255,0)');
strokeWeight(4);
line(partA.position.x, partA.position.y, partB.position.x, partB.position.y);
}
}
}
</script>
</body>
</html>
上記のプログラムです、
画像の領域でx座標が370.0より小さくなったらcount_valueを1回だけ加算したいと思っていますが、xが370より低い値の時にcount_valueはすごいスピードで加算され続けます、x座標が370を超えた時に1加算されるようにするにはどのようにアプローチすればよいのでしょうか?while文等も試したのですが、解消されません。
よろしくお願いいたします。
0