Reputation: 25
I'm not sure how to go about this, but I need help in getting my microsoft custom vision to work. I'm using javascript to link my html document to custom vision but I don't know how to use a local image file I have in the same folder as my html and js files, could anybody assist me with any codes?
},
type: "POST",
// Request body
data: "{body}",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
The instructions tell me to change {body} to
Upvotes: 0
Views: 601
Reputation: 14599
A few points:
Prediction-Key
is present 2 times (key
in uppercase vs lowercase). You just need it 1 timeSo if you want to do it "from scratch", you can do the following, having to choose the file manually:
<!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>
<input type='file' accept='image/*' onchange='openFile(event)' />
<br />
<img id='output' style="height:100px; width:100px;" />
<script type="text/javascript">
var openFile = function(file) {
var input = file.target;
var reader = new FileReader();
reader.onload = function(){
var dataURL = reader.result;
var params = {
// Request parameters
"application": "myTestApp"
};
var parts = dataURL.split(';base64,');
var contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for (var i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
var imgContent = new Blob([uInt8Array], { type: contentType });
$.ajax({
url: "https://southcentralus.api.cognitive.microsoft.com/customvision/v3.0/Prediction/__YOUR_APPLICATION_ID__/classify/iterations/__YOUR_ITERATION_ID__/image?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Prediction-Key","__YOUR_PREDICTION_KEY__");
xhrObj.setRequestHeader("Content-Type","application/octet-stream");
},
type: "POST",
// Request body
data: imgContent,
processData: false
})
.done(function(data) {
alert("success");
console.log(data);
})
.fail(function() {
alert("error");
});
};
reader.readAsDataURL(input.files[0]);
};
</script>
</body>
</html>
In addition, ou can have a look to existing samples in Node.js here, where they are calling prediction from local files: https://github.com/Azure-Samples/cognitive-services-node-sdk-samples/tree/master/Samples/customvision
Upvotes: 1