Browse Source

!11323 lite android目标检测demo优化

From: @zhouyifengCode
Reviewed-by: @zhanghaibo5,@hangangqiang
Signed-off-by: @zhanghaibo5
tags/v1.2.0-rc1
mindspore-ci-bot Gitee 4 years ago
parent
commit
bcba6d5fea
2 changed files with 86 additions and 18 deletions
  1. +76
    -18
      model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/objectdetect/CameraFragment.java
  2. +10
    -0
      model_zoo/official/lite/object_detection/app/src/main/res/layout/fragment_camera.xml

+ 76
- 18
model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/objectdetect/CameraFragment.java View File

@@ -38,12 +38,9 @@ import android.view.Surface;
import android.view.TextureView; import android.view.TextureView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import com.mindspore.hiobject.R; import com.mindspore.hiobject.R;
import com.mindspore.hiobject.help.RecognitionObjectBean; import com.mindspore.hiobject.help.RecognitionObjectBean;
import com.mindspore.hiobject.help.TrackingMobile; import com.mindspore.hiobject.help.TrackingMobile;
@@ -57,11 +54,17 @@ import java.util.List;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import static com.mindspore.hiobject.help.RecognitionObjectBean.getRecognitionList; import static com.mindspore.hiobject.help.RecognitionObjectBean.getRecognitionList;




