-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
132 lines (118 loc) · 2.94 KB
/
App.js
File metadata and controls
132 lines (118 loc) · 2.94 KB
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
124
125
126
127
128
129
130
131
132
import React, { useState, useEffect } from 'react';
import {
StyleSheet,
View,
SafeAreaView,
Text,
TextInput,
ActivityIndicator,
} from 'react-native';
import {
Camera,
useCameraDevices,
useFrameProcessor,
} from 'react-native-vision-camera';
import { labelImage } from 'vision-camera-image-labeler';
import Animated, {
useAnimatedProps,
useSharedValue,
} from 'react-native-reanimated';
const AnimatedText = Animated.createAnimatedComponent(TextInput);
export default function App() {
const [cameraPermission, setCameraPermission] = useState();
const detectorResult = useSharedValue('');
useEffect(() => {
(async () => {
const cameraPermissionStatus = await Camera.requestCameraPermission();
setCameraPermission(cameraPermissionStatus);
})();
}, []);
console.log(`Camera permission status: ${cameraPermission}`);
const devices = useCameraDevices();
const cameraDevice = devices.back;
const frameProcessor = useFrameProcessor(frame => {
'worklet';
const imageLabels = labelImage(frame);
console.log('Image labels:', imageLabels);
detectorResult.value = imageLabels[0]?.label;
}, []);
const animatedTextProps = useAnimatedProps(
() => ({ text: detectorResult.value }),
[detectorResult.value],
);
const renderDetectorContent = () => {
if (cameraDevice && cameraPermission === 'authorized') {
return (
<Camera
style={styles.camera}
device={cameraDevice}
isActive={true}
frameProcessor={frameProcessor}
frameProcessorFps={3}
/>
);
}
return <ActivityIndicator size="large" color="#1C6758" />;
};
return (
<View style={styles.screen}>
<SafeAreaView style={styles.saveArea}>
<View style={styles.header}>
<Text style={styles.headerText}>React Native Image Detector</Text>
</View>
</SafeAreaView>
<View style={styles.caption}>
<Text style={styles.captionText}>
Welcome To React-Native-Vision-Camera Tutorial
</Text>
</View>
{renderDetectorContent()}
<AnimatedText
style={styles.detectorValueText}
animatedProps={animatedTextProps}
editable={false}
multiline={true}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
backgroundColor: '#EEF2E6',
},
saveArea: {
backgroundColor: '#3D8361',
},
header: {
height: 50,
backgroundColor: '#3D8361',
alignItems: 'center',
justifyContent: 'center',
},
headerText: {
color: '#ffffff',
fontSize: 20,
},
caption: {
height: 120,
justifyContent: 'center',
alignItems: 'center',
},
captionText: {
color: '#100F0F',
fontSize: 16,
fontWeight: '600',
},
camera: {
height: 540,
width: '92%',
alignSelf: 'center',
},
detectorValueText: {
paddingVertical: 20,
textAlign: 'center',
color: '#100F0F',
fontSize: 24,
},
});