Skip to content

Commit 8c5e427

Browse files
committed
chore(example): add label detection to mlkit example app
1 parent 84ef60a commit 8c5e427

4 files changed

Lines changed: 53 additions & 5 deletions

File tree

examples/mlkit/App.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ export default class CameraScreen extends React.Component {
4040
canDetectFaces: false,
4141
canDetectText: false,
4242
canDetectBarcode: false,
43+
canDetectLabels: false,
4344
faces: [],
4445
textBlocks: [],
4546
barcodes: [],
47+
labels: [],
4648
};
4749

4850
toggleFacing() {
@@ -234,8 +236,24 @@ export default class CameraScreen extends React.Component {
234236
</React.Fragment>
235237
);
236238

239+
labelsDetected = ({ labels = [] }) => this.setState({ labels });
240+
241+
renderLabels = () => (
242+
<View style={styles.labelsContainer} pointerEvents="none">
243+
{this.state.labels.sort((a, b) => b.confidence - a.confidence).map(this.renderLabel)}
244+
</View>
245+
);
246+
247+
renderLabel = ({ text, confidence }) => (
248+
<React.Fragment key={text}>
249+
<View>
250+
<Text style={[styles.labelText]}>{`${text} ${confidence.toFixed(2)}`}</Text>
251+
</View>
252+
</React.Fragment>
253+
);
254+
237255
renderCamera() {
238-
const { canDetectFaces, canDetectText, canDetectBarcode } = this.state;
256+
const { canDetectFaces, canDetectText, canDetectBarcode, canDetectLabels } = this.state;
239257
return (
240258
<RNCamera
241259
ref={ref => {
@@ -271,6 +289,7 @@ export default class CameraScreen extends React.Component {
271289
onFacesDetected={canDetectFaces ? this.facesDetected : null}
272290
onTextRecognized={canDetectText ? this.textRecognized : null}
273291
onGoogleVisionBarcodesDetected={canDetectBarcode ? this.barcodeRecognized : null}
292+
onLabelsDetected={canDetectLabels ? this.labelsDetected : null}
274293
googleVisionBarcodeType={RNCamera.Constants.GoogleVisionBarcodeDetection.BarcodeType.ALL}
275294
googleVisionBarcodeMode={
276295
RNCamera.Constants.GoogleVisionBarcodeDetection.BarcodeMode.ALTERNATE
@@ -306,20 +325,25 @@ export default class CameraScreen extends React.Component {
306325
}}
307326
>
308327
<TouchableOpacity onPress={this.toggle('canDetectFaces')} style={styles.flipButton}>
309-
<Text style={styles.flipText}>
328+
<Text style={styles.detectText}>
310329
{!canDetectFaces ? 'Detect Faces' : 'Detecting Faces'}
311330
</Text>
312331
</TouchableOpacity>
313332
<TouchableOpacity onPress={this.toggle('canDetectText')} style={styles.flipButton}>
314-
<Text style={styles.flipText}>
333+
<Text style={styles.detectText}>
315334
{!canDetectText ? 'Detect Text' : 'Detecting Text'}
316335
</Text>
317336
</TouchableOpacity>
318337
<TouchableOpacity onPress={this.toggle('canDetectBarcode')} style={styles.flipButton}>
319-
<Text style={styles.flipText}>
338+
<Text style={styles.detectText}>
320339
{!canDetectBarcode ? 'Detect Barcode' : 'Detecting Barcode'}
321340
</Text>
322341
</TouchableOpacity>
342+
<TouchableOpacity onPress={this.toggle('canDetectLabels')} style={styles.flipButton}>
343+
<Text style={styles.detectText}>
344+
{!canDetectLabels ? 'Detect Labels' : 'Detecting Labels'}
345+
</Text>
346+
</TouchableOpacity>
323347
</View>
324348
</View>
325349
<View
@@ -403,6 +427,7 @@ export default class CameraScreen extends React.Component {
403427
{!!canDetectFaces && this.renderLandmarks()}
404428
{!!canDetectText && this.renderTextBlocks()}
405429
{!!canDetectBarcode && this.renderBarcodes()}
430+
{!!canDetectLabels && this.renderLabels()}
406431
</RNCamera>
407432
);
408433
}
@@ -435,6 +460,16 @@ const styles = StyleSheet.create({
435460
color: 'white',
436461
fontSize: 15,
437462
},
463+
detectText: {
464+
color: 'white',
465+
fontSize: 12,
466+
textAlign: 'center',
467+
},
468+
labelText: {
469+
color: 'white',
470+
fontWeight: 'bold',
471+
fontSize: 15,
472+
},
438473
zoomText: {
439474
position: 'absolute',
440475
bottom: 70,
@@ -451,6 +486,15 @@ const styles = StyleSheet.create({
451486
left: 0,
452487
top: 0,
453488
},
489+
labelsContainer: {
490+
position: 'absolute',
491+
bottom: 0,
492+
right: 0,
493+
left: 0,
494+
top: 0,
495+
paddingTop: 150,
496+
alignItems: 'center',
497+
},
454498
face: {
455499
padding: 10,
456500
borderWidth: 2,

examples/mlkit/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Face Recognition: draws polygons around faces and red circles on top of face lan
1010

1111
Text Recognition: draws polygons around text blocks and recognized within them.
1212

13+
Label Detection: lists detected labels with confidence levels.
14+
1315
### Setup
1416

1517
1. Run `yarn install`.

examples/mlkit/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ android {
102102
minSdkVersion rootProject.ext.minSdkVersion
103103
targetSdkVersion rootProject.ext.targetSdkVersion
104104
missingDimensionStrategy 'react-native-camera', 'mlkit'
105+
multiDexEnabled true
105106
versionCode 1
106107
versionName "1.0"
107108
}

examples/mlkit/ios/Podfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ target 'mlkit' do
2727
pod 'react-native-camera', path: '../../../', subspecs: [
2828
'TextDetector',
2929
'FaceDetectorMLKit',
30-
'BarcodeDetectorMLKit'
30+
'BarcodeDetectorMLKit',
31+
'LabelDetectorMLKit',
3132
]
3233

3334
pod 'Firebase/Core'

0 commit comments

Comments
 (0)