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