1 const video = document.getElementById("video") 2 3 Promise.allSettled([ 4 faceapi.nets.ssdMobilenetv1.loadFromUri("./models"), //detect the faces 5 faceapi.nets.faceLandmark68Net.loadFromUri("./models"), // point out where are faces 6 faceapi.nets.faceRecognitionNet.loadFromUri("./models") // recozize the faces 7 ]).then(startVideo); 8 9 async function startVideo() { 10 navigator.getUserMedia( 11 { 12 video: {} }, 13 stream => (video.srcObject = stream), 14 err => console.error(err) 15 ); 16 17 console.log("webcam added") 18 } 19 20 video.addEventListener("playing", async () => { 21 const container = document.createElement("div") 22 container.style.position ="relative" 23 document.body.append(container) 24 25 const labeledFaceDescriptors = await loadLabeledImages() 26 27 const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6*100) 28 29 const canvas = faceapi.createCanvasFromMedia(video) 30 container.append(video) 31 container.append(canvas) 32 const displaySize = { width:video.width, height: video.height} 33 faceapi.matchDimensions(canvas, displaySize) 34 35 setInterval(async () => { 36 const detections = await faceapi.detectAllFaces( 37 video, new faceapi.SsdMobilenetv1Options()) 38 .withFaceLandmarks().withFaceDescriptors() 39 40 const resizedDetections = faceapi.resizeResults(detections,displaySize); 41 42 canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height) 43 44 const results = resizedDetections.map(d => faceMatcher.findBestMatch(d.descriptor)) 45 46 results.forEach((result, i) => { 47 const box = resizedDetections[i].detection.box 48 const drawBox = new faceapi.draw.DrawBox(box, { label : result.toString() }) 49 drawBox.draw(canvas) 50 }) 51 }, 100) 52 }) 53 54 function loadLabeledImages() { 55 const labels = ['manohar','Nabin','Nehal Prasad'] 56 return Promise.all( 57 labels.map(async (label) => { 58 const descriptions = [] 59 for(let i = 1; i<=2; i++){ 60 const img = await faceapi.fetchImage(`./labeled_images/${label}/${i}.jpg`) 61 const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor() 62 descriptions.push(detections.descriptor) 63 return new faceapi.LabeledFaceDescriptors(label, descriptions) 64 } 65 }) 66 ) 67 }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)