@@ -53,7 +53,7 @@ private void configureHostedModelSource() {
5353 private void configureLocalModelSource () {
5454 // [START mlkit_local_model_source]
5555 FirebaseLocalModelSource localSource =
56- new FirebaseLocalModelSource .Builder ("my_local_model" ) // Assign a name for this model
56+ new FirebaseLocalModelSource .Builder ("my_local_model" ) // Assign a name to this model
5757 .setAssetFilePath ("my_model.tflite" )
5858 .build ();
5959 FirebaseModelManager .getInstance ().registerLocalModelSource (localSource );
@@ -78,7 +78,7 @@ private FirebaseModelInputOutputOptions createInputOutputOptions() throws Fireba
7878 FirebaseModelInputOutputOptions inputOutputOptions =
7979 new FirebaseModelInputOutputOptions .Builder ()
8080 .setInputFormat (0 , FirebaseModelDataType .FLOAT32 , new int []{1 , 224 , 224 , 3 })
81- .setOutputFormat (0 , FirebaseModelDataType .FLOAT32 , new int []{1 , 1000 })
81+ .setOutputFormat (0 , FirebaseModelDataType .FLOAT32 , new int []{1 , 5 })
8282 .build ();
8383 // [END mlkit_create_io_options]
8484
@@ -88,18 +88,19 @@ private FirebaseModelInputOutputOptions createInputOutputOptions() throws Fireba
8888 private float [][][][] bitmapToInputArray () {
8989 // [START mlkit_bitmap_input]
9090 Bitmap bitmap = getYourInputImage ();
91+ bitmap = Bitmap .createScaledBitmap (bitmap , 224 , 224 , true );
9192
9293 int batchNum = 0 ;
9394 float [][][][] input = new float [1 ][224 ][224 ][3 ];
9495 for (int x = 0 ; x < 224 ; x ++) {
9596 for (int y = 0 ; y < 224 ; y ++) {
9697 int pixel = bitmap .getPixel (x , y );
97- // Normalize channel values to [0 .0, 1.0]. This requirement varies by
98+ // Normalize channel values to [-1 .0, 1.0]. This requirement varies by
9899 // model. For example, some models might require values to be normalized
99- // to the range [-1 .0, 1.0] instead.
100- input [batchNum ][x ][y ][0 ] = Color .red (pixel ) / 255 .0f ;
101- input [batchNum ][x ][y ][1 ] = Color .green (pixel ) / 255 .0f ;
102- input [batchNum ][x ][y ][2 ] = Color .blue (pixel ) / 255 .0f ;
100+ // to the range [0 .0, 1.0] instead.
101+ input [batchNum ][x ][y ][0 ] = ( Color .red (pixel ) - 127 ) / 128 .0f ;
102+ input [batchNum ][x ][y ][1 ] = ( Color .green (pixel ) - 127 ) / 128 .0f ;
103+ input [batchNum ][x ][y ][2 ] = ( Color .blue (pixel ) - 127 ) / 128 .0f ;
103104 }
104105 }
105106 // [END mlkit_bitmap_input]
0 commit comments