-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
169 lines (146 loc) · 4.62 KB
/
index.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html lang="ja">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/control_utils/control_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
<meta charset="utf-8" />
</head>
<body>
<video class="input_video"></video>
<script>
// ●Hands - mediapipe https://google.github.io/mediapipe/solutions/hands.html
const isFlipped = true;
let keypointsHand = [];
const videoElement = document.getElementsByClassName("input_video")[0];
videoElement.style.display = "none";
function onHandsResults(results) {
keypointsHand = results.multiHandLandmarks;
}
const hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
},
});
hands.setOptions({
selfieMode: isFlipped,
maxNumHands: 1, // 最大1つに制限
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5,
});
hands.onResults(onHandsResults);
const camera = new Camera(videoElement, {
onFrame: async () => {
await hands.send({ image: videoElement });
},
width: 1280,
height: 720,
});
camera.start();
let imagePNG;
let videoImage;
let flag = {
thumb: false,
index: false,
middle: false,
ring: false,
pinky: false,
};
function preload() {
imagePNG = loadImage("./fire.png");
}
function setup() {
createCanvas(720, 450);
videoImage = createGraphics(640, 360);
}
function draw() {
clear();
background("rgba(100, 100, 255, 0.2)");
videoImage.drawingContext.drawImage(
videoElement,
0,
0,
videoImage.width,
videoImage.height
);
push();
if (isFlipped) {
translate(width, 0);
scale(-1, 1);
}
// image(videoImage, 0, 0);
displayWidth = width;
displayHeight = (width * videoImage.height) / videoImage.width;
image(videoImage, 0, 0, displayWidth, displayHeight);
pop();
if (keypointsHand.length > 0) {
// 親指と人差し指の先は、4 と 8
// 中指と薬指と小指は、12 と 16 と 20
// 中指と小指の根元は 9 と 17
const tip = {
thumb: keypointsHand[0][4],
index: keypointsHand[0][8],
middle: keypointsHand[0][12],
ring: keypointsHand[0][16],
pinky: keypointsHand[0][20],
};
const mcp = {
middle: keypointsHand[0][9],
pinky: keypointsHand[0][17],
};
const distance = {
index: dist(mcp.middle.x, mcp.middle.y, tip.index.x, tip.index.y),
middle: dist(mcp.middle.x, mcp.middle.y, tip.middle.x, tip.middle.y),
ring: dist(mcp.pinky.x, mcp.pinky.y, tip.ring.x, tip.ring.y),
pinky: dist(mcp.pinky.x, mcp.pinky.y, tip.pinky.x, tip.pinky.y),
thumb: dist(mcp.pinky.x, mcp.pinky.y, tip.thumb.x, tip.thumb.y),
};
for (const property in distance) {
if (distance[property] > 0.15) {
flag[property] = true;
}
if (flag[property]) {
drawFire(tip[property], displayWidth, displayHeight);
}
}
if (!Object.values(flag).includes(false)) {
// 実装予定のものあり
}
}
push();
fill(0, 0, 0);
rect(5, 5, 270, 80);
textSize(14);
fill(255, 255, 255);
text("手をグーの状態でカメラにうつして", 20, 30);
text("指を1本ずつ指を立ててください。", 20, 50);
text("rキーで表示がリセットされます。", 20, 70);
pop();
}
function drawFire(position, inputWidth, inputHeight) {
// ellipse(position.x * inputWidth, position.y * inputHeight, 10);
push();
imageMode(CENTER);
tint(255, 128);
image(
imagePNG,
position.x * inputWidth,
position.y * inputHeight,
imagePNG.width * 0.15,
imagePNG.height * 0.15
);
pop();
}
function keyPressed() {
if (key === "r") {
for (const property in flag) {
flag[property] = false;
}
}
}
</script>
</body>
</html>