<html>
<body>
<div id="div">
<script>
var fontSize = 14;
var response = {
"id": "cae1ff4f-bb23-4a6b-460d-a7a68b690a36",
"predictions": [
{
"faces": [
{
"bottom": 1402,
"left": 425,
"right": 1224,
"top": 603
},
{
"bottom": 1757,
"left": 1845,
"right": 2644,
"top": 958
}
],
"name": "IMG_05162.jpg",
"numberOfFaces": 2
}
],
"processedTime": "2019-04-17T08:05:09.478159+00:00",
"status": "DONE"
}
function createCanvas(oImg, items) {
var oCanvas = document.createElement("CANVAS");
var ctx = oCanvas.getContext("2d");
function resizeCanvas() {
// set the image width to the window width
imgRatioW = oImg.naturalWidth / window.innerWidth;
oImg.width = window.innerWidth;
oImg.height = oImg.naturalHeight / imgRatioW ;
// adjust the canvas dimension
oCanvas.width = oImg.width;
oCanvas.height = oImg.height;
// get the width & height for the image / canvas
var itemRatioW = oCanvas.width / oImg.naturalWidth;
var itemRatioH = oCanvas.height / oImg.naturalHeight;
// draw the canvas
ctx.drawImage(oImg, 0, 0, oImg.width, oImg.height);
ctx.lineWidth="3";
ctx.strokeStyle="red";
ctx.fillStyle = "white";
ctx.font = fontSize + "px Arial";
// display the items
for (var i = 0; i < items.length; i++) {
var text = "Face #" + i;
// get the box attributes
var left = items[i].left * itemRatioW;
var top = items[i].top * itemRatioH;
var width = (items[i].right - items[i].left) * itemRatioW;
var height = (items[i].bottom - items[i].top ) * itemRatioH;
// draw the box
ctx.strokeRect(left, top, width, height);
// write the text with the box angle
ctx.save();
ctx.fillStyle = 'red';
ctx.fillRect(left, top, ctx.measureText(text).width + ctx.lineWidth, ctx.lineWidth + fontSize);
// write the text in the box
ctx.fillStyle = "white";
ctx.fillText(text, left, top + fontSize);
ctx.restore();
}
}
// call the resize function
resizeCanvas();
// add the event listener
window.addEventListener('resize', resizeCanvas, false);
return oCanvas;
}
window.onload = function() {
for (var idx = 0; idx < response.predictions.length; idx++) {
var items = response.predictions[idx].faces;
var name = response.predictions[idx].name;
var oImg = document.createElement("IMG");
oImg.onload = function(){
document.getElementById("div").appendChild(createCanvas(this, items));
}
oImg.src = name;
}
};
</script>
</div>
</body>
</html>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
9 | |
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 | |
2 |