
如何快速實(shí)現(xiàn)REST API集成以優(yōu)化業(yè)務(wù)流程
env.allowLocalModels = false;
pipeline
和 env
是從 @xenova/transformers
包中導(dǎo)入的。env.allowLocalModels = false;
?將環(huán)境變量?allowLocalModels
?設(shè)置為?false
,表示不允許使用本地模型。const fileUpload = document.getElementById('file-upload');
const imageContainer = document.getElementById('image-container')
fileUpload.addEventListener('change', function (e) {
// 當(dāng)選擇文件時(shí)觸發(fā)的事件監(jiān)聽(tīng)器
});
const reader = new FileReader();
reader.onload = function (e2) {
// 文件讀取完成時(shí)執(zhí)行的函數(shù)
};
reader.readAsDataURL(file)
FileReader
對(duì)象。onload
事件監(jiān)聽(tīng)器被附加到讀取器上,當(dāng)文件讀取完成時(shí)執(zhí)行一個(gè)函數(shù)。readAsDataURL
?方法在讀取器上被調(diào)用,將所選文件的內(nèi)容讀取為數(shù)據(jù)URL。javascriptCopy code
const image = document.createElement('img');
image.src = e2.target.result;
imageContainer.appendChild(image)
onload
函數(shù)內(nèi)部,創(chuàng)建了一個(gè) <img>
元素。src
屬性設(shè)置為讀取文件作為數(shù)據(jù)URL的結(jié)果。detect(image)
detect
函數(shù)。const detector = await pipeline("object-detection", "Xenova/detr-resnet-50")
const output = await detector(image.src, {
threshold: 0.1,
percentage: true
})
pipeline
函數(shù)從指定的模型("Xenova/detr-resnet-50"
)實(shí)例化一個(gè)對(duì)象檢測(cè)模型("object-detection"
)。await
等待 detector
對(duì)象,確保模型完全加載后再繼續(xù)。detector
對(duì)象在上傳的圖像上執(zhí)行對(duì)象檢測(cè)。output
?包含對(duì)象檢測(cè)任務(wù)的結(jié)果。output.forEach(renderBox)
renderBox
?函數(shù)以渲染邊界框。function renderBox({ box, label }) {
// 渲染邊界框的函數(shù)
}
renderBox
函數(shù)接受一個(gè)具有 box
(邊界框的坐標(biāo))和 label
(檢測(cè)到的對(duì)象的標(biāo)簽)的對(duì)象。<div>
元素來(lái)表示邊界框。<span>
元素來(lái)在邊界框內(nèi)顯示檢測(cè)到的對(duì)象的標(biāo)簽。<!--
* @func 文件上傳和對(duì)象檢測(cè)功能
* @desc 實(shí)現(xiàn)了圖片上傳功能,并利用Transformer模型進(jìn)行對(duì)象檢測(cè),并在圖片上標(biāo)記檢測(cè)到的對(duì)象
* @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之圖片識(shí)別,兩種語(yǔ)言</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>
<!-- 頁(yè)面主體內(nèi)容 -->
<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">
// 導(dǎo)入transformers nlp任務(wù)的pipeline和env對(duì)象
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')
// 監(jiān)聽(tīng)文件上傳事件
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)
})
// 獲取狀態(tài)信息元素
const status = document.getElementById('status');
// 檢測(cè)圖片的AI任務(wù)
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)
}
// 渲染檢測(cè)框函數(shù)
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>
還需要調(diào)整參數(shù),加強(qiáng)精確度
這篇文章,我們探討了將AI對(duì)象檢測(cè)與前端Web開(kāi)發(fā)無(wú)縫集成的方法。通過(guò)按照所述步驟并利用現(xiàn)成的AI庫(kù),開(kāi)發(fā)人員可以為其Web應(yīng)用程序增加強(qiáng)大的圖像識(shí)別功能。這種AI和前端技術(shù)的融合為在Web上創(chuàng)建智能和交互式用戶體驗(yàn)開(kāi)啟了廣闊的可能性。
文章轉(zhuǎn)自微信公眾號(hào)@web前端開(kāi)發(fā)營(yíng)
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)