11import { ImageSource } from "tns-core-modules/image-source" ;
22import { MLKitVisionOptions , } from "../" ;
3+ import { MLKitScanBarcodesResultBounds } from "../barcodescanning" ;
34import { MLKitObjectDetectionOptions , MLKitObjectDetectionResult , MLKitObjectDetectionResultItem } from "./" ;
45import { MLKitObjectDetection as MLKitObjectDetectionBase , ObjectDetectionCategory } from "./objectdetection-common" ;
56
@@ -22,9 +23,11 @@ export class MLKitObjectDetection extends MLKitObjectDetectionBase {
2223 objects : [ ]
2324 } ;
2425
26+ const image : android . graphics . Bitmap = this . lastVisionImage && this . lastVisionImage . getBitmap ? this . lastVisionImage . getBitmap ( ) : null ;
27+
2528 // see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
2629 for ( let i = 0 ; i < objects . size ( ) ; i ++ ) {
27- result . objects . push ( getMLKitObjectDetectionResultItem ( objects . get ( i ) ) ) ;
30+ result . objects . push ( getMLKitObjectDetectionResultItem ( objects . get ( i ) , image ) ) ;
2831 }
2932
3033 this . notify ( {
@@ -57,6 +60,9 @@ export function detectObjects(options: MLKitObjectDetectionOptions): Promise<MLK
5760 try {
5861 const firebaseObjectDetector = getDetector ( options . classify , options . multiple ) ;
5962
63+ const image : android . graphics . Bitmap = options . image instanceof ImageSource ? options . image . android : options . image . imageSource . android ;
64+ const firImage = com . google . firebase . ml . vision . common . FirebaseVisionImage . fromBitmap ( image ) ;
65+
6066 const onSuccessListener = new com . google . android . gms . tasks . OnSuccessListener ( {
6167 onSuccess : objects => {
6268 const result = < MLKitObjectDetectionResult > {
@@ -65,7 +71,7 @@ export function detectObjects(options: MLKitObjectDetectionOptions): Promise<MLK
6571
6672 if ( objects ) {
6773 for ( let i = 0 ; i < objects . size ( ) ; i ++ ) {
68- result . objects . push ( getMLKitObjectDetectionResultItem ( objects . get ( i ) ) ) ;
74+ result . objects . push ( getMLKitObjectDetectionResultItem ( objects . get ( i ) , image ) ) ;
6975 }
7076 }
7177
@@ -79,7 +85,7 @@ export function detectObjects(options: MLKitObjectDetectionOptions): Promise<MLK
7985 } ) ;
8086
8187 firebaseObjectDetector
82- . processImage ( getImage ( options ) )
88+ . processImage ( firImage )
8389 . addOnSuccessListener ( onSuccessListener )
8490 . addOnFailureListener ( onFailureListener ) ;
8591
@@ -90,18 +96,33 @@ export function detectObjects(options: MLKitObjectDetectionOptions): Promise<MLK
9096 } ) ;
9197}
9298
93- function getMLKitObjectDetectionResultItem ( obj : com . google . firebase . ml . vision . objects . FirebaseVisionObject ) : MLKitObjectDetectionResultItem {
99+ function getMLKitObjectDetectionResultItem ( obj : com . google . firebase . ml . vision . objects . FirebaseVisionObject , image : android . graphics . Bitmap ) : MLKitObjectDetectionResultItem {
94100 return {
95101 id : obj . getTrackingId ( ) ? obj . getTrackingId ( ) . intValue ( ) : undefined ,
96102 confidence : obj . getClassificationConfidence ( ) ? obj . getClassificationConfidence ( ) . doubleValue ( ) : undefined ,
97103 category : ObjectDetectionCategory [ obj . getClassificationCategory ( ) ] ,
98- // TODO
99- image : undefined ,
100- bounds : undefined
104+ bounds : boundingBoxToBounds ( obj . getBoundingBox ( ) ) ,
105+ image : ! image ? null : {
106+ width : image . getWidth ( ) ,
107+ height : image . getHeight ( )
108+ }
101109 } ;
102110}
103111
104112function getImage ( options : MLKitVisionOptions ) : any /* com.google.firebase.ml.vision.common.FirebaseVisionImage */ {
105113 const image : android . graphics . Bitmap = options . image instanceof ImageSource ? options . image . android : options . image . imageSource . android ;
106114 return com . google . firebase . ml . vision . common . FirebaseVisionImage . fromBitmap ( image ) ;
107115}
116+
117+ function boundingBoxToBounds ( rect : any ) : MLKitScanBarcodesResultBounds {
118+ return {
119+ origin : {
120+ x : rect . left ,
121+ y : rect . top
122+ } ,
123+ size : {
124+ width : rect . width ( ) ,
125+ height : rect . height ( )
126+ }
127+ } ;
128+ }
0 commit comments