-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrender-predictions.js
43 lines (33 loc) · 1.16 KB
/
render-predictions.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
import {throttle} from "lodash";
export const renderPredictions = (predictions, ctx) => {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
// Fonts
const font = "16px sans-serif";
ctx.font = font;
ctx.textBaseline = "top";
predictions.forEach((prediction) => {
const [x, y, width, height] = prediction["bbox"];
const isPerson = prediction.class === "person";
// bounding box
ctx.strokeStyle = isPerson ? "#FF0000" : "#00FFFF";
ctx.lineWidth = 4;
ctx.strokeRect(x, y, width, height);
// fill the color
ctx.fillStyle = `rgba(255, 0, 0, ${isPerson ? 0.2 : 0})`; // Set the fill color to red
ctx.fillRect(x, y, width, height);
// Draw the label background.
ctx.fillStyle = isPerson ? "#FF0000" : "#00FFFF";
const textWidth = ctx.measureText(prediction.class).width;
const textHeight = parseInt(font, 10); // base 10
ctx.fillRect(x, y, textWidth + 4, textHeight + 4);
ctx.fillStyle = "#000000";
ctx.fillText(prediction.class, x, y);
if (isPerson) {
playAudio();
}
});
};
const playAudio = throttle(() => {
const audio = new Audio("/pols-aagyi-pols.mp3");
audio.play();
}, 2000);