env.allowLocalModels = false;

Step2:文件上傳事件監聽器

const fileUpload = document.getElementById('file-upload');
const imageContainer = document.getElementById('image-container')
fileUpload.addEventListener('change', function (e) {
// 當選擇文件時觸發的事件監聽器
});

Step3:FileReader 讀取上傳的圖像

const reader = new FileReader();
reader.onload = function (e2) {
// 文件讀取完成時執行的函數
};
reader.readAsDataURL(file)

Step4:顯示上傳的圖像

javascriptCopy code
const image = document.createElement('img');
image.src = e2.target.result;
imageContainer.appendChild(image)

Step5:啟動AI檢測

detect(image)

Step6:使用AI模型進行對象檢測

const detector = await pipeline("object-detection", "Xenova/detr-resnet-50")
const output = await detector(image.src, {
threshold: 0.1,
percentage: true
})

Step7:渲染檢測到的框

output.forEach(renderBox)

Step8:渲染邊界框

function renderBox({ box, label }) {
// 渲染邊界框的函數
}

完整代碼

<!--
* @func 文件上傳和對象檢測功能
* @desc 實現了圖片上傳功能,并利用Transformer模型進行對象檢測,并在圖片上標記檢測到的對象
* @author [Your Name]
* @data 2024-04-17
-->
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>nlp之圖片識別,兩種語言</title>
<!-- CSS 樣式 -->
<style>
.container {
margin: 40px auto;
width: max(50vw, 400px);
display: flex;
flex-direction: column;
align-items: center;
}

.custom-file-upload {
display: flex;
align-items: center;
cursor: pointer;
gap: 10px;
border: 2px solid black;
padding: 8px 16px;
border-radius: 6px;
}

#file-upload {
display: none;
}

#image-container {
width: 100%;
margin-top: 20px;
position: relative;
}

#image-container>img {
width: 100%;
}

.bounding-box {
position: absolute;
box-sizing: border-box;
}

.bounding-box-label {
position: absolute;
color: white;
font-size: 12px;
}
</style>
</head>

<body>
<!-- 頁面主體內容 -->
<main class="container">
<label for="file-upload" class="custom-file-upload">
<input type="file" accept="image/*" id="file-upload">
上傳圖片
</label>
<div id="image-container"></div>
<p id="status"></p>
</main>

<!-- JavaScript 代碼 -->
<script type="module">
// 導入transformers nlp任務的pipeline和env對象
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0"
// 允許本地模型
env.allowLocalModels = false;

// 獲取文件上傳和圖片容器元素
const fileUpload = document.getElementById('file-upload');
const imageContainer = document.getElementById('image-container')

// 監聽文件上傳事件
fileUpload.addEventListener('change', function (e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function (e2) {
const image = document.createElement('img');
image.src = e2.target.result;
imageContainer.appendChild(image)
detect(image)
}
reader.readAsDataURL(file)
})

// 獲取狀態信息元素
const status = document.getElementById('status');

// 檢測圖片的AI任務
const detect = async (image) => {
status.textContent = "分析中..."
const detector = await pipeline("object-detection", "Xenova/detr-resnet-50")
const output = await detector(image.src, {
threshold: 0.1,
percentage: true
})
output.forEach(renderBox)
}

// 渲染檢測框函數
function renderBox({ box, label }) {
const { xmax, xmin, ymax, ymin } = box
const boxElement = document.createElement("div");
boxElement.className = "bounding-box"
Object.assign(boxElement.style, {
borderColor: '#123123',
borderWidth: '1px',
borderStyle: 'solid',
left: 100 * xmin + '%',
top: 100 * ymin + '%',
width: 100 * (xmax - xmin) + "%",
height: 100 * (ymax - ymin) + "%"
})

const labelElement = document.createElement('span');
labelElement.textContent = label;
labelElement.className = "bounding-box-label"
labelElement.style.backgroundColor = '#000000'

boxElement.appendChild(labelElement);
imageContainer.appendChild(boxElement);
}
</script>
</body>

</html>


效果圖

還需要調整參數,加強精確度

總結

這篇文章,我們探討了將AI對象檢測與前端Web開發無縫集成的方法。通過按照所述步驟并利用現成的AI庫,開發人員可以為其Web應用程序增加強大的圖像識別功能。這種AI和前端技術的融合為在Web上創建智能和交互式用戶體驗開啟了廣闊的可能性。

文章轉自微信公眾號@web前端開發營

上一篇:

Transformers 框架 Pipeline 任務詳解:文本分類(text-classification)

下一篇:

AI對抗訓練:解密數字水印和圖片篡改檢測技術
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費