首先介绍什麽是 ml5,
ml5 是基於 TensorFlow.js 在浏览器中提供友善的机械学习使用介面。(https://github.com/ml5js/ml5-library)
接着备料,
<html>
<head>
<meta charset="UTF-8">
<title>Image classification using MobileNet and p5.js</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script>
</head>
<body>
<h1>Image classification using MobileNet and p5.js</h1>
<script src="sketch.js"></script>
</body>
</html>
// Initialize the Image Classifier method with MobileNet. A callback needs to be passed.
let classifier;
// A variable to hold the image we want to classify
let img;
function preload() {
classifier = ml5.imageClassifier('MobileNet');
img = loadImage('./images/dog.jpg');
}
function setup() {
createCanvas(400, 400);
classifier.classify(img, gotResult);
image(img, 0, 0);
}
// A function to run when we get any errors and the results
function gotResult(error, results) {
// Display error in the console
if (error) {
console.error(error);
} else {
// The results are in an array ordered by confidence.
console.log(results);
createDiv(`Label: ${results[0].label}`);
createDiv(`Confidence: ${nf(results[0].confidence, 0, 2)}`);
}
}
备料完成後,就可启动 Live Server,
启动方式可参照 (https://mnya.tw/cc/word/1430.html)。
启动後,会出现预设的浏览器视窗,
视窗内容应如下。
假设预设浏览器是 Chrome,按下 F12 键,在开发人员工具中,选择 Console 标签,
展开内容,应出现如下讯息。
ml5 所提供的机械学习介面告知我, dog.jpg 的图片内容最有可能为拉布拉多犬 (Labrador retriever),
我认为也是如此。
在 images 资料夹里放入一张 cat.jpg 的图片,并将 sketch.js 档案里的程序码,
img = loadImage('./images/dog.jpg');
改成
img = loadImage('./images/cat.jpg');
,结果如下。
辨识结果为花猫 (tabby, tabby cat),
与我的认知相同。
所以,透过 ml5 所提供的机械学习介面,可完成图片识别的需求。
人们经常会被资料抽象和封装混淆,把抽象的概念当作封装或信息隐藏。事实并非如此。以下定义来自 ISO/...
之前介绍的只是 JavaScript 的基本语法,今天要来介绍 DOM(Document Objec...
MVC模式的架构元件被设计用来处理开发中的应用程序的不同方面。MVC设计模式的作用是将表现层与业务逻...
OpenStack 早期在部署方面相当复杂也难以维护,但是在近期 DevOps 跟 Containe...
前言 在二十多岁的最後一年,决定再让自己任性一回,开始在 CMoney 参加工程师培训营,给自己半年...