Skip to content

Commit f680777

Browse files
authored
Clean up hardcoded logic and read from Assets folder directly (#34)
1 parent 1aec012 commit f680777

File tree

1 file changed

+29
-6
lines changed
  • dl3/android/DeepLabV3Demo/app/src/main/java/org/pytorch/executorchexamples/dl3

1 file changed

+29
-6
lines changed

dl3/android/DeepLabV3Demo/app/src/main/java/org/pytorch/executorchexamples/dl3/MainActivity.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.widget.ImageView;
2121
import android.widget.ProgressBar;
2222
import java.io.IOException;
23+
import java.util.ArrayList;
2324
import java.util.Objects;
2425
import org.pytorch.executorch.EValue;
2526
import org.pytorch.executorch.Module;
@@ -33,6 +34,9 @@ public class MainActivity extends Activity implements Runnable {
3334
private Module mModule = null;
3435
private String mImagename = "corgi.jpeg";
3536

37+
private String[] mImageFiles;
38+
private int mCurrentImageIndex = 0;
39+
3640
// see http://host.robots.ox.ac.uk:8080/pascal/VOC/voc2007/segexamples/index.html for the list of
3741
// classes with indexes
3842
private static final int CLASSNUM = 21;
@@ -51,10 +55,29 @@ private void populateImage() {
5155
}
5256
}
5357

58+
private void populateImagePathFromAssets() {
59+
try {
60+
String[] allFiles = getAssets().list("");
61+
ArrayList<String> imageList = new ArrayList<>();
62+
for (String file : allFiles) {
63+
if (file.endsWith(".jpg") || file.endsWith(".jpeg") || file.endsWith(".png")) {
64+
imageList.add(file);
65+
}
66+
}
67+
mImageFiles = imageList.toArray(new String[0]);
68+
mCurrentImageIndex = 0;
69+
mImagename = mImageFiles.length > 0 ? mImageFiles[0] : null;
70+
} catch (IOException e) {
71+
Log.e("ImageSegmentation", "Error listing assets", e);
72+
finish();
73+
}
74+
}
75+
5476
@Override
5577
protected void onCreate(Bundle savedInstanceState) {
5678
super.onCreate(savedInstanceState);
5779
setContentView(R.layout.activity_main);
80+
populateImagePathFromAssets();
5881

5982
try {
6083
mBitmap = BitmapFactory.decodeStream(getAssets().open(mImagename), null, null);
@@ -73,13 +96,13 @@ protected void onCreate(Bundle savedInstanceState) {
7396
buttonNext.setOnClickListener(
7497
new View.OnClickListener() {
7598
public void onClick(View v) {
76-
if (Objects.equals(mImagename, "corgi.jpeg")) {
77-
mImagename = "dog.jpg";
78-
} else if (Objects.equals(mImagename, "dog.jpg")) {
79-
mImagename = "deeplab.jpg";
80-
} else {
81-
mImagename = "corgi.jpeg";
99+
if (mImageFiles == null || mImageFiles.length == 0) {
100+
// No images available
101+
return;
82102
}
103+
// Move to the next image, wrap around if at the end
104+
mCurrentImageIndex = (mCurrentImageIndex + 1) % mImageFiles.length;
105+
mImagename = mImageFiles[mCurrentImageIndex];
83106
populateImage();
84107
}
85108
});

0 commit comments

Comments
 (0)