Public
Edited
Jan 29
3 stars
Insert cell
Insert cell
tranformers=import("https://cdn.jsdelivr.net/npm/@xenova/transformers")
Insert cell
pipeline=tranformers.pipeline
Insert cell
Insert cell
texts = [
"The questionnaire was not optimized for mobile devices.",
"Please reduce the number of required fields.",
]
Insert cell
Insert cell
embedder = await pipeline('feature-extraction', 'jinaai/jina-embeddings-v2-base-code', { quantized: false })
Insert cell
Insert cell
token_embeddings = await embedder(texts);
Insert cell
embeddings = await embedder(texts, { pooling: 'mean' });
Insert cell
Insert cell
reshapeArray(embeddings.data,embeddings.dims)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
detector = await pipeline("object-detection", "Xenova/yolos-tiny");
Insert cell
Insert cell
{
if (photo instanceof HTMLCanvasElement){
console.log(photo)
let image = photo.toDataURL('image/jpeg')

let img= new Image();
img.src = image;

return new Promise( (resolve,reject) => {
img.onload = async function() {
let ogWidth = img.width;
let ogHeight = img.height;
let ctx = photo.getContext('2d');
console.log(`photo width: ${photo.width} height: ${photo.height}`)
console.log(`img width: ${img.width} height: ${img.height}`)
let output= await detector(image, { threshold: 0.9 });
output.forEach( o => {
console.log(o)
let scaleX = 0.5
let scaleY = 0.5
let xmin = Math.floor(o.box.xmin * scaleX);
let xmax = Math.floor(o.box.xmax * scaleX);
let ymin = Math.floor(o.box.ymin * scaleY);
let ymax = Math.floor(o.box.ymax * scaleY);
let bwid = xmax - xmin;
let bhei = ymax - ymin;

ctx.lineWidth = 2;
ctx.strokeStyle = 'red';
ctx.strokeRect(xmin, ymin, bwid, bhei);

console.log(`iw: ${img.width} ih: ${img.height}\nscaleX: ${scaleX} scaleY: ${scaleY}\nxmin: ${xmin} xmax: ${xmax} ymin: ${ymin} ymax:${ymax}\nwid: ${bwid} heoght: ${bhei}`)
})

resolve(output)
}
})
} else {
return "... waiting for photo ..."
}
}
Insert cell
Insert cell
function reshapeArray(typedArray,dim){
let newArray=[]
for (let row=0;row< dim[0]; row++){
newArray.push(Array.from(typedArray.slice(row*dim[1],(row+1)*dim[1])))
}
return newArray
}
Insert cell
function screenShot () {
const context = DOM.context2d(500, 375);
context.drawImage(video, 0, 0, 500, 375);
return context.canvas
}
Insert cell
mutable capabilities = "Waiting..."
Insert cell
checkMediaDevices = {
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({video: true})
.then(function(stream) {
video.srcObject = stream;
mutable capabilities = stream.getVideoTracks()[0].getCapabilities();
})
.catch(function(err0r) {
mutable capabilities = "No Video Camera";
});
return "OK"
}
return "navigator does not support mediaDevices"
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more