5

enter image description here

I am fairly new to JavaScript and I am working on a project the uses facial recognition to detect emotion. The facial recognition part is detecting emotion but I need to access the resulting emotion. There's constant feedback coming from the facial recognition API and the results constantly change under the "div" id= results tag

<div id="results" style="word-wrap:break-word;">
<span> Timestamp: 93.12 </span>
<br>
<span>Number of faces found: 1</span>
<br>
<span> Appearance: {"gender":"Male","glasses":"Yes","age":"25 - 34","ethnicity":"Black African"}</span><br><span>Emotions: {"joy":0,"sadness":0,"disgust":1,"contempt":0,"anger":0,"fear":0,"surprise":0,"valence":0,"engagement":0}
</span>
<br>
<span> Emoji: 
<img class="chromoji" title="Neutral Face" alt=":neutral_face:"     src="chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/apple/1f610.png"></span>
<br>
</div>

The title "Neutral Face" or alt is one of the attributes of the dominant emotion needed

I tried using solutions from

Extract property of a tag in HTML using Javascript and How to get title attribute from link within a class with javascript

in my actual code I have

<div id="results" style="word-wrap:break-word;">
               <script>
                 //var images = document.querySelectorAll('img');
                 var emotion = document.getElementByTagName("img")[0].getAttributes("title");
                 console.log(emotion)
               </script>
             </div>

I tried to use the last two lines of code in this snippet in a JS file that produces the results:

detector.addEventListener("onImageResultsSuccess", function(faces, image, timestamp) {
  $('#results').html("");
  log('#results', "Timestamp: " + timestamp.toFixed(2));
  log('#results', "Number of faces found: " + faces.length);
  if (faces.length > 0) {
    log('#results', "Appearance: " + JSON.stringify(faces[0].appearance));
    log('#results', "Emotions: " + JSON.stringify(faces[0].emotions, function(key, val) {
      return val.toFixed ? Number(val.toFixed(0)) : val;
    }));
    log('#results', "Emoji: " + faces[0].emojis.dominantEmoji);
    drawFeaturePoints(image, faces[0].featurePoints);
    var motion =  faces[0].emojis.dominantEmoji;
    log('#results', motion);
  }
});

also on a separate time I added this to the JavaScript file that needs the feedback for the emotion recognition

var tag = document.getElementByTagName("img")
      var emotion= tag.getAttribute("title");
      console.log(emotion);

also on another separate trial I did this in the index html file

<script type="text/javascript">
                var image = document.getElementsByTagName("img")[0];
                var title = image.getAttribute("title");
                console.log(title); // shows the value of title for the element "image"
              </script>

The main idea of the project is to detect a users emotion and play music based on that emotion

My Info
  • 39
  • 8
Kelvin Njeri
  • 159
  • 1
  • 3
  • 10

3 Answers3

8

You can try this:

<script type="text/javascript">
    var image = document.getElementsByTagName("img")[0];
    var title = image.getAttribute("title");

    console.log(title); // shows the value of title for the element "image"
</script>

See demo here: http://codepen.io/anon/pen/vyVQBJ

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Mukesh Panchal
  • 1,956
  • 2
  • 18
  • 31
0

In one of your comments, you said you tried

var emotion = document.getElementByTagName("img")[0].getAttributes("title"‌​);

Try instead using

var emotion = document.getElementsByTagName("img")[0].getAttribute("title"‌​);

The function getAttribute() has no s at the end, while the function getElementsByTagName has an s after elements. Also, in your script tag, declare it as javascript like,

<script type="text/javascript"> // your code </script>

Don't forget a semicolon after console.log(emotion);

Nick Winner
  • 261
  • 2
  • 11
0

Instead of using getAttributes("title"‌​) use getAttribute("title"‌​) as there is no function like getAttributes in JavaScript.

Working demo :

var emotion = document.getElementsByTagName("img")[0].getAttribute("title");
console.log(emotion);
<img class="chromoji" title="Neutral Face" alt=":neutral_face:"     src="https://i.stack.imgur.com/cp7qK.png" width="400" height="200">
Debug Diva
  • 26,058
  • 13
  • 70
  • 123