-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPU.js
773 lines (726 loc) · 22.7 KB
/
CPU.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
const labels = [
"person",
"bicycle",
"car",
"motorcycle",
"airplane",
"bus",
"train",
"truck",
"boat",
"traffic light",
"fire hydrant",
"stop sign",
"parking meter",
"bench",
"bird",
"cat",
"dog",
"horse",
"sheep",
"cow",
"elephant",
"bear",
"zebra",
"giraffe",
"backpack",
"umbrella",
"handbag",
"tie",
"suitcase",
"frisbee",
"skis",
"snowboard",
"sports ball",
"kite",
"baseball bat",
"baseball glove",
"skateboard",
"surfboard",
"tennis racket",
"bottle",
"wine glass",
"cup",
"fork",
"knife",
"spoon",
"bowl",
"banana",
"apple",
"sandwich",
"orange",
"broccoli",
"carrot",
"hot dog",
"pizza",
"donut",
"cake",
"chair",
"couch",
"potted plant",
"bed",
"dining table",
"toilet",
"tv",
"laptop",
"mouse",
"remote",
"keyboard",
"cell phone",
"microwave",
"oven",
"toaster",
"sink",
"refrigerator",
"book",
"clock",
"vase",
"scissors",
"teddy bear",
"hair drier",
"toothbrush",
];
let counter = 0;
const useState = (defaultValue) => {
let value = defaultValue;
const getValue = () => value;
const setValue = (newValue) => (value = newValue);
return [getValue, setValue];
};
const numClass = labels.length;
const [session, setSession] = useState(null);
let mySession;
const modelInputShape = [1, 3, 640, 640];
async function Yolov5IMG(imgIMG) {
return new Promise(async (resolve) => {
let canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 640;
canvas.id = "canvas";
const topk = 100;
const iouThreshold = 0.4;
const confThreshold = 0.2;
const classThreshold = 0.2;
/**
* Get divisible image size by stride
* @param {Number} stride
* @param {Number} width
* @param {Number} height
* @returns {Number[2]} image size [w, h]
*/
const divStride = (stride, width, height) => {
if (width % stride !== 0) {
if (width % stride >= stride / 2)
width = (Math.floor(width / stride) + 1) * stride;
else width = Math.floor(width / stride) * stride;
}
if (height % stride !== 0) {
if (height % stride >= stride / 2)
height = (Math.floor(height / stride) + 1) * stride;
else height = Math.floor(height / stride) * stride;
}
return [width, height];
};
/**
* Preprocessing image
* @param {HTMLImageElement} source image source
* @param {Number} modelWidth model input width
* @param {Number} modelHeight model input height
* @param {Number} stride model stride
* @return preprocessed image and configs
*/
const preprocessing = (source, modelWidth, modelHeight, stride = 32) => {
const mat = cv.imread(source); // read from img tag
const matC3 = new cv.Mat(mat.rows, mat.cols, cv.CV_8UC3); // new image matrix
cv.cvtColor(mat, matC3, cv.COLOR_RGBA2BGR); // RGBA to BGR
const [w, h] = divStride(stride, matC3.cols, matC3.rows);
cv.resize(matC3, matC3, new cv.Size(w, h));
// padding image to [n x n] dim
const maxSize = Math.max(matC3.rows, matC3.cols); // get max size from width and height
const xPad = maxSize - matC3.cols, // set xPadding
xRatio = maxSize / matC3.cols; // set xRatio
const yPad = maxSize - matC3.rows, // set yPadding
yRatio = maxSize / matC3.rows; // set yRatio
const matPad = new cv.Mat(); // new mat for padded image
cv.copyMakeBorder(
matC3,
matPad,
0,
yPad,
0,
xPad,
cv.BORDER_CONSTANT,
[0, 0, 0, 255]
); // padding black
const input = cv.blobFromImage(
matPad,
1 / 255.0, // normalize
new cv.Size(modelWidth, modelHeight), // resize to model input size
new cv.Scalar(0, 0, 0),
true, // swapRB
false // crop
); // preprocessing image matrix
// release mat opencv
mat.delete();
matC3.delete();
matPad.delete();
console.log("[input, xRatio, yRatio]", [input, xRatio, yRatio]);
return [input, xRatio, yRatio];
};
/**
* Handle overflow boxes based on maxSize
* @param {Number[4]} box box in [x, y, w, h] format
* @param {Number} maxSize
* @returns non overflow boxes
*/
const overflowBoxes = (box, maxSize) => {
box[0] = box[0] >= 0 ? box[0] : 0;
box[1] = box[1] >= 0 ? box[1] : 0;
box[2] = box[0] + box[2] <= maxSize ? box[2] : maxSize - box[0];
box[3] = box[1] + box[3] <= maxSize ? box[3] : maxSize - box[1];
return box;
};
class Colors {
// ultralytics color palette https://ultralytics.com/
constructor() {
this.palette = [
"#FF3838",
"#FF9D97",
"#FF701F",
"#FFB21D",
"#CFD231",
"#48F90A",
"#92CC17",
"#3DDB86",
"#1A9334",
"#00D4BB",
"#2C99A8",
"#00C2FF",
"#344593",
"#6473FF",
"#0018EC",
"#8438FF",
"#520085",
"#CB38FF",
"#FF95C8",
"#FF37C7",
];
this.n = this.palette.length;
}
get = (i) => this.palette[Math.floor(i) % this.n];
static hexToRgba = (hex, alpha) => {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16),
alpha,
]
: null;
};
}
const colors = new Colors();
async function detectImage(
image,
canvas,
session,
topk,
iouThreshold,
confThreshold,
classThreshold,
inputShape
) {
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); // clean canvas
const [modelWidth, modelHeight] = inputShape.slice(2);
const maxSize = Math.max(modelWidth, modelHeight);
// const [input, xRatio, yRatio] =
const [input, xRatio, yRatio] = preprocessing(
image,
modelWidth,
modelHeight
);
const tensor = new ort.Tensor("float32", input.data32F, inputShape); // to ort.Tensor
const config = new ort.Tensor(
"float32",
new Float32Array([topk, iouThreshold, confThreshold])
); // nms config tensor
const { output0, output1 } = await session.net.run({ images: tensor });
// run session and get output layer
const { selected_idx } = await session.nms.run({
detection: output0,
config: config,
}); // get selected idx from nms
const boxes = [];
const overlay = cv.Mat.zeros(modelHeight, modelWidth, cv.CV_8UC4);
// looping through output
for (let idx = 0; idx < output0.dims[1]; idx++) {
if (!selected_idx.data.includes(idx)) continue; // skip if index isn't selected
const data = output0.data.slice(
idx * output0.dims[2],
(idx + 1) * output0.dims[2]
); // get rows
let box = data.slice(0, 4);
const confidence = data[4]; // detection confidence
const scores = data.slice(5, 5 + numClass); // classes probability scores
let score = Math.max(...scores); // maximum probability scores
const label = scores.indexOf(score); // class id of maximum probability scores
score *= confidence; // multiply score by conf
const color = colors.get(label); // get color
// filtering by score thresholds
if (score >= classThreshold) {
box = overflowBoxes(
[
box[0] - 0.5 * box[2], // before upscale x
box[1] - 0.5 * box[3], // before upscale y
box[2], // before upscale w
box[3], // before upscale h
],
maxSize
); // keep boxes in maxSize range
const [x, y, w, h] = overflowBoxes(
[
Math.floor(box[0] * xRatio), // upscale left
Math.floor(box[1] * yRatio), // upscale top
Math.floor(box[2] * xRatio), // upscale width
Math.floor(box[3] * yRatio), // upscale height
],
maxSize
); // keep boxes in maxSize range
boxes.push({
label: labels[label],
probability: score,
color: color,
bounding: [x, y, w, h], // upscale box
}); // update boxes to draw later
const mask = new ort.Tensor(
"float32",
new Float32Array([
...box, // original scale box
...data.slice(5 + numClass), // mask data
])
); // mask input
const maskConfig = new ort.Tensor(
"float32",
new Float32Array([
maxSize,
x, // upscale x
y, // upscale y
w, // upscale width
h, // upscale height
...Colors.hexToRgba(color, 120), // color in RGBA
])
); // mask config
const { mask_filter } = await session.mask.run({
detection: mask,
mask: output1,
config: maskConfig,
}); // get mask
const mask_mat = cv.matFromArray(
mask_filter.dims[0],
mask_filter.dims[1],
cv.CV_8UC4,
mask_filter.data
); // mask result to Mat
cv.addWeighted(overlay, 1, mask_mat, 1, 0, overlay); // Update mask overlay
mask_mat.delete(); // delete unused Mat
}
}
const mask_img = new ImageData(
new Uint8ClampedArray(overlay.data),
overlay.cols,
overlay.rows
);
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const newData = new Uint8ClampedArray(
imageData.data.map((value, index) => {
if (mask_img.data[index % mask_img.data.length] >= 50) {
switch (index % 4) {
case 0:
return 100;
case 1:
return 100;
case 2:
return 100;
case 3:
return 100;
default:
return value;
}
}
return value;
})
);
imageData.data.set(newData);
ctx.putImageData(imageData, 0, 0);
var canvasURL = document.createElement("canvas");
canvasURL.width = image.width;
canvasURL.height = image.height;
var ctxURL = canvasURL.getContext("2d");
ctxURL.drawImage(canvas, 0, 0, image.width, image.height);
if (imgIMG.tagName !== "IMG" && imgIMG.tagName !== "VIDEO") {
imgIMG.style.backgroundImage =
'url("' + canvasURL.toDataURL("image/png") + '")';
} else {
imgIMG.src = canvasURL.toDataURL("image/png");
}
input.delete(); // delete unused Mat
overlay.delete(); // delete unused Mat
//resolve(canvas.toDataURL("image/png"));
resolve();
console.log("Number Of Image =>" + counter++);
if (imgs[counter] !== undefined) {
let end = performance.now();
const elapsedTime = ((end - start) / 1000).toFixed(2);
document.getElementById("timeElapsed").innerText = elapsedTime;
console.log("timeElapsed :", elapsedTime);
await Yolov5IMG(imgs[counter], counter);
}
}
async function runInference() {
const img = new Image();
if (imgIMG.tagName != "IMG" && imgIMG.tagName != "VIDEO") {
let srcBG = JSON.parse(imgIMG.oldsrc.replace(/^url\((.*)\)$/, "$1"));
img.src = srcBG;
} else {
img.src = imgIMG.src;
}
img.onload = function () {
detectImage(
img,
canvas,
mySession,
topk,
iouThreshold,
confThreshold,
classThreshold,
modelInputShape
);
};
}
//resolve(runInference());
await runInference();
// resolve(canvas.toDataURL("image/png"));
});
}
async function Yolov8IMG(imgIMG) {
return new Promise(async (resolve) => {
const topk = 100;
const iouThreshold = 0.45;
const scoreThreshold = 0.2;
let canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 640;
canvas.id = "canvas";
/**
* Get divisible image size by stride
* @param {Number} stride
* @param {Number} width
* @param {Number} height
* @returns {Number[2]} image size [w, h]
*/
const divStride = (stride, width, height) => {
if (width % stride !== 0) {
if (width % stride >= stride / 2)
width = (Math.floor(width / stride) + 1) * stride;
else width = Math.floor(width / stride) * stride;
}
if (height % stride !== 0) {
if (height % stride >= stride / 2)
height = (Math.floor(height / stride) + 1) * stride;
else height = Math.floor(height / stride) * stride;
}
return [width, height];
};
/**
* Preprocessing image
* @param {HTMLImageElement} source image source
* @param {Number} modelWidth model input width
* @param {Number} modelHeight model input height
* @param {Number} stride model stride
* @return preprocessed image and configs
*/
const preprocessing = (source, modelWidth, modelHeight, stride = 32) => {
const mat = cv.imread(source); // read from img tag
const matC3 = new cv.Mat(mat.rows, mat.cols, cv.CV_8UC3); // new image matrix
cv.cvtColor(mat, matC3, cv.COLOR_RGBA2BGR); // RGBA to BGR
const [w, h] = divStride(stride, matC3.cols, matC3.rows);
cv.resize(matC3, matC3, new cv.Size(w, h));
// padding image to [n x n] dim
const maxSize = Math.max(matC3.rows, matC3.cols); // get max size from width and height
const xPad = maxSize - matC3.cols, // set xPadding
xRatio = maxSize / matC3.cols; // set xRatio
const yPad = maxSize - matC3.rows, // set yPadding
yRatio = maxSize / matC3.rows; // set yRatio
const matPad = new cv.Mat(); // new mat for padded image
cv.copyMakeBorder(
matC3,
matPad,
0,
yPad,
0,
xPad,
cv.BORDER_CONSTANT,
[0, 0, 0, 255]
); // padding black
const input = cv.blobFromImage(
matPad,
1 / 255.0, // normalize
new cv.Size(modelWidth, modelHeight), // resize to model input size
new cv.Scalar(0, 0, 0),
true, // swapRB
false // crop
); // preprocessing image matrix
// release mat opencv
mat.delete();
matC3.delete();
matPad.delete();
return [input, xRatio, yRatio];
};
/**
* Handle overflow boxes based on maxSize
* @param {Number[4]} box box in [x, y, w, h] format
* @param {Number} maxSize
* @returns non overflow boxes
*/
const overflowBoxes = (box, maxSize) => {
box[0] = box[0] >= 0 ? box[0] : 0;
box[1] = box[1] >= 0 ? box[1] : 0;
box[2] = box[0] + box[2] <= maxSize ? box[2] : maxSize - box[0];
box[3] = box[1] + box[3] <= maxSize ? box[3] : maxSize - box[1];
return box;
};
class Colors {
// ultralytics color palette https://ultralytics.com/
constructor() {
this.palette = [
"#FF3838",
"#FF9D97",
"#FF701F",
"#FFB21D",
"#CFD231",
"#48F90A",
"#92CC17",
"#3DDB86",
"#1A9334",
"#00D4BB",
"#2C99A8",
"#00C2FF",
"#344593",
"#6473FF",
"#0018EC",
"#8438FF",
"#520085",
"#CB38FF",
"#FF95C8",
"#FF37C7",
];
this.n = this.palette.length;
}
get = (i) => this.palette[Math.floor(i) % this.n];
static hexToRgba = (hex, alpha) => {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16),
alpha,
]
: null;
};
}
const colors = new Colors();
async function detectImage(
image,
canvas,
session,
topk,
iouThreshold,
scoreThreshold,
inputShape
) {
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); // clean canvas
const [modelWidth, modelHeight] = inputShape.slice(2);
const maxSize = Math.max(modelWidth, modelHeight); // max size in input model
const [input, xRatio, yRatio] = preprocessing(
image,
modelWidth,
modelHeight
); // preprocess frame
const tensor = new ort.Tensor("float32", input.data32F, inputShape); // to ort.Tensor
const config = new ort.Tensor(
"float32",
new Float32Array([
80, // num class
topk, // topk per class
iouThreshold, // iou threshold
scoreThreshold, // score threshold
])
); // nms config tensor
const { output0, output1 } = await session.net.run({ images: tensor }); // run session and get output layer. out1: detect layer, out2: seg layer
const { selected } = await session.nms.run({
detection: output0,
config: config,
}); // perform nms and filter boxes
const boxes = []; // ready to draw boxes
const overlay = cv.Mat.zeros(modelHeight, modelWidth, cv.CV_8UC4); // create overlay to draw segmentation object
// looping through output
for (let idx = 0; idx < selected.dims[1]; idx++) {
const data = selected.data.slice(
idx * selected.dims[2],
(idx + 1) * selected.dims[2]
); // get rows
let box = data.slice(0, 4); // det boxes
const scores = data.slice(4, 4 + numClass); // det classes probability scores
const score = Math.max(...scores); // maximum probability scores
const label = scores.indexOf(score); // class id of maximum probability scores
const color = colors.get(label); // get color
box = overflowBoxes(
[
box[0] - 0.5 * box[2], // before upscale x
box[1] - 0.5 * box[3], // before upscale y
box[2], // before upscale w
box[3], // before upscale h
],
maxSize
); // keep boxes in maxSize range
const [x, y, w, h] = overflowBoxes(
[
Math.floor(box[0] * xRatio), // upscale left
Math.floor(box[1] * yRatio), // upscale top
Math.floor(box[2] * xRatio), // upscale width
Math.floor(box[3] * yRatio), // upscale height
],
maxSize
); // upscale boxes
boxes.push({
label: labels[label],
probability: score,
color: color,
bounding: [x, y, w, h], // upscale box
}); // update boxes to draw later
const mask = new ort.Tensor(
"float32",
new Float32Array([
...box, // original scale box
...data.slice(4 + numClass), // mask data
])
); // mask input
const maskConfig = new ort.Tensor(
"float32",
new Float32Array([
maxSize,
x, // upscale x
y, // upscale y
w, // upscale width
h, // upscale height
...Colors.hexToRgba(color, 120), // color in RGBA
])
); // mask config
const { mask_filter } = await session.mask.run({
detection: mask,
mask: output1,
config: maskConfig,
}); // get mask
const mask_mat = cv.matFromArray(
mask_filter.dims[0],
mask_filter.dims[1],
cv.CV_8UC4,
mask_filter.data
); // mask result to Mat
cv.addWeighted(overlay, 1, mask_mat, 1, 0, overlay); // Update mask overlay
mask_mat.delete(); // delete unused Mat
}
const mask_img = new ImageData(
new Uint8ClampedArray(overlay.data),
overlay.cols,
overlay.rows
); // create image data from mask overlay
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const newData = new Uint8ClampedArray(
imageData.data.map((value, index) => {
if (mask_img.data[index % mask_img.data.length] >= 50) {
switch (index % 4) {
case 0:
return 100;
case 1:
return 100;
case 2:
return 100;
case 3:
return 100;
default:
return value;
}
}
return value;
})
);
imageData.data.set(newData);
ctx.putImageData(imageData, 0, 0);
// console.log("Original Image SRC:", image.src);
// console.log("Modified Image SRC:", canvas.toDataURL("image/png"));
var canvasURL = document.createElement("canvas");
canvasURL.width = image.width;
canvasURL.height = image.height;
var ctxURL = canvasURL.getContext("2d");
ctxURL.drawImage(canvas, 0, 0, image.width, image.height);
if (imgIMG.tagName !== "IMG" && imgIMG.tagName !== "VIDEO") {
imgIMG.style.backgroundImage =
'url("' + canvasURL.toDataURL("image/png") + '")';
} else {
imgIMG.src = canvasURL.toDataURL("image/png");
}
input.delete(); // delete unused Mat
overlay.delete(); // delete unused Mat
resolve();
console.log("Number Of Image =>" + counter++);
if (imgs[counter] !== undefined) {
await Yolov8IMG(imgs[counter]);
} else {
let end = performance.now();
console.log("Time Elapsed : ", (end - start) / 1000);
}
}
function runInference() {
const img = new Image();
img.crossOrigin = "Anonymous";
img.src = imgIMG.src;
img.onload = function () {
detectImage(
img,
canvas,
mySession,
topk,
iouThreshold,
scoreThreshold,
modelInputShape
);
};
}
runInference();
});
}
let imgs;
let start = 0;
let div = document.getElementById("performance");
window.onload = async function () {
imgs = document.getElementsByTagName("img");
cv["onRuntimeInitialized"] = async () => {
const [yolo, nms, mask] = await Promise.all([
ort.InferenceSession.create("model/yolov5n-seg.onnx"),
ort.InferenceSession.create("model/nms-yolov5.onnx"),
ort.InferenceSession.create("model/mask-yolov5-seg.onnx"),
]);
console.log(yolo);
mySession = setSession({ net: yolo, nms: nms, mask: mask });
console.log("Time Started");
start = performance.now();
await Yolov5IMG(imgs[counter]);
};
};