机器学习:使用React.js + ml5.js构建图像识别应用程序
author:一佰互联 2019-03-30   click:193

简介:作为Web开发人员,我们知道可以在浏览器中运行机器学习(Machine Learning)。但从哪里开始呢?2018年3月30日,TensorFlow团队展示了他们著名的机器学习(ML)框架TensorFlow的Javascript版本。TensorFlow.js允许我们 ...

机器学习:使用React.js + ml5.js构建图像识别应用程序

作为Web开发人员,我们知道可以在浏览器中运行机器学习(Machine Learning)。但从哪里开始呢?2018年3月30日,TensorFlow团队展示了他们著名的机器学习(ML)框架TensorFlow的Javascript版本。TensorFlow.js允许我们导入预训练的模型,重新训练现有的导入模型,最后使用Javascript完全在浏览器中定义,训练和运行机器学习模型。在这篇文章中,我们将使用React.js和一个名为ml5.js(https://ml5js.org/)的机器学习库构建一个简单的图像识别应用程序。ml5.js构建于Tensorflow.js之上,是一个友好的高级界面,可以访问浏览器中的机器学习算法和模型。首先,我们将使用终端创建一个名为图像识别的React.js应用程序,使用create-react-app包:
create-react-app image-recognition
现在,在App.js文件里面,我们将删除所有不必要的代码,只用ML5.js呈现一个名为Image classification的标题。

机器学习:使用React.js + ml5.js构建图像识别应用程序

我们要分类的图像是这张惊人的老虎画面(tiger.jpg):

机器学习:使用React.js + ml5.js构建图像识别应用程序

完成后,我们将在h1标签下方的应用程序中显示图像:

机器学习:使用React.js + ml5.js构建图像识别应用程序

ml5.js入门

完成后,我们将开始实施ml5.js. 该库将使我们的应用程序能够识别我们刚刚下载的图像上的内容。首先,使用终端安装ml5.js库:
yarn add ml5
安装完成后,我们将从ml5.js库中导入所有内容。然后,我们将在App组件中创建一个名为classifyImg的函数,该函数将:
  1. 使用MobileNet初始化Image Classifier方法。
  2. 加载模型。
  3. 将图像放入一个名为image的变量中。
  4. 获取5个模型的预测并在控制台中输出它们。
创建classifyImg函数后,我们将在ComponentDidMount生命周期方法中调用它,js代码如下:

机器学习:使用React.js + ml5.js构建图像识别应用程序

您应该在控制台中看到按概率排序的5个预测对象的数组。第一个预测应该是“老虎,Panthera tigris”,这是一个非常准确的猜测。一旦我们有了5个预测,我们就会将它们保存在App组件的状态中。为此,我们需要:
  1. 用一个空数组作为值来初始化App组件状态。。
  2. 创建一个名为setPredictions的函数,该函数将模型预测数组作为参数,并使用这5个预测更新预测状态(以前为空数组)。
  3. 我们将返回预测并使用Promises链接来调用新创建的setPredictions函数,而不是在控制台中打印结果,并将模型预测作为参数。
Javascript代码如下:

机器学习:使用React.js + ml5.js构建图像识别应用程序

在浏览器中显示结果!

既然我们已经在App组件状态中进行了预测,那么我们要做的最后一件事就是将预测输出给用户。为此,在render方法中,我们将创建一个预测变量,该变量将在加载时存储默认值(现在div为空)。一旦模型做出预测并且它们存储在状态中,我们将映射预测数组以显示(1)模型的预测和(2)5个预测中的每个预测的概率。最后,我们将在我们的应用程序中显示预测,就在我们的 老虎图像下方,Javascript代码如下:
import React, { Component } from "react";import "./App.css";import tiger from "./tiger.jpg";// Importing ml5.js as ml5import * as ml5 from "ml5";class App extends Component { state = { predictions: [] // Set the empty array predictions state } setPredictions = (pred) => { // Set the prediction state with the model predictions this.setState({ predictions: pred }); } classifyImg = () => { // Initialize the Image Classifier method with MobileNet const classifier = ml5.imageClassifier("MobileNet", modelLoaded); // When the model is loaded function modelLoaded() { console.log("Model Loaded!"); } // Put the image to classify inside a variable const image = document.getElementById("image"); // Make a prediction with a selected image classifier.predict(image, 5, function(err, results) { // Return the results return results; }) .then((results) => { // Set the predictions in the state this.setPredictions(results) }) } componentDidMount(){ // once the component has mount, start the classification this.classifyImg(); } render() { // First set the predictions to a default value while loading let predictions = (<div className="loader"></div>); // Map over the predictions and return each prediction with probability if(this.state.predictions.length > 0){ predictions = this.state.predictions.map((pred, i) => { let { className, probability } = pred; // round the probability with 2 decimal probability = Math.floor(probability * 10000) / 100 + "%"; return ( <div key={ i + "" }>{ i+1 }. Prediction: { className } at { probability } </div> ) }) }	  return ( <div className="App"> <h1>Image classification with ML5.js</h1> <img src={ tiger } id="image" width="400" alt="" /> { predictions } </div> ); }}export default App;

机器学习:使用React.js + ml5.js构建图像识别应用程序

现在您的应用程序已完全正常工作,您可以创建自己的样式或删除App.css文件中的所有内容,并将以下样式复制并粘贴到其中:
h1 { margin: 0 0 1.4rem;}.App { padding: 1rem 0; display: flex; flex-direction: column; justify-content: center; align-items: center; min-height: calc(100vh - 2rem);}.App div { background-color: #eff6fe; width: 300px; padding: 10px 50px;}.App div:nth-of-type(odd) { background-color: #dfeeff;}div.loader { animation: loader-spin infinite 1.2s linear; padding: 0; background-color: transparent !important; height: 50px; width: 50px; border: 5px solid #eee; border-bottom: 5px solid #007bff; border-radius: 50% !important; margin: 2rem;}@keyframes loader-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); }}

机器学习:使用React.js + ml5.js构建图像识别应用程序

这是最终样式:

机器学习:使用React.js + ml5.js构建图像识别应用程序

本文仅代表作者个人观点,不代表巅云官方发声,对观点有疑义请先联系作者本人进行修改,若内容非法请联系平台管理员,邮箱2522407257@qq.com。更多相关资讯,请到巅云www.yx10011.com学习互联网营销技术请到巅云建站www.yx10011.com。