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

Commit 8ec3732

Browse files
#699 Add ML Kit support
1 parent 0437b6e commit 8ec3732

File tree

15 files changed

+571
-391
lines changed

15 files changed

+571
-391
lines changed

demo/app/app.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ button {
8282
}
8383

8484
.barcodescanner {
85-
width: 320;
86-
height: 260;
85+
height: 420;
8786
margin-top: 16;
8887
}
8988

demo/app/main-page.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as pages from 'tns-core-modules/ui/page';
33
import { HelloWorldModel } from './main-view-model';
44
import { MLKitRecognizeTextResult } from "nativescript-plugin-firebase/mlkit/textrecognition";
55
import { MLKitScanBarcodesResult } from "nativescript-plugin-firebase/mlkit/barcodescanning";
6+
import { MLKitDetectFacesResult } from "nativescript-plugin-firebase/mlkit/facedetection";
67

78
const model = new HelloWorldModel();
89

@@ -24,5 +25,30 @@ export function onBarcodesScanResult(scanResult: any) {
2425

2526
export function onTextRecognitionResult(scanResult: any) {
2627
const value: MLKitRecognizeTextResult = scanResult.value;
27-
model.set("textValue", value.features.map(feature => feature.text).join("\n"));
28+
model.set("textValue", value.features.map(feature => feature.text).join("\n\n"));
29+
}
30+
31+
export function onFaceDetectionResult(scanResult: any) {
32+
const value: MLKitDetectFacesResult = scanResult.value;
33+
if (value.faces.length > 0) {
34+
let allSmilingAndEyesOpen = true;
35+
value.faces.forEach(face => {
36+
allSmilingAndEyesOpen = allSmilingAndEyesOpen && face.smilingProbability && face.leftEyeOpenProbability && face.rightEyeOpenProbability &&
37+
face.smilingProbability > 0.7 && face.leftEyeOpenProbability > 0.7 && face.rightEyeOpenProbability > 0.7;
38+
});
39+
model.set("allOK", `All smiling and eyes open? ${allSmilingAndEyesOpen ? 'Yes, screen grabbed:' : 'Nope. Sad.'}`);
40+
// model.set("textValue", value.faces.map(face => JSON.stringify(face)).join("\n"));
41+
model.set("textValue", value.faces.map(face => `Smiling? ${round(face.smilingProbability)}%\nLeft eye open? ${round(face.leftEyeOpenProbability)}%\nRight eye open? ${round(face.rightEyeOpenProbability)}%`).join("\n\n"));
42+
43+
if (allSmilingAndEyesOpen && value.imageSource) {
44+
model.set("lastMatch", value.imageSource);
45+
}
46+
}
47+
}
48+
49+
function round (input) {
50+
if (isNaN(input)) {
51+
return 0;
52+
}
53+
return Math.round(input * 100);
2854
}

demo/app/main-page.xml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<TabView loaded="pageLoaded" class="page" xmlns:FirebaseMLKitBarcodeScanning="nativescript-plugin-firebase/mlkit/barcodescanning">
1+
<TabView
2+
loaded="pageLoaded"
3+
class="page"
4+
xmlns:FirebaseMLKitBarcodeScanning="nativescript-plugin-firebase/mlkit/barcodescanning"
5+
xmlns:FirebaseMLKitFaceDetection="nativescript-plugin-firebase/mlkit/facedetection">
26
<TabView.items>
37

48
<TabViewItem title="ML Kit">
@@ -146,11 +150,24 @@
146150
class="barcodescanner"
147151
scanResult="onBarcodesScanResult" />
148152

149-
<!--<Label row="3" text="Barcode format:" />-->
150-
<!--<Label row="3" col="1" text="{{ barcodeFormat }}" />-->
153+
<!--FirebaseMLKitFaceDetection:MLKitFaceDetection
154+
row="2"
155+
colSpan="2"
156+
class="barcodescanner"
157+
scanResult="onFaceDetectionResult" /-->
158+
159+
<Label row="3" text="Barcode format:" />
160+
<Label row="3" col="1" text="{{ barcodeFormat }}" />
161+
162+
<Label row="4" text="Barcode value:" />
163+
<Label row="4" col="1" text="{{ barcodeValue }}" />
164+
165+
<!--<Label row="3" colSpan="2" text="{{ allOK }}" textWrap="true" />-->
166+
167+
<!--<Label row="4" text="{{ textValue }}" textWrap="true" />-->
168+
<!--<Label row="4" col="1" text="If all above 70%, the image is shown here." textWrap="true" visibility="{{ lastMatch ? 'collapsed' : 'visible' }} " />-->
169+
<!--<Image row="4" col="1" src="{{ lastMatch }}" visibility="{{ lastMatch ? 'visible' : 'collapsed' }} " />-->
151170

152-
<!--<Label row="4" text="Barcode value:" />-->
153-
<Label row="3" colSpan="2" text="{{ textValue }}" textWrap="true" />
154171

155172

156173

demo/app_resources/Android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<meta-data
3333
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
34-
android:value="text,barcode" />
34+
android:value="text,barcode,face" />
3535

3636
<activity
3737
android:name="com.tns.NativeScriptActivity"

src/mlkit/barcodescanning/barcodescanning-common.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ContentView } from "tns-core-modules/ui/content-view";
21
import { booleanConverter } from "tns-core-modules/ui/core/view-base";
32
import { Property } from "tns-core-modules/ui/core/properties";
3+
import { MLKitCameraView } from "../mlkit-cameraview";
44

55
export enum BarcodeFormat {
66
// UNKNOWN = -1,
@@ -20,10 +20,6 @@ export enum BarcodeFormat {
2020
AZTEC = 4096,
2121
}
2222

23-
export function getBarcodeFormatFromIndex(index: number): string {
24-
return BarcodeFormat[index];
25-
}
26-
2723
export const formatsProperty = new Property<MLKitBarcodeScanner, string>({
2824
name: "formats",
2925
defaultValue: null,
@@ -47,7 +43,7 @@ export const reportDuplicatesProperty = new Property<MLKitBarcodeScanner, boolea
4743
valueConverter: booleanConverter
4844
});
4945

50-
export abstract class MLKitBarcodeScanner extends ContentView {
46+
export abstract class MLKitBarcodeScanner extends MLKitCameraView {
5147
static scanResultEvent: string = "scanResult";
5248

5349
protected formats: string;

0 commit comments

Comments
 (0)