Skip to content

Commit e1f610e

Browse files
committed
improve ui
1 parent 00828c2 commit e1f610e

3 files changed

Lines changed: 63 additions & 34 deletions

File tree

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.Manifest;
1212
import android.app.Activity;
1313
import android.content.Context;
14+
import android.content.Intent;
1415
import android.content.pm.PackageManager;
1516
import android.graphics.Bitmap;
1617
import android.graphics.BitmapFactory;
@@ -67,6 +68,7 @@ public class MainActivity extends Activity implements Runnable {
6768
private int mCurrentImageIndex = 0;
6869

6970
private static final int REQUEST_READ_EXTERNAL_STORAGE = 1001;
71+
private static final int REQUEST_PICK_IMAGE = 1002;
7072
private static final String LOCAL_IMAGE_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/";
7173

7274
// see http://host.robots.ox.ac.uk:8080/pascal/VOC/voc2007/segexamples/index.html for the list of
@@ -147,6 +149,34 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
147149
populateImagePathFromAssets();
148150
}
149151

152+
// Handle image picker result
153+
@Override
154+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
155+
super.onActivityResult(requestCode, resultCode, data);
156+
if (requestCode == REQUEST_PICK_IMAGE && resultCode == RESULT_OK && data != null) {
157+
try {
158+
android.net.Uri imageUri = data.getData();
159+
if (imageUri != null) {
160+
InputStream inputStream = getContentResolver().openInputStream(imageUri);
161+
Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream);
162+
if (selectedBitmap != null) {
163+
// Resize to 224x224 for the model
164+
mBitmap = Bitmap.createScaledBitmap(selectedBitmap, 224, 224, true);
165+
mImageView.setImageBitmap(mBitmap);
166+
mImagename = null; // Clear file reference since we're using picked image
167+
showUIMessage(this, "Image loaded - tap Run to segment");
168+
}
169+
if (inputStream != null) {
170+
inputStream.close();
171+
}
172+
}
173+
} catch (Exception e) {
174+
Log.e("MainActivity", "Error loading picked image", e);
175+
showUIMessage(this, "Failed to load image");
176+
}
177+
}
178+
}
179+
150180
private void loadImagesFromLocal() {
151181
// Load images from /data/local & /sdcard/Pictures
152182
boolean hasAnyLocalFiles = populateImagePathFromLocal();
@@ -284,15 +314,13 @@ public void onClick(View v) {
284314
resetImage.setOnClickListener(
285315
v -> showImage());
286316

287-
// Refresh Button for External Storage
288-
final Button loadAndRefreshButton = findViewById(R.id.loadAndRefreshButton);
289-
loadAndRefreshButton.setOnClickListener(
317+
// Pick Image Button - opens gallery
318+
final Button pickImageButton = findViewById(R.id.loadAndRefreshButton);
319+
pickImageButton.setOnClickListener(
290320
v -> {
291-
mImageFiles.clear();
292-
checkAndRequestStoragePermission();
293-
294-
populateImagePathFromAssets();
295-
showImage();
321+
Intent intent = new Intent(Intent.ACTION_PICK);
322+
intent.setType("image/*");
323+
startActivityForResult(intent, REQUEST_PICK_IMAGE);
296324
}
297325
);
298326
}

dl3/android/DeepLabV3Demo/app/src/main/res/layout/activity_main.xml

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
app:layout_constraintStart_toStartOf="parent"
5151
app:layout_constraintTop_toBottomOf="@+id/progressBar" />
5252

53-
<!-- Row 1: Next and Reset buttons -->
53+
<!-- Row 1: Next | Pick Image -->
5454
<Button
5555
android:id="@+id/nextButton"
5656
android:layout_width="0dp"
@@ -61,63 +61,64 @@
6161
android:textAllCaps="false"
6262
android:textColor="#424242"
6363
android:textSize="13sp"
64-
app:layout_constraintBottom_toTopOf="@+id/loadAndRefreshButton"
65-
app:layout_constraintEnd_toStartOf="@+id/resetImage"
64+
app:layout_constraintBottom_toTopOf="@+id/xnnpackButton"
65+
app:layout_constraintEnd_toStartOf="@+id/loadAndRefreshButton"
6666
app:layout_constraintHeight_percent="0.06"
6767
app:layout_constraintHorizontal_chainStyle="spread"
6868
app:layout_constraintStart_toStartOf="parent"
6969
app:layout_constraintTop_toBottomOf="@+id/inferenceTimeText" />
7070

7171
<Button
72-
android:id="@+id/resetImage"
72+
android:id="@+id/loadAndRefreshButton"
7373
android:layout_width="0dp"
7474
android:layout_height="0dp"
7575
android:layout_marginStart="4dp"
7676
android:background="@drawable/btn_secondary"
77-
android:text="@string/reset"
77+
android:text="@string/load_and_refresh"
7878
android:textAllCaps="false"
7979
android:textColor="#424242"
8080
android:textSize="13sp"
81-
app:layout_constraintBottom_toTopOf="@+id/loadAndRefreshButton"
81+
app:layout_constraintBottom_toTopOf="@+id/xnnpackButton"
8282
app:layout_constraintEnd_toEndOf="parent"
8383
app:layout_constraintHeight_percent="0.06"
8484
app:layout_constraintStart_toEndOf="@+id/nextButton"
8585
app:layout_constraintTop_toBottomOf="@+id/inferenceTimeText" />
8686

87-
<!-- Row 2: Load and Refresh button -->
87+
<!-- Row 2: Run | Reset -->
8888
<Button
89-
android:id="@+id/loadAndRefreshButton"
89+
android:id="@+id/xnnpackButton"
9090
android:layout_width="0dp"
9191
android:layout_height="0dp"
9292
android:layout_marginTop="4dp"
93-
android:background="@drawable/btn_secondary"
94-
android:text="@string/load_and_refresh"
93+
android:layout_marginEnd="4dp"
94+
android:background="@drawable/btn_success"
95+
android:text="@string/run_xnnpack"
9596
android:textAllCaps="false"
96-
android:textColor="#424242"
97-
android:textSize="13sp"
98-
app:layout_constraintBottom_toTopOf="@+id/xnnpackButton"
99-
app:layout_constraintEnd_toEndOf="parent"
100-
app:layout_constraintHeight_percent="0.06"
97+
android:textColor="#FFFFFF"
98+
android:textSize="15sp"
99+
android:textStyle="bold"
100+
app:layout_constraintBottom_toTopOf="@+id/modelStatusText"
101+
app:layout_constraintEnd_toStartOf="@+id/resetImage"
102+
app:layout_constraintHeight_percent="0.07"
101103
app:layout_constraintStart_toStartOf="parent"
102104
app:layout_constraintTop_toBottomOf="@+id/nextButton" />
103105

104-
<!-- Row 3: Run button (primary action) -->
105106
<Button
106-
android:id="@+id/xnnpackButton"
107+
android:id="@+id/resetImage"
107108
android:layout_width="0dp"
108109
android:layout_height="0dp"
109-
android:layout_marginTop="8dp"
110-
android:background="@drawable/btn_success"
111-
android:text="@string/run_xnnpack"
110+
android:layout_marginTop="4dp"
111+
android:layout_marginStart="4dp"
112+
android:background="@drawable/btn_secondary"
113+
android:text="@string/reset"
112114
android:textAllCaps="false"
113-
android:textColor="#FFFFFF"
114-
android:textSize="15sp"
115-
android:textStyle="bold"
115+
android:textColor="#424242"
116+
android:textSize="13sp"
116117
app:layout_constraintBottom_toTopOf="@+id/modelStatusText"
117118
app:layout_constraintEnd_toEndOf="parent"
118119
app:layout_constraintHeight_percent="0.07"
119-
app:layout_constraintStart_toStartOf="parent"
120-
app:layout_constraintTop_toBottomOf="@+id/loadAndRefreshButton" />
120+
app:layout_constraintStart_toEndOf="@+id/xnnpackButton"
121+
app:layout_constraintTop_toBottomOf="@+id/nextButton" />
121122

122123
<!-- Model Status Text -->
123124
<TextView

dl3/android/DeepLabV3Demo/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<string name="segmentation_demo">Segmentation Demo</string>
1010
<string name="run_xnnpack">Run</string>
1111
<string name="reset">Reset</string>
12-
<string name="load_and_refresh">Load And Refresh</string>
12+
<string name="load_and_refresh">Pick Image</string>
1313
<string name="run">Run</string>
1414
<string name="inference_time_placeholder">Inference: -- ms</string>
1515
<string name="download_model">Download Model</string>

0 commit comments

Comments
 (0)