|
| 1 | +var model; |
| 2 | +cocoSsd.load().then( |
| 3 | +function(res){ |
| 4 | +model = res; |
| 5 | +alert("Model is ready"); |
| 6 | +}, |
| 7 | +function(){ |
| 8 | +console.log("model did not load"); |
| 9 | +} |
| 10 | + |
| 11 | +); |
| 12 | + |
| 13 | +function invoke_upload_image() |
| 14 | +{ |
| 15 | + document.getElementById("upload-btn").click(); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +function upload_image(){ |
| 20 | + |
| 21 | +const canvas = document.getElementById("canvas"); |
| 22 | +const ctx = canvas.getContext("2d"); |
| 23 | + |
| 24 | +var input_elem = document.querySelector("input[type=file]"); |
| 25 | +var file = input_elem.files[0]; |
| 26 | +const image = document.getElementById('img'); |
| 27 | +var reader = new FileReader(); |
| 28 | +reader.addEventListener( |
| 29 | +"load", |
| 30 | +function(){ |
| 31 | + image.src = reader.result; |
| 32 | + setTimeout(function(){ |
| 33 | + if(image.height > 500){ |
| 34 | + image.width = image.height * (500/image.height); |
| 35 | + image.height = 500; |
| 36 | + } |
| 37 | + |
| 38 | + model.detect(image).then(function(predictions){ |
| 39 | + |
| 40 | + draw_res(canvas, ctx, image, predictions); |
| 41 | + }); |
| 42 | + },1000); |
| 43 | + |
| 44 | +},false |
| 45 | +); |
| 46 | + |
| 47 | +if(file){ |
| 48 | + ctx.clearRect(0,0,ctx.canvas.width, ctx.canvas.height); |
| 49 | + reader.readAsDataURL(file); |
| 50 | +} |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +function draw_res(canvas, ctx, image, predictions){ |
| 55 | +canvas.height = image.height; |
| 56 | +const font = "16px sans-serif"; |
| 57 | +canvas.width = image.width; |
| 58 | +ctx.clearRect(0,0,ctx.canvas.width, ctx.canvas.height); |
| 59 | +ctx.drawImage(image,0,0,ctx.canvas.width, ctx.canvas.height); |
| 60 | +ctx.font = font; |
| 61 | +ctx.textBaseline = "top"; |
| 62 | +ctx.strokeStyle = "#00FFFF"; |
| 63 | +ctx.lineWidth = 3; |
| 64 | +ctx.fillStyle = "#00FFFF"; |
| 65 | +draw_box(ctx, predictions, font); |
| 66 | +draw_label(ctx, predictions); |
| 67 | + |
| 68 | +} |
| 69 | +// predictions = [{bbox: [10,20,300,500]}] |
| 70 | +function draw_box(ctx, predictions, font) { |
| 71 | + console.log(predictions); |
| 72 | + predictions.forEach((prediction) => { |
| 73 | + // predictions = [{bbox: [10,20,300,500]}] |
| 74 | + const x = prediction.bbox[0]; |
| 75 | + const y = prediction.bbox[1]; |
| 76 | + const width = prediction.bbox[2]; |
| 77 | + const height = prediction.bbox[3]; |
| 78 | + ctx.strokeRect(x, y, width, height); |
| 79 | + const textWidth = ctx.measureText(prediction.class).width; |
| 80 | + const textHeight = parseInt(font, 10); // base 10 |
| 81 | + ctx.fillRect(x, y, textWidth + 4, textHeight + 4); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function draw_label(ctx, predictions) { |
| 86 | + predictions.forEach((prediction) => { |
| 87 | + const x = prediction.bbox[0]; |
| 88 | + const y = prediction.bbox[1]; |
| 89 | + |
| 90 | + ctx.fillStyle = "#000000"; |
| 91 | + ctx.fillText(prediction.class, x, y); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | + |
0 commit comments