|
| 1 | +import React, { |
| 2 | + useCallback, |
| 3 | + useContext, |
| 4 | + useEffect, |
| 5 | + useMemo, |
| 6 | + useRef, |
| 7 | + useState, |
| 8 | +} from 'react'; |
| 9 | +import { |
| 10 | + StatusBar, |
| 11 | + StyleSheet, |
| 12 | + Text, |
| 13 | + TouchableOpacity, |
| 14 | + View, |
| 15 | +} from 'react-native'; |
| 16 | +import { useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 17 | + |
| 18 | +import { |
| 19 | + Camera, |
| 20 | + getCameraFormat, |
| 21 | + Templates, |
| 22 | + useCameraDevices, |
| 23 | + useCameraPermission, |
| 24 | + useFrameOutput, |
| 25 | +} from 'react-native-vision-camera'; |
| 26 | +import { scheduleOnRN } from 'react-native-worklets'; |
| 27 | +import { |
| 28 | + Detection, |
| 29 | + SSDLITE_320_MOBILENET_V3_LARGE, |
| 30 | + useObjectDetection, |
| 31 | +} from 'react-native-executorch'; |
| 32 | +import { GeneratingContext } from '../../context'; |
| 33 | +import Spinner from '../../components/Spinner'; |
| 34 | +import ColorPalette from '../../colors'; |
| 35 | + |
| 36 | +export default function ObjectDetectionLiveScreen() { |
| 37 | + const insets = useSafeAreaInsets(); |
| 38 | + |
| 39 | + const model = useObjectDetection({ model: SSDLITE_320_MOBILENET_V3_LARGE }); |
| 40 | + const { setGlobalGenerating } = useContext(GeneratingContext); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + setGlobalGenerating(model.isGenerating); |
| 44 | + }, [model.isGenerating, setGlobalGenerating]); |
| 45 | + const [detectionCount, setDetectionCount] = useState(0); |
| 46 | + const [fps, setFps] = useState(0); |
| 47 | + const lastFrameTimeRef = useRef(Date.now()); |
| 48 | + |
| 49 | + const cameraPermission = useCameraPermission(); |
| 50 | + const devices = useCameraDevices(); |
| 51 | + const device = devices.find((d) => d.position === 'back') ?? devices[0]; |
| 52 | + |
| 53 | + const format = useMemo(() => { |
| 54 | + if (device == null) return undefined; |
| 55 | + try { |
| 56 | + return getCameraFormat(device, Templates.FrameProcessing); |
| 57 | + } catch { |
| 58 | + return undefined; |
| 59 | + } |
| 60 | + }, [device]); |
| 61 | + |
| 62 | + const updateStats = useCallback((results: Detection[]) => { |
| 63 | + setDetectionCount(results.length); |
| 64 | + const now = Date.now(); |
| 65 | + const timeDiff = now - lastFrameTimeRef.current; |
| 66 | + if (timeDiff > 0) { |
| 67 | + setFps(Math.round(1000 / timeDiff)); |
| 68 | + } |
| 69 | + lastFrameTimeRef.current = now; |
| 70 | + }, []); |
| 71 | + |
| 72 | + const frameOutput = useFrameOutput({ |
| 73 | + pixelFormat: 'rgb', |
| 74 | + dropFramesWhileBusy: true, |
| 75 | + onFrame(frame) { |
| 76 | + 'worklet'; |
| 77 | + if (!model.runOnFrame) { |
| 78 | + frame.dispose(); |
| 79 | + return; |
| 80 | + } |
| 81 | + try { |
| 82 | + const result = model.runOnFrame(frame, 0.5); |
| 83 | + if (result) { |
| 84 | + scheduleOnRN(updateStats, result); |
| 85 | + } |
| 86 | + } catch { |
| 87 | + // ignore frame errors |
| 88 | + } finally { |
| 89 | + frame.dispose(); |
| 90 | + } |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + if (!model.isReady) { |
| 95 | + return ( |
| 96 | + <Spinner |
| 97 | + visible={!model.isReady} |
| 98 | + textContent={`Loading the model ${(model.downloadProgress * 100).toFixed(0)} %`} |
| 99 | + /> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + if (!cameraPermission.hasPermission) { |
| 104 | + return ( |
| 105 | + <View style={styles.centered}> |
| 106 | + <Text style={styles.message}>Camera access needed</Text> |
| 107 | + <TouchableOpacity |
| 108 | + onPress={() => cameraPermission.requestPermission()} |
| 109 | + style={styles.button} |
| 110 | + > |
| 111 | + <Text style={styles.buttonText}>Grant Permission</Text> |
| 112 | + </TouchableOpacity> |
| 113 | + </View> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + if (device == null) { |
| 118 | + return ( |
| 119 | + <View style={styles.centered}> |
| 120 | + <Text style={styles.message}>No camera device found</Text> |
| 121 | + </View> |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + return ( |
| 126 | + <View style={styles.container}> |
| 127 | + <StatusBar barStyle="light-content" translucent /> |
| 128 | + |
| 129 | + <Camera |
| 130 | + style={StyleSheet.absoluteFill} |
| 131 | + device={device} |
| 132 | + outputs={[frameOutput]} |
| 133 | + isActive={true} |
| 134 | + format={format} |
| 135 | + /> |
| 136 | + |
| 137 | + <View |
| 138 | + style={[styles.bottomBarWrapper, { paddingBottom: insets.bottom + 12 }]} |
| 139 | + pointerEvents="none" |
| 140 | + > |
| 141 | + <View style={styles.bottomBar}> |
| 142 | + <View style={styles.statItem}> |
| 143 | + <Text style={styles.statValue}>{detectionCount}</Text> |
| 144 | + <Text style={styles.statLabel}>objects</Text> |
| 145 | + </View> |
| 146 | + <View style={styles.statDivider} /> |
| 147 | + <View style={styles.statItem}> |
| 148 | + <Text style={styles.statValue}>{fps}</Text> |
| 149 | + <Text style={styles.statLabel}>fps</Text> |
| 150 | + </View> |
| 151 | + </View> |
| 152 | + </View> |
| 153 | + </View> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +const styles = StyleSheet.create({ |
| 158 | + container: { |
| 159 | + flex: 1, |
| 160 | + backgroundColor: 'black', |
| 161 | + }, |
| 162 | + centered: { |
| 163 | + flex: 1, |
| 164 | + backgroundColor: 'black', |
| 165 | + justifyContent: 'center', |
| 166 | + alignItems: 'center', |
| 167 | + gap: 16, |
| 168 | + }, |
| 169 | + message: { |
| 170 | + color: 'white', |
| 171 | + fontSize: 18, |
| 172 | + }, |
| 173 | + button: { |
| 174 | + paddingHorizontal: 24, |
| 175 | + paddingVertical: 12, |
| 176 | + backgroundColor: ColorPalette.primary, |
| 177 | + borderRadius: 24, |
| 178 | + }, |
| 179 | + buttonText: { |
| 180 | + color: 'white', |
| 181 | + fontSize: 15, |
| 182 | + fontWeight: '600', |
| 183 | + letterSpacing: 0.3, |
| 184 | + }, |
| 185 | + bottomBarWrapper: { |
| 186 | + position: 'absolute', |
| 187 | + bottom: 0, |
| 188 | + left: 0, |
| 189 | + right: 0, |
| 190 | + alignItems: 'center', |
| 191 | + }, |
| 192 | + bottomBar: { |
| 193 | + flexDirection: 'row', |
| 194 | + alignItems: 'center', |
| 195 | + backgroundColor: 'rgba(0, 0, 0, 0.55)', |
| 196 | + borderRadius: 24, |
| 197 | + paddingHorizontal: 28, |
| 198 | + paddingVertical: 10, |
| 199 | + gap: 24, |
| 200 | + }, |
| 201 | + statItem: { |
| 202 | + alignItems: 'center', |
| 203 | + }, |
| 204 | + statValue: { |
| 205 | + color: 'white', |
| 206 | + fontSize: 22, |
| 207 | + fontWeight: '700', |
| 208 | + letterSpacing: -0.5, |
| 209 | + }, |
| 210 | + statLabel: { |
| 211 | + color: 'rgba(255,255,255,0.55)', |
| 212 | + fontSize: 11, |
| 213 | + fontWeight: '500', |
| 214 | + textTransform: 'uppercase', |
| 215 | + letterSpacing: 0.8, |
| 216 | + }, |
| 217 | + statDivider: { |
| 218 | + width: 1, |
| 219 | + height: 32, |
| 220 | + backgroundColor: 'rgba(255,255,255,0.2)', |
| 221 | + }, |
| 222 | +}); |
0 commit comments