Skip to content

Commit

Permalink
Config input into sequencial data to fake temporal data
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienDabernat committed Nov 6, 2023
1 parent ecb9ce5 commit 56751b8
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 187 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions collect/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/addons/p5.sound.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8" />

</head>

<body>
<script src="sketch.js"></script>
<script src="/collect/sketch.js"></script>
</body>

</html>
141 changes: 89 additions & 52 deletions collect/sketch.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,129 @@
let brain;
let state = 'waiting';
let targetLabel; // Déclarer targetLabel à la portée globale
let targetLabel;
let rawData = []; // Stocke les données brutes de l'accéléromètre
let sequenceLength = 10; // La longueur de la séquence pour chaque axe
let sequenceCounter = 0; // Compteur pour le nombre de séquences ajoutées
let maxSequences = 40; // Nombre maximal de séquences à collecter
let collectionInterval; // Pour stocker l'identifiant de l'intervalle de collecte

// Variables pour les données d'accélération
let accelerationX = 0;
let accelerationY = 0;
let accelerationZ = 0;

// Charger un son pour le bip (remplacer 'bip.mp3' par le chemin de votre fichier son)
// Charger un son pour le bip
let bipSound;

function preload() {
bipSound = loadSound('../sound/bip.mp3'); // Assurez-vous que le fichier 'bip.mp3' est à la racine du projet
bipSound = loadSound('../sound/bip.mp3');
}

function setup() {
createCanvas(640, 480);
background(255);

// Écouter les événements d'accéléromètre
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', (event) => {
accelerationX = event.accelerationIncludingGravity.x;
accelerationY = event.accelerationIncludingGravity.y;
accelerationZ = event.accelerationIncludingGravity.z;
});
} else {
console.log("DeviceMotionEvent is not supported");
// 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);
}

let options = {
inputs: 3,
outputs: 4,
const options = {
inputs: inputs,
outputs: 3, // Nombre de labels de classification
task: 'classification',
debug: true
};

console.log(options)
brain = ml5.neuralNetwork(options);

// Création et configuration des boutons de collecte pour différents labels
createCollectButton('Ligne', 'line');
createCollectButton('Carré', 'square');
createCollectButton('Infini', 'infinite');
createCollectButton('Rien', 'nothing');
// Boutons pour la collecte de données
createCollectButton('Ligne', 'line', 1);
createCollectButton('Circle', 'circle', 2);
createCollectButton('Rien', 'nothing', 3);

// 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 createCollectButton(buttonText, label) {
function collectData(x, y, z) {
// Arrondir les valeurs à deux décimales
let roundedX = parseFloat(x.toFixed(3));
let roundedY = parseFloat(y.toFixed(3));
let roundedZ = parseFloat(z.toFixed(3));

rawData.push({ x: roundedX, y: roundedY, z: roundedZ });

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;
}
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') {
if (state === 'waiting' && sequenceCounter < maxSequences) {
targetLabel = label;
state = 'preparing';
console.log(`Preparing to collect for label: ${label}`);
setTimeout(() => startCollecting(label), 3000); // Commence la collecte après un délai de 3 secondes
setTimeout(() => startCollecting(), 1500);
}
}

function startCollecting(targetLabel) {
bipSound.play(); // Jouer le bip sonore
console.log(`Collecting for label: ${targetLabel}`);
state = 'collecting';
setTimeout(stopCollecting, 15000); // Arrête la collecte après 15 secondes
}

