19
19
import android .content .res .AssetManager ;
20
20
import android .graphics .Bitmap ;
21
21
import android .graphics .BitmapFactory ;
22
+ import android .support .annotation .NonNull ;
22
23
import android .support .v7 .app .AppCompatActivity ;
23
24
import android .os .Bundle ;
24
25
import android .util .Log ;
32
33
import android .widget .Toast ;
33
34
34
35
import com .google .android .gms .tasks .Continuation ;
36
+ import com .google .android .gms .tasks .OnFailureListener ;
35
37
import com .google .android .gms .tasks .Task ;
36
38
import com .google .firebase .ml .common .FirebaseMLException ;
37
39
import com .google .firebase .ml .custom .FirebaseModelDataType ;
42
44
import com .google .firebase .ml .custom .FirebaseModelOptions ;
43
45
import com .google .firebase .ml .custom .FirebaseModelOutputs ;
44
46
import com .google .firebase .ml .custom .model .FirebaseCloudModelSource ;
47
+ import com .google .firebase .ml .custom .model .FirebaseLocalModelSource ;
45
48
import com .google .firebase .ml .custom .model .FirebaseModelDownloadConditions ;
46
49
47
50
import java .io .BufferedReader ;
@@ -68,13 +71,13 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
68
71
private Integer mImageMaxWidth ;
69
72
// Max height (portrait mode)
70
73
private Integer mImageMaxHeight ;
71
- private boolean mIsLandScape ;
72
74
private final String [] mFilePaths =
73
75
new String []{"mountain.jpg" , "tennis.jpg" };
74
76
/**
75
77
* Name of the model file hosted with Firebase.
76
78
*/
77
79
private static final String HOSTED_MODEL_NAME = "mobilenet_v1_224_quant" ;
80
+ private static final String LOCAL_MODEL_ASSET = "mobilenet_v1.0_224_quant.tflite" ;
78
81
/**
79
82
* Name of the label file stored in Assets.
80
83
*/
@@ -144,7 +147,7 @@ public void onClick(View v) {
144
147
});
145
148
146
149
int [] inputDims = {DIM_BATCH_SIZE , DIM_IMG_SIZE_X , DIM_IMG_SIZE_Y , DIM_PIXEL_SIZE };
147
- int [] outputDims = {1 , mLabelList .size ()};
150
+ int [] outputDims = {DIM_BATCH_SIZE , mLabelList .size ()};
148
151
try {
149
152
mDataOptions =
150
153
new FirebaseModelInputOutputOptions .Builder ()
@@ -155,6 +158,9 @@ public void onClick(View v) {
155
158
.Builder ()
156
159
.requireWifi ()
157
160
.build ();
161
+ FirebaseLocalModelSource localModelSource =
162
+ new FirebaseLocalModelSource .Builder ("asset" )
163
+ .setAssetFilePath (LOCAL_MODEL_ASSET ).build ();
158
164
159
165
FirebaseCloudModelSource cloudSource = new FirebaseCloudModelSource .Builder
160
166
(HOSTED_MODEL_NAME )
@@ -165,10 +171,12 @@ public void onClick(View v) {
165
171
// for updates
166
172
.build ();
167
173
FirebaseModelManager manager = FirebaseModelManager .getInstance ();
174
+ manager .registerLocalModelSource (localModelSource );
168
175
manager .registerCloudModelSource (cloudSource );
169
176
FirebaseModelOptions modelOptions =
170
177
new FirebaseModelOptions .Builder ()
171
178
.setCloudModelName (HOSTED_MODEL_NAME )
179
+ .setLocalModelName ("asset" )
172
180
.build ();
173
181
mInterpreter = FirebaseModelInterpreter .getInstance (modelOptions );
174
182
} catch (FirebaseMLException e ) {
@@ -191,6 +199,13 @@ private void runModelInference() {
191
199
// Here's where the magic happens!!
192
200
mInterpreter
193
201
.run (inputs , mDataOptions )
202
+ .addOnFailureListener (new OnFailureListener () {
203
+ @ Override
204
+ public void onFailure (@ NonNull Exception e ) {
205
+ e .printStackTrace ();
206
+ showToast ("Error running model inference" );
207
+ }
208
+ })
194
209
.continueWith (
195
210
new Continuation <FirebaseModelOutputs , List <String >>() {
196
211
@ Override
@@ -337,15 +352,9 @@ public static Bitmap getBitmapFromAsset(Context context, String filePath) {
337
352
private Integer getImageMaxWidth () {
338
353
if (mImageMaxWidth == null ) {
339
354
// Calculate the max width in portrait mode. This is done lazily since we need to
340
- // wait for
341
- // a UI layout pass to get the right values. So delay it to first time image
355
+ // wait for a UI layout pass to get the right values. So delay it to first time image
342
356
// rendering time.
343
- if (mIsLandScape ) {
344
- mImageMaxWidth =
345
- mImageView .getHeight ();
346
- } else {
347
- mImageMaxWidth = mImageView .getWidth ();
348
- }
357
+ mImageMaxWidth = mImageView .getWidth ();
349
358
}
350
359
351
360
return mImageMaxWidth ;
@@ -356,15 +365,10 @@ private Integer getImageMaxWidth() {
356
365
private Integer getImageMaxHeight () {
357
366
if (mImageMaxHeight == null ) {
358
367
// Calculate the max width in portrait mode. This is done lazily since we need to
359
- // wait for
360
- // a UI layout pass to get the right values. So delay it to first time image
368
+ // wait for a UI layout pass to get the right values. So delay it to first time image
361
369
// rendering time.
362
- if (mIsLandScape ) {
363
- mImageMaxHeight = mImageView .getWidth ();
364
- } else {
365
- mImageMaxHeight =
366
- mImageView .getHeight ();
367
- }
370
+ mImageMaxHeight =
371
+ mImageView .getHeight ();
368
372
}
369
373
370
374
return mImageMaxHeight ;
@@ -376,8 +380,8 @@ private Pair getTargetedWidthHeight() {
376
380
int targetHeight ;
377
381
int maxWidthForPortraitMode = getImageMaxWidth ();
378
382
int maxHeightForPortraitMode = getImageMaxHeight ();
379
- targetWidth = mIsLandScape ? maxHeightForPortraitMode : maxWidthForPortraitMode ;
380
- targetHeight = mIsLandScape ? maxWidthForPortraitMode : maxHeightForPortraitMode ;
383
+ targetWidth = maxWidthForPortraitMode ;
384
+ targetHeight = maxHeightForPortraitMode ;
381
385
return new Pair <>(targetWidth , targetHeight );
382
386
}
383
387
}
0 commit comments