MCPcopy Index your code
hub / github.com/tensorflow/tfjs-examples / App

Function App

react-native/image-classification/react-native-cli/App.tsx:7–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import * as mobilenet from '@tensorflow-models/mobilenet';
6
7const App = () => {
8 const [isTfReady, setIsTfReady] = useState(false);
9 const [result, setResult] = useState('');
10 const image = useRef(null);
11
12 const load = async () => {
13 try {
14 // Load mobilenet.
15 await tf.ready();
16 const model = await mobilenet.load();
17 setIsTfReady(true);
18
19 // Start inference and show result.
20 const image = require('./basketball.jpg');
21 const imageAssetPath = Image.resolveAssetSource(image);
22 const response = await fetch(imageAssetPath.uri, {}, { isBinary: true });
23 const imageDataArrayBuffer = await response.arrayBuffer();
24 const imageData = new Uint8Array(imageDataArrayBuffer);
25 const imageTensor = decodeJpeg(imageData);
26 const prediction = await model.classify(imageTensor);
27 if (prediction && prediction.length > 0) {
28 setResult(
29 `${prediction[0].className} (${prediction[0].probability.toFixed(3)})`
30 );
31 }
32 } catch (err) {
33 console.log(err);
34 }
35 };
36
37 useEffect(() => {
38 load();
39 }, []);
40
41 return (
42 <View
43 style={{
44 height: '100%',
45 display: 'flex',
46 flexDirection: 'column',
47 alignItems: 'center',
48 justifyContent: 'center',
49 }}>
50 <Image
51 ref={image}
52 source={require('./basketball.jpg')}
53 style={{width: 200, height: 200}}
54 />
55 {!isTfReady && <Text>Loading TFJS model...</Text>}
56 {isTfReady && result === '' && <Text>Classifying...</Text>}
57 {result !== '' && <Text>{result}</Text>}
58 </View>
59 );
60};
61
62export default App;

Callers

nothing calls this directly

Calls 1

loadFunction · 0.70

Tested by

no test coverage detected