10
10
11
11
import android .app .Activity ;
12
12
import android .content .Context ;
13
- import android .content .Intent ;
14
13
import android .graphics .Bitmap ;
15
14
import android .graphics .BitmapFactory ;
16
15
import android .os .Bundle ;
22
21
import android .widget .Button ;
23
22
import android .widget .ImageView ;
24
23
import android .widget .ProgressBar ;
25
- import android .widget .Toast ;
26
24
import com .example .executorchdemo .executor .EValue ;
27
25
import com .example .executorchdemo .executor .Module ;
28
26
import com .example .executorchdemo .executor .Tensor ;
36
34
37
35
public class MainActivity extends Activity implements Runnable {
38
36
private ImageView mImageView ;
39
- private Button mButtonSegment ;
37
+ private Button mButtonXnnpack ;
38
+ private Button mButtonHtp ;
40
39
private ProgressBar mProgressBar ;
41
40
private Bitmap mBitmap = null ;
42
41
private Module mModule = null ;
43
- private String mBackend = "xnnpack" ;
44
42
private String mImagename = "corgi.jpeg" ;
45
43
46
44
// see http://host.robots.ox.ac.uk:8080/pascal/VOC/voc2007/segexamples/index.html for the list of
@@ -50,11 +48,6 @@ public class MainActivity extends Activity implements Runnable {
50
48
private static final int PERSON = 15 ;
51
49
private static final int SHEEP = 17 ;
52
50
53
- private void openClassificationActivity () {
54
- Intent intent = new Intent (this , ClassificationActivity .class );
55
- startActivity (intent );
56
- }
57
-
58
51
public static String assetFilePath (Context context , String assetName ) throws IOException {
59
52
File file = new File (context .getFilesDir (), assetName );
60
53
if (file .exists () && file .length () > 0 ) {
@@ -74,6 +67,17 @@ public static String assetFilePath(Context context, String assetName) throws IOE
74
67
}
75
68
}
76
69
70
+ private void populateImage () {
71
+ try {
72
+ mBitmap = BitmapFactory .decodeStream (getAssets ().open (mImagename ));
73
+ mBitmap = Bitmap .createScaledBitmap (mBitmap , 224 , 224 , true );
74
+ mImageView .setImageBitmap (mBitmap );
75
+ } catch (IOException e ) {
76
+ Log .e ("ImageSegmentation" , "Error reading assets" , e );
77
+ finish ();
78
+ }
79
+ }
80
+
77
81
@ Override
78
82
protected void onCreate (Bundle savedInstanceState ) {
79
83
super .onCreate (savedInstanceState );
@@ -94,11 +98,20 @@ protected void onCreate(Bundle savedInstanceState) {
94
98
finish ();
95
99
}
96
100
101
+ try {
102
+ mModule =
103
+ Module .load (MainActivity .assetFilePath (getApplicationContext (), "dl3_xnnpack_fp32.pte" ));
104
+
105
+ } catch (IOException e ) {
106
+ Log .e ("ImageSegmentation" , "Error reading assets" , e );
107
+ finish ();
108
+ }
109
+
97
110
mImageView = findViewById (R .id .imageView );
98
111
mImageView .setImageBitmap (mBitmap );
99
112
100
- final Button buttonRestart = findViewById (R .id .restartButton );
101
- buttonRestart .setOnClickListener (
113
+ final Button buttonNext = findViewById (R .id .nextButton );
114
+ buttonNext .setOnClickListener (
102
115
new View .OnClickListener () {
103
116
public void onClick (View v ) {
104
117
if (Objects .equals (mImagename , "corgi.jpeg" )) {
@@ -108,79 +121,62 @@ public void onClick(View v) {
108
121
} else {
109
122
mImagename = "corgi.jpeg" ;
110
123
}
111
- try {
112
- mBitmap = BitmapFactory .decodeStream (getAssets ().open (mImagename ));
113
- mBitmap = Bitmap .createScaledBitmap (mBitmap , 224 , 224 , true );
114
- mImageView .setImageBitmap (mBitmap );
115
- } catch (IOException e ) {
116
- Log .e ("ImageSegmentation" , "Error reading assets" , e );
117
- finish ();
118
- }
124
+ populateImage ();
119
125
}
120
126
});
121
127
122
- final Button buttonSwitchBackend = findViewById (R .id .switchBackendButton );
123
- buttonSwitchBackend .setText ("Use Qualcomm backend" );
124
- buttonSwitchBackend .setOnClickListener (
128
+ mButtonXnnpack = findViewById (R .id .xnnpackButton );
129
+ mButtonHtp = findViewById (R .id .htpButton );
130
+ mProgressBar = (ProgressBar ) findViewById (R .id .progressBar );
131
+ mButtonXnnpack .setOnClickListener (
125
132
new View .OnClickListener () {
126
133
public void onClick (View v ) {
127
134
try {
128
- if (Objects .equals (mBackend , "xnnpack" )) {
129
- mBackend = "qnn" ;
130
- mModule .destroy ();
131
- mModule =
132
- Module .load (
133
- MainActivity .assetFilePath (getApplicationContext (), "dlv3_qnn.pte" ));
134
- buttonSwitchBackend .setText ("Use XNNPACK backend" );
135
- } else {
136
- mBackend = "xnnpack" ;
137
- mModule .destroy ();
138
- mModule =
139
- Module .load (
140
- MainActivity .assetFilePath (
141
- getApplicationContext (), "dl3_xnnpack_fp32.pte" ));
142
- buttonSwitchBackend .setText ("Use Qualcomm backend" );
143
- }
135
+ mModule .destroy ();
136
+ mModule =
137
+ Module .load (
138
+ MainActivity .assetFilePath (getApplicationContext (), "dl3_xnnpack_fp32.pte" ));
144
139
} catch (IOException e ) {
145
140
Log .e ("ImageSegmentation" , "Error reading assets" , e );
146
141
finish ();
147
142
}
148
- Toast toast =
149
- Toast .makeText (getApplicationContext (), "Using: " + mBackend , Toast .LENGTH_SHORT );
150
- toast .setMargin (50 , 50 );
151
- toast .show ();
152
- }
153
- });
154
143
155
- final Button classificationDemoButton = findViewById (R .id .classificationDemoButton );
156
- classificationDemoButton .setOnClickListener (
157
- new View .OnClickListener () {
158
- public void onClick (View v ) {
159
- openClassificationActivity ();
144
+ mButtonXnnpack .setEnabled (false );
145
+ mProgressBar .setVisibility (ProgressBar .VISIBLE );
146
+ mButtonXnnpack .setText (getString (R .string .run_model ));
147
+
148
+ Thread thread = new Thread (MainActivity .this );
149
+ thread .start ();
160
150
}
161
151
});
162
152
163
- mButtonSegment = findViewById (R .id .segmentButton );
164
- mProgressBar = (ProgressBar ) findViewById (R .id .progressBar );
165
- mButtonSegment .setOnClickListener (
153
+ mButtonHtp .setOnClickListener (
166
154
new View .OnClickListener () {
167
155
public void onClick (View v ) {
168
- mButtonSegment .setEnabled (false );
156
+ try {
157
+ mModule .destroy ();
158
+ mModule =
159
+ Module .load (MainActivity .assetFilePath (getApplicationContext (), "dlv3_qnn.pte" ));
160
+ } catch (IOException e ) {
161
+ Log .e ("ImageSegmentation" , "Error reading assets" , e );
162
+ finish ();
163
+ }
164
+ mButtonHtp .setEnabled (false );
169
165
mProgressBar .setVisibility (ProgressBar .VISIBLE );
170
- mButtonSegment .setText (getString (R .string .run_model ));
166
+ mButtonHtp .setText (getString (R .string .run_model ));
171
167
172
168
Thread thread = new Thread (MainActivity .this );
173
169
thread .start ();
174
170
}
175
171
});
176
172
177
- try {
178
- mModule =
179
- Module . load ( MainActivity . assetFilePath ( getApplicationContext (), "dl3_xnnpack_fp32.pte" ));
180
- } catch ( IOException e ) {
181
- Log . e ( "ImageSegmentation" , "Error reading assets" , e );
182
- finish ();
183
- }
173
+ final Button resetImage = findViewById ( R . id . resetImage );
174
+ resetImage . setOnClickListener (
175
+ new View . OnClickListener () {
176
+ public void onClick ( View v ) {
177
+ populateImage ( );
178
+ }
179
+ });
184
180
}
185
181
186
182
@ Override
@@ -240,17 +236,11 @@ public void run() {
240
236
@ Override
241
237
public void run () {
242
238
mImageView .setImageBitmap (transferredBitmap );
243
- mButtonSegment .setEnabled (true );
244
- mButtonSegment .setText ("segment" );
239
+ mButtonXnnpack .setEnabled (true );
240
+ mButtonXnnpack .setText (R .string .run_xnnpack );
241
+ mButtonHtp .setEnabled (true );
242
+ mButtonHtp .setText (R .string .run_htp );
245
243
mProgressBar .setVisibility (ProgressBar .INVISIBLE );
246
-
247
- Toast toast =
248
- Toast .makeText (
249
- getApplicationContext (),
250
- "Inference time (ms): " + inferenceTime ,
251
- Toast .LENGTH_SHORT );
252
- toast .setMargin (50 , 50 );
253
- toast .show ();
254
244
}
255
245
});
256
246
}
0 commit comments