Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit ac6e0d3

Browse files
Harden against ML Kit results being null when recognized in the cloud #786
1 parent 16a64a8 commit ac6e0d3

File tree

5 files changed

+58
-46
lines changed

5 files changed

+58
-46
lines changed

src/mlkit/barcodescanning/index.android.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase {
2727
barcodes: []
2828
};
2929

30-
// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
31-
for (let i = 0; i < barcodes.size(); i++) {
32-
const barcode = barcodes.get(i);
33-
result.barcodes.push({
34-
value: barcode.getRawValue(),
35-
format: BarcodeFormat[barcode.getFormat()],
36-
android: barcode
37-
});
30+
if (barcodes) {
31+
// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
32+
for (let i = 0; i < barcodes.size(); i++) {
33+
const barcode = barcodes.get(i);
34+
result.barcodes.push({
35+
value: barcode.getRawValue(),
36+
format: BarcodeFormat[barcode.getFormat()],
37+
android: barcode
38+
});
39+
}
3840
}
3941

4042
this.notify({
@@ -70,14 +72,16 @@ export function scanBarcodesOnDevice(options: MLKitScanBarcodesOnDeviceOptions):
7072
barcodes: []
7173
};
7274

73-
// There are more details available, see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
74-
for (let i = 0; i < barcodes.size(); i++) {
75-
const barcode = barcodes.get(i);
76-
result.barcodes.push({
77-
value: barcode.getRawValue(),
78-
format: BarcodeFormat[barcode.getFormat()],
79-
android: barcode
80-
});
75+
if (barcodes) {
76+
// There are more details available, see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
77+
for (let i = 0; i < barcodes.size(); i++) {
78+
const barcode = barcodes.get(i);
79+
result.barcodes.push({
80+
value: barcode.getRawValue(),
81+
format: BarcodeFormat[barcode.getFormat()],
82+
android: barcode
83+
});
84+
}
8185
}
8286

8387
resolve(result);

src/mlkit/facedetection/index.android.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class MLKitFaceDetection extends MLKitFaceDetectionBase {
1919
return new com.google.android.gms.tasks.OnSuccessListener({
2020
onSuccess: faces => {
2121

22-
if (faces.size() === 0) return;
22+
if (!faces || faces.size() === 0) return;
2323

2424
// const imageSource = new ImageSource();
2525
// imageSource.setNativeSource(this.lastVisionImage.getBitmapForDebugging());
@@ -69,21 +69,23 @@ export function detectFacesOnDevice(options: MLKitDetectFacesOnDeviceOptions): P
6969
const firebaseVisionFaceDetector = getFaceDetector(options);
7070

7171
const onSuccessListener = new com.google.android.gms.tasks.OnSuccessListener({
72-
onSuccess: (faces) => {
72+
onSuccess: faces => {
7373

7474
const result = <MLKitDetectFacesOnDeviceResult>{
7575
faces: []
7676
};
7777

78-
// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
79-
for (let i = 0; i < faces.size(); i++) {
80-
const face = faces.get(i);
81-
result.faces.push({
82-
smilingProbability: face.getSmilingProbability() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.UNCOMPUTED_PROBABILITY ? face.getSmilingProbability() : undefined,
83-
leftEyeOpenProbability: face.getLeftEyeOpenProbability() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.UNCOMPUTED_PROBABILITY ? face.getLeftEyeOpenProbability() : undefined,
84-
rightEyeOpenProbability: face.getRightEyeOpenProbability() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.UNCOMPUTED_PROBABILITY ? face.getRightEyeOpenProbability() : undefined,
85-
trackingId: face.getTrackingId() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.INVALID_ID ? face.getTrackingId() : undefined
86-
});
78+
if (faces) {
79+
// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
80+
for (let i = 0; i < faces.size(); i++) {
81+
const face = faces.get(i);
82+
result.faces.push({
83+
smilingProbability: face.getSmilingProbability() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.UNCOMPUTED_PROBABILITY ? face.getSmilingProbability() : undefined,
84+
leftEyeOpenProbability: face.getLeftEyeOpenProbability() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.UNCOMPUTED_PROBABILITY ? face.getLeftEyeOpenProbability() : undefined,
85+
rightEyeOpenProbability: face.getRightEyeOpenProbability() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.UNCOMPUTED_PROBABILITY ? face.getRightEyeOpenProbability() : undefined,
86+
trackingId: face.getTrackingId() !== com.google.firebase.ml.vision.face.FirebaseVisionFace.INVALID_ID ? face.getTrackingId() : undefined
87+
});
88+
}
8789
}
8890

8991
resolve(result);

src/mlkit/imagelabeling/index.android.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ export function labelImageOnDevice(options: MLKitImageLabelingOnDeviceOptions):
6565
labels: []
6666
};
6767

68-
for (let i = 0; i < labels.size(); i++) {
69-
const label = labels.get(i);
70-
result.labels.push({
71-
text: label.getLabel(),
72-
confidence: label.getConfidence()
73-
});
68+
if (labels) {
69+
for (let i = 0; i < labels.size(); i++) {
70+
const label = labels.get(i);
71+
result.labels.push({
72+
text: label.getLabel(),
73+
confidence: label.getConfidence()
74+
});
75+
}
7476
}
7577

7678
resolve(result);
@@ -111,12 +113,14 @@ export function labelImageCloud(options: MLKitImageLabelingCloudOptions): Promis
111113
labels: []
112114
};
113115

114-
for (let i = 0; i < labels.size(); i++) {
115-
const label = labels.get(i);
116-
result.labels.push({
117-
text: label.getLabel(),
118-
confidence: label.getConfidence()
119-
});
116+
if (labels) {
117+
for (let i = 0; i < labels.size(); i++) {
118+
const label = labels.get(i);
119+
result.labels.push({
120+
text: label.getLabel(),
121+
confidence: label.getConfidence()
122+
});
123+
}
120124
}
121125

122126
resolve(result);

src/mlkit/landmarkrecognition/index.android.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ export function recognizeLandmarksCloud(options: MLKitLandmarkRecognitionCloudOp
2626
landmarks: []
2727
};
2828

29-
for (let i = 0; i < landmarks.size(); i++) {
30-
const landmark = landmarks.get(i);
31-
result.landmarks.push({
32-
name: landmark.getLandmark(),
33-
confidence: landmark.getConfidence()
34-
});
29+
if (landmarks) {
30+
for (let i = 0; i < landmarks.size(); i++) {
31+
const landmark = landmarks.get(i);
32+
result.landmarks.push({
33+
name: landmark.getLandmark(),
34+
confidence: landmark.getConfidence()
35+
});
36+
}
3537
}
3638

3739
resolve(result);

src/mlkit/textrecognition/index.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function recognizeTextCloud(options: MLKitRecognizeTextCloudOptions): Pro
132132
const onSuccessListener = new com.google.android.gms.tasks.OnSuccessListener({
133133
onSuccess: firebaseVisionCloudText => {
134134
resolve({
135-
text: firebaseVisionCloudText.getText()
135+
text: firebaseVisionCloudText ? firebaseVisionCloudText.getText() : null
136136
});
137137
firebaseVisionCloudTextDetector.close();
138138
}

0 commit comments

Comments
 (0)