-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
123 lines (106 loc) · 3.14 KB
/
App.jsx
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
import React, { useState, useEffect, useRef } from "react";
import * as tf from "@tensorflow/tfjs";
import "@tensorflow/tfjs-backend-webgl"; // set backend to webgl
import { Webcam } from "./utils/webcam";
import { renderBoxes } from "./utils/renderBox";
import "./style/App.css";
const generateClassColors = (numClasses) => {
const colors = [];
const hueStep = 360 / numClasses;
for (let i = 0; i < numClasses; i++) {
const hue = i * hueStep;
const color = `hsl(${hue}, 100%, 50%)`;
colors.push(color);
}
colors[0] = `hsl(0%, 0%, 0%)`; //setting the background as None
return colors;
};
const App = () => {
const [loading, setLoading] = useState({ loading: true, progress: 0 });
const videoRef = useRef(null);
const canvasRef = useRef(null);
const webcam = new Webcam();
// configs
const modelName = "clothes_model";
// Define labels and colors
const labels = [
"Background",
"Hat",
"Hair",
"Sunglasses",
"Upper-clothes",
"Skirt",
"Pants",
"Dress",
"Belt",
"Left-shoe",
"Right-shoe",
"Face",
"Left-leg",
"Right-leg",
"Left-arm",
"Right-arm",
"Bag",
"Scarf",
];
const colors = generateClassColors(labels.length);
const detectFrame = async (model) => {
const model_dim = [512, 512];
tf.engine().startScope();
const input = tf.tidy(() => {
const img = tf.image
.resizeBilinear(tf.browser.fromPixels(videoRef.current), model_dim)
.div(255.0)
.expandDims(0);
return img;
});
await model.executeAsync(input).then((res) => {
res = res.arraySync()[0];
const rawImage = videoRef.current;
const canvas = canvasRef.current;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
renderBoxes(canvasRef, res, rawImage);
tf.dispose(res);
});
requestAnimationFrame(() => detectFrame(model));
tf.engine().endScope();
};
useEffect(() => {
tf.loadGraphModel(`${window.location.origin}/${modelName}_web_model/model.json`, {
onProgress: (fractions) => {
setLoading({ loading: true, progress: fractions });
},
}).then(async (segformer) => {
const dummyInput = tf.ones(segformer.inputs[0].shape);
await segformer.executeAsync(dummyInput).then((warmupResult) => {
tf.dispose(warmupResult);
tf.dispose(dummyInput);
setLoading({ loading: false, progress: 1 });
webcam.open(videoRef, () => detectFrame(segformer));
});
});
}, []);
console.warn = () => {};
return (
<div className="App">
<h2 className="title">Segformer for Clothes Segmentation with TensorFlow.js</h2>
<div className="content">
<video autoPlay playsInline muted ref={videoRef} id="frame" />
<canvas width={640} height={640} ref={canvasRef} />
</div>
<div className="legend">
{labels.map((label, index) => (
<div
key={label}
className="legend-item"
style={{ backgroundColor: colors[index]}}
>
{label}
</div>
))}
</div>
</div>
);
};
export default App;