0

I have the following javascript code, which takes a photo from a webcam and compares it with another photo stored on the server. What I am trying to do is to take the photo from the webcam automatically, without waiting for the user to click on the take photo button. I am just using

document.getElementById('take_snap').click()

but this code is doing nothing. When I click on the button manually my code works perfectly, but the above code doesn't work. This is my code:

const imageUploads = document.getElementById('take_snap')
const image_url =       document.getElementById('results')
var pid=$('#student_id').val();
 
Promise.all([
  faceapi.nets.faceRecognitionNet.loadFromUri('models'),
  faceapi.nets.faceLandmark68Net.loadFromUri('models'),
  faceapi.nets.ssdMobilenetv1.loadFromUri('models')
]).then(start)

async function start() {
  const container = document.createElement('div')
  container.style.position = 'relative'
  document.body.append(container)
  const labeledFaceDescriptors = await loadLabeledImages()
  const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6)
  let image
  let canvas
  document.body.append('You can start')
    imageUploads.click();
  
  imageUploads.addEventListener('click', async () => {
 
    if (image) image.remove()
    if (canvas) canvas.remove()
        Webcam.snap( function(data_uri) {
        // display results in page
          document.getElementById('results').innerHTML = 
          '<img src="'+data_uri+'" id="img_tag"/>';
        var filename=  upload_img(data_uri)
         
         document.getElementById('results').innerHTML = 
           '<img src="upload/'+filename+'" id="img_tags"/>';
           

      } );
      
 console.log( document.getElementById('img_tags').getAttribute('src'));
              image = await faceapi.fetchImage(document.getElementById('img_tags').getAttribute('src'))
         
    container.append(image)
     
    canvas = faceapi.createCanvasFromMedia(image)
 
    container.append(canvas)

    const displaySize = { width: image.width, height: image.height }
    faceapi.matchDimensions(canvas, displaySize)
    const detections = await faceapi.detectAllFaces(image).withFaceLandmarks().withFaceDescriptors()
    const resizedDetections = faceapi.resizeResults(detections, displaySize)
 
    const results = resizedDetections.map(d => faceMatcher.findBestMatch(d.descriptor))
//console.log(  'descriptor'+d.descriptor)
        console.log(detections)
    results.forEach((result, i) => {console.log('iiiiiiiiiiiii'+i)
      const box = resizedDetections[i].detection.box
      const drawBox = new faceapi.draw.DrawBox(box, { label: result.toString() })
console.log('resultttt'+ result.toString())
if(result.toString()!=null || result.toString()!='unknown')
{
    $('#tamam').show();
    
 
}
if(result.toString().indexOf('unknown'))
{
    
    $('#tamam').hide();
    
}
      drawBox.draw(canvas)
    })
  })
}

as I mentioned above the code works fine when I click on the button manually but it seems that this code not working when using autoclick.

imageUploads.click();
Riz-waan
  • 603
  • 3
  • 13
  • 1
    Does this answer your question? [How do I programmatically click on an element in JavaScript?](https://stackoverflow.com/questions/809057/how-do-i-programmatically-click-on-an-element-in-javascript) – Siddharth Seth Jun 24 '20 at 18:49
  • thank you for your reply but I get this error when run the code Uncaught (in promise) TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'. at start (script.js:21) – majed bwedany Jun 24 '20 at 18:55
  • Where is this script located in the page...I mean is it included at the bottom of the body tag right? Because otherwise it would not work... – Siddharth Seth Jun 24 '20 at 19:35

0 Answers0