1

我正在尝试从上传的图像中检测面部情绪。我似乎找不到任何用于情绪检测的示例代码。

https://azure.microsoft.com/en-us/try/cognitive-services/my-apis/?apiSlug=face-api&country=Canada&allowContact=true

我找到了这个

https://docs.microsoft.com/en-us/azure/cognitive-services/emotion/quickstarts/javascript

但 url 端点不起作用。然后我尝试了常规的人脸 api,但即使我得到了resource not found.

有谁知道发生了什么?

谢谢

var FACE = new function () {

    this.listen = function() {

        var camera = document.getElementById('camera');

        camera.addEventListener('change', function(e) {
            var imageFile = e.target.files[0];     
            var reader = new FileReader();
            var fileType;

            //wire up the listener for the async 'loadend' event
            reader.addEventListener('loadend', function () {    
                //get the result of the async readAsArrayBuffer call
                var fileContentArrayBuffer = reader.result;

                sendImage(fileContentArrayBuffer, fileType);
            });

            if (imageFile) {
                //save the mime type of the file
                fileType = imageFile.type;

                //read the file asynchronously
                reader.readAsArrayBuffer(imageFile);
            }   
        });

        function sendImage(fileContentArrayBuffer, fileType) {

            $.ajax({
                // NOTE: You must use the same location in your REST call as you used to obtain your subscription keys.
                //   For example, if you obtained your subscription keys from westcentralus, replace "westus" in the 
                //   URL below with "westcentralus".
                url: "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/",
                beforeSend: function(xhrObj){
                    // Request headers, also supports "application/octet-stream"
                    xhrObj.setRequestHeader("Content-Type","application/json");

                    // NOTE: Replace the "Ocp-Apim-Subscription-Key" value with a valid subscription key.
                    xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","my key");
                },
                //don't forget this!
                processData: false,
                type: "POST",
                // Request body
                data: new Blob([fileContentArrayBuffer], { type: fileType })
            }).done(function(data) {
                alert(data);
                // Get face rectangle dimensions
                var faceRectangle = data[0].faceRectangle;
                var faceRectangleList = $('#faceRectangle');

                // Append to DOM
                for (var prop in faceRectangle) {
                    faceRectangleList.append("<li> " + prop + ": " + faceRectangle[prop] + "</li>");
                }

                // Get emotion confidence scores
                var scores = data[0].scores;
                var scoresList = $('#scores');

                // Append to DOM
                for(var prop in scores) {
                    scoresList.append("<li> " + prop + ": " + scores[prop] + "</li>")
                }
            }).fail(function(err) {
                alert("Error: " + JSON.stringify(err));
            });
        }
    };
};
4

2 回答 2

0

你能仔细检查你的 api 区域吗?因为在区域中找不到给定 api 键的资源时会发生此错误。为了访问情绪,您需要将参数传递给 api,这将为您提供包含情绪的面部属性作为响应。

于 2018-02-24T15:21:13.407 回答
0

假设你有你的密钥,Emotion API 的请求 URL 应该是 https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize?

您可能还想看看这个网站。它有类似的代码。

对不起,我不能使用评论功能,因为我是新来的,没有足够的声誉这样做。

于 2018-02-19T15:33:15.330 回答