[Bug Fix] fix CopyShutterBitmap and async camera pause (#617)

* [Backend] fix lite backend save model error

* [Backend] fixed typos

* [FlyCV] optimize the integration of FlyCV

* [cmake] close some tests options

* [cmake] close some test option

* [FlyCV] remove un-need warnings

* [FlyCV] remove un-need GetMat method

* [FlyCV] optimize FlyCV codes

* [cmake] remove un-need cmake function in examples/CMakelists

* [cmake] support gflags for Android

* [Android] Run button shutter in sub Ui Thread

* [Android] Update CameraSurfaceView

* [Android] Update Android SDK usage docs

* [Android] Add facedet Android app example

* [cmake] fix FastDeploy.cmake.in errors for Android

* [Doc] update SetProcLibCpuNumThreads API doc

* [Bug Fix] fix CopyShutterBitmap and async camera pause
This commit is contained in:
DefTruth
2022-11-17 10:59:47 +08:00
committed by GitHub
parent 9e6d916cad
commit bd53d48f9a
7 changed files with 413 additions and 29 deletions

View File

@@ -38,6 +38,7 @@ 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.vision.DetectionResult;
import com.baidu.paddle.fastdeploy.vision.Visualize;
import com.baidu.paddle.fastdeploy.vision.detection.PicoDet;
import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap;
@@ -72,6 +73,7 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe
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;
@@ -148,6 +150,7 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe
resultPageView.setVisibility(View.GONE);
cameraPageView.setVisibility(View.VISIBLE);
TYPE = REALTIME_DETECT;
isShutterBitmapCopied = false;
svPreview.onResume();
break;
}
@@ -189,7 +192,7 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe
}
private void copyBitmapFromCamera(Bitmap ARGB8888ImageBitmap) {
if (ARGB8888ImageBitmap == null) {
if (isShutterBitmapCopied || ARGB8888ImageBitmap == null) {
return;
}
if (!ARGB8888ImageBitmap.isRecycled()) {
@@ -197,7 +200,8 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe
shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true);
originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true);
}
SystemClock.sleep(TIME_SLEEP_INTERVAL); // 50ms
SystemClock.sleep(TIME_SLEEP_INTERVAL);
isShutterBitmapCopied = true;
}
}
@@ -253,10 +257,10 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe
boolean modified = false;
long tc = System.currentTimeMillis();
DetectionResult result = predictor.predict(
ARGB8888ImageBitmap, true, DetectionSettingsActivity.scoreThreshold);
DetectionResult result = predictor.predict(ARGB8888ImageBitmap);
timeElapsed += (System.currentTimeMillis() - tc);
frameCounter++;
Visualize.visDetection(ARGB8888ImageBitmap, result, DetectionSettingsActivity.scoreThreshold);
modified = result.initialized();
if (!savedImagePath.isEmpty()) {
@@ -265,6 +269,7 @@ public class DetectionMainActivity extends Activity implements View.OnClickListe
}
}
frameCounter++;
if (frameCounter >= 30) {
final int fps = (int) (1000 / (timeElapsed / 30));
runOnUiThread(new Runnable() {

View File

@@ -37,6 +37,7 @@ 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.vision.FaceDetectionResult;
import com.baidu.paddle.fastdeploy.vision.Visualize;
import com.baidu.paddle.fastdeploy.vision.facedet.SCRFD;
import static com.baidu.paddle.fastdeploy.app.ui.Utils.decodeBitmap;
@@ -65,12 +66,13 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
private ImageView backInResult;
private SeekBar confidenceSeekbar;
private TextView seekbarText;
private float resultConfThreshold = 1.0f;
private float resultNum = 1.0f;
private ResultListView detectResultView;
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;
@@ -147,6 +149,7 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
resultPageView.setVisibility(View.GONE);
cameraPageView.setVisibility(View.VISIBLE);
TYPE = REALTIME_DETECT;
isShutterBitmapCopied = false;
svPreview.onResume();
break;
}
@@ -169,8 +172,8 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
svPreview.onPause();
cameraPageView.setVisibility(View.GONE);
resultPageView.setVisibility(View.VISIBLE);
seekbarText.setText(resultConfThreshold + "");
confidenceSeekbar.setProgress((int) (resultConfThreshold * 100));
seekbarText.setText(resultNum + "");
confidenceSeekbar.setProgress((int) (resultNum * 100));
if (shutterBitmap != null && !shutterBitmap.isRecycled()) {
resultImage.setImageBitmap(shutterBitmap);
} else {
@@ -188,7 +191,7 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
}
private void copyBitmapFromCamera(Bitmap ARGB8888ImageBitmap) {
if (ARGB8888ImageBitmap == null) {
if (isShutterBitmapCopied || ARGB8888ImageBitmap == null) {
return;
}
if (!ARGB8888ImageBitmap.isRecycled()) {
@@ -197,6 +200,7 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888, true);
}
SystemClock.sleep(TIME_SLEEP_INTERVAL);
isShutterBitmapCopied = true;
}
}
@@ -207,8 +211,8 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
if (resultCode == Activity.RESULT_OK) {
cameraPageView.setVisibility(View.GONE);
resultPageView.setVisibility(View.VISIBLE);
seekbarText.setText(resultConfThreshold + "");
confidenceSeekbar.setProgress((int) (resultConfThreshold * 100));
seekbarText.setText(resultNum + "");
confidenceSeekbar.setProgress((int) (resultNum * 100));
Uri uri = data.getData();
String path = getRealPathFromURI(this, uri);
picBitmap = decodeBitmap(path, 720, 1280);
@@ -228,6 +232,7 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
isRealtimeStatusRunning = true;
realtimeToggleButton.setImageResource(R.drawable.realtime_start_btn);
tvStatus.setVisibility(View.GONE);
// Camera is still working but detecting loop is on pause.
svPreview.setOnTextureChangedListener(new CameraSurfaceView.OnTextureChangedListener() {
@Override
public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) {
@@ -253,10 +258,11 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
long tc = System.currentTimeMillis();
FaceDetectionResult result = predictor.predict(
ARGB8888ImageBitmap, true, FaceDetSettingsActivity.scoreThreshold, 0.4f);
ARGB8888ImageBitmap, FaceDetSettingsActivity.scoreThreshold, 0.4f);
timeElapsed += (System.currentTimeMillis() - tc);
frameCounter++;
Visualize.visFaceDetection(ARGB8888ImageBitmap, result);
modified = result.initialized();
if (!savedImagePath.isEmpty()) {
@@ -265,6 +271,7 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
}
}
frameCounter++;
if (frameCounter >= 30) {
final int fps = (int) (1000 / (timeElapsed / 30));
runOnUiThread(new Runnable() {
@@ -346,9 +353,9 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float resultConfidence = seekBar.getProgress() / 100f;
BigDecimal bd = new BigDecimal(resultConfidence);
resultConfThreshold = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
seekbarText.setText(resultConfThreshold + "");
confidenceSeekbar.setProgress((int) (resultConfThreshold * 100));
resultNum = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
seekbarText.setText(resultNum + "");
confidenceSeekbar.setProgress((int) (resultNum * 100));
}
@Override
@@ -364,19 +371,19 @@ public class FaceDetMainActivity extends Activity implements View.OnClickListene
if (TYPE == ALBUM_SELECT) {
SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms
if (!picBitmap.isRecycled()) {
predictor.predict(picBitmap, true, resultConfThreshold, 0.4f);
predictor.predict(picBitmap, true, resultNum, 0.4f);
resultImage.setImageBitmap(picBitmap);
picBitmap = originPicBitmap.copy(Bitmap.Config.ARGB_8888, true);
}
resultConfThreshold = 1.0f;
resultNum = 1.0f;
} else {
SystemClock.sleep(TIME_SLEEP_INTERVAL * 10); // 500ms
if (!shutterBitmap.isRecycled()) {
predictor.predict(shutterBitmap, true, resultConfThreshold, 0.4f);
predictor.predict(shutterBitmap, true, resultNum, 0.4f);
resultImage.setImageBitmap(shutterBitmap);
shutterBitmap = originShutterBitmap.copy(Bitmap.Config.ARGB_8888, true);
}
resultConfThreshold = 1.0f;
resultNum = 1.0f;
}
}
});

View File

@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
<include
layout="@layout/segmentation_camera_page"
android:id="@+id/camera_page"></include>
<include
layout="@layout/segmentation_result_page"
android:id="@+id/result_page"
android:visibility="gone"></include>
</FrameLayout>

View File

@@ -1,6 +1,159 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".segmentation.SegmentationMainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWindow">
<com.baidu.paddle.fastdeploy.app.ui.layout.ActionBarLayout
android:id="@+id/action_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/back_in_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="true"
android:paddingLeft="40px"
android:paddingTop="60px"
android:paddingRight="60px"
android:paddingBottom="40px"
android:src="@drawable/back_btn" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50px"
android:orientation="horizontal">
<TextView
android:id="@+id/action_takepicture_btn"
style="@style/action_btn_selected"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="@string/action_bar_take_photo"
android:textAlignment="center"
android:visibility="gone" />
<TextView
android:id="@+id/action_realtime_btn"
style="@style/action_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/action_bar_realtime"
android:textAlignment="center" />
</LinearLayout>
<com.baidu.paddle.fastdeploy.app.ui.view.CameraSurfaceView
android:id="@+id/sv_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/contral"
android:layout_below="@+id/action_bar_main"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/album_select"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="145dp"
android:background="@drawable/album_btn"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="60dp"
android:layout_marginRight="30dp"
android:textColor="@color/colorText"
android:textSize="@dimen/small_font_size" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/top_bar_height"
android:layout_alignParentTop="true"
android:background="@color/colorTopBar">
<ImageButton
android:id="@+id/btn_settings"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@null"
android:scaleType="fitXY"
android:src="@drawable/btn_settings" />
</RelativeLayout>
<LinearLayout
android:id="@+id/contral"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorBottomBar"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_bar_top_margin"
android:orientation="vertical"></LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/large_button_height">
<ImageButton
android:id="@+id/btn_switch"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
android:background="#00000000"
android:scaleType="fitXY"
android:src="@drawable/switch_side_btn" />
<ImageButton
android:id="@+id/btn_shutter"
android:layout_width="@dimen/large_button_width"
android:layout_height="@dimen/large_button_height"
android:layout_centerInParent="true"
android:background="@null"
android:scaleType="fitXY"
android:src="@drawable/take_picture_btn" />
<ImageView
android:id="@+id/realtime_toggle_btn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="60dp"
android:scaleType="fitXY"
android:src="@drawable/realtime_stop_btn" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_bar_bottom_margin"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

View File

@@ -1,6 +1,160 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<com.baidu.paddle.fastdeploy.app.ui.layout.ActionBarLayout
android:id="@+id/action_bar_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/back_in_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="true"
android:paddingLeft="40px"
android:paddingTop="60px"
android:paddingRight="60px"
android:paddingBottom="40px"
android:src="@drawable/back_btn" />
<TextView
android:id="@+id/model_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50px"
android:textColor="@color/textColor"
android:textSize="@dimen/action_btn_text_size" />
</com.baidu.paddle.fastdeploy.app.ui.layout.ActionBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="700px">
<ImageView
android:id="@+id/result_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bk_result_image_padding" />
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40px"
android:layout_marginTop="26px"
android:layout_marginBottom="20px"
android:text="@string/result_label"
android:textColor="@color/bk_black"
android:textSize="56px"
android:visibility="visible" />
<LinearLayout
android:id="@+id/result_seekbar_section"
android:layout_width="match_parent"
android:layout_height="130px"
android:layout_marginLeft="@dimen/result_list_padding_lr"
android:layout_marginRight="@dimen/result_list_padding_lr"
android:layout_marginBottom="@dimen/result_list_gap_width"
android:background="@drawable/result_page_border_section_bk"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:paddingLeft="30px"
android:text="@string/result_table_header_confidence"
android:textColor="@color/table_result_tableheader_text_color"
android:textSize="@dimen/result_list_view_text_size" />
<SeekBar
android:id="@+id/confidence_seekbar"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="6"
android:focusable="false"
android:maxHeight="8px"
android:progressDrawable="@drawable/seekbar_progress_result"
android:splitTrack="false"
android:thumb="@drawable/seekbar_handle" />
<TextView
android:id="@+id/seekbar_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingRight="30px"
android:textSize="@dimen/result_list_view_text_size"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/result_list_padding_lr"
android:layout_marginRight="@dimen/result_list_padding_lr"
android:layout_marginBottom="@dimen/result_list_gap_width"
android:background="@drawable/result_page_border_section_bk"
android:visibility="visible">
<TextView
style="@style/list_result_view_tablehead_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result_table_header_index"
android:textColor="@color/table_result_tableheader_text_color" />
<TextView
style="@style/list_result_view_tablehead_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result_table_header_name"
android:textColor="@color/table_result_tableheader_text_color" />
<TextView
style="@style/list_result_view_tablehead_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="right"
android:text="@string/result_table_header_confidence"
android:textColor="@color/table_result_tableheader_text_color" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15px"
android:paddingLeft="@dimen/result_list_padding_lr"
android:paddingRight="@dimen/result_list_padding_lr">
<com.baidu.paddle.fastdeploy.app.ui.view.ResultListView
android:id="@+id/result_list_view"
android:layout_width="match_parent"
android:layout_height="700px"
android:divider="#FFFFFF"
android:dividerHeight="@dimen/result_list_gap_width"></com.baidu.paddle.fastdeploy.app.ui.view.ResultListView>
</ScrollView>
</FrameLayout>
</LinearLayout>
</FrameLayout>

View File

@@ -1,6 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:background="@drawable/result_page_border_section_bk">
</android.support.constraint.ConstraintLayout>
<TextView
android:id="@+id/index"
style="@style/list_result_view_item_style"
android:layout_width="wrap_content"
android:layout_weight="0.2" />
<TextView
android:id="@+id/name"
style="@style/list_result_view_item_style"
android:layout_width="wrap_content"
android:layout_weight="0.6"
android:maxWidth="300px" />
<TextView
android:id="@+id/confidence"
style="@style/list_result_view_item_style"
android:layout_weight="0.2"
android:layout_width="wrap_content" />
</LinearLayout>

View File

@@ -1,4 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="@string/SEGMENTATION_MODEL_DIR_DEFAULT"
android:key="@string/CHOOSE_PRE_INSTALLED_MODEL_KEY"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="Choose Pre-Installed Models" />
<EditTextPreference
android:defaultValue="@string/SEGMENTATION_MODEL_DIR_DEFAULT"
android:key="@string/MODEL_DIR_KEY"
android:title="Model Dir" />
<ListPreference
android:defaultValue="@string/CPU_THREAD_NUM_DEFAULT"
android:entries="@array/cpu_thread_num_entries"
android:entryValues="@array/cpu_thread_num_values"
android:key="@string/CPU_THREAD_NUM_KEY"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="CPU Thread Num" />
<ListPreference
android:defaultValue="@string/CPU_POWER_MODE_DEFAULT"
android:entries="@array/cpu_power_mode_entries"
android:entryValues="@array/cpu_power_mode_values"
android:key="@string/CPU_POWER_MODE_KEY"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="CPU Power Mode" />
<EditTextPreference
android:key="@string/SCORE_THRESHOLD_KEY"
android:defaultValue="@string/SCORE_THRESHOLD_DEFAULT"
android:title="Score Threshold: (0.0, 1.0)" />
<ListPreference
android:defaultValue="@string/ENABLE_LITE_FP16_MODE_DEFAULT"
android:entries="@array/enable_lite_fp16_mode_entries"
android:entryValues="@array/enable_lite_fp16_mode_values"
android:key="@string/ENABLE_LITE_FP16_MODE_KEY"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="Enable Lite FP16" />
</PreferenceScreen>