使用 JavaScript 进行图像分类
图像分类的意义就是从图像中提取尽可能多的信息。例如,当您将图像上传到 Google 相册时,它会从图像中提取信息并根据该信息建议位置。
我们可以使用OpenCV检测图像中的每一个微小信息并预测图像。
使用 JavaScript 从头开始训练和测试模型需要付出大量的努力,而且还需要包含不同图像的正确数据集。因此,在本教程中,我们将使用ml5.js的预训练模型对图像进行分类。
ml5.js 库包含各种预先训练的模型,使开发人员的工作更轻松。此外,它还使用浏览器的 GPU 来执行数学运算,使其更加高效。
语法
用户可以按照以下语法使用ml5.js库对图像进行分类。
image_classifier.predict(image, function (err, outputs) { if (err) { return alert(err); } else { output.innerText = outputs[0].label; } });
在上述语法中,“image_classifier”是从 ml5.js 库导入的预训练图像分类模型。我们通过传递图像作为第一个参数和回调函数作为第二个参数来调用“预测”方法。在回调函数中,我们得到输出或错误。
步骤
第 1 步 - 使用 CDN 在网页代码中添加“ml5.js”库。
第 2 步 - 添加输入以上传文件并分类按钮。
第 3 步 - 在 JavaScript 中,从 ml5.js 访问所需的 HTML 元素和“MobileNet”模型。另外,模型加载完成后执行 modelLoad() 函数。
步骤 4 - 之后,每当用户上传图像时,都会触发事件并在回调函数中读取图像。另外,在屏幕上显示图像。
步骤 5 - 当用户按下分类图像按钮时,使用图像分类器的预测方法来预测有关图像的信息。
示例 1
在下面的示例中,我们通过 CDN 将“ml5.js”库添加到
部分。之后,每当用户上传图像时,我们都会读取它并将其显示在屏幕上。接下来,当用户按下分类按钮时,我们使用预测方法从图像中提取特征。在输出中,用户可以在图像下方显示有关图像的信息。<html> <head> <script src="https://unpkg.com/ml5@latest/dist/ml5.min.js"></script> </head> <body> <h2>Creating the <i> Image classifier </i> using the ml5.js in JavaScript.</h2> <h4 id = "content"> Wait until model loads. </h4> <input type = "file" name = "Image" id = "upload_image" accept = "jpg,jpeg,png"> <br> <br> <img src = "" class = "image" id = "show_image" width = "300px" height = "300px"> <br> <button class = "button" id = "triggerClassify"> Classify the image </button> <br> <h2 id = "output"> </h2> <script> window.onload = function () { // access all HTML elements and image classifier const image_classifier = ml5.imageClassifier("MobileNet", modelLoaded); const triggerClassify = document.getElementById("triggerClassify"); const upload_image = document.getElementById("upload_image"); const show_image = document.getElementById("show_image"); const output = document.getElementById("output"); // when the model is loaded, show the message function modelLoaded() { let content = document.getElementById("content"); content.innerText = "Model is loaded! Now, test it by uploading the image."; } // When the user uploads the image, show it on the screen upload_image.onchange = function () { if (this.files && this.files[0]) { // using FileReader to read the image var reader = new FileReader(); reader.onload = function (e) { show_image.src = e.target.result; }; reader.readAsDataURL(this.files[0]); } }; // classify the image when the user clicks the button triggerClassify.onclick = function (e) { // predict the image using the model image_classifier.predict(show_image, function (err, outputs) { if (err) { return err; } else { // show the output output.innerText = outputs[0].label; } }); }; } </script> </body> </html>
示例
在下面的示例中,用户可以将图像链接粘贴到输入字段中。之后,每当他们按下获取图像按钮时,它就会在网页上显示图像。接下来,当用户单击分类图像按钮时,他们可以在屏幕上看到包含图像信息的输出。
<html> <head> <script src="https://unpkg.com/ml5@latest/dist/ml5.min.js"></script> </head> <body> <h2>Creating the <i> Image classifier </i> using the ml5.js in JavaScript.</h2> <h4 id = "content"> Wait until model loads. </h4> <input type = "text" id = "link_input" placeholder = "Paste image link here"> <button id = "fetch_image"> Fetch Image </button> <br> <br> <img src = "" id = "show_image" width = "300px" height = "300px" crossorigin = "anonymous"> <img src = "" class = "image" id = "imageView"> <br> <button class = "button" id = "triggerClassify"> Classify the image </button> <br> <h2 id = "output"> </h2> <script> window.onload = function () { // access all HTML elements and image classifier const image_classifier = ml5.imageClassifier("MobileNet", modelLoaded); const triggerClassify = document.getElementById("triggerClassify"); let link_input = document.getElementById("link_input"); const show_image = document.getElementById("show_image"); const output = document.getElementById("output"); // when the model is loaded, show the message function modelLoaded() { let content = document.getElementById("content"); content.innerText = "Model is loaded! Now, test it by uploading the image."; } fetch_image.onclick = function (e) { let link = link_input.value; console.log(link); if (link != null && link != undefined) { show_image.src = link; } }; triggerClassify.onclick = function (e) { image_classifier.predict(show_image, function (err, outputs) { if (err) { console.error(err); } else { output.innerText = outputs[0].label; } }); }; } </script> </body> </html>
用户学会了使用 JavaScript 中的预训练模型对图像进行分类。我们使用“ml5.js”库来提取图像特征。我们可以使用现实生活中的图像分类对图像进行分类。此外,图像分类还有许多其他用例。
以上是使用 JavaScript 进行图像分类的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

本文讨论了使用浏览器开发人员工具的有效JavaScript调试,专注于设置断点,使用控制台和分析性能。

本文说明了如何使用源地图通过将其映射回原始代码来调试JAVASCRIPT。它讨论了启用源地图,设置断点以及使用Chrome DevTools和WebPack之类的工具。

Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

本教程将介绍如何使用 Chart.js 创建饼图、环形图和气泡图。此前,我们已学习了 Chart.js 的四种图表类型:折线图和条形图(教程二),以及雷达图和极地区域图(教程三)。 创建饼图和环形图 饼图和环形图非常适合展示某个整体被划分为不同部分的比例。例如,可以使用饼图展示野生动物园中雄狮、雌狮和幼狮的百分比,或不同候选人在选举中获得的投票百分比。 饼图仅适用于比较单个参数或数据集。需要注意的是,饼图无法绘制值为零的实体,因为饼图中扇形的角度取决于数据点的数值大小。这意味着任何占比为零的实体

深入探讨console.log输出差异的根源本文将分析一段代码中console.log函数输出结果的差异,并解释其背后的原因。�...
