forked from DamienDabernat/ml5-accelerometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
154 lines (135 loc) · 5.04 KB
/
sketch.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
146
147
148
149
150
151
152
153
154
let brain;
let state = 'waiting';
let targetLabel;
let rawData = []; // Stocke les données brutes de l'accéléromètre
let sequenceLength = 15; // La longueur de la séquence pour chaque axe
let sequenceCounter = 0; // Compteur pour le nombre de séquences ajoutées
let maxSequences = 100; // Nombre maximal de séquences à collecter
let collectionInterval; // Pour stocker l'identifiant de l'intervalle de collecte
// Charger un son pour le bip
let bipSound;
function preload() {
bipSound = loadSound('../sound/bip.mp3');
}
function setup() {
createCanvas(640, 480);
background(255);
// Configuration du réseau de neurones
let inputs = [];
for (let i = 0; i < sequenceLength; i++) {
inputs.push('x' + i);
inputs.push('y' + i);
inputs.push('z' + i);
//inputs.push('xOrien' + i);
//inputs.push('yOrien' + i);
//inputs.push('zOrien' + i);
}
const options = {
inputs: inputs,
outputs: 4, // Nombre de labels de classification
task: 'classification',
debug: true
};
console.log(options)
brain = ml5.neuralNetwork(options);
// Boutons pour la collecte de données
createCollectButton('LigneHorizontal', 'hline', 1);
createCollectButton('Circle', 'circle', 2);
createCollectButton('LigneVertical', 'vline', 3);
// createCollectButton('Triangle', 'triangle', 4);
createCollectButton('Rien', 'nothing', 5);
// Bouton pour télécharger les données
let downloadButton = createButton('Télécharger les données');
downloadButton.position(19, 19);
downloadButton.mousePressed(() => {
brain.saveData('collectedData');
});
}
function collectData(x, y, z, xOrientation, yOrientation, zOrientation) {
// Arrondir les valeurs à deux décimales
let roundedX = parseFloat(x.toFixed(3));
let roundedY = parseFloat(y.toFixed(3));
let roundedZ = parseFloat(z.toFixed(3));
let roundedXOrientation = parseFloat(xOrientation.toFixed(3));
let roundedYOrientation = parseFloat(yOrientation.toFixed(3));
let roundedZOrientation = parseFloat(zOrientation.toFixed(3));
rawData.push({ x: roundedX, y: roundedY, z: roundedZ, xO : roundedXOrientation, yO : roundedYOrientation, zO : roundedZOrientation });
if (rawData.length === sequenceLength) {
let dataObject = {};
for (let i = 0; i < sequenceLength; i++) {
dataObject['x' + i] = rawData[i].x;
dataObject['y' + i] = rawData[i].y;
dataObject['z' + i] = rawData[i].z;
//dataObject['xOrien' + i] = rawData[i].xO;
//dataObject['yOrien' + i] = rawData[i].yO;
//dataObject['zOrien' + i] = rawData[i].zO;
}
console.log(dataObject)
brain.addData(dataObject, { label: targetLabel });
sequenceCounter++;
rawData = []; // Réinitialiser pour la prochaine séquence
if (sequenceCounter >= maxSequences) {
clearInterval(collectionInterval); // Arrêtez la collecte
state = 'waiting';
sequenceCounter = 0; // Réinitialiser pour la prochaine fois
bipSound.play(); // Jouer le son pour indiquer la fin
}
}
}
function createCollectButton(buttonText, label, index) {
let button = createButton(buttonText);
button.position(19, (19 + index * 40));
button.mousePressed(() => {
prepareToCollect(label);
});
}
function prepareToCollect(label) {
if (state === 'waiting' && sequenceCounter < maxSequences) {
targetLabel = label;
state = 'preparing';
setTimeout(() => startCollecting(), 1500);
}
}
let latestDeviceOrientationEvent;
function startCollecting() {
if (state === 'preparing') {
bipSound.play();
state = 'collecting';
collectionInterval = setInterval(() => {
if (window.DeviceMotionEvent && window.latestDeviceMotionEvent && latestDeviceOrientationEvent) {
let eventMotion = window.latestDeviceMotionEvent;
let eventOrientation = latestDeviceOrientationEvent
collectData(
eventMotion.accelerationIncludingGravity.x,
eventMotion.accelerationIncludingGravity.y,
eventMotion.accelerationIncludingGravity.z,
eventOrientation.x,
eventOrientation.y,
eventOrientation.z
);
}
}, 100);
}
}
window.addEventListener("deviceorientation", (event) => {
latestDeviceOrientationEvent = {
x : event.alpha,
y : event.beta,
z : event.gamma
}
});
function draw() {
background(255);
fill(0);
textSize(16);
textAlign(LEFT, TOP);
text('Collecte: ' + state, 50, 50);
text('Label: ' + targetLabel, 50, 70);
text('Séquences collectées: ' + sequenceCounter, 50, 90);
}
// Gestionnaire d'événements pour l'accéléromètre
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', (event) => {
window.latestDeviceMotionEvent = event;
});
}