Skip to content

Commit 41f3d92

Browse files
authored
Merge branch 'main' into neuroevolution
2 parents 4d3976e + f04ce90 commit 41f3d92

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import poseDetection from "./PoseDetection";
44
import * as tf from "@tensorflow/tfjs";
55
import * as tfvis from "@tensorflow/tfjs-vis";
66
import p5Utils from "./utils/p5Utils";
7+
import setBackend from "./utils/setBackend";
78

89
export default Object.assign(
910
{ p5Utils },
@@ -13,5 +14,6 @@ export default Object.assign(
1314
neuralNetwork,
1415
handpose,
1516
poseDetection,
17+
setBackend,
1618
}
1719
);

src/utils/setBackend.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as tf from "@tensorflow/tfjs";
2+
3+
/**
4+
* Manually set the Tensorflow.js Backend to the specified backend.
5+
* Determines how the operation is done for the following features:
6+
* - handpose under "tfjs" runtime
7+
* - poseDetection
8+
* - facemesh
9+
* - neuralNetwork
10+
*
11+
* @param {string} backend the backend to set to. Must be "cpu", "webgl", or "webgpu".
12+
* @return {boolean} true if the backend was set successfully.
13+
*/
14+
export default function setBackend(backend) {
15+
switch (backend) {
16+
case "cpu":
17+
tf.setBackend("cpu");
18+
break;
19+
case "webgl":
20+
tf.setBackend("webgl");
21+
break;
22+
case "webgpu":
23+
tf.setBackend("webgpu");
24+
break;
25+
default:
26+
throw new Error(
27+
`Invalid backend: ${backend}. The backend parameter must be set to one of the following strings: "cpu", "webgl", or "webgpu".`
28+
);
29+
}
30+
31+
return true;
32+
}

0 commit comments

Comments
 (0)