Skip to content

Commit

Permalink
Tweak automl remote model demo.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 350430296
Change-Id: I04fb4485e0a8939dc58a68e258a0fcbabe9decbb
  • Loading branch information
Google ML Kit authored and zhouyiself committed Jan 6, 2021
1 parent 2606a61 commit cefba78
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 72 deletions.
12 changes: 6 additions & 6 deletions android/automl/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,27 @@ dependencies {
// -------------------------------------------------------

implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.guava:guava:17.0'
implementation 'com.google.guava:guava:27.1-android'

// For how to setup gradle dependencies in Android X, see:
// https://developer.android.com/training/testing/set-up-project#gradle-dependencies
// Core library
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test:core:1.3.0'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'

// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-livedata:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

// CameraX
implementation "androidx.camera:camera-camera2:1.0.0-SNAPSHOT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.camera.core.ExperimentalGetImage;
import androidx.camera.core.ImageProxy;
import androidx.exifinterface.media.ExifInterface;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -72,6 +73,7 @@ public static Bitmap getBitmap(ByteBuffer data, FrameMetadata metadata) {
/** Converts a YUV_420_888 image from CameraX API to a bitmap. */
@RequiresApi(VERSION_CODES.KITKAT)
@Nullable
@ExperimentalGetImage
public static Bitmap getBitmap(ImageProxy image) {
FrameMetadata frameMetadata = new FrameMetadata.Builder()
.setWidth(image.getWidth())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.android.gms.common.images.Size;
import com.google.mlkit.vision.automl.demo.preference.PreferenceUtils;
import java.io.IOException;
import java.lang.Thread.State;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.IdentityHashMap;
Expand Down Expand Up @@ -136,7 +135,6 @@ public CameraSource(Activity activity, GraphicOverlay overlay) {
public void release() {
synchronized (processorLock) {
stop();
processingRunnable.release();
cleanScreen();

if (frameProcessor != null) {
Expand Down Expand Up @@ -611,15 +609,6 @@ private class FrameProcessingRunnable implements Runnable {

FrameProcessingRunnable() {}

/**
* Releases the underlying receiver. This is only safe to do after the associated thread has
* completed, which is managed in camera source's release method above.
*/
@SuppressLint("Assert")
void release() {
assert (processingThread.getState() == State.TERMINATED);
}

/** Marks the runnable as active/not active. Signals any blocked threads to continue. */
void setActive(boolean active) {
synchronized (lock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private void bindPreviewUseCase() {
}

Preview.Builder builder = new Preview.Builder();
Size targetResolution = PreferenceUtils.getCameraXTargetResolution(this);
Size targetResolution = PreferenceUtils.getCameraXTargetResolution(this, lensFacing);
if (targetResolution != null) {
builder.setTargetResolution(targetResolution);
}
Expand Down Expand Up @@ -322,7 +322,7 @@ private void bindAnalysisUseCase() {
}

ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
Size targetResolution = PreferenceUtils.getCameraXTargetResolution(this);
Size targetResolution = PreferenceUtils.getCameraXTargetResolution(this, lensFacing);
if (targetResolution != null) {
builder.setTargetResolution(targetResolution);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,104 @@

package com.google.mlkit.vision.automl.demo.preference;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Build.VERSION_CODES;
import android.preference.ListPreference;
import android.preference.PreferenceCategory;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.util.Size;
import androidx.annotation.StringRes;
import androidx.camera.core.CameraSelector;
import com.google.mlkit.vision.automl.demo.R;
import java.util.Arrays;
import java.util.List;

/** Configures CameraX live preview demo settings. */
@RequiresApi(VERSION_CODES.LOLLIPOP)
public class CameraXLivePreviewPreferenceFragment extends LivePreviewPreferenceFragment {

public CameraXLivePreviewPreferenceFragment() {
isCameraXSetting = true;
@Override
void setUpCameraPreferences() {
PreferenceCategory cameraPreference =
(PreferenceCategory) findPreference(getString(R.string.pref_category_key_camera));

cameraPreference.removePreference(
findPreference(getString(R.string.pref_key_rear_camera_preview_size)));
cameraPreference.removePreference(
findPreference(getString(R.string.pref_key_front_camera_preview_size)));
setUpCameraXTargetAnalysisSizePreference(
R.string.pref_key_camerax_rear_camera_target_resolution, CameraSelector.LENS_FACING_BACK);
setUpCameraXTargetAnalysisSizePreference(
R.string.pref_key_camerax_front_camera_target_resolution, CameraSelector.LENS_FACING_FRONT);
}

private void setUpCameraXTargetAnalysisSizePreference(
@StringRes int previewSizePrefKeyId, int lensFacing) {
ListPreference pref = (ListPreference) findPreference(getString(previewSizePrefKeyId));
CameraCharacteristics cameraCharacteristics =
getCameraCharacteristics(getActivity(), lensFacing);
String[] entries;
if (cameraCharacteristics != null) {
StreamConfigurationMap map =
cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] outputSizes = map.getOutputSizes(SurfaceTexture.class);
entries = new String[outputSizes.length];
for (int i = 0; i < outputSizes.length; i++) {
entries[i] = outputSizes[i].toString();
}
} else {
entries =
new String[] {
"2000x2000",
"1600x1600",
"1200x1200",
"1000x1000",
"800x800",
"600x600",
"400x400",
"200x200",
"100x100",
};
}
pref.setEntries(entries);
pref.setEntryValues(entries);
pref.setSummary(pref.getEntry() == null ? "Default" : pref.getEntry());
pref.setOnPreferenceChangeListener(
(preference, newValue) -> {
String newStringValue = (String) newValue;
pref.setSummary(newStringValue);
PreferenceUtils.saveString(getActivity(), previewSizePrefKeyId, newStringValue);
return true;
});
}

@Nullable
public static CameraCharacteristics getCameraCharacteristics(
Context context, Integer lensFacing) {
CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
try {
List<String> cameraList = Arrays.asList(cameraManager.getCameraIdList());
for (String availableCameraId : cameraList) {
CameraCharacteristics availableCameraCharacteristics =
cameraManager.getCameraCharacteristics(availableCameraId);
Integer availableLensFacing =
availableCameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
if (availableLensFacing == null) {
continue;
}
if (availableLensFacing.equals(lensFacing)) {
return availableCameraCharacteristics;
}
}
} catch (CameraAccessException e) {
// Accessing camera ID info got error
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
/** Configures live preview demo settings. */
public class LivePreviewPreferenceFragment extends PreferenceFragment {

protected boolean isCameraXSetting;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -43,19 +41,13 @@ public void onCreate(Bundle savedInstanceState) {
PreferenceUtils.setUpRemoteModelNamePreferences(this);
}

private void setUpCameraPreferences() {
void setUpCameraPreferences() {
PreferenceCategory cameraPreference =
(PreferenceCategory) findPreference(getString(R.string.pref_category_key_camera));

if (isCameraXSetting) {
cameraPreference.removePreference(
findPreference(getString(R.string.pref_key_rear_camera_preview_size)));
cameraPreference.removePreference(
findPreference(getString(R.string.pref_key_front_camera_preview_size)));
setUpCameraXTargetAnalysisSizePreference();
} else {
cameraPreference.removePreference(
findPreference(getString(R.string.pref_key_camerax_target_resolution)));
cameraPreference.removePreference(
findPreference(getString(R.string.pref_key_camerax_rear_camera_target_resolution)));
cameraPreference.removePreference(
findPreference(getString(R.string.pref_key_camerax_front_camera_target_resolution)));
setUpCameraPreviewSizePreference(
R.string.pref_key_rear_camera_preview_size,
R.string.pref_key_rear_camera_picture_size,
Expand All @@ -64,7 +56,6 @@ private void setUpCameraPreferences() {
R.string.pref_key_front_camera_preview_size,
R.string.pref_key_front_camera_picture_size,
CameraSource.CAMERA_FACING_FRONT);
}
}

private void setUpCameraPreviewSizePreference(
Expand Down Expand Up @@ -129,33 +120,4 @@ private void setUpCameraPreviewSizePreference(
}
}
}

private void setUpCameraXTargetAnalysisSizePreference() {
ListPreference pref =
(ListPreference) findPreference(getString(R.string.pref_key_camerax_target_resolution));
String[] entries = new String[] {
"2000x2000",
"1600x1600",
"1200x1200",
"1000x1000",
"800x800",
"600x600",
"400x400",
"200x200",
"100x100",
};
pref.setEntries(entries);
pref.setEntryValues(entries);
pref.setSummary(pref.getEntry() == null ? "Default" : pref.getEntry());
pref.setOnPreferenceChangeListener(
(preference, newValue) -> {
String newStringValue = (String) newValue;
pref.setSummary(newStringValue);
PreferenceUtils.saveString(
getActivity(),
R.string.pref_key_camerax_target_resolution,
newStringValue);
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.camera.core.CameraSelector;
import com.google.android.gms.common.images.Size;
import com.google.common.base.Preconditions;
import com.google.mlkit.vision.automl.demo.CameraSource;
Expand Down Expand Up @@ -69,8 +70,14 @@ public static SizePair getCameraPreviewSizePair(Context context, int cameraId) {

@RequiresApi(VERSION_CODES.LOLLIPOP)
@Nullable
public static android.util.Size getCameraXTargetResolution(Context context) {
String prefKey = context.getString(R.string.pref_key_camerax_target_resolution);
public static android.util.Size getCameraXTargetResolution(Context context, int lensfacing) {
Preconditions.checkArgument(
lensfacing == CameraSelector.LENS_FACING_BACK
|| lensfacing == CameraSelector.LENS_FACING_FRONT);
String prefKey =
lensfacing == CameraSelector.LENS_FACING_BACK
? context.getString(R.string.pref_key_camerax_rear_camera_target_resolution)
: context.getString(R.string.pref_key_camerax_front_camera_target_resolution);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
try {
return android.util.Size.parseSize(sharedPreferences.getString(prefKey, null));
Expand Down
6 changes: 4 additions & 2 deletions android/automl/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
<string name="pref_key_rear_camera_picture_size" translatable="false">rcpts</string>
<string name="pref_key_front_camera_preview_size" translatable="false">fcpvs</string>
<string name="pref_key_front_camera_picture_size" translatable="false">fcpts</string>
<string name="pref_key_camerax_target_resolution" translatable="false">ctas</string>
<string name="pref_key_camerax_rear_camera_target_resolution" translatable="false">crctas</string>
<string name="pref_key_camerax_front_camera_target_resolution" translatable="false">cfctas</string>
<string name="pref_key_camera_live_viewport" translatable="false">clv</string>
<string name="pref_title_rear_camera_preview_size">Rear camera preview size</string>
<string name="pref_title_front_camera_preview_size">Front camera preview size</string>
<string name="pref_title_camerax_target_resolution">CameraX target resolution</string>
<string name="pref_title_camerax_rear_camera_target_resolution">CameraX rear camera target resolution</string>
<string name="pref_title_camerax_front_camera_target_resolution">CameraX front camera target resolution</string>
<string name="pref_title_camera_live_viewport">Enable live viewport</string>
<string name="pref_summary_camera_live_viewport">Do not block camera preview drawing on detection</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
android:persistent="true"
android:title="@string/pref_title_front_camera_preview_size"/>

<ListPreference
android:key="@string/pref_key_camerax_target_resolution"
<ListPreference
android:key="@string/pref_key_camerax_rear_camera_target_resolution"
android:persistent="true"
android:title="@string/pref_title_camerax_rear_camera_target_resolution"/>

<ListPreference
android:key="@string/pref_key_camerax_front_camera_target_resolution"
android:persistent="true"
android:title="@string/pref_title_camerax_target_resolution"/>
android:title="@string/pref_title_camerax_front_camera_target_resolution"/>


<SwitchPreference
android:defaultValue="false"
Expand Down
2 changes: 1 addition & 1 deletion android/automl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit cefba78

Please sign in to comment.