-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
146 lines (102 loc) · 3.54 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const incrementButton = document.getElementById("Button");
const counterDisplay = document.getElementById("counter");
let capture;
let posenet;
let singlePose,skeleton;
let leftelbowx,leftelbowy;
let rightelbowx,rightelbowy;
let x1,y1,x3,y3,x4,y4,x5,y5;
let count=0;
let direction=0;
function setup() {
createCanvas(800, 600);
capture = createCapture(VIDEO)
capture.hide();
posenet = ml5.poseNet(capture, modelLoaded);
posenet.on('pose',receivedPoses);
}
function updateDisplay(){
counterDisplay.textContent = count;
}
//setInterval(logPoses,3000);
setInterval(function() {
//console.log(poses);
}, 3000); // 3000 milliseconds = 3 seconds
function receivedPoses(poses){
//setInterval(console.log(poses));
if(poses.length > 0){
singlePose = poses[0].pose;
skeleton = poses[0].skeleton;
//left elbow
leftelbowx=singlePose.leftElbow.x+75;
leftelbowy=singlePose.leftElbow.y;
rightelbowx=singlePose.rightElbow.x;
rightelbowy=singlePose.rightElbow.y;
x1=singlePose.leftEar.x+125;
y1=singlePose.leftEar.y;
//left wrist
x3=singlePose.leftWrist.x+40;
y3=singlePose.leftWrist.y+100;
//right wrist
x4=singlePose.rightWrist.x+20;
y4=singlePose.rightWrist.y+100;
x5=singlePose.rightEye.x;
y5=singlePose.rightEye.y;
// Calculate the magnitudes of the vectors
let b1 = Math.sqrt(Math.pow(x1 - leftelbowx, 2) + Math.pow(y1 - leftelbowy, 2)) *
Math.sqrt(Math.pow(x3 - leftelbowx, 2) + Math.pow(y3 - leftelbowy, 2));
// Calculate the dot product of the vectors
let dotProduct = (x1 - leftelbowx) * (x3 - leftelbowx) + (y1 - leftelbowy) * (y3 - leftelbowy);
// Calculate the angle in radians
let angle = Math.acos(dotProduct / b1);
// Convert the angle from radians to degrees if needed
// let angleInDegrees = angle * (180 / Math.PI);
let v3 = Math.sqrt(Math.pow(x5 - rightelbowx, 2) + Math.pow(y5 - rightelbowy, 2));
let angle2 = Math.acos(
((x4 - rightelbowx) * (x5 - rightelbowx) + (y4 - rightelbowy) * (y5 - rightelbowy)) /
(Math.sqrt(Math.pow(x4 - rightelbowx, 2) + Math.pow(y4 - rightelbowy, 2)) * v3)
);
//console.log("received",57.3*angle);
//console.log("angle2",57.3*angle2);
if(57.3*angle>120 && 57.3*angle2>120){
if(direction==0){
direction=1;
count+=0.5;
updateDisplay();
}}
else if(57.3*angle<90 && 57.3*angle2<90){
if(direction==1){
direction=0;
count+=0.5;
updateDisplay();
}
}
console.log(count);
//counterDisplay.textContent=`Count: ${counter}`;
//updateDisplay();
}
}
function modelLoaded() {
console.log('Model has loaded');
}
function draw() {
image(capture,0,0,800,600);
fill(255,0,0);
ellipse(x1,y1,20,50);
//ellipse(leftShoulderX+50,(y+25)/2,20,50);
//ellipse(rightShoulderX,(y2+15)/2,20,50);
ellipse(rightelbowx,rightelbowy,20,50);
ellipse(leftelbowx,leftelbowy,20,50);
ellipse(x3,y3,20,50);
ellipse(x4,y4,20,50);
fill(255,255,0);
ellipse(x5,y5,20,50);
stroke(255,255,0);
strokeWeight(5);
line(x4,y4,rightelbowx,rightelbowy);
line(x5,y5,rightelbowx,rightelbowy);
stroke(254,210,200);
strokeWeight(3);
line(x1,y1,leftelbowx,leftelbowy);
line(x3,y3,leftelbowx,leftelbowy);
}