影像分類的意義就是從影像中提取盡可能多的信息。例如,當您將圖像上傳到 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 id="Creating-the-i-Image-classifier-i-using-the-ml-js-in-JavaScript">Creating the <i> Image classifier </i> using the ml5.js in JavaScript.</h2> <h4 id="Wait-until-model-loads"> Wait until model loads. </h4> <input type = "file" name = "Image" id = "upload_image" accept = "jpg,jpeg,png"> <br> <br> <img src="/static/imghwm/default1.png" data-src="https://unpkg.com/ml5@latest/dist/ml5.min.js" class="lazy" src = "" class = "image" id = "show_image" width = "300px" height = "300px" alt="使用 JavaScript 進行圖像分類" > <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></script> </head> <body> <h2 id="Creating-the-i-Image-classifier-i-using-the-ml-js-in-JavaScript">Creating the <i> Image classifier </i> using the ml5.js in JavaScript.</h2> <h4 id="Wait-until-model-loads"> 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" alt="使用 JavaScript 進行圖像分類" > <img src = "" class = "image" id = "imageView" alt="使用 JavaScript 進行圖像分類" > <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中文網其他相關文章!

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver Mac版
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器