function stopCollecting() {
bipSound.play(); // Jouer le bip sonore
state = 'waiting';
console.log('Collection stopped');
brain.saveData('collectedData.json'); // Sauvegarde les données collectées
function startCollecting() {
if (state === 'preparing') {
bipSound.play();
state = 'collecting';
collectionInterval = setInterval(() => {
if (window.DeviceMotionEvent && window.latestDeviceMotionEvent) {
let event = window.latestDeviceMotionEvent;
collectData(
event.accelerationIncludingGravity.x,
event.accelerationIncludingGravity.y,
event.accelerationIncludingGravity.z
);
}
}, 100);
}
}

function draw() {
background(255);
fill(0);
noStroke();
textSize(32);
textSize(16);
textAlign(LEFT, TOP);
text(`X: ${accelerationX.toFixed(2)}`, 10, 40);
text(`Y: ${accelerationY.toFixed(2)}`, 10, 80);
text(`Z: ${accelerationZ.toFixed(2)}`, 10, 120);

if (state === 'collecting') {
let inputs = [accelerationX, accelerationY, accelerationZ];
let target = [targetLabel];
brain.addData(inputs, target);
}
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;
});
}
2 changes: 1 addition & 1 deletion model/model.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"modelTopology":{"class_name":"Sequential","config":[{"class_name":"Dense","config":{"units":16,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true,"batch_input_shape":[null,3],"dtype":"float32"}},{"class_name":"Dense","config":{"units":4,"activation":"softmax","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense2","trainable":true}}],"keras_version":"tfjs-layers 1.2.2","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./model.weights.bin"],"weights":[{"name":"dense_Dense1/kernel","shape":[3,16],"dtype":"float32"},{"name":"dense_Dense1/bias","shape":[16],"dtype":"float32"},{"name":"dense_Dense2/kernel","shape":[16,4],"dtype":"float32"},{"name":"dense_Dense2/bias","shape":[4],"dtype":"float32"}]}]}
{"modelTopology":{"class_name":"Sequential","config":[{"class_name":"Dense","config":{"units":16,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true,"batch_input_shape":[null,30],"dtype":"float32"}},{"class_name":"Dense","config":{"units":3,"activation":"softmax","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense2","trainable":true}}],"keras_version":"tfjs-layers 1.2.2","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./model.weights.bin"],"weights":[{"name":"dense_Dense1/kernel","shape":[30,16],"dtype":"float32"},{"name":"dense_Dense1/bias","shape":[16],"dtype":"float32"},{"name":"dense_Dense2/kernel","shape":[16,3],"dtype":"float32"},{"name":"dense_Dense2/bias","shape":[3],"dtype":"float32"}]}]}
Binary file modified model/model.weights.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion model/model_meta.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"inputUnits":[3],"outputUnits":4,"inputs":{"0":{"dtype":"number","min":-11.3,"max":18.900000000000002},"1":{"dtype":"number","min":-10.600000000000001,"max":4.4},"2":{"dtype":"number","min":-0.30000000000000004,"max":25}},"outputs":{"0":{"dtype":"string","min":0,"max":1,"uniqueValues":["nothing","line","square","infinite"],"legend":{"nothing":[1,0,0,0],"line":[0,1,0,0],"square":[0,0,1,0],"infinite":[0,0,0,1]}}},"isNormalized":true}
{"inputUnits":[30],"outputUnits":3,"inputs":{"x0":{"dtype":"number","min":-6.2,"max":7.8},"y0":{"dtype":"number","min":-3.9,"max":3.2},"z0":{"dtype":"number","min":4.7,"max":14},"x1":{"dtype":"number","min":-7.9,"max":6.7},"y1":{"dtype":"number","min":-3.7,"max":3.4},"z1":{"dtype":"number","min":2.1,"max":14.4},"x2":{"dtype":"number","min":-7.8,"max":6.4},"y2":{"dtype":"number","min":-4.1,"max":3.7},"z2":{"dtype":"number","min":2.6,"max":13.2},"x3":{"dtype":"number","min":-6.4,"max":6.9},"y3":{"dtype":"number","min":-3.4,"max":3.6},"z3":{"dtype":"number","min":3.9,"max":14.1},"x4":{"dtype":"number","min":-6.4,"max":8.7},"y4":{"dtype":"number","min":-4.4,"max":3.7},"z4":{"dtype":"number","min":4.1,"max":14.1},"x5":{"dtype":"number","min":-6.5,"max":6.7},"y5":{"dtype":"number","min":-4,"max":3.7},"z5":{"dtype":"number","min":2.7,"max":15.1},"x6":{"dtype":"number","min":-7.2,"max":7.6},"y6":{"dtype":"number","min":-3.9,"max":4.1},"z6":{"dtype":"number","min":6.5,"max":15.9},"x7":{"dtype":"number","min":-6.9,"max":6.9},"y7":{"dtype":"number","min":-3.9,"max":4.3},"z7":{"dtype":"number","min":4.8,"max":15.9},"x8":{"dtype":"number","min":-6.6,"max":7.4},"y8":{"dtype":"number","min":-3.5,"max":4},"z8":{"dtype":"number","min":4,"max":14.3},"x9":{"dtype":"number","min":-7,"max":7.5},"y9":{"dtype":"number","min":-3.4,"max":4.3},"z9":{"dtype":"number","min":5.4,"max":13.8}},"outputs":{"label":{"dtype":"string","min":0,"max":1,"uniqueValues":["nothing","circle","line"],"legend":{"nothing":[1,0,0],"circle":[0,1,0],"line":[0,0,1]}}},"isNormalized":true}
1 change: 1 addition & 0 deletions model2/model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"modelTopology":{"class_name":"Sequential","config":[{"class_name":"Dense","config":{"units":16,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true,"batch_input_shape":[null,30],"dtype":"float32"}},{"class_name":"Dense","config":{"units":3,"activation":"softmax","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense2","trainable":true}}],"keras_version":"tfjs-layers 1.2.2","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./model.weights.bin"],"weights":[{"name":"dense_Dense1/kernel","shape":[30,16],"dtype":"float32"},{"name":"dense_Dense1/bias","shape":[16],"dtype":"float32"},{"name":"dense_Dense2/kernel","shape":[16,3],"dtype":"float32"},{"name":"dense_Dense2/bias","shape":[3],"dtype":"float32"}]}]}
Binary file added model2/model.weights.bin
Binary file not shown.
1 change: 1 addition & 0 deletions model2/model_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"inputUnits":[30],"outputUnits":3,"inputs":{"x0":{"dtype":"number","min":-6.2,"max":7.8},"y0":{"dtype":"number","min":-3.9,"max":3.2},"z0":{"dtype":"number","min":4.7,"max":14},"x1":{"dtype":"number","min":-7.9,"max":6.7},"y1":{"dtype":"number","min":-3.7,"max":3.4},"z1":{"dtype":"number","min":2.1,"max":14.4},"x2":{"dtype":"number","min":-7.8,"max":6.4},"y2":{"dtype":"number","min":-4.1,"max":3.7},"z2":{"dtype":"number","min":2.6,"max":13.2},"x3":{"dtype":"number","min":-6.4,"max":6.9},"y3":{"dtype":"number","min":-3.4,"max":3.6},"z3":{"dtype":"number","min":3.9,"max":14.1},"x4":{"dtype":"number","min":-6.4,"max":8.7},"y4":{"dtype":"number","min":-4.4,"max":3.7},"z4":{"dtype":"number","min":4.1,"max":14.1},"x5":{"dtype":"number","min":-6.5,"max":6.7},"y5":{"dtype":"number","min":-4,"max":3.7},"z5":{"dtype":"number","min":2.7,"max":15.1},"x6":{"dtype":"number","min":-7.2,"max":7.6},"y6":{"dtype":"number","min":-3.9,"max":4.1},"z6":{"dtype":"number","min":6.5,"max":15.9},"x7":{"dtype":"number","min":-6.9,"max":6.9},"y7":{"dtype":"number","min":-3.9,"max":4.3},"z7":{"dtype":"number","min":4.8,"max":15.9},"x8":{"dtype":"number","min":-6.6,"max":7.4},"y8":{"dtype":"number","min":-3.5,"max":4},"z8":{"dtype":"number","min":4,"max":14.3},"x9":{"dtype":"number","min":-7,"max":7.5},"y9":{"dtype":"number","min":-3.4,"max":4.3},"z9":{"dtype":"number","min":5.4,"max":13.8}},"outputs":{"label":{"dtype":"string","min":0,"max":1,"uniqueValues":["nothing","circle","line"],"legend":{"nothing":[1,0,0],"circle":[0,1,0],"line":[0,0,1]}}},"isNormalized":true}
2 changes: 1 addition & 1 deletion run/run.html → run/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</head>

<body>
<script src="run.js"></script>
<script src="/run/sketch.js"></script>
</body>

</html>
104 changes: 0 additions & 104 deletions run/run.js

This file was deleted.

Loading

0 comments on commit 56751b8

Please sign in to comment.