代码之家  ›  专栏  ›  技术社区  ›  user8250085

如何使用Microsoft Face API?

  •  1
  • user8250085  · 技术社区  · 7 年前

    我收到错误,我想发送图像并从中接收数据。缺少什么?

    我也想知道如何创建一个按钮来发送图像/视频和接收信息。

    <!DOCTYPE html>
    <html>
    <head>
        <title>JSSample</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    </head>
    <body>
    
    <script type="text/javascript">
        $(function() {
            var params = {
                // Request parameters
                "returnFaceId": "true",
                "returnFaceLandmarks": "false",
                "returnFaceAttributes": "{string}",
            };
    
            $.ajax({
                url: "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?" + $.param(params),
                beforeSend: function(xhrObj){
                    // Request headers
                    xhrObj.setRequestHeader("Content-Type","application/json");
                    xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","<redacted>");
                },
                type: "POST",
                // Request body
                data: "{body}",
            })
            .done(function(data) {
                alert("success");
            })
            .fail(function() {
                alert("error");
            });
        });
    </script>
    </body>
    </html>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Aaron Chen    7 年前

    可以找到包含人脸检测的完整JavaScript代码 here .

    1. 复制以下内容并将其保存到以下文件中: detectFaces.html
    2. subscriptionKey 使用有效的订阅密钥获取值。
    3. 更改 uriBase
    4. 将文件拖放到浏览器中。
    5. 单击 Analyze faces 按钮
    <!DOCTYPE html>
    <html>
    <head>
        <title>Detect Faces Sample</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    </head>
    <body>
    
    <script type="text/javascript">
        function processImage() {
    
            // Replace the subscriptionKey string value with your valid subscription key.
            var subscriptionKey = "13hc77781f7e4b19b5fcdd72a8df7156";
    
            // Replace or verify the region.
            //
            // You must use the same region in your REST API call as you used to obtain your subscription keys.
            // For example, if you obtained your subscription keys from the westus region, replace
            // "westcentralus" in the URI below with "westus".
            //
            // NOTE: Free trial subscription keys are generated in the westcentralus region, so if you are using
            // a free trial subscription key, you should not need to change this region.
            var uriBase = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect";
    
            // Request parameters.
            var params = {
                "returnFaceId": "true",
                "returnFaceLandmarks": "false",
                "returnFaceAttributes": "age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise",
            };
    
            // Display the image.
            var sourceImageUrl = document.getElementById("inputImage").value;
            document.querySelector("#sourceImage").src = sourceImageUrl;
    
            // Perform the REST API call.
            $.ajax({
                url: uriBase + "?" + $.param(params),
    
                // Request headers.
                beforeSend: function(xhrObj){
                    xhrObj.setRequestHeader("Content-Type","application/json");
                    xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
                },
    
                type: "POST",
    
                // Request body.
                data: '{"url": ' + '"' + sourceImageUrl + '"}',
            })
    
            .done(function(data) {
                // Show formatted JSON on webpage.
                $("#responseTextArea").val(JSON.stringify(data, null, 2));
            })
    
            .fail(function(jqXHR, textStatus, errorThrown) {
                // Display error message.
                var errorString = (errorThrown === "") ? "Error. " : errorThrown + " (" + jqXHR.status + "): ";
                errorString += (jqXHR.responseText === "") ? "" : (jQuery.parseJSON(jqXHR.responseText).message) ? 
                    jQuery.parseJSON(jqXHR.responseText).message : jQuery.parseJSON(jqXHR.responseText).error.message;
                alert(errorString);
            });
        };
    </script>
    
    <h1>Detect Faces:</h1>
    Enter the URL to an image that includes a face or faces, then click the <strong>Analyze face</strong> button.
    <br><br>
    Image to analyze: <input type="text" name="inputImage" id="inputImage" value="https://upload.wikimedia.org/wikipedia/commons/c/c3/RH_Louise_Lillian_Gish.jpg" />
    <button onclick="processImage()">Analyze face</button>
    <br><br>
    <div id="wrapper" style="width:1020px; display:table;">
        <div id="jsonOutput" style="width:600px; display:table-cell;">
            Response:
            <br><br>
            <textarea id="responseTextArea" class="UIInput" style="width:580px; height:400px;"></textarea>
        </div>
        <div id="imageDiv" style="width:420px; display:table-cell;">
            Source image:
            <br><br>
            <img id="sourceImage" width="400" />
        </div>
    </div>
    </body>
    </html>
    

    enter image description here