-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
133 lines (112 loc) · 4.84 KB
/
main.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
import * as THREE from 'https://cdn.skypack.dev/three@0.132.2';
var container = document.getElementById("ASL-Gestures");
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
let renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setClearColor(0x000000, 0.1); // the second param is the alpha channel
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
var sentenceList = []
var sentenceIndex = 0;
var wordIndex = 0;
var frameIndex = 0;
fetch("subtitles-1.txt")
.then((res) => res.text())
.then((text) => {
console.log(text);
sentenceList = text.split(";");
fetch('hand_landmarks.json')
.then(response => response.json())
.then(data => {
function drawPoint(x, y, z) {
const pointRadius = 1; // Increase the radius value as needed
const geometry = new THREE.SphereGeometry(pointRadius, 32, 16);
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
const sphere = new THREE.Mesh(geometry, material);
sphere.position.x = x;
sphere.position.y = y;
sphere.position.z = z;
scene.add(sphere);
}
function drawLine(x1, y1, z1, x2, y2, z2) {
const points = [];
points.push(new THREE.Vector3(x1, y1, z1));
points.push(new THREE.Vector3(x2, y2, z2));
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({ color: 0xffffff });
const line = new THREE.Line(geometry, material);
scene.add(line);
}
function redistributeElements(left, right) {
if (left.length > 21) {
const redistributedElements = left.splice(21);
right.push(...redistributedElements);
} else if (right.length > 21) {
const redistributedElements = right.splice(21);
left.push(...redistributedElements);
}
}
function connectLines(frameIndex) {
const nodeConnections = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [5, 9], [9, 10], [10, 11], [11, 12], [9, 13], [13, 14], [14, 15], [15, 16], [13, 17], [17, 18], [18, 19], [19, 20], [0, 17]];
var left = data[wordList[wordIndex]][frameIndex]['Left Hand Coordinates'];
var right = data[wordList[wordIndex]][frameIndex]['Right Hand Coordinates'];
redistributeElements(left, right);
nodeConnections.forEach(function (node) {
const u = node[0];
const v = node[1];
if (left[u] && left[v]) {
const l1 = left[u];
const l2 = left[v];
drawLine(l1[0] * 50, l1[1] * -50, l1[2] * 50, l2[0] * 50, l2[1] * -50, l2[2] * 50);
}
if (right[u] && right[v]) {
const r1 = right[u];
const r2 = right[v];
drawLine(r1[0] * 50, r1[1] * -50, r1[2] * 50, r2[0] * 50, r2[1] * -50, r2[2] * 50);
}
})
}
let clock = new THREE.Clock();
let delta = 0;
let interval = 1 / 5;
console.log(sentenceList[sentenceIndex])
var sentence = sentenceList[sentenceIndex].toLowerCase();
var wordList = sentence.split(" ");
function render() {
requestAnimationFrame(render);
delta += clock.getDelta();
if (delta > interval) {
delta = delta % interval;
if (sentenceList.length > 0 && sentenceIndex < sentenceList.length) {
console.log(wordList[wordIndex])
var left = data[wordList[wordIndex]][frameIndex]['Left Hand Coordinates'];
var right = data[wordList[wordIndex]][frameIndex]['Right Hand Coordinates'];
for (var i = 0; i < left.length; i++) {
drawPoint(left[i][0] * 50, left[i][1] * -50, left[i][2] * 50);
}
for (var i = 0; i < right.length; i++) {
drawPoint(right[i][0] * 50, right[i][1] * -50, right[i][2] * 50);
}
connectLines(frameIndex);
frameIndex++;
if (frameIndex >= data[wordList[wordIndex]].length) {
frameIndex = 0;
wordIndex++;
}
if (wordIndex < wordList.length) {
wordIndex = 0;
sentenceIndex++;
sentence = sentenceList[sentenceIndex]
sentence = sentence.toLowerCase();
wordList = sentence.split(" ");
}
}
renderer.render(scene, camera);
scene.remove.apply(scene, scene.children);
}
}
render();
})
camera.position.set(27.5, -30, 25);
})
.catch((e) => console.error(e));