|
const { createApp, ref, onMounted } = Vue; |
|
import { HfInference } from "https://cdn.skypack.dev/@huggingface/inference@latest"; |
|
|
|
const getRandomImageUrl = async () => { |
|
const randomImageRequest = await fetch("https://source.unsplash.com/random/640x480") |
|
return randomImageRequest.url |
|
} |
|
const ObjectDatectionModels = ["facebook/detr-resnet-50"] |
|
|
|
const getImageBuffer = async (imageUrl) => { |
|
const imageRequest = await fetch(imageUrl) |
|
const imageBuffer = await imageRequest.arrayBuffer() |
|
return imageBuffer |
|
} |
|
|
|
const drawDetectedObjects = (img, detections) => { |
|
const container = document.createElement('div'); |
|
container.id = 'detected-objects-container'; |
|
container.style.top = `${img.offsetTop}px`; |
|
container.style.left = `${img.offsetLeft}px`; |
|
container.style.width = `${img.width}px`; |
|
container.style.height = `${img.height}px`; |
|
img.parentNode.insertBefore(container, img); |
|
|
|
detections.forEach(detection => { |
|
const { xmin, ymin, xmax, ymax } = detection.box; |
|
const label = detection.label; |
|
const confidence = Math.floor(detection.score * 100) + '%'; |
|
|
|
const box = document.createElement('div'); |
|
box.classList.add('bounding-box'); |
|
box.style.left = `${xmin}px`; |
|
box.style.top = `${ymin}px`; |
|
box.style.width = `${xmax - xmin}px`; |
|
box.style.height = `${ymax - ymin}px`; |
|
|
|
const tooltip = document.createElement('div'); |
|
tooltip.classList.add('tooltip'); |
|
tooltip.innerText = `${label}: ${confidence}`; |
|
box.appendChild(tooltip); |
|
|
|
container.appendChild(box); |
|
}); |
|
}; |
|
const app = createApp({ |
|
setup() { |
|
const token = ref(localStorage.getItem("token") || ""); |
|
const models = ref(ObjectDatectionModels); |
|
const selectedModel = ref(""); |
|
const imageUrl = ref(""); |
|
const detectedObjects = ref([]); |
|
|
|
const run = async () => { |
|
try { |
|
const hf = new HfInference(token.value); |
|
const imageData = await getImageBuffer(imageUrl.value); |
|
const result = await hf.objectDetection({ |
|
data: imageData, |
|
model: selectedModel.value, |
|
}); |
|
console.log(result); |
|
detectedObjects.value = result |
|
drawDetectedObjects(document.getElementById('current-image'), result) |
|
} catch (e) { |
|
console.log(e); |
|
} |
|
}; |
|
|
|
const shuffle = async () => { |
|
imageUrl.value = await getRandomImageUrl() |
|
document.getElementById('detected-objects-container').remove() |
|
document.getElementsByClassName('bounding-box').forEach(box => box.remove()) |
|
}; |
|
|
|
onMounted(async () => { |
|
const localStorageToken = localStorage.getItem("token") |
|
if (localStorageToken) { |
|
token.value = localStorageToken; |
|
} |
|
selectedModel.value = models.value[0] |
|
imageUrl.value = await getRandomImageUrl() |
|
window.onresize = () => { |
|
document.getElementById('detected-objects-container').remove() |
|
document.getElementsByClassName('bounding-box').forEach(box => box.remove()) |
|
} |
|
}); |
|
|
|
return { |
|
token, |
|
run, |
|
shuffle, |
|
models, |
|
selectedModel, |
|
imageUrl |
|
}; |
|
}, |
|
}); |
|
|
|
app.mount("#generate-text-stream-app"); |
|
|