From a4904269d568a2201c8de31a5b30ff9d3480260c Mon Sep 17 00:00:00 2001 From: Winter <1558270516@qq.com> Date: Thu, 17 Nov 2022 19:18:47 +0800 Subject: [PATCH] [Android] Configure face recognition and segmentation details page (#619) * 1.Classification Code submission; 2.Specification code format. * Specification code format. * Update strings.xml * 1.Display ocr, detection and classification results. 2.Specification code format. * Configure face recognition and semantic segmentation details page. Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> --- .../ClassificationMainActivity.java | 186 ++++++++---- .../detection/DetectionMainActivity.java | 124 ++++---- .../examples/facedet/FaceDetMainActivity.java | 107 +++---- .../app/examples/ocr/OcrMainActivity.java | 267 +++++++++++++++--- .../SegmentationMainActivity.java | 154 ++++------ .../baidu/paddle/fastdeploy/app/ui/Utils.java | 24 ++ .../res/layout/classification_camera_page.xml | 2 +- .../main/res/layout/detection_camera_page.xml | 2 +- .../src/main/res/layout/ocr_camera_page.xml | 5 +- 9 files changed, 565 insertions(+), 306 deletions(-) diff --git a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/classification/ClassificationMainActivity.java b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/classification/ClassificationMainActivity.java index 14c1c329f2..e052df7a84 100644 --- a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/classification/ClassificationMainActivity.java +++ b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/classification/ClassificationMainActivity.java @@ -2,6 +2,7 @@ import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap; import static com.baidu.paddle.fastdeploy.app.ui.Utils.getRealPathFromURI; +import static com.baidu.paddle.fastdeploy.app.ui.Utils.readTxt; import android.Manifest; import android.annotation.SuppressLint; @@ -36,9 +37,9 @@ import com.baidu.paddle.fastdeploy.app.ui.view.adapter.BaseResultAdapter; import com.baidu.paddle.fastdeploy.app.ui.view.model.BaseResultModel; import com.baidu.paddle.fastdeploy.vision.ClassifyResult; +import com.baidu.paddle.fastdeploy.vision.Visualize; import com.baidu.paddle.fastdeploy.vision.classification.PaddleClasModel; -import java.io.File; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -62,27 +63,33 @@ public class ClassificationMainActivity extends Activity implements View.OnClick private SeekBar confidenceSeekbar; private TextView seekbarText; private float resultNum = 1.0f; - private ResultListView detectResultView; + private ResultListView resultView; private Bitmap shutterBitmap; - private Bitmap originShutterBitmap; private Bitmap picBitmap; - private Bitmap originPicBitmap; + private boolean isShutterBitmapCopied = false; public static final int TYPE_UNKNOWN = -1; public static final int BTN_SHUTTER = 0; public static final int ALBUM_SELECT = 1; - private static int TYPE = TYPE_UNKNOWN; + public static final int REALTIME_DETECT = 2; + private static int TYPE = REALTIME_DETECT; private static final int REQUEST_PERMISSION_CODE_STORAGE = 101; private static final int INTENT_CODE_PICK_IMAGE = 100; + private static final int TIME_SLEEP_INTERVAL = 50; // ms - String savedImagePath = "result.jpg"; long timeElapsed = 0; long frameCounter = 0; // Call 'init' and 'release' manually later PaddleClasModel predictor = new PaddleClasModel(); + private float[] scores; + private int[] labelId; + private boolean initialized; + private List labelText; + private List results = new ArrayList<>(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -114,12 +121,8 @@ public void onClick(View v) { break; case R.id.btn_shutter: TYPE = BTN_SHUTTER; - svPreview.onPause(); - cameraPageView.setVisibility(View.GONE); - resultPageView.setVisibility(View.VISIBLE); - seekbarText.setText(resultNum + ""); - confidenceSeekbar.setProgress((int) (resultNum * 100)); - resultImage.setImageBitmap(shutterBitmap); + shutterAndPauseCamera(); + resultView.setAdapter(null); break; case R.id.btn_settings: startActivity(new Intent(ClassificationMainActivity.this, ClassificationSettingsActivity.class)); @@ -130,7 +133,7 @@ public void onClick(View v) { case R.id.back_in_preview: finish(); break; - case R.id.albumSelect: + case R.id.iv_select: TYPE = ALBUM_SELECT; // Judge whether authority has been granted. if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { @@ -141,16 +144,84 @@ public void onClick(View v) { intent.setType("image/*"); startActivityForResult(intent, INTENT_CODE_PICK_IMAGE); } + resultView.setAdapter(null); break; case R.id.back_in_result: - resultPageView.setVisibility(View.GONE); - cameraPageView.setVisibility(View.VISIBLE); - svPreview.onResume(); + back(); break; } } + @Override + public void onBackPressed() { + super.onBackPressed(); + back(); + } + + private void back() { + resultPageView.setVisibility(View.GONE); + cameraPageView.setVisibility(View.VISIBLE); + TYPE = REALTIME_DETECT; + isShutterBitmapCopied = false; + svPreview.onResume(); + results.clear(); + if (scores != null) { + scores = null; + } + if (labelId != null) { + labelId = null; + } + } + + private void shutterAndPauseCamera() { + new Thread(new Runnable() { + @Override + public void run() { + try { + // Sleep some times to ensure picture has been correctly shut. + Thread.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms + } catch (InterruptedException e) { + e.printStackTrace(); + } + runOnUiThread(new Runnable() { + @SuppressLint("SetTextI18n") + public void run() { + // These code will run in main thread. + svPreview.onPause(); + cameraPageView.setVisibility(View.GONE); + resultPageView.setVisibility(View.VISIBLE); + seekbarText.setText(resultNum + ""); + confidenceSeekbar.setProgress((int) (resultNum * 100)); + if (shutterBitmap != null && !shutterBitmap.isRecycled()) { + resultImage.setImageBitmap(shutterBitmap); + } else { + new AlertDialog.Builder(ClassificationMainActivity.this) + .setTitle("Empty Result!") + .setMessage("Current picture is empty, please shutting it again!") + .setCancelable(true) + .show(); + } + } + }); + + } + }).start(); + } + + private void copyBitmapFromCamera(Bitmap ARGB8888ImageBitmap) { + if (isShutterBitmapCopied || ARGB8888ImageBitmap == null) { + return; + } + if (!ARGB8888ImageBitmap.isRecycled()) { + synchronized (this) { + shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); + } + SystemClock.sleep(TIME_SLEEP_INTERVAL); + isShutterBitmapCopied = true; + } + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); @@ -163,7 +234,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri uri = data.getData(); String path = getRealPathFromURI(this, uri); picBitmap = decodeBitmap(path, 720, 1280); - originPicBitmap = picBitmap.copy(Bitmap.Config.ARGB_8888, true); resultImage.setImageBitmap(picBitmap); } } @@ -190,34 +260,17 @@ public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { @Override public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { - String savedImagePath = ""; - synchronized (this) { - savedImagePath = Utils.getDCIMDirectory() + File.separator + "result.jpg"; - } if (TYPE == BTN_SHUTTER) { - shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); - originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); - } else { - // Only reference in predict loops. - shutterBitmap = ARGB8888ImageBitmap; - originShutterBitmap = ARGB8888ImageBitmap; + copyBitmapFromCamera(ARGB8888ImageBitmap); + return false; } boolean modified = false; - long tc = System.currentTimeMillis(); - ClassifyResult result = predictor.predict( - ARGB8888ImageBitmap, true, ClassificationSettingsActivity.scoreThreshold); - + ClassifyResult result = predictor.predict(ARGB8888ImageBitmap); timeElapsed += (System.currentTimeMillis() - tc); - frameCounter++; - + Visualize.visClassification(ARGB8888ImageBitmap, result, resultNum, 12); modified = result.initialized(); - if (!savedImagePath.isEmpty()) { - synchronized (this) { - ClassificationMainActivity.this.savedImagePath = "result.jpg"; - } - } - + frameCounter++; if (frameCounter >= 30) { final int fps = (int) (1000 / (timeElapsed / 30)); runOnUiThread(new Runnable() { @@ -259,7 +312,7 @@ protected void onDestroy() { } public void initView() { - TYPE = BTN_SHUTTER; + TYPE = REALTIME_DETECT; svPreview = (CameraSurfaceView) findViewById(R.id.sv_preview); svPreview.setOnTextureChangedListener(this); tvStatus = (TextView) findViewById(R.id.tv_status); @@ -273,7 +326,7 @@ public void initView() { realtimeToggleButton.setOnClickListener(this); backInPreview = findViewById(R.id.back_in_preview); backInPreview.setOnClickListener(this); - albumSelectButton = findViewById(R.id.albumSelect); + albumSelectButton = findViewById(R.id.iv_select); albumSelectButton.setOnClickListener(this); cameraPageView = findViewById(R.id.camera_page); resultPageView = findViewById(R.id.result_page); @@ -282,15 +335,7 @@ public void initView() { backInResult.setOnClickListener(this); confidenceSeekbar = findViewById(R.id.confidence_seekbar); seekbarText = findViewById(R.id.seekbar_text); - detectResultView = findViewById(R.id.result_list_view); - - List results = new ArrayList<>(); - results.add(new BaseResultModel(1, "cup", 0.4f)); - results.add(new BaseResultModel(2, "pen", 0.6f)); - results.add(new BaseResultModel(3, "tang", 1.0f)); - final BaseResultAdapter adapter = new BaseResultAdapter(this, R.layout.classification_result_page_item, results); - detectResultView.setAdapter(adapter); - detectResultView.invalidate(); + resultView = findViewById(R.id.result_list_view); confidenceSeekbar.setMax(100); confidenceSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @@ -301,6 +346,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { resultNum = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); seekbarText.setText(resultNum + ""); confidenceSeekbar.setProgress((int) (resultNum * 100)); + results.clear(); } @Override @@ -315,16 +361,11 @@ public void onStopTrackingTouch(SeekBar seekBar) { public void run() { if (TYPE == ALBUM_SELECT) { SystemClock.sleep(500); - predictor.predict(picBitmap, savedImagePath, resultNum); - resultImage.setImageBitmap(picBitmap); - picBitmap = originPicBitmap.copy(Bitmap.Config.ARGB_8888, true); - resultNum = 1.0f; + detail(picBitmap); } else { SystemClock.sleep(500); - predictor.predict(shutterBitmap, savedImagePath, resultNum); - resultImage.setImageBitmap(shutterBitmap); - shutterBitmap = originShutterBitmap.copy(Bitmap.Config.ARGB_8888, true); - resultNum = 1.0f; + svPreview.onPause(); + detail(shutterBitmap); } } }); @@ -332,6 +373,34 @@ public void run() { }); } + private void detail(Bitmap bitmap) { + ClassifyResult result = predictor.predict(bitmap, true, ClassificationSettingsActivity.scoreThreshold); + if (scores == null) { + scores = result.mScores; + } + if (labelId == null) { + labelId = result.mLabelIds; + } + initialized = result.initialized(); + if (initialized) { + for (int i = 0; i < labelId.length; i++) { + for (int j = 0; j < labelText.size(); j++) { + if (scores[i] > resultNum) { + if (labelId[i] == Integer.parseInt(labelText.get(j).substring(0, labelText.get(j).indexOf(" ")))) { + results.add(new BaseResultModel(labelId[i], labelText.get(j), scores[i])); + } + } + } + } + } + BaseResultAdapter adapter = new BaseResultAdapter(getBaseContext(), R.layout.ocr_result_page_item, results); + resultView.setAdapter(adapter); + resultView.invalidate(); + + resultImage.setImageBitmap(bitmap); + resultNum = 1.0f; + } + @SuppressLint("ApplySharedPref") public void initSettings() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -352,6 +421,7 @@ public void checkAndUpdateSettings() { String paramsFile = realModelDir + "/" + "inference.pdiparams"; String configFile = realModelDir + "/" + "inference_cls.yaml"; String labelFile = realLabelPath; + labelText = readTxt(labelFile); RuntimeOption option = new RuntimeOption(); option.setCpuThreadNum(ClassificationSettingsActivity.cpuThreadNum); option.setLitePowerMode(ClassificationSettingsActivity.cpuPowerMode); diff --git a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/detection/DetectionMainActivity.java b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/detection/DetectionMainActivity.java index e3da008bb2..3b8c750113 100644 --- a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/detection/DetectionMainActivity.java +++ b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/detection/DetectionMainActivity.java @@ -8,18 +8,14 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.database.Cursor; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.SystemClock; import android.preference.PreferenceManager; -import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; -import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.Window; @@ -31,7 +27,6 @@ import com.baidu.paddle.fastdeploy.RuntimeOption; import com.baidu.paddle.fastdeploy.app.examples.R; -import com.baidu.paddle.fastdeploy.app.examples.facedet.FaceDetMainActivity; import com.baidu.paddle.fastdeploy.app.ui.view.CameraSurfaceView; import com.baidu.paddle.fastdeploy.app.ui.view.ResultListView; import com.baidu.paddle.fastdeploy.app.ui.Utils; @@ -43,8 +38,8 @@ import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap; import static com.baidu.paddle.fastdeploy.app.ui.Utils.getRealPathFromURI; +import static com.baidu.paddle.fastdeploy.app.ui.Utils.readTxt; -import java.io.File; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -68,11 +63,9 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe private SeekBar confidenceSeekbar; private TextView seekbarText; private float resultNum = 1.0f; - private ResultListView detectResultView; + private ResultListView resultView; private Bitmap shutterBitmap; - private Bitmap originShutterBitmap; private Bitmap picBitmap; - private Bitmap originPicBitmap; private boolean isShutterBitmapCopied = false; public static final int TYPE_UNKNOWN = -1; @@ -85,13 +78,18 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe private static final int INTENT_CODE_PICK_IMAGE = 100; private static final int TIME_SLEEP_INTERVAL = 50; // ms - String savedImagePath = "result.jpg"; long timeElapsed = 0; long frameCounter = 0; // Call 'init' and 'release' manually later PicoDet predictor = new PicoDet(); + private float[] scores; + private int[] labelId; + private boolean initialized; + private List labelText; + private List results = new ArrayList<>(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -124,6 +122,7 @@ public void onClick(View v) { case R.id.btn_shutter: TYPE = BTN_SHUTTER; shutterAndPauseCamera(); + resultView.setAdapter(null); break; case R.id.btn_settings: startActivity(new Intent(DetectionMainActivity.this, DetectionSettingsActivity.class)); @@ -134,7 +133,7 @@ public void onClick(View v) { case R.id.back_in_preview: finish(); break; - case R.id.albumSelect: + case R.id.iv_select: TYPE = ALBUM_SELECT; // Judge whether authority has been granted. if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { @@ -145,17 +144,35 @@ public void onClick(View v) { intent.setType("image/*"); startActivityForResult(intent, INTENT_CODE_PICK_IMAGE); } + resultView.setAdapter(null); break; case R.id.back_in_result: - resultPageView.setVisibility(View.GONE); - cameraPageView.setVisibility(View.VISIBLE); - TYPE = REALTIME_DETECT; - isShutterBitmapCopied = false; - svPreview.onResume(); + back(); break; } } + @Override + public void onBackPressed() { + super.onBackPressed(); + back(); + } + + private void back() { + resultPageView.setVisibility(View.GONE); + cameraPageView.setVisibility(View.VISIBLE); + TYPE = REALTIME_DETECT; + isShutterBitmapCopied = false; + svPreview.onResume(); + results.clear(); + if (scores != null) { + scores = null; + } + if (labelId != null) { + labelId = null; + } + } + private void shutterAndPauseCamera() { new Thread(new Runnable() { @Override @@ -198,7 +215,6 @@ private void copyBitmapFromCamera(Bitmap ARGB8888ImageBitmap) { if (!ARGB8888ImageBitmap.isRecycled()) { synchronized (this) { shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); - originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); } SystemClock.sleep(TIME_SLEEP_INTERVAL); isShutterBitmapCopied = true; @@ -217,7 +233,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri uri = data.getData(); String path = getRealPathFromURI(this, uri); picBitmap = decodeBitmap(path, 720, 1280); - originPicBitmap = picBitmap.copy(Bitmap.Config.ARGB_8888, true); resultImage.setImageBitmap(picBitmap); } } @@ -248,27 +263,12 @@ public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { copyBitmapFromCamera(ARGB8888ImageBitmap); return false; } - - String savedImagePath = ""; - synchronized (this) { - savedImagePath = Utils.getDCIMDirectory() + File.separator + "result.jpg"; - } - boolean modified = false; - long tc = System.currentTimeMillis(); DetectionResult result = predictor.predict(ARGB8888ImageBitmap); timeElapsed += (System.currentTimeMillis() - tc); - Visualize.visDetection(ARGB8888ImageBitmap, result, DetectionSettingsActivity.scoreThreshold); - modified = result.initialized(); - if (!savedImagePath.isEmpty()) { - synchronized (this) { - DetectionMainActivity.this.savedImagePath = "result.jpg"; - } - } - frameCounter++; if (frameCounter >= 30) { final int fps = (int) (1000 / (timeElapsed / 30)); @@ -325,7 +325,7 @@ public void initView() { realtimeToggleButton.setOnClickListener(this); backInPreview = findViewById(R.id.back_in_preview); backInPreview.setOnClickListener(this); - albumSelectButton = findViewById(R.id.albumSelect); + albumSelectButton = findViewById(R.id.iv_select); albumSelectButton.setOnClickListener(this); cameraPageView = findViewById(R.id.camera_page); resultPageView = findViewById(R.id.result_page); @@ -334,15 +334,7 @@ public void initView() { backInResult.setOnClickListener(this); confidenceSeekbar = findViewById(R.id.confidence_seekbar); seekbarText = findViewById(R.id.seekbar_text); - detectResultView = findViewById(R.id.result_list_view); - - List results = new ArrayList<>(); - results.add(new BaseResultModel(1, "cup", 0.4f)); - results.add(new BaseResultModel(2, "pen", 0.6f)); - results.add(new BaseResultModel(3, "tang", 1.0f)); - final BaseResultAdapter adapter = new BaseResultAdapter(this, R.layout.detection_result_page_item, results); - detectResultView.setAdapter(adapter); - detectResultView.invalidate(); + resultView = findViewById(R.id.result_list_view); confidenceSeekbar.setMax(100); confidenceSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @@ -353,6 +345,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { resultNum = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); seekbarText.setText(resultNum + ""); confidenceSeekbar.setProgress((int) (resultNum * 100)); + results.clear(); } @Override @@ -367,20 +360,11 @@ public void onStopTrackingTouch(SeekBar seekBar) { public void run() { if (TYPE == ALBUM_SELECT) { SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); - if (!picBitmap.isRecycled()) { - predictor.predict(picBitmap, true, resultNum); - resultImage.setImageBitmap(picBitmap); - picBitmap = originPicBitmap.copy(Bitmap.Config.ARGB_8888, true); - } - resultNum = 1.0f; + detail(picBitmap); } else { SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); - if (!shutterBitmap.isRecycled()) { - predictor.predict(shutterBitmap, true, resultNum); - resultImage.setImageBitmap(shutterBitmap); - shutterBitmap = originShutterBitmap.copy(Bitmap.Config.ARGB_8888, true); - } - resultNum = 1.0f; + svPreview.onPause(); + detail(shutterBitmap); } } }); @@ -388,6 +372,35 @@ public void run() { }); } + private void detail(Bitmap bitmap) { + DetectionResult result = predictor.predict( + bitmap, true, DetectionSettingsActivity.scoreThreshold); + if (scores == null) { + scores = result.mScores; + } + if (labelId == null) { + labelId = result.mLabelIds; + } + initialized = result.initialized(); + if (initialized) { + for (int i = 0; i < labelId.length; i++) { + for (int j = 0; j < labelText.size(); j++) { + if (scores[i] > resultNum) { + if (labelId[i] == j) { + results.add(new BaseResultModel(labelId[i], labelText.get(j), scores[i])); + } + } + } + } + } + BaseResultAdapter adapter = new BaseResultAdapter(getBaseContext(), R.layout.ocr_result_page_item, results); + resultView.setAdapter(adapter); + resultView.invalidate(); + + resultImage.setImageBitmap(bitmap); + resultNum = 1.0f; + } + @SuppressLint("ApplySharedPref") public void initSettings() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -408,6 +421,7 @@ public void checkAndUpdateSettings() { String paramsFile = realModelDir + "/" + "model.pdiparams"; String configFile = realModelDir + "/" + "infer_cfg.yml"; String labelFile = realLabelPath; + labelText = readTxt(labelFile); RuntimeOption option = new RuntimeOption(); option.setCpuThreadNum(DetectionSettingsActivity.cpuThreadNum); option.setLitePowerMode(DetectionSettingsActivity.cpuPowerMode); diff --git a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/facedet/FaceDetMainActivity.java b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/facedet/FaceDetMainActivity.java index c7c270faa9..934f568b3a 100644 --- a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/facedet/FaceDetMainActivity.java +++ b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/facedet/FaceDetMainActivity.java @@ -8,14 +8,11 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.database.Cursor; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.SystemClock; import android.preference.PreferenceManager; -import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; @@ -43,7 +40,6 @@ import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap; import static com.baidu.paddle.fastdeploy.app.ui.Utils.getRealPathFromURI; -import java.io.File; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -67,11 +63,9 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene private SeekBar confidenceSeekbar; private TextView seekbarText; private float resultNum = 1.0f; - private ResultListView detectResultView; + private ResultListView resultView; private Bitmap shutterBitmap; - private Bitmap originShutterBitmap; private Bitmap picBitmap; - private Bitmap originPicBitmap; private boolean isShutterBitmapCopied = false; public static final int TYPE_UNKNOWN = -1; @@ -84,13 +78,16 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene private static final int INTENT_CODE_PICK_IMAGE = 100; private static final int TIME_SLEEP_INTERVAL = 50; // ms - String savedImagePath = "result.jpg"; long timeElapsed = 0; long frameCounter = 0; // Call 'init' and 'release' manually later SCRFD predictor = new SCRFD(); + public float[] scores; // [n] + public boolean initialized = false; + private List results = new ArrayList<>(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -123,6 +120,7 @@ public void onClick(View v) { case R.id.btn_shutter: TYPE = BTN_SHUTTER; shutterAndPauseCamera(); + resultView.setAdapter(null); break; case R.id.btn_settings: startActivity(new Intent(FaceDetMainActivity.this, FaceDetSettingsActivity.class)); @@ -144,17 +142,32 @@ public void onClick(View v) { intent.setType("image/*"); startActivityForResult(intent, INTENT_CODE_PICK_IMAGE); } + resultView.setAdapter(null); break; case R.id.back_in_result: - resultPageView.setVisibility(View.GONE); - cameraPageView.setVisibility(View.VISIBLE); - TYPE = REALTIME_DETECT; - isShutterBitmapCopied = false; - svPreview.onResume(); + back(); break; } } + @Override + public void onBackPressed() { + super.onBackPressed(); + back(); + } + + private void back() { + resultPageView.setVisibility(View.GONE); + cameraPageView.setVisibility(View.VISIBLE); + TYPE = REALTIME_DETECT; + isShutterBitmapCopied = false; + svPreview.onResume(); + results.clear(); + if (scores != null) { + scores = null; + } + } + private void shutterAndPauseCamera() { new Thread(new Runnable() { @Override @@ -197,7 +210,6 @@ private void copyBitmapFromCamera(Bitmap ARGB8888ImageBitmap) { if (!ARGB8888ImageBitmap.isRecycled()) { synchronized (this) { shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); - originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); } SystemClock.sleep(TIME_SLEEP_INTERVAL); isShutterBitmapCopied = true; @@ -216,7 +228,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri uri = data.getData(); String path = getRealPathFromURI(this, uri); picBitmap = decodeBitmap(path, 720, 1280); - originPicBitmap = picBitmap.copy(Bitmap.Config.ARGB_8888, true); resultImage.setImageBitmap(picBitmap); } } @@ -248,29 +259,14 @@ public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { copyBitmapFromCamera(ARGB8888ImageBitmap); return false; } - - String savedImagePath = ""; - synchronized (this) { - savedImagePath = Utils.getDCIMDirectory() + File.separator + "result.jpg"; - } - boolean modified = false; - long tc = System.currentTimeMillis(); + SystemClock.sleep(TIME_SLEEP_INTERVAL); FaceDetectionResult result = predictor.predict( ARGB8888ImageBitmap, FaceDetSettingsActivity.scoreThreshold, 0.4f); - timeElapsed += (System.currentTimeMillis() - tc); - Visualize.visFaceDetection(ARGB8888ImageBitmap, result); - modified = result.initialized(); - if (!savedImagePath.isEmpty()) { - synchronized (this) { - FaceDetMainActivity.this.savedImagePath = "result.jpg"; - } - } - frameCounter++; if (frameCounter >= 30) { final int fps = (int) (1000 / (timeElapsed / 30)); @@ -336,16 +332,7 @@ public void initView() { backInResult.setOnClickListener(this); confidenceSeekbar = findViewById(R.id.confidence_seekbar); seekbarText = findViewById(R.id.seekbar_text); - detectResultView = findViewById(R.id.result_list_view); - - List results = new ArrayList<>(); - // TODO: add model results from FaceDetectionResult instead of using fake data. - results.add(new BaseResultModel(1, "face", 0.4f)); - results.add(new BaseResultModel(2, "face", 0.6f)); - results.add(new BaseResultModel(3, "face", 1.0f)); - final BaseResultAdapter adapter = new BaseResultAdapter(this, R.layout.facedet_result_page_item, results); - detectResultView.setAdapter(adapter); - detectResultView.invalidate(); + resultView = findViewById(R.id.result_list_view); confidenceSeekbar.setMax(100); confidenceSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @@ -356,6 +343,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { resultNum = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); seekbarText.setText(resultNum + ""); confidenceSeekbar.setProgress((int) (resultNum * 100)); + results.clear(); } @Override @@ -370,20 +358,11 @@ public void onStopTrackingTouch(SeekBar seekBar) { public void run() { if (TYPE == ALBUM_SELECT) { SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms - if (!picBitmap.isRecycled()) { - predictor.predict(picBitmap, true, resultNum, 0.4f); - resultImage.setImageBitmap(picBitmap); - picBitmap = originPicBitmap.copy(Bitmap.Config.ARGB_8888, true); - } - resultNum = 1.0f; + detail(picBitmap); } else { SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms - if (!shutterBitmap.isRecycled()) { - predictor.predict(shutterBitmap, true, resultNum, 0.4f); - resultImage.setImageBitmap(shutterBitmap); - shutterBitmap = originShutterBitmap.copy(Bitmap.Config.ARGB_8888, true); - } - resultNum = 1.0f; + svPreview.onPause(); + detail(shutterBitmap); } } }); @@ -391,6 +370,28 @@ public void run() { }); } + private void detail(Bitmap bitmap) { + FaceDetectionResult result = predictor.predict(bitmap, true, FaceDetSettingsActivity.scoreThreshold, 0.4f); + if (scores == null) { + scores = result.mScores; + } + initialized = result.initialized(); + Log.e("GBD", initialized + "---initialized"); + if (initialized) { + for (int i = 0; i < scores.length; i++) { + if (scores[i] > resultNum) { + results.add(new BaseResultModel(i + 1, "face", scores[i])); + } + } + } + BaseResultAdapter adapter = new BaseResultAdapter(getBaseContext(), R.layout.ocr_result_page_item, results); + resultView.setAdapter(adapter); + resultView.invalidate(); + + resultImage.setImageBitmap(bitmap); + resultNum = 1.0f; + } + @SuppressLint("ApplySharedPref") public void initSettings() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); diff --git a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/ocr/OcrMainActivity.java b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/ocr/OcrMainActivity.java index 55f7294828..321b03ecbe 100644 --- a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/ocr/OcrMainActivity.java +++ b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/ocr/OcrMainActivity.java @@ -1,5 +1,7 @@ package com.baidu.paddle.fastdeploy.app.examples.ocr; +import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap; +import static com.baidu.paddle.fastdeploy.app.ui.Utils.getRealPathFromURI; import android.Manifest; import android.annotation.SuppressLint; @@ -10,32 +12,39 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Bitmap; +import android.net.Uri; import android.os.Bundle; +import android.os.SystemClock; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.view.View; +import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.ImageButton; import android.widget.ImageView; +import android.widget.SeekBar; import android.widget.TextView; -import android.widget.Toast; import com.baidu.paddle.fastdeploy.RuntimeOption; import com.baidu.paddle.fastdeploy.app.examples.R; -import com.baidu.paddle.fastdeploy.app.ui.Utils; import com.baidu.paddle.fastdeploy.app.ui.view.CameraSurfaceView; -import com.baidu.paddle.fastdeploy.vision.OCRResult; +import com.baidu.paddle.fastdeploy.app.ui.view.ResultListView; +import com.baidu.paddle.fastdeploy.app.ui.Utils; +import com.baidu.paddle.fastdeploy.app.ui.view.adapter.BaseResultAdapter; +import com.baidu.paddle.fastdeploy.app.ui.view.model.BaseResultModel; import com.baidu.paddle.fastdeploy.pipeline.PPOCRv2; +import com.baidu.paddle.fastdeploy.vision.OCRResult; +import com.baidu.paddle.fastdeploy.vision.Visualize; import com.baidu.paddle.fastdeploy.vision.ocr.Classifier; import com.baidu.paddle.fastdeploy.vision.ocr.DBDetector; import com.baidu.paddle.fastdeploy.vision.ocr.Recognizer; -import java.io.File; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; public class OcrMainActivity extends Activity implements View.OnClickListener, CameraSurfaceView.OnTextureChangedListener { private static final String TAG = OcrMainActivity.class.getSimpleName(); @@ -48,14 +57,40 @@ public class OcrMainActivity extends Activity implements View.OnClickListener, C ImageView realtimeToggleButton; boolean isRealtimeStatusRunning = false; ImageView backInPreview; + private ImageView albumSelectButton; + private View cameraPageView; + private ViewGroup resultPageView; + private ImageView resultImage; + private ImageView backInResult; + private SeekBar confidenceSeekbar; + private TextView seekbarText; + private float resultNum = 1.0f; + private ResultListView resultView; + private Bitmap shutterBitmap; + private Bitmap picBitmap; + private boolean isShutterBitmapCopied = false; - String savedImagePath = "result.jpg"; - int lastFrameIndex = 0; - long lastFrameTime; + public static final int TYPE_UNKNOWN = -1; + public static final int BTN_SHUTTER = 0; + public static final int ALBUM_SELECT = 1; + public static final int REALTIME_DETECT = 2; + private static int TYPE = REALTIME_DETECT; + + private static final int REQUEST_PERMISSION_CODE_STORAGE = 101; + private static final int INTENT_CODE_PICK_IMAGE = 100; + private static final int TIME_SLEEP_INTERVAL = 50; // ms + + long timeElapsed = 0; + long frameCounter = 0; // Call 'init' and 'release' manually later PPOCRv2 predictor = new PPOCRv2(); + private String[] texts; + private float[] recScores; + private boolean initialized; + private List results = new ArrayList<>(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -86,12 +121,9 @@ public void onClick(View v) { svPreview.switchCamera(); break; case R.id.btn_shutter: - @SuppressLint("SimpleDateFormat") - SimpleDateFormat date = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); - synchronized (this) { - savedImagePath = Utils.getDCIMDirectory() + File.separator + date.format(new Date()).toString() + ".png"; - } - Toast.makeText(OcrMainActivity.this, "Save snapshot to " + savedImagePath, Toast.LENGTH_SHORT).show(); + TYPE = BTN_SHUTTER; + shutterAndPauseCamera(); + resultView.setAdapter(null); break; case R.id.btn_settings: startActivity(new Intent(OcrMainActivity.this, OcrSettingsActivity.class)); @@ -102,6 +134,109 @@ public void onClick(View v) { case R.id.back_in_preview: finish(); break; + case R.id.iv_select: + TYPE = ALBUM_SELECT; + // Judge whether authority has been granted. + if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + // If this permission was requested before the application but the user refused the request, this method will return true. + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_CODE_STORAGE); + } else { + Intent intent = new Intent(Intent.ACTION_PICK); + intent.setType("image/*"); + startActivityForResult(intent, INTENT_CODE_PICK_IMAGE); + } + resultView.setAdapter(null); + break; + case R.id.back_in_result: + back(); + break; + } + } + + @Override + public void onBackPressed() { + super.onBackPressed(); + back(); + } + + private void back() { + resultPageView.setVisibility(View.GONE); + cameraPageView.setVisibility(View.VISIBLE); + TYPE = REALTIME_DETECT; + isShutterBitmapCopied = false; + svPreview.onResume(); + results.clear(); + if (texts != null) { + texts = null; + } + if (recScores != null) { + recScores = null; + } + } + + private void shutterAndPauseCamera() { + new Thread(new Runnable() { + @Override + public void run() { + try { + // Sleep some times to ensure picture has been correctly shut. + Thread.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms + } catch (InterruptedException e) { + e.printStackTrace(); + } + runOnUiThread(new Runnable() { + @SuppressLint("SetTextI18n") + public void run() { + // These code will run in main thread. + svPreview.onPause(); + cameraPageView.setVisibility(View.GONE); + resultPageView.setVisibility(View.VISIBLE); + seekbarText.setText(resultNum + ""); + confidenceSeekbar.setProgress((int) (resultNum * 100)); + if (shutterBitmap != null && !shutterBitmap.isRecycled()) { + resultImage.setImageBitmap(shutterBitmap); + } else { + new AlertDialog.Builder(OcrMainActivity.this) + .setTitle("Empty Result!") + .setMessage("Current picture is empty, please shutting it again!") + .setCancelable(true) + .show(); + } + } + }); + + } + }).start(); + } + + private void copyBitmapFromCamera(Bitmap ARGB8888ImageBitmap) { + if (isShutterBitmapCopied || ARGB8888ImageBitmap == null) { + return; + } + if (!ARGB8888ImageBitmap.isRecycled()) { + synchronized (this) { + shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); + } + SystemClock.sleep(TIME_SLEEP_INTERVAL); + isShutterBitmapCopied = true; + } + } + + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == INTENT_CODE_PICK_IMAGE) { + if (resultCode == Activity.RESULT_OK) { + cameraPageView.setVisibility(View.GONE); + resultPageView.setVisibility(View.VISIBLE); + seekbarText.setText(resultNum + ""); + confidenceSeekbar.setProgress((int) (resultNum * 100)); + Uri uri = data.getData(); + String path = getRealPathFromURI(this, uri); + picBitmap = decodeBitmap(path, 720, 1280); + resultImage.setImageBitmap(picBitmap); + } } } @@ -126,29 +261,27 @@ public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { @Override public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { - String savedImagePath = ""; - synchronized (this) { - savedImagePath = OcrMainActivity.this.savedImagePath; + if (TYPE == BTN_SHUTTER) { + copyBitmapFromCamera(ARGB8888ImageBitmap); + return false; } boolean modified = false; - OCRResult result = predictor.predict(ARGB8888ImageBitmap, savedImagePath); + long tc = System.currentTimeMillis(); + OCRResult result = predictor.predict(ARGB8888ImageBitmap, true); + timeElapsed += (System.currentTimeMillis() - tc); + Visualize.visOcr(ARGB8888ImageBitmap, result); modified = result.initialized(); - if (!savedImagePath.isEmpty()) { - synchronized (this) { - OcrMainActivity.this.savedImagePath = "result.jpg"; - } - } - lastFrameIndex++; - if (lastFrameIndex >= 30) { - final int fps = (int) (lastFrameIndex * 1e9 / (System.nanoTime() - lastFrameTime)); + frameCounter++; + if (frameCounter >= 30) { + final int fps = (int) (1000 / (timeElapsed / 30)); runOnUiThread(new Runnable() { @SuppressLint("SetTextI18n") public void run() { tvStatus.setText(Integer.toString(fps) + "fps"); } }); - lastFrameIndex = 0; - lastFrameTime = System.nanoTime(); + frameCounter = 0; + timeElapsed = 0; } return modified; } @@ -180,6 +313,7 @@ protected void onDestroy() { } public void initView() { + TYPE = REALTIME_DETECT; svPreview = (CameraSurfaceView) findViewById(R.id.sv_preview); svPreview.setOnTextureChangedListener(this); tvStatus = (TextView) findViewById(R.id.tv_status); @@ -193,6 +327,75 @@ public void initView() { realtimeToggleButton.setOnClickListener(this); backInPreview = findViewById(R.id.back_in_preview); backInPreview.setOnClickListener(this); + albumSelectButton = findViewById(R.id.iv_select); + albumSelectButton.setOnClickListener(this); + cameraPageView = findViewById(R.id.camera_page); + resultPageView = findViewById(R.id.result_page); + resultImage = findViewById(R.id.result_image); + backInResult = findViewById(R.id.back_in_result); + backInResult.setOnClickListener(this); + confidenceSeekbar = findViewById(R.id.confidence_seekbar); + seekbarText = findViewById(R.id.seekbar_text); + resultView = findViewById(R.id.result_list_view); + + confidenceSeekbar.setMax(100); + confidenceSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + float resultConfidence = seekBar.getProgress() / 100f; + BigDecimal bd = new BigDecimal(resultConfidence); + resultNum = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); + seekbarText.setText(resultNum + ""); + confidenceSeekbar.setProgress((int) (resultNum * 100)); + results.clear(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + runOnUiThread(new Runnable() { + @Override + public void run() { + if (TYPE == ALBUM_SELECT) { + SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); + detail(picBitmap); + } else { + SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); + svPreview.onPause(); + detail(shutterBitmap); + } + } + }); + } + }); + } + + private void detail(Bitmap bitmap) { + OCRResult result = predictor.predict(bitmap, true); + if (texts == null) { + texts = result.mText; + } + if (recScores == null) { + recScores = result.mRecScores; + } + initialized = result.initialized(); + if (initialized) { + for (int i = 0; i < texts.length; i++) { + if (recScores[i] > resultNum) { + results.add(new BaseResultModel(i + 1, texts[i], recScores[i])); + } + } + } + BaseResultAdapter adapter = new BaseResultAdapter(getBaseContext(), R.layout.ocr_result_page_item, results); + resultView.setAdapter(adapter); + resultView.invalidate(); + + resultImage.setImageBitmap(bitmap); + resultNum = 1.0f; } @SuppressLint("ApplySharedPref") @@ -215,9 +418,9 @@ public void checkAndUpdateSettings() { String realDetModelDir = realModelDir + "/" + detModelName; String realClsModelDir = realModelDir + "/" + clsModelName; String realRecModelDir = realModelDir + "/" + recModelName; - String srcDetModelDir = OcrSettingsActivity.modelDir + "/" + detModelName; - String srcClsModelDir = OcrSettingsActivity.modelDir + "/" + clsModelName; - String srcRecModelDir = OcrSettingsActivity.modelDir + "/" + recModelName; + String srcDetModelDir = OcrSettingsActivity.modelDir + "/" + detModelName; + String srcClsModelDir = OcrSettingsActivity.modelDir + "/" + clsModelName; + String srcRecModelDir = OcrSettingsActivity.modelDir + "/" + recModelName; Utils.copyDirectoryFromAssets(this, srcDetModelDir, realDetModelDir); Utils.copyDirectoryFromAssets(this, srcClsModelDir, realClsModelDir); Utils.copyDirectoryFromAssets(this, srcRecModelDir, realRecModelDir); diff --git a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/segmentation/SegmentationMainActivity.java b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/segmentation/SegmentationMainActivity.java index 404252814d..57624477ab 100644 --- a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/segmentation/SegmentationMainActivity.java +++ b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/segmentation/SegmentationMainActivity.java @@ -1,8 +1,5 @@ package com.baidu.paddle.fastdeploy.app.examples.segmentation; -import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap; -import static com.baidu.paddle.fastdeploy.app.ui.Utils.getRealPathFromURI; - import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; @@ -31,16 +28,17 @@ import com.baidu.paddle.fastdeploy.RuntimeOption; import com.baidu.paddle.fastdeploy.app.examples.R; -import com.baidu.paddle.fastdeploy.app.ui.Utils; import com.baidu.paddle.fastdeploy.app.ui.view.CameraSurfaceView; import com.baidu.paddle.fastdeploy.app.ui.view.ResultListView; -import com.baidu.paddle.fastdeploy.app.ui.view.adapter.BaseResultAdapter; +import com.baidu.paddle.fastdeploy.app.ui.Utils; import com.baidu.paddle.fastdeploy.app.ui.view.model.BaseResultModel; import com.baidu.paddle.fastdeploy.vision.SegmentationResult; import com.baidu.paddle.fastdeploy.vision.Visualize; import com.baidu.paddle.fastdeploy.vision.segmentation.PaddleSegModel; -import java.io.File; +import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap; +import static com.baidu.paddle.fastdeploy.app.ui.Utils.getRealPathFromURI; + import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -64,11 +62,9 @@ public class SegmentationMainActivity extends Activity implements View.OnClickLi private SeekBar confidenceSeekbar; private TextView seekbarText; private float resultNum = 1.0f; - private ResultListView detectResultView; + private ResultListView resultView; private Bitmap shutterBitmap; - private Bitmap originShutterBitmap; private Bitmap picBitmap; - private Bitmap originPicBitmap; private boolean isShutterBitmapCopied = false; public static final int TYPE_UNKNOWN = -1; @@ -81,12 +77,12 @@ public class SegmentationMainActivity extends Activity implements View.OnClickLi private static final int INTENT_CODE_PICK_IMAGE = 100; private static final int TIME_SLEEP_INTERVAL = 50; // ms - String savedImagePath = "result.jpg"; long timeElapsed = 0; long frameCounter = 0; // Call 'init' and 'release' manually later PaddleSegModel predictor = new PaddleSegModel(); + private List results = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -120,6 +116,7 @@ public void onClick(View v) { case R.id.btn_shutter: TYPE = BTN_SHUTTER; shutterAndPauseCamera(); + resultView.setAdapter(null); break; case R.id.btn_settings: startActivity(new Intent(SegmentationMainActivity.this, SegmentationSettingsActivity.class)); @@ -141,17 +138,29 @@ public void onClick(View v) { intent.setType("image/*"); startActivityForResult(intent, INTENT_CODE_PICK_IMAGE); } + resultView.setAdapter(null); break; case R.id.back_in_result: - resultPageView.setVisibility(View.GONE); - cameraPageView.setVisibility(View.VISIBLE); - TYPE = REALTIME_DETECT; - isShutterBitmapCopied = false; - svPreview.onResume(); + back(); break; } } + @Override + public void onBackPressed() { + super.onBackPressed(); + back(); + } + + private void back() { + resultPageView.setVisibility(View.GONE); + cameraPageView.setVisibility(View.VISIBLE); + TYPE = REALTIME_DETECT; + isShutterBitmapCopied = false; + svPreview.onResume(); + results.clear(); + } + private void shutterAndPauseCamera() { new Thread(new Runnable() { @Override @@ -172,7 +181,7 @@ public void run() { seekbarText.setText(resultNum + ""); confidenceSeekbar.setProgress((int) (resultNum * 100)); if (shutterBitmap != null && !shutterBitmap.isRecycled()) { - resultImage.setImageBitmap(shutterBitmap); + detail(shutterBitmap); } else { new AlertDialog.Builder(SegmentationMainActivity.this) .setTitle("Empty Result!") @@ -193,7 +202,6 @@ private void copyBitmapFromCamera(Bitmap ARGB8888ImageBitmap) { if (!ARGB8888ImageBitmap.isRecycled()) { synchronized (this) { shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); - originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true); } SystemClock.sleep(TIME_SLEEP_INTERVAL); isShutterBitmapCopied = true; @@ -211,9 +219,10 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { confidenceSeekbar.setProgress((int) (resultNum * 100)); Uri uri = data.getData(); String path = getRealPathFromURI(this, uri); - picBitmap = decodeBitmap(path, 720, 1280); - originPicBitmap = picBitmap.copy(Bitmap.Config.ARGB_8888, true); - resultImage.setImageBitmap(picBitmap); + Bitmap bitmap = decodeBitmap(path, 720, 1280); + picBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); + SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms + detail(picBitmap); } } } @@ -244,27 +253,12 @@ public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) { copyBitmapFromCamera(ARGB8888ImageBitmap); return false; } - - String savedImagePath = ""; - synchronized (this) { - savedImagePath = Utils.getDCIMDirectory() + File.separator + "result.jpg"; - } - boolean modified = false; - long tc = System.currentTimeMillis(); SegmentationResult result = predictor.predict(ARGB8888ImageBitmap); timeElapsed += (System.currentTimeMillis() - tc); - Visualize.visSegmentation(ARGB8888ImageBitmap, result); - modified = result.initialized(); - if (!savedImagePath.isEmpty()) { - synchronized (this) { - SegmentationMainActivity.this.savedImagePath = "result.jpg"; - } - } - frameCounter++; if (frameCounter >= 30) { final int fps = (int) (1000 / (timeElapsed / 30)); @@ -308,16 +302,8 @@ protected void onDestroy() { public void initView() { TYPE = REALTIME_DETECT; - // For front face camera and human seg, the smaller width and height - // may get both better result and performance. EXPECTED_PREVIEW_HEIGHT - // should be 'width' and EXPECTED_PREVIEW_WIDTH should be 'height' if - // your camera display orientation is rotate (degree == 90 || degree == 270). - // The transformation will auto process in camera. - CameraSurfaceView.EXPECTED_PREVIEW_HEIGHT = 360; - CameraSurfaceView.EXPECTED_PREVIEW_WIDTH = 720; svPreview = (CameraSurfaceView) findViewById(R.id.sv_preview); svPreview.setOnTextureChangedListener(this); - svPreview.switchCamera(); // switch to front camera for human seg tvStatus = (TextView) findViewById(R.id.tv_status); btnSwitch = (ImageButton) findViewById(R.id.btn_switch); btnSwitch.setOnClickListener(this); @@ -338,16 +324,7 @@ public void initView() { backInResult.setOnClickListener(this); confidenceSeekbar = findViewById(R.id.confidence_seekbar); seekbarText = findViewById(R.id.seekbar_text); - detectResultView = findViewById(R.id.result_list_view); - - List results = new ArrayList<>(); - // TODO: add model results from SegmentationResult instead of using fake data. - results.add(new BaseResultModel(1, "human", 1.0f)); - results.add(new BaseResultModel(2, "human", 1.0f)); - results.add(new BaseResultModel(3, "human", 1.0f)); - final BaseResultAdapter adapter = new BaseResultAdapter(this, R.layout.facedet_result_page_item, results); - detectResultView.setAdapter(adapter); - detectResultView.invalidate(); + resultView = findViewById(R.id.result_list_view); confidenceSeekbar.setMax(100); confidenceSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @@ -358,6 +335,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { resultNum = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); seekbarText.setText(resultNum + ""); confidenceSeekbar.setProgress((int) (resultNum * 100)); + results.clear(); } @Override @@ -367,32 +345,29 @@ public void onStartTrackingTouch(SeekBar seekBar) { @Override public void onStopTrackingTouch(SeekBar seekBar) { - runOnUiThread(new Runnable() { - @Override - public void run() { - if (TYPE == ALBUM_SELECT) { - SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms - if (!picBitmap.isRecycled()) { - predictor.predict(picBitmap, true, resultNum); - resultImage.setImageBitmap(picBitmap); - picBitmap = originPicBitmap.copy(Bitmap.Config.ARGB_8888, true); - } - resultNum = 1.0f; - } else { - SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms - if (!shutterBitmap.isRecycled()) { - predictor.predict(shutterBitmap, true, resultNum); - resultImage.setImageBitmap(shutterBitmap); - shutterBitmap = originShutterBitmap.copy(Bitmap.Config.ARGB_8888, true); - } - resultNum = 1.0f; - } - } - }); +// runOnUiThread(new Runnable() { +// @Override +// public void run() { +// if (TYPE == ALBUM_SELECT) { +// SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms +// detail(picBitmap); +// } else { +// SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms +// svPreview.onPause(); +// detail(shutterBitmap); +// } +// } +// }); } }); } + private void detail(Bitmap bitmap) { + predictor.predict(bitmap, true, 0.6f); + resultImage.setImageBitmap(bitmap); + resultNum = 1.0f; + } + @SuppressLint("ApplySharedPref") public void initSettings() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -448,32 +423,5 @@ private boolean checkAllPermissions() { return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED; } -} - - - - - - - - - - - - - - - - - - - - - - - - - - - +} diff --git a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/ui/Utils.java b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/ui/Utils.java index 427da7f245..6da24c2f6a 100644 --- a/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/ui/Utils.java +++ b/java/android/app/src/main/java/com/baidu/paddle/fastdeploy/app/ui/Utils.java @@ -16,12 +16,16 @@ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.util.ArrayList; import java.util.List; public class Utils { @@ -286,4 +290,24 @@ public static String getRealPathFromURI(Context context, Uri contentURI) { } return result; } + + public static List readTxt(String txtPath) { + File file = new File(txtPath); + if (file.isFile() && file.exists()) { + try { + FileInputStream fileInputStream = new FileInputStream(file); + InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String text; + List labels = new ArrayList<>(); + while ((text = bufferedReader.readLine()) != null) { + labels.add(text); + } + return labels; + } catch (Exception e) { + e.printStackTrace(); + } + } + return null; + } } diff --git a/java/android/app/src/main/res/layout/classification_camera_page.xml b/java/android/app/src/main/res/layout/classification_camera_page.xml index 15b009e244..441c3e5911 100644 --- a/java/android/app/src/main/res/layout/classification_camera_page.xml +++ b/java/android/app/src/main/res/layout/classification_camera_page.xml @@ -64,7 +64,7 @@ android:layout_centerInParent="true" /> -