· 5 years ago · Mar 12, 2021, 08:58 AM
1<!DOCTYPE html>
2<html>
3<head>
4 <title>Analyze Sample</title>
5 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
6</head>
7<body>
8
9<script type="text/javascript">
10 function processImage() {
11 // **********************************************
12 // *** Update or verify the following values. ***
13 // **********************************************
14
15 var subscriptionKey = document.getElementById("subscriptionKey").value;
16 var endpoint = document.getElementById("endpointUrl").value;
17
18 var uriBase = endpoint + "vision/v3.1/analyze";
19
20 // Request parameters.
21 var params = {
22 "visualFeatures": "Categories,Description,Color",
23 "details": "",
24 "language": "en",
25 };
26
27 // Display the image.
28 var sourceImageUrl = document.getElementById("inputImage").value;
29 document.querySelector("#sourceImage").src = sourceImageUrl;
30
31 // Make the REST API call.
32 $.ajax({
33 url: uriBase + "?" + $.param(params),
34
35 // Request headers.
36 beforeSend: function(xhrObj){
37 xhrObj.setRequestHeader("Content-Type","application/json");
38 xhrObj.setRequestHeader(
39 "Ocp-Apim-Subscription-Key", subscriptionKey);
40 },
41
42 type: "POST",
43
44 // Request body.
45 data: '{"url": ' + '"' + sourceImageUrl + '"}',
46 })
47
48 .done(function(data) {
49 // Show formatted JSON on webpage.
50 $("#responseTextArea").val(JSON.stringify(data, null, 2));
51 })
52
53 .fail(function(jqXHR, textStatus, errorThrown) {
54 // Display error message.
55 var errorString = (errorThrown === "") ? "Error. " :
56 errorThrown + " (" + jqXHR.status + "): ";
57 errorString += (jqXHR.responseText === "") ? "" :
58 jQuery.parseJSON(jqXHR.responseText).message;
59 alert(errorString);
60 });
61 };
62</script>
63
64<h1>Analyze image:</h1>
65Enter the URL to an image, then click the <strong>Analyze image</strong> button.
66<br><br>
67Subscription key:
68<input type="text" name="subscriptionKey" id="subscriptionKey"
69 value="" />
70Endpoint URL:
71<input type="text" name="endpointUrl" id="endpointUrl"
72 value="" />
73<br><br>
74Image to analyze:
75<input type="text" name="inputImage" id="inputImage"
76 value="https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg" />
77<button onclick="processImage()">Analyze image</button>
78<br><br>
79<div id="wrapper" style="width:1020px; display:table;">
80 <div id="jsonOutput" style="width:600px; display:table-cell;">
81 Response:
82 <br><br>
83 <textarea id="responseTextArea" class="UIInput"
84 style="width:580px; height:400px;"></textarea>
85 </div>
86 <div id="imageDiv" style="width:420px; display:table-cell;">
87 Source image:
88 <br><br>
89 <img id="sourceImage" width="400" />
90 </div>
91</div>
92</body>
93</html>