Skip to content

Commit 9d7f1a6

Browse files
authored
Merge pull request #32 from ml5js/format-documents
format all js documents
2 parents 2e70dd3 + aa776f1 commit 9d7f1a6

File tree

16 files changed

+809
-676
lines changed

16 files changed

+809
-676
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/dist
2+
/coverage

examples/Handpose/sketch.js

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
let handpose;
2-
let video;
3-
let hands = [];
4-
5-
function setup() {
6-
createCanvas(640, 480);
7-
8-
// Create the video and hide it
9-
video = createCapture(VIDEO);
10-
video.size(width, height);
11-
video.hide();
12-
13-
// Load the model and attach an event
14-
handpose = ml5.handpose(video, modelReady);
15-
handpose.on("hand", gotHands);
16-
}
17-
18-
// Event for hand detection
19-
function gotHands(results) {
20-
// Always save the latest output from the model in global variable "hands"
21-
hands = results;
22-
}
23-
24-
// Event for when model loaded
25-
function modelReady() {
26-
console.log("Model ready!");
27-
}
28-
29-
function draw() {
30-
// Draw the video
31-
image(video, 0, 0, width, height);
32-
33-
// Draw all the tracked hand points
34-
for (let i = 0; i < hands.length; i++) {
35-
let hand = hands[i];
36-
for (let j = 0; j < hand.keypoints.length; j++) {
37-
let keypoint = hand.keypoints[j];
38-
fill(0, 255, 0);
39-
noStroke();
40-
circle(keypoint.x, keypoint.y, 10);
41-
}
42-
}
43-
}
1+
let handpose;
2+
let video;
3+
let hands = [];
4+
5+
function setup() {
6+
createCanvas(640, 480);
7+
8+
// Create the video and hide it
9+
video = createCapture(VIDEO);
10+
video.size(width, height);
11+
video.hide();
12+
13+
// Load the model and attach an event
14+
handpose = ml5.handpose(video, modelReady);
15+
handpose.on("hand", gotHands);
16+
}
17+
18+
// Event for hand detection
19+
function gotHands(results) {
20+
// Always save the latest output from the model in global variable "hands"
21+
hands = results;
22+
}
23+
24+
// Event for when model loaded
25+
function modelReady() {
26+
console.log("Model ready!");
27+
}
28+
29+
function draw() {
30+
// Draw the video
31+
image(video, 0, 0, width, height);
32+
33+
// Draw all the tracked hand points
34+
for (let i = 0; i < hands.length; i++) {
35+
let hand = hands[i];
36+
for (let j = 0; j < hand.keypoints.length; j++) {
37+
let keypoint = hand.keypoints[j];
38+
fill(0, 255, 0);
39+
noStroke();
40+
circle(keypoint.x, keypoint.y, 10);
41+
}
42+
}
43+
}
Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
1-
// Step 1: load data or create some data
2-
let data = [
3-
{ r: 255, g: 0, b: 0, color: "red-ish" },
4-
{ r: 254, g: 0, b: 0, color: "red-ish" },
5-
{ r: 253, g: 0, b: 0, color: "red-ish" },
6-
{ r: 0, g: 255, b: 0, color: "green-ish" },
7-
{ r: 0, g: 254, b: 0, color: "green-ish" },
8-
{ r: 0, g: 253, b: 0, color: "green-ish" },
9-
{ r: 0, g: 0, b: 255, color: "blue-ish" },
10-
{ r: 0, g: 0, b: 254, color: "blue-ish" },
11-
{ r: 0, g: 0, b: 253, color: "blue-ish" },
12-
];
13-
14-
let classifer;
15-
let r = 255;
16-
let g = 0;
17-
let b = 0;
18-
let rSlider, gSlider, bSlider;
19-
let label = "training";
20-
21-
function setup() {
22-
createCanvas(640, 240);
23-
rSlider = createSlider(0, 255, 255).position(10, 20);
24-
gSlider = createSlider(0, 255, 0).position(10, 40);
25-
bSlider = createSlider(0, 255, 0).position(10, 60);
26-
27-
// Step 2: set your neural network options
28-
let options = {
29-
task: "classification",
30-
debug: true,
31-
};
32-
33-
// Step 3: initialize your neural network
34-
classifier = ml5.neuralNetwork(options);
35-
36-
// Step 4: add data to the neural network
37-
for (let i = 0; i < data.length; i++) {
38-
let item = data[i];
39-
let inputs = [item.r, item.g, item.b];
40-
let outputs = [item.color];
41-
classifier.addData(inputs, outputs);
42-
}
43-
44-
// Step 5: normalize your data;
45-
classifier.normalizeData();
46-
47-
// Step 6: train your neural network
48-
const trainingOptions = {
49-
epochs: 32,
50-
batchSize: 12,
51-
};
52-
classifier.train(trainingOptions, finishedTraining);
53-
}
54-
// Step 7: use the trained model
55-
function finishedTraining() {
56-
classify();
57-
}
58-
59-
// Step 8: make a classification
60-
function classify() {
61-
const input = [r, g, b];
62-
classifier.classify(input, handleResults);
63-
}
64-
65-
function draw() {
66-
r = rSlider.value();
67-
g = gSlider.value();
68-
b = bSlider.value();
69-
background(r, g, b);
70-
textAlign(CENTER, CENTER);
71-
textSize(64);
72-
text(label, width / 2, height / 2);
73-
}
74-
75-
// Step 9: define a function to handle the results of your classification
76-
function handleResults(results, error) {
77-
if (error) {
78-
console.error(error);
79-
return;
80-
}
81-
label = results[0].label;
82-
// console.log(results); // {label: 'red', confidence: 0.8};
83-
classify();
84-
}
1+
// Step 1: load data or create some data
2+
let data = [
3+
{ r: 255, g: 0, b: 0, color: "red-ish" },
4+
{ r: 254, g: 0, b: 0, color: "red-ish" },
5+
{ r: 253, g: 0, b: 0, color: "red-ish" },
6+
{ r: 0, g: 255, b: 0, color: "green-ish" },
7+
{ r: 0, g: 254, b: 0, color: "green-ish" },
8+
{ r: 0, g: 253, b: 0, color: "green-ish" },
9+
{ r: 0, g: 0, b: 255, color: "blue-ish" },
10+
{ r: 0, g: 0, b: 254, color: "blue-ish" },
11+
{ r: 0, g: 0, b: 253, color: "blue-ish" },
12+
];
13+
14+
let classifer;
15+
let r = 255;
16+
let g = 0;
17+
let b = 0;
18+
let rSlider, gSlider, bSlider;
19+
let label = "training";
20+
21+
function setup() {
22+
createCanvas(640, 240);
23+
rSlider = createSlider(0, 255, 255).position(10, 20);
24+
gSlider = createSlider(0, 255, 0).position(10, 40);
25+
bSlider = createSlider(0, 255, 0).position(10, 60);
26+
27+
// Step 2: set your neural network options
28+
let options = {
29+
task: "classification",
30+
debug: true,
31+
};
32+
33+
// Step 3: initialize your neural network
34+
classifier = ml5.neuralNetwork(options);
35+
36+
// Step 4: add data to the neural network
37+
for (let i = 0; i < data.length; i++) {
38+
let item = data[i];
39+
let inputs = [item.r, item.g, item.b];
40+
let outputs = [item.color];
41+
classifier.addData(inputs, outputs);
42+
}
43+
44+
// Step 5: normalize your data;
45+
classifier.normalizeData();
46+
47+
// Step 6: train your neural network
48+
const trainingOptions = {
49+
epochs: 32,
50+
batchSize: 12,
51+
};
52+
classifier.train(trainingOptions, finishedTraining);
53+
}
54+
// Step 7: use the trained model
55+
function finishedTraining() {
56+
classify();
57+
}
58+
59+
// Step 8: make a classification
60+
function classify() {
61+
const input = [r, g, b];
62+
classifier.classify(input, handleResults);
63+
}
64+
65+
function draw() {
66+
r = rSlider.value();
67+
g = gSlider.value();
68+
b = bSlider.value();
69+
background(r, g, b);
70+
textAlign(CENTER, CENTER);
71+
textSize(64);
72+
text(label, width / 2, height / 2);
73+
}
74+
75+
// Step 9: define a function to handle the results of your classification
76+
function handleResults(results, error) {
77+
if (error) {
78+
console.error(error);
79+
return;
80+
}
81+
label = results[0].label;
82+
// console.log(results); // {label: 'red', confidence: 0.8};
83+
classify();
84+
}

0 commit comments

Comments
 (0)