Skip to content

Commit f735677

Browse files
committed
faster model
1 parent 3adaafe commit f735677

File tree

10 files changed

+2612
-22
lines changed

10 files changed

+2612
-22
lines changed

components/ObjectDetectionCamera.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const WebcamComponent = (props: any) => {
1111
const liveDetection = useRef<boolean>(false);
1212
const [session, setSession] = useState<any>(null);
1313
const [facingMode, setFacingMode] = useState<string>("environment");
14-
14+
const [modelResolution, setModelResolution] = useState<string>("320x320");
1515
const originalSize = useRef<number[]>([0, 0]);
1616

1717
useEffect(() => {
@@ -53,7 +53,6 @@ const WebcamComponent = (props: any) => {
5353
[outputTensor, inferenceTime] = await runModelUtils.runModel(session, data);
5454

5555
props.postprocess(outputTensor, props.inferenceTime, ctx);
56-
const totalEndTime = performance.now();
5756
setInferenceTime(inferenceTime);
5857
};
5958

@@ -64,13 +63,13 @@ const WebcamComponent = (props: any) => {
6463
}
6564
liveDetection.current = true;
6665
while (liveDetection.current) {
67-
const startTime = performance.now();
66+
const startTime = Date.now()
6867
const ctx = capture();
6968
if (!ctx) return;
7069
await runModel(ctx);
71-
setTotalTime(performance.now() - startTime);
70+
setTotalTime(Date.now() - startTime);
7271
await new Promise<void>((resolve) =>
73-
requestAnimationFrame(() => resolve())
72+
requestAnimationFrame(() => resolve())
7473
);
7574
}
7675
};
@@ -169,9 +168,9 @@ const WebcamComponent = (props: any) => {
169168
<div className="flex gap-1 justify-center items-center items-stretch">
170169
<button
171170
onClick={async () => {
172-
const startTime = performance.now();
171+
const startTime = Date.now();
173172
await processImage();
174-
setTotalTime(performance.now() - startTime);
173+
setTotalTime(Date.now() - startTime);
175174
}}
176175
className="p-2 border-dashed border-2 rounded-xl hover:translate-y-1 "
177176
>
@@ -183,7 +182,6 @@ const WebcamComponent = (props: any) => {
183182
liveDetection.current = false;
184183
} else {
185184
runLiveDetection();
186-
console.log("asdf");
187185
}
188186
}}
189187
//on hover, shift the button up
@@ -205,6 +203,21 @@ const WebcamComponent = (props: any) => {
205203
>
206204
Switch Camera
207205
</button>
206+
<button
207+
onClick={() => {
208+
reset();
209+
if (modelResolution == "320x320"){
210+
setModelResolution("640x640")
211+
props.changeModelResolution("640x640")
212+
}else{
213+
setModelResolution('320x320')
214+
props.changeModelResolution("320x320")
215+
}
216+
}}
217+
className="p-2 border-dashed border-2 rounded-xl hover:translate-y-1 "
218+
>
219+
Change Speed
220+
</button>
208221
<button
209222
onClick={reset}
210223
className="p-2 border-dashed border-2 rounded-xl hover:translate-y-1 "
@@ -215,7 +228,7 @@ const WebcamComponent = (props: any) => {
215228
</div>
216229
<div className="flex gap-3 flex-row flex-wrap justify-between items-center px-5 w-full">
217230
<div>
218-
{"Model Inference Time: " + inferenceTime + "ms"}
231+
{"Model Inference Time: " + inferenceTime.toFixed() + "ms"}
219232
<br />
220233
{"Total Time: " + totalTime.toFixed() + "ms"}
221234
<br />

components/models/Yolo.tsx

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ import ops from "ndarray-ops";
44
import ObjectDetectionCamera from "../ObjectDetectionCamera";
55
import { round } from "lodash";
66
import { yoloClasses } from "../../data/yolo_classes";
7+
import { useState } from "react";
8+
const mapResolution = new Map();
9+
mapResolution.set("320x320", [320, 320])
10+
mapResolution.set("640x640", [640, 640])
711

8-
const INPUT_DIM_WIDTH = 640;
9-
const INPUT_DIM_HEIGHT = 640;
1012

1113
const Yolo = (props: any) => {
14+
const [modelInputDimensions, setModelInputDimensions] = useState<number[]>([
15+
320, 320,
16+
]);
17+
18+
const changeModelResolution = (model: string) => {
19+
if (mapResolution.get(model)) {
20+
setModelInputDimensions(mapResolution.get(model));
21+
}
22+
}
23+
1224
const resizeCanvasCtx = (
1325
ctx: CanvasRenderingContext2D,
1426
targetWidth: number,
@@ -49,13 +61,17 @@ const Yolo = (props: any) => {
4961
};
5062

5163
const preprocess = (ctx: CanvasRenderingContext2D) => {
52-
const resizedCtx = resizeCanvasCtx(ctx, INPUT_DIM_WIDTH, INPUT_DIM_HEIGHT);
64+
const resizedCtx = resizeCanvasCtx(
65+
ctx,
66+
modelInputDimensions[0],
67+
modelInputDimensions[1]
68+
);
5369

5470
const imageData = resizedCtx.getImageData(
5571
0,
5672
0,
57-
INPUT_DIM_HEIGHT,
58-
INPUT_DIM_WIDTH
73+
modelInputDimensions[0],
74+
modelInputDimensions[1]
5975
);
6076
const { data, width, height } = imageData;
6177
// data processing
@@ -104,8 +120,8 @@ const Yolo = (props: any) => {
104120
inferenceTime: number,
105121
ctx: CanvasRenderingContext2D
106122
) => {
107-
const dx = ctx.canvas.width / INPUT_DIM_WIDTH;
108-
const dy = ctx.canvas.height / INPUT_DIM_HEIGHT;
123+
const dx = ctx.canvas.width / modelInputDimensions[0];
124+
const dy = ctx.canvas.height / modelInputDimensions[1];
109125

110126
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
111127
for (let i = 0; i < tensor.dims[0]; i++) {
@@ -156,7 +172,8 @@ const Yolo = (props: any) => {
156172
preprocess={preprocess}
157173
postprocess={postprocess}
158174
resizeCanvasCtx={resizeCanvasCtx}
159-
modelUri={"./_next/static/chunks/pages/yolov7-tiny.onnx"}
175+
modelUri={`./_next/static/chunks/pages/yolov7-tiny_${modelInputDimensions[0]}x${modelInputDimensions[1]}.onnx`}
176+
changeModelResolution={changeModelResolution}
160177
/>
161178
);
162179
};

models/yolov7-tiny_320x320.onnx

23.8 MB
Binary file not shown.
File renamed without changes.

public/sw.js

Lines changed: 101 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/sw.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)