From 8ec3732c430d7889c3f9b8a5aa618262eec18fc7 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Thu, 10 May 2018 23:22:22 +0200 Subject: [PATCH] #699 Add ML Kit support --- demo/app/app.css | 3 +- demo/app/main-page.ts | 28 +- demo/app/main-page.xml | 27 +- .../Android/src/main/AndroidManifest.xml | 2 +- .../barcodescanning/barcodescanning-common.ts | 8 +- src/mlkit/barcodescanning/index.android.ts | 344 ++---------------- src/mlkit/barcodescanning/index.d.ts | 6 +- src/mlkit/barcodescanning/index.ios.ts | 56 +-- .../facedetection/facedetection-common.ts | 32 ++ src/mlkit/facedetection/index.android.ts | 106 ++++++ src/mlkit/facedetection/index.d.ts | 20 + src/mlkit/index.ts | 15 +- src/mlkit/mlkit-cameraview.android.ts | 312 ++++++++++++++++ src/mlkit/mlkit-cameraview.ios.ts | 0 src/mlkit/textrecognition/index.d.ts | 3 +- 15 files changed, 571 insertions(+), 391 deletions(-) create mode 100644 src/mlkit/facedetection/facedetection-common.ts create mode 100644 src/mlkit/facedetection/index.android.ts create mode 100644 src/mlkit/facedetection/index.d.ts create mode 100644 src/mlkit/mlkit-cameraview.android.ts create mode 100644 src/mlkit/mlkit-cameraview.ios.ts diff --git a/demo/app/app.css b/demo/app/app.css index e7394b8a..81e53074 100644 --- a/demo/app/app.css +++ b/demo/app/app.css @@ -82,8 +82,7 @@ button { } .barcodescanner { - width: 320; - height: 260; + height: 420; margin-top: 16; } diff --git a/demo/app/main-page.ts b/demo/app/main-page.ts index 6b679cee..e425b747 100644 --- a/demo/app/main-page.ts +++ b/demo/app/main-page.ts @@ -3,6 +3,7 @@ import * as pages from 'tns-core-modules/ui/page'; import { HelloWorldModel } from './main-view-model'; import { MLKitRecognizeTextResult } from "nativescript-plugin-firebase/mlkit/textrecognition"; import { MLKitScanBarcodesResult } from "nativescript-plugin-firebase/mlkit/barcodescanning"; +import { MLKitDetectFacesResult } from "nativescript-plugin-firebase/mlkit/facedetection"; const model = new HelloWorldModel(); @@ -24,5 +25,30 @@ export function onBarcodesScanResult(scanResult: any) { export function onTextRecognitionResult(scanResult: any) { const value: MLKitRecognizeTextResult = scanResult.value; - model.set("textValue", value.features.map(feature => feature.text).join("\n")); + model.set("textValue", value.features.map(feature => feature.text).join("\n\n")); +} + +export function onFaceDetectionResult(scanResult: any) { + const value: MLKitDetectFacesResult = scanResult.value; + if (value.faces.length > 0) { + let allSmilingAndEyesOpen = true; + value.faces.forEach(face => { + allSmilingAndEyesOpen = allSmilingAndEyesOpen && face.smilingProbability && face.leftEyeOpenProbability && face.rightEyeOpenProbability && + face.smilingProbability > 0.7 && face.leftEyeOpenProbability > 0.7 && face.rightEyeOpenProbability > 0.7; + }); + model.set("allOK", `All smiling and eyes open? ${allSmilingAndEyesOpen ? 'Yes, screen grabbed:' : 'Nope. Sad.'}`); + // model.set("textValue", value.faces.map(face => JSON.stringify(face)).join("\n")); + 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")); + + if (allSmilingAndEyesOpen && value.imageSource) { + model.set("lastMatch", value.imageSource); + } + } +} + +function round (input) { + if (isNaN(input)) { + return 0; + } + return Math.round(input * 100); } diff --git a/demo/app/main-page.xml b/demo/app/main-page.xml index 34c88e19..d73f8ae6 100644 --- a/demo/app/main-page.xml +++ b/demo/app/main-page.xml @@ -1,4 +1,8 @@ - + @@ -146,11 +150,24 @@ class="barcodescanner" scanResult="onBarcodesScanResult" /> - - + + +