Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit 13962cd

Browse files
yining1023joeyklee
authored andcommitted
ml5.imageClassifier load model from url (#156)
* imageClassifier load a model from url * added options to local model from local files * use preload to load model * added one comment * classify(video, callback)
1 parent cca4fda commit 13962cd

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<html>
2+
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Webcam Image Classification using a pre-trianed customized model and p5.js</title>
6+
7+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>
8+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script>
9+
10+
<script src="http://localhost:8080/ml5.js" type="text/javascript"></script>
11+
</head>
12+
13+
<body>
14+
<h1>Webcam Image Classification using a pre-trianed customized model and p5.js</h1>
15+
<p>If you cover your webcam, this model will classify it as "1", otherwise will classify anything else else "0" </p>
16+
<script src="sketch.js"></script>
17+
</body>
18+
19+
</html>

p5js/ImageClassification/ImageClassification_Video_Load/model/image-model.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2018 ml5
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
/* ===
7+
ml5 Example
8+
Webcam Image Classification using a pre-trianed customized model and p5.js
9+
This example uses p5 preload function to create the classifier
10+
=== */
11+
12+
const checkpoint = 'https://storage.googleapis.com/tm-pro-a6966.appspot.com/eyeo-test-yining/model.json';
13+
let classifier;
14+
let video;
15+
let resultsP;
16+
17+
function preload() {
18+
// Create a camera input
19+
video = createCapture(VIDEO);
20+
// Initialize the Image Classifier method with a pre-trained customized model and the video as the second argument
21+
classifier = ml5.imageClassifier(checkpoint);
22+
}
23+
24+
function setup() {
25+
noCanvas();
26+
// ml5 also supports using callback pattern to create the classifier
27+
// classifier = ml5.imageClassifier(checkpoint, video, modelReady);
28+
// If you would like to load the model from local files
29+
// classifier = ml5.imageClassifier('model/image-model.json', video, modelReady);
30+
resultsP = createP('Loading model and video...');
31+
classifyVideo();
32+
}
33+
34+
// Get a prediction for the current video frame
35+
function classifyVideo() {
36+
classifier.classify(video, gotResult);
37+
}
38+
39+
// If you use callback pattern to create the classifier, you can use the following callback function
40+
// function modelReady() {
41+
// console.log('Model Ready');
42+
// classifyVideo();
43+
// }
44+
45+
// When we get a result
46+
function gotResult(err, results) {
47+
// The results are in an array ordered by confidence.
48+
resultsP.html('Label: ' + results[0].label + ' ' + nf(results[0].confidence, 0, 2));
49+
classifyVideo();
50+
}

0 commit comments

Comments
 (0)