From daffbca6ea2a6e4250ee09e032c9e5046384121a Mon Sep 17 00:00:00 2001 From: Damien Brun Date: Thu, 16 Nov 2017 16:24:48 +0100 Subject: [PATCH] Move convertYUV420ToARGB8888 to background thread because it could cause delay/freezing time on certain mobile devices. --- .../tensorflow/demo/ClassifierActivity.java | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/android/src/org/tensorflow/demo/ClassifierActivity.java b/android/src/org/tensorflow/demo/ClassifierActivity.java index 916ef55e2..7c0d9507d 100644 --- a/android/src/org/tensorflow/demo/ClassifierActivity.java +++ b/android/src/org/tensorflow/demo/ClassifierActivity.java @@ -177,6 +177,10 @@ public void drawCallback(final Canvas canvas) { public void onImageAvailable(final ImageReader reader) { Image image = null; + final int yRowStride; + final int uvRowStride; + final int uvPixelStride; + try { image = reader.acquireLatestImage(); @@ -195,19 +199,9 @@ public void onImageAvailable(final ImageReader reader) { final Plane[] planes = image.getPlanes(); fillBytes(planes, yuvBytes); - final int yRowStride = planes[0].getRowStride(); - final int uvRowStride = planes[1].getRowStride(); - final int uvPixelStride = planes[1].getPixelStride(); - ImageUtils.convertYUV420ToARGB8888( - yuvBytes[0], - yuvBytes[1], - yuvBytes[2], - previewWidth, - previewHeight, - yRowStride, - uvRowStride, - uvPixelStride, - rgbBytes); + yRowStride = planes[0].getRowStride(); + uvRowStride = planes[1].getRowStride(); + uvPixelStride = planes[1].getPixelStride(); image.close(); } catch (final Exception e) { @@ -219,19 +213,30 @@ public void onImageAvailable(final ImageReader reader) { return; } - rgbFrameBitmap.setPixels(rgbBytes, 0, previewWidth, 0, 0, previewWidth, previewHeight); - final Canvas canvas = new Canvas(croppedBitmap); - canvas.drawBitmap(rgbFrameBitmap, frameToCropTransform, null); - - // For examining the actual TF input. - if (SAVE_PREVIEW_BITMAP) { - ImageUtils.saveBitmap(croppedBitmap); - } - runInBackground( new Runnable() { @Override public void run() { + ImageUtils.convertYUV420ToARGB8888( + yuvBytes[0], + yuvBytes[1], + yuvBytes[2], + previewWidth, + previewHeight, + yRowStride, + uvRowStride, + uvPixelStride, + rgbBytes); + + rgbFrameBitmap.setPixels(rgbBytes, 0, previewWidth, 0, 0, previewWidth, previewHeight); + final Canvas canvas = new Canvas(croppedBitmap); + canvas.drawBitmap(rgbFrameBitmap, frameToCropTransform, null); + + // For examining the actual TF input. + if (SAVE_PREVIEW_BITMAP) { + ImageUtils.saveBitmap(croppedBitmap); + } + final long startTime = SystemClock.uptimeMillis(); final List results = classifier.recognizeImage(croppedBitmap); lastProcessingTimeMs = SystemClock.uptimeMillis() - startTime;