| 67 | |
| 68 | useEffect(() => { |
| 69 | async function prepare() { |
| 70 | rafId.current = null; |
| 71 | |
| 72 | // Set initial orientation. |
| 73 | const curOrientation = await ScreenOrientation.getOrientationAsync(); |
| 74 | setOrientation(curOrientation); |
| 75 | |
| 76 | // Listens to orientation change. |
| 77 | ScreenOrientation.addOrientationChangeListener((event) => { |
| 78 | setOrientation(event.orientationInfo.orientation); |
| 79 | }); |
| 80 | |
| 81 | // Camera permission. |
| 82 | await Camera.requestCameraPermissionsAsync(); |
| 83 | |
| 84 | // Wait for tfjs to initialize the backend. |
| 85 | await tf.ready(); |
| 86 | |
| 87 | // Load movenet model. |
| 88 | // https://github.com/tensorflow/tfjs-models/tree/master/pose-detection |
| 89 | const movenetModelConfig: posedetection.MoveNetModelConfig = { |
| 90 | modelType: posedetection.movenet.modelType.SINGLEPOSE_LIGHTNING, |
| 91 | enableSmoothing: true, |
| 92 | }; |
| 93 | if (LOAD_MODEL_FROM_BUNDLE) { |
| 94 | const modelJson = require('./offline_model/model.json'); |
| 95 | const modelWeights1 = require('./offline_model/group1-shard1of2.bin'); |
| 96 | const modelWeights2 = require('./offline_model/group1-shard2of2.bin'); |
| 97 | movenetModelConfig.modelUrl = bundleResourceIO(modelJson, [ |
| 98 | modelWeights1, |
| 99 | modelWeights2, |
| 100 | ]); |
| 101 | } |
| 102 | const model = await posedetection.createDetector( |
| 103 | posedetection.SupportedModels.MoveNet, |
| 104 | movenetModelConfig |
| 105 | ); |
| 106 | setModel(model); |
| 107 | |
| 108 | // Ready! |
| 109 | setTfReady(true); |
| 110 | } |
| 111 | |
| 112 | prepare(); |
| 113 | }, []); |