|
| 1 | +import React from 'react' |
| 2 | +import ReactDOM from 'react-dom' |
| 3 | +import MagicDropzone from 'react-magic-dropzone' |
| 4 | + |
| 5 | +import * as tf from '@tensorflow/tfjs' |
| 6 | +import './styles.css' |
| 7 | + |
| 8 | +const MODEL_URL = './web_model/tensorflowjs_model.pb' |
| 9 | +const WEIGHTS_URL = './web_model/weights_manifest.json' |
| 10 | + |
| 11 | +class App extends React.Component { |
| 12 | + state = { |
| 13 | + model: null, |
| 14 | + preview: '', |
| 15 | + predictions: [] |
| 16 | + } |
| 17 | + |
| 18 | + componentDidMount() { |
| 19 | + tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL).then(model => { |
| 20 | + this.setState({ |
| 21 | + model: model |
| 22 | + }) |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + onDrop = (accepted, rejected, links) => { |
| 27 | + this.setState({ preview: accepted[0].preview || links[0] }) |
| 28 | + } |
| 29 | + |
| 30 | + cropToCanvas = (image, canvas, ctx) => { |
| 31 | + const naturalWidth = image.naturalWidth |
| 32 | + const naturalHeight = image.naturalHeight |
| 33 | + |
| 34 | + canvas.width = image.width |
| 35 | + canvas.height = image.height |
| 36 | + |
| 37 | + ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height) |
| 38 | + if (naturalWidth > naturalHeight) { |
| 39 | + ctx.drawImage( |
| 40 | + image, |
| 41 | + (naturalWidth - naturalHeight) / 2, |
| 42 | + 0, |
| 43 | + naturalHeight, |
| 44 | + naturalHeight, |
| 45 | + 0, |
| 46 | + 0, |
| 47 | + ctx.canvas.width, |
| 48 | + ctx.canvas.height |
| 49 | + ) |
| 50 | + } else { |
| 51 | + ctx.drawImage( |
| 52 | + image, |
| 53 | + 0, |
| 54 | + (naturalHeight - naturalWidth) / 2, |
| 55 | + naturalWidth, |
| 56 | + naturalWidth, |
| 57 | + 0, |
| 58 | + 0, |
| 59 | + ctx.canvas.width, |
| 60 | + ctx.canvas.height |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + onImageChange = e => { |
| 66 | + const c = document.getElementById('canvas') |
| 67 | + const ctx = c.getContext('2d') |
| 68 | + this.cropToCanvas(e.target, c, ctx) |
| 69 | + |
| 70 | + const batched = tf.tidy(() => { |
| 71 | + const img = tf.fromPixels(c) |
| 72 | + // Reshape to a single-element batch so we can pass it to executeAsync. |
| 73 | + return img.expandDims(0) |
| 74 | + }) |
| 75 | + |
| 76 | + const height = batched.shape[1] |
| 77 | + const width = batched.shape[2] |
| 78 | + |
| 79 | + this.state.model.executeAsync(batched).then(result => { |
| 80 | + const boxes = result[0].dataSync() |
| 81 | + const scores = result[1].dataSync() |
| 82 | + const labels = result[2].dataSync() |
| 83 | + |
| 84 | + // clean the webgl tensors |
| 85 | + batched.dispose() |
| 86 | + tf.dispose(result) |
| 87 | + |
| 88 | + const prevBackend = tf.getBackend() |
| 89 | + // run post process in cpu |
| 90 | + tf.setBackend('cpu') |
| 91 | + const indexTensor = tf.tidy(() => { |
| 92 | + const boxes2 = tf.tensor2d(boxes, [ |
| 93 | + result[0].shape[1], |
| 94 | + result[0].shape[2] |
| 95 | + ]) |
| 96 | + return tf.image.nonMaxSuppression( |
| 97 | + boxes2, |
| 98 | + scores, |
| 99 | + 20, // maxNumBoxes |
| 100 | + 0.5, // iou_threshold |
| 101 | + 0.5 // score_threshold |
| 102 | + ) |
| 103 | + }) |
| 104 | + const indexes = indexTensor.dataSync() |
| 105 | + indexTensor.dispose() |
| 106 | + // restore previous backend |
| 107 | + tf.setBackend(prevBackend) |
| 108 | + |
| 109 | + const predictions = this.buildDetectedObjects( |
| 110 | + width, |
| 111 | + height, |
| 112 | + boxes, |
| 113 | + scores, |
| 114 | + indexes, |
| 115 | + labels |
| 116 | + ) |
| 117 | + |
| 118 | + // Font options. |
| 119 | + const font = '16px sans-serif' |
| 120 | + ctx.font = font |
| 121 | + ctx.textBaseline = 'top' |
| 122 | + |
| 123 | + predictions.forEach(prediction => { |
| 124 | + const x = prediction.bbox[0] |
| 125 | + const y = prediction.bbox[1] |
| 126 | + const width = prediction.bbox[2] |
| 127 | + const height = prediction.bbox[3] |
| 128 | + // Draw the bounding box. |
| 129 | + ctx.strokeStyle = '#00FFFF' |
| 130 | + ctx.lineWidth = 4 |
| 131 | + ctx.strokeRect(x, y, width, height) |
| 132 | + // Draw the label background. |
| 133 | + ctx.fillStyle = '#00FFFF' |
| 134 | + const textWidth = ctx.measureText(prediction.class).width |
| 135 | + const textHeight = parseInt(font, 10) // base 10 |
| 136 | + ctx.fillRect(x, y, textWidth + 4, textHeight + 4) |
| 137 | + }) |
| 138 | + |
| 139 | + predictions.forEach(prediction => { |
| 140 | + const x = prediction.bbox[0] |
| 141 | + const y = prediction.bbox[1] |
| 142 | + // Draw the text last to ensure it's on top. |
| 143 | + ctx.fillStyle = '#000000' |
| 144 | + ctx.fillText(prediction.class, x, y) |
| 145 | + }) |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + buildDetectedObjects = (width, height, boxes, scores, indexes, classes) => { |
| 150 | + const count = indexes.length |
| 151 | + const objects = [] |
| 152 | + for (let i = 0; i < count; i++) { |
| 153 | + const bbox = [] |
| 154 | + for (let j = 0; j < 4; j++) { |
| 155 | + bbox[j] = boxes[indexes[i] * 4 + j] |
| 156 | + } |
| 157 | + const minY = bbox[0] * height |
| 158 | + const minX = bbox[1] * width |
| 159 | + const maxY = bbox[2] * height |
| 160 | + const maxX = bbox[3] * width |
| 161 | + bbox[0] = minX |
| 162 | + bbox[1] = minY |
| 163 | + bbox[2] = maxX - minX |
| 164 | + bbox[3] = maxY - minY |
| 165 | + objects.push({ |
| 166 | + bbox: bbox, |
| 167 | + class: classes[indexes[i]], |
| 168 | + score: scores[indexes[i]] |
| 169 | + }) |
| 170 | + } |
| 171 | + return objects |
| 172 | + } |
| 173 | + |
| 174 | + render() { |
| 175 | + return ( |
| 176 | + <div className="Dropzone-page"> |
| 177 | + {this.state.model ? ( |
| 178 | + <MagicDropzone |
| 179 | + className="Dropzone" |
| 180 | + accept="image/jpeg, image/png, .jpg, .jpeg, .png" |
| 181 | + multiple={false} |
| 182 | + onDrop={this.onDrop} |
| 183 | + > |
| 184 | + {this.state.preview ? ( |
| 185 | + <img |
| 186 | + alt="upload preview" |
| 187 | + onLoad={this.onImageChange} |
| 188 | + className="Dropzone-img" |
| 189 | + src={this.state.preview} |
| 190 | + /> |
| 191 | + ) : ( |
| 192 | + 'Choose or drop a file.' |
| 193 | + )} |
| 194 | + <canvas id="canvas" /> |
| 195 | + </MagicDropzone> |
| 196 | + ) : ( |
| 197 | + <div className="Dropzone">Loading model...</div> |
| 198 | + )} |
| 199 | + </div> |
| 200 | + ) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +const rootElement = document.getElementById('root') |
| 205 | +ReactDOM.render(<App />, rootElement) |
0 commit comments