
我是Javascript的新手,我正在做一个使用面部识别来检测情绪的项目。面部识别部分是检测情绪,但我需要访问产生的情绪。面部识别应用程序接口不断提供反馈,并且结果在"div“id=结果标签下不断变化
<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>标题“中立的脸”或alt是所需的主要优先购买权的属性之一
我尝试使用下面的解决方案
Extract property of a tag in HTML using Javascript和How to get title attribute from link within a class with javascript
在我的实际代码中我有
<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>我尝试在生成结果的JS文件中使用此代码片段中的最后两行代码
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);
}
});另外,在另一个单独的时间,我将其添加到JS文件中,该文件需要用于情感识别的反馈
var tag = document.getElementByTagName("img")
var emotion= tag.getAttribute("title");
console.log(emotion);另外,在另一个单独的试验中,我在索引html文件中执行了此操作
<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>该项目的主要思想是检测用户的情绪,并根据该情绪播放音乐
发布于 2016-12-13 13:34:10
您可以尝试这样做:
<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>请在此处查看演示:http://codepen.io/anon/pen/vyVQBJ
发布于 2016-12-13 12:24:20
在你的一条评论中,你说你试过
var emotion = document.getElementByTagName("img")[0].getAttributes("title");请尝试使用
var emotion = document.getElementsByTagName("img")[0].getAttribute("title");函数getAttribute()的末尾没有s,而函数getElementsByTagName的后面有s个元素。另外,在script标记中,将其声明为javascript,
<script type="text/javascript"> // your code </script>不要忘了console.log(情感)后面的分号;
发布于 2016-12-13 14:00:33
因为在JavaScript中没有像getAttributes这样的函数,所以不使用getAttributes("title"),而使用getAttribute("title")。
工作演示:
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">
https://stackoverflow.com/questions/41113577
复制相似问题