图像分类的意义就是从图像中提取尽可能多的信息。例如,当您将图像上传到 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 进行图像分类的详细内容。