Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deferred processing for better performance of android #71

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ await imageClassifier.loadModel();
The `UltralyticsYoloCameraPreview` widget is used to display the camera preview and the results of the prediction.

```dart
final _controller = UltralyticsYoloCameraController();
final _controller = UltralyticsYoloCameraController(
deferredProcessing: true, // deferred processing for better performance of android (Android only, default: false)
);
UltralyticsYoloCameraPreview(
predictor: predictor, // Your prediction model data
controller: _controller, // Ultralytics camera controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.ultralytics.ultralytics_yolo.predict.Predictor;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;


public class CameraPreview {
Expand All @@ -30,14 +33,16 @@ public class CameraPreview {
private Activity activity;
private PreviewView mPreviewView;
private boolean busy = false;
private boolean deferredProcessing = false;

public CameraPreview(Context context) {
this.context = context;
}

public void openCamera(int facing, Activity activity, PreviewView mPreviewView) {
public void openCamera(int facing, Activity activity, PreviewView mPreviewView, boolean deferredProcessing) {
this.activity = activity;
this.mPreviewView = mPreviewView;
this.deferredProcessing = deferredProcessing;

final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
cameraProviderFuture.addListener(() -> {
Expand All @@ -55,6 +60,8 @@ private void bindPreview(int facing) {
if (!busy) {
busy = true;

final boolean isMirrored = (facing == CameraSelector.LENS_FACING_FRONT);

Preview cameraPreview = new Preview.Builder()
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
.build();
Expand All @@ -65,15 +72,42 @@ private void bindPreview(int facing) {

ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.setBackpressureStrategy(deferredProcessing ? ImageAnalysis.STRATEGY_BLOCK_PRODUCER : ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
.build();
imageAnalysis.setAnalyzer(Runnable::run, imageProxy -> {
predictor.predict(imageProxy, facing == CameraSelector.LENS_FACING_FRONT);

//clear stream for next image
imageProxy.close();
});
if (deferredProcessing) {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
final AtomicBoolean isPredicting = new AtomicBoolean(false);

imageAnalysis.setAnalyzer(Runnable::run, imageProxy -> {
if (isPredicting.get()) {
imageProxy.close();
return;
}

isPredicting.set(true);

executorService.submit(() -> {
try {
predictor.predict(imageProxy, isMirrored);
} catch (Exception e) {
e.printStackTrace();
} finally {
//clear stream for next image
imageProxy.close();

isPredicting.set(false);
}
});
});
} else {
imageAnalysis.setAnalyzer(Runnable::run, imageProxy -> {
predictor.predict(imageProxy, isMirrored);
//clear stream for next image
imageProxy.close();
});
}

// Unbind use cases before rebinding
cameraProvider.unbindAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class NativeView implements PlatformView {

final int lensDirection = (int) creationParams.get("lensDirection");
// final String format = (String) creationParams.get("format");
final boolean deferredProcessing = (boolean) creationParams.getOrDefault("deferredProcessing", false);

// if (Objects.requireNonNull(format).equals("tflite")) {
view = LayoutInflater.from(context).inflate(R.layout.activity_tflite_camera, null);
mPreviewView = view.findViewById(R.id.previewView);
startTfliteCamera(lensDirection);
startTfliteCamera(lensDirection, deferredProcessing);
// }

}
Expand All @@ -44,7 +45,7 @@ public View getView() {
public void dispose() {
}

private void startTfliteCamera(int facing) {
cameraPreview.openCamera(facing, activity, mPreviewView);
private void startTfliteCamera(int facing, boolean deferredProcessing) {
cameraPreview.openCamera(facing, activity, mPreviewView, deferredProcessing);
}
}
9 changes: 8 additions & 1 deletion lib/camera_preview/ultralytics_yolo_camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class UltralyticsYoloCameraValue {
UltralyticsYoloCameraValue({
required this.lensDirection,
required this.strokeWidth,
required this.deferredProcessing,
});

/// The direction of the camera lens
Expand All @@ -15,27 +16,33 @@ class UltralyticsYoloCameraValue {
/// The width of the stroke used to draw the bounding boxes
final double strokeWidth;

/// Whether the processing of the frames should be deferred (android only)
final bool deferredProcessing;

/// Creates a copy of this [UltralyticsYoloCameraValue] but with
/// the given fields
UltralyticsYoloCameraValue copyWith({
int? lensDirection,
double? strokeWidth,
bool? deferredProcessing,
}) =>
UltralyticsYoloCameraValue(
lensDirection: lensDirection ?? this.lensDirection,
strokeWidth: strokeWidth ?? this.strokeWidth,
deferredProcessing: deferredProcessing ?? this.deferredProcessing,
);
}

/// ValueNotifier that holds the state of the camera
class UltralyticsYoloCameraController
extends ValueNotifier<UltralyticsYoloCameraValue> {
/// Constructor to create an instance of [UltralyticsYoloCameraController]
UltralyticsYoloCameraController()
UltralyticsYoloCameraController({bool deferredProcessing = false})
: super(
UltralyticsYoloCameraValue(
lensDirection: 1,
strokeWidth: 2.5,
deferredProcessing: deferredProcessing,
),
);

Expand Down
2 changes: 2 additions & 0 deletions lib/camera_preview/ultralytics_yolo_camera_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class _UltralyticsYoloCameraPreviewState
final creationParams = <String, dynamic>{
'lensDirection': widget.controller.value.lensDirection,
'format': widget.predictor?.model.format.name,
'deferredProcessing':
widget.controller.value.deferredProcessing,
};

switch (defaultTargetPlatform) {
Expand Down
Loading