Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the title attribute using javascript

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 emption 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 JS 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

like image 485
Kelvin Njeri Avatar asked Dec 13 '16 04:12

Kelvin Njeri


Video Answer


1 Answers

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

like image 92
Mukesh Panchal Avatar answered Oct 24 '22 09:10

Mukesh Panchal