@@ -2,11 +2,16 @@ import { ImageSource } from "tns-core-modules/image-source";
22import { MLKitDetectFacesOnDeviceOptions , MLKitDetectFacesOnDeviceResult } from "./" ;
33import { MLKitOptions } from "../index" ;
44import { MLKitFaceDetection as MLKitFaceDetectionBase } from "./facedetection-common" ;
5+ import { ios as iosUtils } from "tns-core-modules/utils/utils" ;
56
67export class MLKitFaceDetection extends MLKitFaceDetectionBase {
78
89 protected createDetector ( ) : any {
9- return getDetector ( ) ;
10+ return getDetector ( {
11+ detectionMode : this . detectionMode ,
12+ enableFaceTracking : this . enableFaceTracking ,
13+ minimumFaceSize : this . minimumFaceSize
14+ } ) ;
1015 }
1116
1217 protected createSuccessListener ( ) : any {
@@ -25,7 +30,8 @@ export class MLKitFaceDetection extends MLKitFaceDetectionBase {
2530 result . faces . push ( {
2631 smilingProbability : face . hasSmilingProbability ? face . smilingProbability : undefined ,
2732 leftEyeOpenProbability : face . hasLeftEyeOpenProbability ? face . leftEyeOpenProbability : undefined ,
28- rightEyeOpenProbability : face . hasRightEyeOpenProbability ? face . rightEyeOpenProbability : undefined
33+ rightEyeOpenProbability : face . hasRightEyeOpenProbability ? face . rightEyeOpenProbability : undefined ,
34+ trackingId : face . hasTrackingID ? face . trackingID : undefined
2935 } ) ;
3036 }
3137
@@ -42,25 +48,32 @@ export class MLKitFaceDetection extends MLKitFaceDetectionBase {
4248 protected rotateRecording ( ) : boolean {
4349 return false ;
4450 }
51+
52+ getVisionOrientation ( imageOrientation : UIImageOrientation ) : FIRVisionDetectorImageOrientation {
53+ if ( imageOrientation === UIImageOrientation . Up && ! iosUtils . isLandscape ( ) ) {
54+ return FIRVisionDetectorImageOrientation . RightTop ;
55+ } else {
56+ return super . getVisionOrientation ( imageOrientation ) ;
57+ }
58+ }
4559}
4660
47- function getDetector ( ) : FIRVisionFaceDetector {
48- // TODO make configurable (see #704)
61+ function getDetector ( options : MLKitDetectFacesOnDeviceOptions ) : FIRVisionFaceDetector {
4962 const firVision : FIRVision = FIRVision . vision ( ) ;
50- const options = FIRVisionFaceDetectorOptions . new ( ) ;
51- options . modeType = FIRVisionFaceDetectorMode . Accurate ;
52- options . landmarkType = FIRVisionFaceDetectorLandmark . All ;
53- options . classificationType = FIRVisionFaceDetectorClassification . All ;
54- options . minFaceSize = 0.1 ;
55- // options.isTrackingEnabled = true;
56- return firVision . faceDetectorWithOptions ( options ) ;
63+ const firOptions = FIRVisionFaceDetectorOptions . new ( ) ;
64+ firOptions . modeType = options . detectionMode === "accurate" ? FIRVisionFaceDetectorMode . Accurate : FIRVisionFaceDetectorMode . Fast ;
65+ // firOptions .landmarkType = FIRVisionFaceDetectorLandmark.All; // TODO
66+ // firOptions .classificationType = FIRVisionFaceDetectorClassification.All; // TODO
67+ firOptions . minFaceSize = options . minimumFaceSize ;
68+ firOptions . isTrackingEnabled = options . enableFaceTracking == = true ;
69+ return firVision . faceDetectorWithOptions ( firOptions ) ;
5770}
5871
5972// TODO somehow this function doesn't work.. probably because of the passed image, but I can't find the cause.. the live camera version works great tho
6073export function detectFacesOnDevice ( options : MLKitDetectFacesOnDeviceOptions ) : Promise < MLKitDetectFacesOnDeviceResult > {
6174 return new Promise ( ( resolve , reject ) => {
6275 try {
63- const faceDetector = getDetector ( ) ;
76+ const faceDetector = getDetector ( options ) ;
6477 faceDetector . detectInImageCompletion ( getImage ( options ) , ( faces : NSArray < FIRVisionFace > , error : NSError ) => {
6578 if ( error !== null ) {
6679 reject ( error . localizedDescription ) ;
@@ -75,7 +88,8 @@ export function detectFacesOnDevice(options: MLKitDetectFacesOnDeviceOptions): P
7588 result . faces . push ( {
7689 smilingProbability : face . hasSmilingProbability ? face . smilingProbability : undefined ,
7790 leftEyeOpenProbability : face . hasLeftEyeOpenProbability ? face . leftEyeOpenProbability : undefined ,
78- rightEyeOpenProbability : face . hasRightEyeOpenProbability ? face . rightEyeOpenProbability : undefined
91+ rightEyeOpenProbability : face . hasRightEyeOpenProbability ? face . rightEyeOpenProbability : undefined ,
92+ trackingId : face . hasTrackingID ? face . trackingID : undefined
7993 } ) ;
8094 }
8195 resolve ( result ) ;
@@ -88,19 +102,7 @@ export function detectFacesOnDevice(options: MLKitDetectFacesOnDeviceOptions): P
88102 } ) ;
89103}
90104
91- // TODO resize the image (here), like the example app?
92105function getImage ( options : MLKitOptions ) : FIRVisionImage {
93106 const image : UIImage = options . image instanceof ImageSource ? options . image . ios : options . image . imageSource . ios ;
94- console . log ( ">> image.imageOrientation: " + image . imageOrientation ) ;
95-
96- const fIRVisionImageMetadata = FIRVisionImageMetadata . new ( ) ;
97- // fIRVisionImageMetadata.orientation = FIRVisionDetectorImageOrientation.TopLeft;
98-
99- // const randomOrientation = Math.floor(Math.random() * 8) + 1;
100- // console.log(">>> randomOrientation: " + randomOrientation);
101- fIRVisionImageMetadata . orientation = 1 ;
102-
103- const fIRVisionImage = FIRVisionImage . alloc ( ) . initWithImage ( image ) ;
104- fIRVisionImage . metadata = fIRVisionImageMetadata ;
105- return fIRVisionImage ;
107+ return FIRVisionImage . alloc ( ) . initWithImage ( image ) ;
106108}
0 commit comments