public class CameraFragment extends Fragment { public class CameraFragment extends Fragment {


private long lasttime = System.currentTimeMillis();

/** /**
* Conversion from screen rotation to JPEG orientation. * Conversion from screen rotation to JPEG orientation.
*/ */
@@ -157,6 +160,11 @@ public class CameraFragment extends Fragment {
*/ */
private ObjectRectView mObjectRectView; private ObjectRectView mObjectRectView;


/**
* An {@link TextView} for display infer time.
*/
private TextView mTvInferInfo;

/** /**
* A {@link CameraCaptureSession } for ic_launcher preview. * A {@link CameraCaptureSession } for ic_launcher preview.
*/ */
@@ -215,6 +223,16 @@ public class CameraFragment extends Fragment {
*/ */
private Handler mBackgroundHandler, mPreBackgroundHandler; private Handler mBackgroundHandler, mPreBackgroundHandler;


/**
* A {@link Handler} for running tasks in the ui thread.
*/
private Handler mUiHandler;

/**
* A {@link Long} UI Thread ID.
*/
private Long mUiThreadId;

/** /**
* An {@link ImageReader} that handles still image capture. * An {@link ImageReader} that handles still image capture.
*/ */
@@ -404,6 +422,8 @@ public class CameraFragment extends Fragment {
public void onViewCreated(final View view, Bundle savedInstanceState) { public void onViewCreated(final View view, Bundle savedInstanceState) {
mTextureView = view.findViewById(R.id.texture); mTextureView = view.findViewById(R.id.texture);
mObjectRectView = view.findViewById(R.id.objRectView); mObjectRectView = view.findViewById(R.id.objRectView);
mTvInferInfo = view.findViewById(R.id.tv_infer_info);
mUiThreadId = Thread.currentThread().getId();
} }


@Override @Override
@@ -612,7 +632,9 @@ public class CameraFragment extends Fragment {
mPreBackgroundThread = new HandlerThread("AlgoBackground"); mPreBackgroundThread = new HandlerThread("AlgoBackground");
mPreBackgroundThread.start(); mPreBackgroundThread.start();
mPreBackgroundHandler = new Handler(mPreBackgroundThread.getLooper()); mPreBackgroundHandler = new Handler(mPreBackgroundThread.getLooper());
mPreBackgroundHandler.postDelayed(periodicClassify, 500);
mPreBackgroundHandler.postDelayed(periodicClassify, 5);

mUiHandler = new Handler();
} }




@@ -620,15 +642,12 @@ public class CameraFragment extends Fragment {


private Runnable periodicClassify = new Runnable() { private Runnable periodicClassify = new Runnable() {
public void run() { public void run() {
synchronized (CameraFragment.this) {
Bitmap bitmap = mTextureView.getBitmap();
if (bitmap != null) {
drawBitmapToRect(bitmap);
}
//重复请求
if (mPreBackgroundHandler != null && !isPreBackgroundThreadPause) {
mPreBackgroundHandler.postDelayed(periodicClassify, 500);
}
Bitmap bitmap = mTextureView.getBitmap();
if (bitmap != null) {
drawBitmapToRect(bitmap);
} else {
Log.d(TAG, "bitmap is null");
mPreBackgroundHandler.postDelayed(periodicClassify, 5);
} }
} }
}; };
@@ -704,22 +723,61 @@ public class CameraFragment extends Fragment {


private List<RecognitionObjectBean> recognitionObjectBeanList; private List<RecognitionObjectBean> recognitionObjectBeanList;


public boolean isPreFinished = true;

public void drawBitmapToRect(Bitmap bitmap) { public void drawBitmapToRect(Bitmap bitmap) {
if (!isPreFinished) {
return;
}
isPreFinished = false;

long stepDelayTime = (System.currentTimeMillis() - lasttime);
Log.d(TAG, "Step Delay time>>>" + stepDelayTime);

if (recognitionObjectBeanList != null) { if (recognitionObjectBeanList != null) {
recognitionObjectBeanList.clear(); recognitionObjectBeanList.clear();
} }


Log.d(TAG, "bitmap_w:" + bitmap.getWidth() + " bitmap_h:" + bitmap.getHeight());
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
String result = mTrackingMobile.MindSpore_runnet(bitmap); String result = mTrackingMobile.MindSpore_runnet(bitmap);
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
Log.d(TAG, "MindSpore_runnet:time>>>" + (endTime - startTime));
long inferTime = (endTime - startTime);
Log.d(TAG, "MindSpore_runnet:time>>>" + inferTime);
Log.d(TAG, "MindSpore_runnet:result>>>" + result); Log.d(TAG, "MindSpore_runnet:result>>>" + result);
isPreFinished = true;
lasttime = System.currentTimeMillis();
mPreBackgroundHandler.post(periodicClassify);
if (TextUtils.isEmpty(result)) { if (TextUtils.isEmpty(result)) {
mObjectRectView.clearCanvas(); mObjectRectView.clearCanvas();
return;
} else {
long startTimeMills = System.currentTimeMillis();
recognitionObjectBeanList = getRecognitionList(result);
long endTimeMills = System.currentTimeMillis();
Log.d(TAG, "getRecognitionList use time>>>" + (endTimeMills - startTimeMills));
long startDraw = System.currentTimeMillis();
mObjectRectView.setInfo(recognitionObjectBeanList);
setInferInfo(inferTime, stepDelayTime);
long endDraw = System.currentTimeMillis();
Log.d(TAG, "Draw time>>>" + (endDraw - startDraw));
}
}

private void runOnUiThread(Runnable runnable) {
if (Thread.currentThread().getId() != mUiThreadId) {
mUiHandler.post(runnable);
} else {
runnable.run();
} }
recognitionObjectBeanList = getRecognitionList(result);
mObjectRectView.setInfo(recognitionObjectBeanList);
}

private void setInferInfo(final long inferTime, final long stepDelayTime) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mTvInferInfo.setText("pre and infer time:" + inferTime + "ms Step_Delay_time:" + stepDelayTime+"ms");
}
});
} }






+ 10
- 0
model_zoo/official/lite/object_detection/app/src/main/res/layout/fragment_camera.xml View File

@@ -15,4 +15,14 @@
android:layout_height="match_parent" android:layout_height="match_parent"
/> />


<TextView
android:id="@+id/tv_infer_info"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:text=""
android:textSize="14dp"
android:textColor="#ff000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout> </RelativeLayout>

Loading…
Cancel
Save