Skip to content

Commit

Permalink
List of included changes:
Browse files Browse the repository at this point in the history
  - Keep cameraX facing when rotating device.
  - Add shadow to info test to make it more visible
  - Reset latency stats for every 500 inference
  - internal improvement
  - Update all mlkit_samples and tests' target version to 30 to catch bugs caused by potential framework changes.

PiperOrigin-RevId: 362627878
Change-Id: Ibae6917f8949bc0c41adac744b34b6dd466156cc
  • Loading branch information
Google ML Kit authored and jackqdyulei committed Mar 16, 2021
1 parent 3f41503 commit db33daf
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion android/vision-quickstart/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
applicationId "com.google.mlkit.vision.demo"
minSdkVersion 16
multiDexEnabled true
targetSdkVersion 29
targetSdkVersion 30

versionCode 11
versionName "1.11"
Expand Down
1 change: 0 additions & 1 deletion android/vision-quickstart/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.google.mlkit.vision.demo"
android:installLocation="auto">

<!-- CameraX libraries require minSdkVersion 21, while this quickstart app
supports low to 16. Needs to use overrideLibrary to make the merger tool
ignore this conflict and import the libraries while keeping the app's lower
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public InferenceInfoGraphic(
textPaint = new Paint();
textPaint.setColor(TEXT_COLOR);
textPaint.setTextSize(TEXT_SIZE);
textPaint.setShadowLayer(5.0f, 0f, 0f, Color.BLACK);
postInvalidate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public final class CameraXLivePreviewActivity extends AppCompatActivity
private static final String SELFIE_SEGMENTATION = "Selfie Segmentation";

private static final String STATE_SELECTED_MODEL = "selected_model";
private static final String STATE_LENS_FACING = "lens_facing";

private PreviewView previewView;
private GraphicOverlay graphicOverlay;
Expand Down Expand Up @@ -130,7 +129,6 @@ protected void onCreate(Bundle savedInstanceState) {

if (savedInstanceState != null) {
selectedModel = savedInstanceState.getString(STATE_SELECTED_MODEL, OBJECT_DETECTION);
lensFacing = savedInstanceState.getInt(STATE_LENS_FACING, CameraSelector.LENS_FACING_BACK);
}
cameraSelector = new CameraSelector.Builder().requireLensFacing(lensFacing).build();

Expand Down Expand Up @@ -200,7 +198,6 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onSaveInstanceState(@NonNull Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putString(STATE_SELECTED_MODEL, selectedModel);
bundle.putInt(STATE_LENS_FACING, lensFacing);
}

@Override
Expand All @@ -219,11 +216,9 @@ public void onNothingSelected(AdapterView<?> parent) {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "Set facing");
if (cameraProvider == null) {
return;
}

int newLensFacing =
lensFacing == CameraSelector.LENS_FACING_FRONT
? CameraSelector.LENS_FACING_BACK
Expand All @@ -232,6 +227,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
new CameraSelector.Builder().requireLensFacing(newLensFacing).build();
try {
if (cameraProvider.hasCamera(newCameraSelector)) {
Log.d(TAG, "Set facing to " + newLensFacing);
lensFacing = newLensFacing;
cameraSelector = newCameraSelector;
bindAllCameraUseCases();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ private Task<T> requestDetectInImage(
long endMs = SystemClock.elapsedRealtime();
long currentFrameLatencyMs = endMs - frameStartMs;
long currentDetectorLatencyMs = endMs - detectorStartMs;
if (numRuns >= 500) {
resetLatencyStats();
}
numRuns++;
frameProcessedInOneSecondInterval++;
totalFrameMs += currentFrameLatencyMs;
Expand Down Expand Up @@ -278,10 +281,18 @@ private Task<T> requestDetectInImage(
public void stop() {
executor.shutdown();
isShutdown = true;
resetLatencyStats();
fpsTimer.cancel();
}

private void resetLatencyStats() {
numRuns = 0;
totalFrameMs = 0;
maxFrameMs = 0;
minFrameMs = Long.MAX_VALUE;
totalDetectorMs = 0;
fpsTimer.cancel();
maxDetectorMs = 0;
minDetectorMs = Long.MAX_VALUE;
}

protected abstract Task<T> detectInImage(InputImage image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class PoseGraphic extends Graphic {
classificationTextPaint = new Paint();
classificationTextPaint.setColor(Color.WHITE);
classificationTextPaint.setTextSize(POSE_CLASSIFICATION_TEXT_SIZE);
classificationTextPaint.setShadowLayer(5.0f, 0f, 0f, Color.BLACK);

whitePaint = new Paint();
whitePaint.setStrokeWidth(STROKE_WIDTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ class CameraXLivePreviewActivity :
STATE_SELECTED_MODEL,
OBJECT_DETECTION
)
lensFacing =
savedInstanceState.getInt(
STATE_LENS_FACING,
CameraSelector.LENS_FACING_BACK
)
}
cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()
setContentView(R.layout.activity_vision_camerax_live_preview)
Expand Down Expand Up @@ -181,7 +176,6 @@ class CameraXLivePreviewActivity :
override fun onSaveInstanceState(bundle: Bundle) {
super.onSaveInstanceState(bundle)
bundle.putString(STATE_SELECTED_MODEL, selectedModel)
bundle.putInt(STATE_LENS_FACING, lensFacing)
}

@Synchronized
Expand All @@ -198,7 +192,6 @@ class CameraXLivePreviewActivity :
}

override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
Log.d(TAG, "Set facing")
if (cameraProvider == null) {
return
}
Expand All @@ -211,6 +204,7 @@ class CameraXLivePreviewActivity :
CameraSelector.Builder().requireLensFacing(newLensFacing).build()
try {
if (cameraProvider!!.hasCamera(newCameraSelector)) {
Log.d(TAG, "Set facing to " + newLensFacing)
lensFacing = newLensFacing
cameraSelector = newCameraSelector
bindAllCameraUseCases()
Expand Down Expand Up @@ -537,7 +531,6 @@ class CameraXLivePreviewActivity :
private const val SELFIE_SEGMENTATION = "Selfie Segmentation"

private const val STATE_SELECTED_MODEL = "selected_model"
private const val STATE_LENS_FACING = "lens_facing"

private fun isPermissionGranted(
context: Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ abstract class VisionProcessorBase<T>(context: Context) : VisionImageProcessor {
val endMs = SystemClock.elapsedRealtime()
val currentFrameLatencyMs = endMs - frameStartMs
val currentDetectorLatencyMs = endMs - detectorStartMs
if (numRuns >= 500) {
resetLatencyStats()
}
numRuns++
frameProcessedInOneSecondInterval++
totalFrameMs += currentFrameLatencyMs
Expand Down Expand Up @@ -271,10 +274,18 @@ abstract class VisionProcessorBase<T>(context: Context) : VisionImageProcessor {
override fun stop() {
executor.shutdown()
isShutdown = true
resetLatencyStats()
fpsTimer.cancel()
}

private fun resetLatencyStats() {
numRuns = 0
totalFrameMs = 0
maxFrameMs = 0
minFrameMs = Long.MAX_VALUE
totalDetectorMs = 0
fpsTimer.cancel()
maxDetectorMs = 0
minDetectorMs = Long.MAX_VALUE
}

protected abstract fun detectInImage(image: InputImage): Task<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class PoseGraphic internal constructor(
classificationTextPaint = Paint()
classificationTextPaint.color = Color.WHITE
classificationTextPaint.textSize = POSE_CLASSIFICATION_TEXT_SIZE
classificationTextPaint.setShadowLayer(5.0f, 0f, 0f, Color.BLACK)

whitePaint = Paint()
whitePaint.strokeWidth = STROKE_WIDTH
Expand Down

0 comments on commit db33daf

Please sign in to comment.