forked from mindspore-Ecosystem/mindspore
!8274 himindspore add net that can download new version apk and install
From: @sishuikang Reviewed-by: @zhang_xue_tong Signed-off-by:
This commit is contained in:
commit
bf1c08ba47
|
@ -51,6 +51,11 @@ android {
|
|||
packagingOptions{
|
||||
pickFirst 'lib/arm64-v8a/libmlkit-label-MS.so'
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
// Before gradle build.
|
||||
|
@ -69,4 +74,15 @@ dependencies {
|
|||
implementation 'com.sun.mail:android-mail:1.6.5'
|
||||
implementation 'com.sun.mail:android-activation:1.6.5'
|
||||
|
||||
|
||||
implementation 'com.trello.rxlifecycle2:rxlifecycle:2.2.2'
|
||||
implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.2'
|
||||
implementation 'io.reactivex.rxjava2:rxjava:2.1.1'
|
||||
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
|
||||
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
|
||||
|
||||
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
|
||||
implementation 'org.greenrobot:eventbus:3.0.0'
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEM" />
|
||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
|
@ -19,6 +21,10 @@
|
|||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<meta-data
|
||||
android:name="com.google.android.actions"
|
||||
android:resource="@xml/file_paths" />
|
||||
|
||||
<activity
|
||||
android:name=".SplashActivity"
|
||||
android:screenOrientation="portrait"
|
||||
|
@ -32,7 +38,7 @@
|
|||
<activity
|
||||
android:name=".imageclassification.ui.ImageMainActivity"
|
||||
android:screenOrientation="portrait"
|
||||
android:theme="@style/Theme.AppCompat.NoActionBar"/>
|
||||
android:theme="@style/Theme.AppCompat.NoActionBar" />
|
||||
<activity
|
||||
android:name=".contract.ContractActivity"
|
||||
android:screenOrientation="portrait"
|
||||
|
@ -53,6 +59,16 @@
|
|||
android:name=".imageclassification.ui.ImageCameraActivity"
|
||||
android:screenOrientation="portrait"
|
||||
android:theme="@style/Theme.AppCompat.NoActionBar" />
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="com.mindspore.himindspore.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths" />
|
||||
</provider>
|
||||
</application>
|
||||
|
||||
</manifest>
|
|
@ -1,34 +1,51 @@
|
|||
package com.mindspore.himindspore;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
import com.mindspore.himindspore.imageclassification.ui.ImageCameraActivity;
|
||||
import com.mindspore.himindspore.base.BaseActivity;
|
||||
import com.mindspore.himindspore.imageclassification.ui.ImageMainActivity;
|
||||
import com.mindspore.himindspore.mvp.MainContract;
|
||||
import com.mindspore.himindspore.mvp.MainPresenter;
|
||||
import com.mindspore.himindspore.net.FileDownLoadObserver;
|
||||
import com.mindspore.himindspore.net.UpdateInfoBean;
|
||||
import com.mindspore.himindspore.objectdetection.ui.ObjectDetectionMainActivity;
|
||||
|
||||
public class SplashActivity extends AppCompatActivity implements View.OnClickListener {
|
||||
import java.io.File;
|
||||
|
||||
public class SplashActivity extends BaseActivity<MainPresenter> implements MainContract.View, View.OnClickListener {
|
||||
|
||||
private static final String TAG = "SplashActivity";
|
||||
private static final int REQUEST_PERMISSION = 1;
|
||||
|
||||
private Button btnImage, btnObject, btnContract,btnAdvice;
|
||||
private Button btnImage, btnObject, btnContract, btnAdvice;
|
||||
private boolean isHasPermssion;
|
||||
|
||||
private static final String CODE_URL ="https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/lite";
|
||||
private static final String HELP_URL ="https://github.com/mindspore-ai/mindspore/issues";
|
||||
private ProgressDialog progressDialog;
|
||||
|
||||
private static final String CODE_URL = "https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/lite";
|
||||
private static final String HELP_URL = "https://github.com/mindspore-ai/mindspore/issues";
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_splash);
|
||||
protected void init() {
|
||||
presenter = new MainPresenter(this);
|
||||
|
||||
btnImage = findViewById(R.id.btn_image);
|
||||
btnObject = findViewById(R.id.btn_object);
|
||||
|
@ -41,6 +58,12 @@ public class SplashActivity extends AppCompatActivity implements View.OnClickLis
|
|||
btnAdvice.setOnClickListener(this);
|
||||
|
||||
requestPermissions();
|
||||
getUpdateInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLayout() {
|
||||
return R.layout.activity_splash;
|
||||
}
|
||||
|
||||
private void requestPermissions() {
|
||||
|
@ -59,6 +82,11 @@ public class SplashActivity extends AppCompatActivity implements View.OnClickLis
|
|||
}
|
||||
}
|
||||
|
||||
private void getUpdateInfo() {
|
||||
presenter.getUpdateInfo();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (R.id.btn_image == view.getId()) {
|
||||
|
@ -75,7 +103,7 @@ public class SplashActivity extends AppCompatActivity implements View.OnClickLis
|
|||
}
|
||||
} else if (R.id.btn_contact == view.getId()) {
|
||||
openBrowser(CODE_URL);
|
||||
}else if (R.id.btn_advice == view.getId()) {
|
||||
} else if (R.id.btn_advice == view.getId()) {
|
||||
openBrowser(HELP_URL);
|
||||
}
|
||||
}
|
||||
|
@ -87,4 +115,134 @@ public class SplashActivity extends AppCompatActivity implements View.OnClickLis
|
|||
intent.setData(uri);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showUpdateResult(UpdateInfoBean bean) {
|
||||
showUpdate(bean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showFail(String s) {
|
||||
|
||||
}
|
||||
|
||||
public void downSuccess() {
|
||||
if (progressDialog != null && progressDialog.isShowing()) {
|
||||
progressDialog.dismiss();
|
||||
}
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setIcon(android.R.drawable.ic_dialog_info);
|
||||
builder.setTitle("下载完成");
|
||||
builder.setMessage("是否安装");
|
||||
builder.setCancelable(false);
|
||||
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
Uri contentUri = FileProvider.getUriForFile(SplashActivity.this, "com.mindspore.himindspore.fileprovider",
|
||||
new File(getApkPath(), "HiMindSpore.apk"));
|
||||
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
|
||||
} else {
|
||||
intent.setDataAndType(Uri.fromFile(new File(getApkPath(), "HiMindSpore.apk")), "application/vnd.android.package-archive");
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
}
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
}
|
||||
});
|
||||
builder.create().show();
|
||||
}
|
||||
|
||||
|
||||
private int now_version;
|
||||
|
||||
public void showUpdate(final UpdateInfoBean updateInfo) {
|
||||
try {
|
||||
PackageManager packageManager = this.getPackageManager();
|
||||
PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0);
|
||||
now_version = packageInfo.versionCode;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (now_version == updateInfo.getVersionCode()) {
|
||||
Toast.makeText(this, "已经是最新版本", Toast.LENGTH_SHORT).show();
|
||||
Log.d(TAG + "版本号是", "onResponse: " + now_version);
|
||||
} else {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setIcon(android.R.drawable.ic_dialog_info);
|
||||
builder.setTitle("请升级新版本" + updateInfo.getVersionName());
|
||||
builder.setMessage(updateInfo.getMessage());
|
||||
builder.setCancelable(false);
|
||||
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Log.e(TAG, String.valueOf(Environment.MEDIA_MOUNTED));
|
||||
downFile();
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
}
|
||||
});
|
||||
builder.create().show();
|
||||
}
|
||||
}
|
||||
|
||||
public void downFile() {
|
||||
progressDialog = new ProgressDialog(this);
|
||||
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
||||
progressDialog.setTitle("正在下载");
|
||||
progressDialog.setMessage("请稍候...");
|
||||
progressDialog.setProgressNumberFormat("%1d Mb/%2d Mb");
|
||||
progressDialog.setProgress(0);
|
||||
progressDialog.show();
|
||||
presenter.downloadApk(getApkPath(), "HiMindSpore.apk", new FileDownLoadObserver<File>() {
|
||||
@Override
|
||||
public void onDownLoadSuccess(File file) {
|
||||
downSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDownLoadFail(Throwable throwable) {
|
||||
Toast.makeText(SplashActivity.this, "下载失败", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgress(final int progress, final long total) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
progressDialog.setMax((int) total / 1024 / 1024);
|
||||
progressDialog.setProgress(progress);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
Log.d(TAG, "downFile: ");
|
||||
}
|
||||
|
||||
public String getApkPath() {
|
||||
String directoryPath = "";
|
||||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
||||
directoryPath = getExternalFilesDir("apk").getAbsolutePath();
|
||||
} else {
|
||||
directoryPath = getFilesDir() + File.separator + "apk";
|
||||
}
|
||||
File file = new File(directoryPath);
|
||||
Log.e("测试路径", directoryPath);
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
return directoryPath;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.mindspore.himindspore.base;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
|
||||
public abstract class BaseActivity<T extends BasePresenter> extends Activity {
|
||||
|
||||
protected T presenter;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstance) {
|
||||
super.onCreate(savedInstance);
|
||||
setContentView(getLayout());
|
||||
init();
|
||||
}
|
||||
|
||||
protected abstract void init();
|
||||
|
||||
public abstract int getLayout();
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.mindspore.himindspore.base;
|
||||
|
||||
public abstract class BasePresenter<T extends BaseActivity> {
|
||||
protected T view;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.mindspore.himindspore.base;
|
||||
|
||||
public interface TrackListener {
|
||||
}
|
|
@ -37,8 +37,8 @@ import androidx.annotation.NonNull;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import com.mindspore.himindspore.base.TrackListener;
|
||||
import com.mindspore.himindspore.imageclassification.help.GarbageTrackingMobile;
|
||||
import com.mindspore.himindspore.track.TrackListener;
|
||||
import com.mindspore.himindspore.imageclassification.help.ImageTrackingMobile;
|
||||
import com.mindspore.himindspore.objectdetection.help.ObjectTrackingMobile;
|
||||
|
||||
|
@ -283,7 +283,7 @@ public class CameraPreview extends TextureView {
|
|||
if (null != track) {
|
||||
garbageTrackingMobile = (GarbageTrackingMobile) track;
|
||||
}
|
||||
}else if (OPEN_TYPE_OBJECT == openType) {
|
||||
} else if (OPEN_TYPE_OBJECT == openType) {
|
||||
if (null != track) {
|
||||
objectTrackingMobile = (ObjectTrackingMobile) track;
|
||||
}
|
||||
|
@ -308,7 +308,7 @@ public class CameraPreview extends TextureView {
|
|||
mBackgroundThread.start();
|
||||
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
|
||||
|
||||
if (OPEN_TYPE_IMAGE == openType || OPEN_TYPE_OBJECT == openType || OPEN_TYPE_IMAGE_CUSTOM == openType ) {
|
||||
if (OPEN_TYPE_IMAGE == openType || OPEN_TYPE_OBJECT == openType || OPEN_TYPE_IMAGE_CUSTOM == openType) {
|
||||
mImageBackgroundThread = new HandlerThread("MINDSPORE");
|
||||
mImageBackgroundThread.start();
|
||||
mImageBackgroundHandler = new Handler(mImageBackgroundThread.getLooper());
|
||||
|
@ -330,12 +330,12 @@ public class CameraPreview extends TextureView {
|
|||
if (bitmap != null) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
// The current bitmap performs the sending request identification operation
|
||||
String ret ="";
|
||||
if (OPEN_TYPE_IMAGE == openType){
|
||||
String ret = "";
|
||||
if (OPEN_TYPE_IMAGE == openType) {
|
||||
ret = null == imageTrackingMobile ? "" : imageTrackingMobile.MindSpore_runnet(bitmap);
|
||||
}else if(OPEN_TYPE_IMAGE_CUSTOM == openType){
|
||||
} else if (OPEN_TYPE_IMAGE_CUSTOM == openType) {
|
||||
ret = null == garbageTrackingMobile ? "" : garbageTrackingMobile.MindSpore_runnet(bitmap);
|
||||
}else if(OPEN_TYPE_OBJECT == openType){
|
||||
} else if (OPEN_TYPE_OBJECT == openType) {
|
||||
ret = null == objectTrackingMobile ? "" : objectTrackingMobile.MindSpore_runnet(bitmap);
|
||||
}
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
@ -348,7 +348,7 @@ public class CameraPreview extends TextureView {
|
|||
}
|
||||
}
|
||||
if (mImageBackgroundHandler != null && !isPreBackgroundThreadPause) {
|
||||
mImageBackgroundHandler.postDelayed(classifyRunnable,1000);
|
||||
mImageBackgroundHandler.postDelayed(classifyRunnable, 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -17,11 +17,10 @@
|
|||
package com.mindspore.himindspore.imageclassification.help;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mindspore.himindspore.track.TrackListener;
|
||||
import com.mindspore.himindspore.base.TrackListener;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
@ -61,7 +60,7 @@ public class GarbageTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* Running model.
|
||||
*
|
||||
*
|
||||
* @param netEnv Inference environment address.
|
||||
* @param img A picture to be inferred.
|
||||
* @return Inference result
|
||||
|
@ -70,7 +69,7 @@ public class GarbageTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* Unbind model data.
|
||||
*
|
||||
*
|
||||
* @param netEnv Inference environment address.
|
||||
* @return Unbound state.
|
||||
*/
|
||||
|
@ -78,14 +77,14 @@ public class GarbageTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* The C++ side is encapsulated into a method of the MSNetWorks class
|
||||
*
|
||||
*
|
||||
* @param modelPath Model file location
|
||||
* @return Load model file status
|
||||
*/
|
||||
public boolean loadModelFromBuf(String modelPath) {
|
||||
ByteBuffer buffer = loadModelFile(modelPath);
|
||||
netEnv = loadModel(buffer, 2); //numThread's default setting is 2.
|
||||
if (netEnv == 0){ // Loading model failed.
|
||||
if (netEnv == 0) { // Loading model failed.
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -112,7 +111,7 @@ public class GarbageTrackingMobile implements TrackListener {
|
|||
/**
|
||||
* Load model file stream.
|
||||
* @param modelPath Model file path.
|
||||
* @return Model ByteBuffer.
|
||||
* @return Model ByteBuffer.
|
||||
*/
|
||||
public ByteBuffer loadModelFile(String modelPath) {
|
||||
InputStream is = null;
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -20,7 +20,7 @@ import android.content.Context;
|
|||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mindspore.himindspore.track.TrackListener;
|
||||
import com.mindspore.himindspore.base.TrackListener;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
@ -60,7 +60,7 @@ public class ImageTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* Running model.
|
||||
*
|
||||
*
|
||||
* @param netEnv Inference environment address.
|
||||
* @param img A picture to be inferred.
|
||||
* @return Inference result
|
||||
|
@ -69,7 +69,7 @@ public class ImageTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* Unbind model data.
|
||||
*
|
||||
*
|
||||
* @param netEnv Inference environment address.
|
||||
* @return Unbound state.
|
||||
*/
|
||||
|
@ -77,14 +77,14 @@ public class ImageTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* The C++ side is encapsulated into a method of the MSNetWorks class
|
||||
*
|
||||
*
|
||||
* @param modelPath Model file location
|
||||
* @return Load model file status
|
||||
*/
|
||||
public boolean loadModelFromBuf(String modelPath) {
|
||||
ByteBuffer buffer = loadModelFile(modelPath);
|
||||
netEnv = loadModel(buffer, 2); //numThread's default setting is 2.
|
||||
if (netEnv == 0){ // Loading model failed.
|
||||
if (netEnv == 0) { // Loading model failed.
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ public class ImageTrackingMobile implements TrackListener {
|
|||
/**
|
||||
* Load model file stream.
|
||||
* @param modelPath Model file path.
|
||||
* @return Model ByteBuffer.
|
||||
* @return Model ByteBuffer.
|
||||
*/
|
||||
public ByteBuffer loadModelFile(String modelPath) {
|
||||
InputStream is = null;
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
|
|
@ -23,9 +23,7 @@ import android.util.Log;
|
|||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
@ -65,10 +63,10 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
|
|||
private GarbageTrackingMobile garbageTrackingMobile;
|
||||
|
||||
@Override
|
||||
protected void onCreate( Bundle savedInstanceState) {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_image_camera);
|
||||
enterType = getIntent().getIntExtra(OPEN_TYPE,TYPE_DEMO);
|
||||
enterType = getIntent().getIntExtra(OPEN_TYPE, TYPE_DEMO);
|
||||
|
||||
cameraPreview = findViewById(R.id.image_camera_preview);
|
||||
bottomLayout = findViewById(R.id.layout_bottom_content);
|
||||
|
@ -77,12 +75,12 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
|
|||
}
|
||||
|
||||
private void init() {
|
||||
if(enterType == TYPE_DEMO) {
|
||||
if (enterType == TYPE_DEMO) {
|
||||
mTrackingMobile = new ImageTrackingMobile(this);
|
||||
String modelPath = "model/mobilenetv2.ms";
|
||||
boolean ret = mTrackingMobile.loadModelFromBuf(modelPath);
|
||||
Log.d(TAG, "Loading model return value: " + ret);
|
||||
}else {
|
||||
} else {
|
||||
garbageTrackingMobile = new GarbageTrackingMobile(this);
|
||||
String garbageModelPath = "model/garbage_mobilenetv2.ms";
|
||||
boolean garbageRet = garbageTrackingMobile.loadModelFromBuf(garbageModelPath);
|
||||
|
@ -95,9 +93,9 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
|
|||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if(enterType == TYPE_DEMO) {
|
||||
if (enterType == TYPE_DEMO) {
|
||||
cameraPreview.onResume(this, CameraPreview.OPEN_TYPE_IMAGE, mTrackingMobile);
|
||||
}else{
|
||||
} else {
|
||||
cameraPreview.onResume(this, CameraPreview.OPEN_TYPE_IMAGE_CUSTOM, garbageTrackingMobile);
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +103,7 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
|
|||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
cameraPreview.onPause();
|
||||
cameraPreview.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -124,7 +122,7 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
|
|||
|
||||
@Override
|
||||
public void onRecognitionDataCallBack(final String result, final String time) {
|
||||
if(enterType == TYPE_DEMO) {
|
||||
if (enterType == TYPE_DEMO) {
|
||||
if (recognitionObjectBeanList != null) {
|
||||
recognitionObjectBeanList.clear();
|
||||
} else {
|
||||
|
@ -175,10 +173,10 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
|
|||
horTextView.setLeftTitle(bean.getName());
|
||||
horTextView.setRightContent(String.format("%.2f", (100 * bean.getScore())) + "%");
|
||||
horTextView.setBottomLineVisible(View.VISIBLE);
|
||||
if (classNum == 1){
|
||||
if (classNum == 1) {
|
||||
horTextView.getTvLeftTitle().setTextColor(getResources().getColor(R.color.text_blue));
|
||||
horTextView.getTvRightContent().setTextColor(getResources().getColor(R.color.text_blue));
|
||||
}else{
|
||||
} else {
|
||||
horTextView.getTvLeftTitle().setTextColor(getResources().getColor(R.color.white));
|
||||
horTextView.getTvRightContent().setTextColor(getResources().getColor(R.color.white));
|
||||
}
|
||||
|
@ -212,7 +210,7 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
|
|||
}
|
||||
}
|
||||
|
||||
private void showLoadView(){
|
||||
private void showLoadView() {
|
||||
TextView textView = new TextView(this);
|
||||
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
textView.setText("Keep moving.");
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.mindspore.himindspore.imageclassification.ui;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.mindspore.himindspore.R;
|
||||
|
||||
public class ImageMainActivity extends AppCompatActivity {
|
||||
|
@ -19,8 +19,8 @@ public class ImageMainActivity extends AppCompatActivity {
|
|||
findViewById(R.id.btn_demo).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Intent intent = new Intent(ImageMainActivity.this,ImageCameraActivity.class);
|
||||
intent.putExtra(ImageCameraActivity.OPEN_TYPE,ImageCameraActivity.TYPE_DEMO);
|
||||
Intent intent = new Intent(ImageMainActivity.this, ImageCameraActivity.class);
|
||||
intent.putExtra(ImageCameraActivity.OPEN_TYPE, ImageCameraActivity.TYPE_DEMO);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
@ -28,8 +28,8 @@ public class ImageMainActivity extends AppCompatActivity {
|
|||
findViewById(R.id.btn_custom).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Intent intent = new Intent(ImageMainActivity.this,ImageCameraActivity.class);
|
||||
intent.putExtra(ImageCameraActivity.OPEN_TYPE,ImageCameraActivity.TYPE_CUSTOM);
|
||||
Intent intent = new Intent(ImageMainActivity.this, ImageCameraActivity.class);
|
||||
intent.putExtra(ImageCameraActivity.OPEN_TYPE, ImageCameraActivity.TYPE_CUSTOM);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.mindspore.himindspore.mvp;
|
||||
|
||||
import com.mindspore.himindspore.net.FileDownLoadObserver;
|
||||
import com.mindspore.himindspore.net.UpdateInfoBean;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface MainContract {
|
||||
|
||||
interface View {
|
||||
void showUpdateResult(UpdateInfoBean object);
|
||||
|
||||
void showFail(String s);
|
||||
|
||||
}
|
||||
|
||||
interface Presenter {
|
||||
void getUpdateInfo();
|
||||
|
||||
void downloadApk(String destDir, String fileName, FileDownLoadObserver<File> fileDownLoadObserver);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.mindspore.himindspore.mvp;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.mindspore.himindspore.SplashActivity;
|
||||
import com.mindspore.himindspore.base.BasePresenter;
|
||||
import com.mindspore.himindspore.net.FileDownLoadObserver;
|
||||
import com.mindspore.himindspore.net.RetrofitHelper;
|
||||
import com.mindspore.himindspore.net.UpdateInfoBean;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.annotations.NonNull;
|
||||
import io.reactivex.functions.Function;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class MainPresenter extends BasePresenter<SplashActivity> implements MainContract.Presenter {
|
||||
|
||||
private static final String TAG = "MainPresenter";
|
||||
private RetrofitHelper retrofitHelper;
|
||||
|
||||
public MainPresenter(SplashActivity mainActivity) {
|
||||
this.view = mainActivity;
|
||||
retrofitHelper = new RetrofitHelper();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getUpdateInfo() {
|
||||
retrofitHelper.getUpdateInfo().enqueue(new Callback<UpdateInfoBean>() {
|
||||
@Override
|
||||
public void onResponse(Call<UpdateInfoBean> call, Response<UpdateInfoBean> response) {
|
||||
Log.i(TAG, "onResponse" + response.toString());
|
||||
view.showUpdateResult(response.body());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<UpdateInfoBean> call, Throwable t) {
|
||||
Log.e(TAG, "onFailure" + t.toString());
|
||||
view.showFail(call.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadApk(final String destDir, final String fileName, final FileDownLoadObserver<File> fileDownLoadObserver) {
|
||||
retrofitHelper.downlaodApk()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(Schedulers.io())
|
||||
.observeOn(Schedulers.computation())
|
||||
.map(new Function<ResponseBody, File>() {
|
||||
@Override
|
||||
public File apply(@NonNull ResponseBody responseBody) throws Exception {
|
||||
return fileDownLoadObserver.saveFile(responseBody, destDir, fileName);
|
||||
}
|
||||
})
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(fileDownLoadObserver);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.mindspore.himindspore.net;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import io.reactivex.observers.DefaultObserver;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
public abstract class FileDownLoadObserver<T> extends DefaultObserver<T> {
|
||||
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
onDownLoadSuccess(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
onDownLoadFail(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
}
|
||||
|
||||
public abstract void onDownLoadSuccess(T t);
|
||||
|
||||
public abstract void onDownLoadFail(Throwable throwable);
|
||||
|
||||
public abstract void onProgress(int progress, long total);
|
||||
|
||||
|
||||
public File saveFile(ResponseBody responseBody, String destFileDir, String destFileName) throws IOException {
|
||||
InputStream is = null;
|
||||
byte[] buf = new byte[2048];
|
||||
int len = 0;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
is = responseBody.byteStream();
|
||||
final long total = responseBody.contentLength();
|
||||
long sum = 0;
|
||||
|
||||
File dir = new File(destFileDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
File file = new File(dir, destFileName);
|
||||
fos = new FileOutputStream(file);
|
||||
while ((len = is.read(buf)) != -1) {
|
||||
sum += len;
|
||||
fos.write(buf, 0, len);
|
||||
final long finalSum = sum;
|
||||
onProgress((int) (finalSum * 100 / total), total);
|
||||
}
|
||||
fos.flush();
|
||||
|
||||
return file;
|
||||
|
||||
} finally {
|
||||
try {
|
||||
if (is != null) is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
if (fos != null) fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.mindspore.himindspore.net;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class RetrofitHelper {
|
||||
public static final String MS_BASE_HOST = "https://download.mindspore.cn/model_zoo/official/lite/apk/";
|
||||
|
||||
private RetrofitService retrofitService;
|
||||
|
||||
|
||||
public RetrofitHelper() {
|
||||
|
||||
OkHttpClient httpClient = new OkHttpClient.Builder()
|
||||
.retryOnConnectionFailure(true)
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl(MS_BASE_HOST)
|
||||
.client(httpClient)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.build();
|
||||
|
||||
retrofitService = retrofit.create(RetrofitService.class);
|
||||
}
|
||||
|
||||
|
||||
public Call<UpdateInfoBean> getUpdateInfo() {
|
||||
return retrofitService.getUpdateInfo();
|
||||
}
|
||||
|
||||
public Observable<ResponseBody> downlaodApk() {
|
||||
return retrofitService.downloadApk();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.mindspore.himindspore.net;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Streaming;
|
||||
|
||||
public interface RetrofitService {
|
||||
|
||||
@GET("version.json")
|
||||
Call<UpdateInfoBean> getUpdateInfo();
|
||||
|
||||
@Streaming
|
||||
@GET("himindsporedemo.apk")
|
||||
Observable<ResponseBody> downloadApk();
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.mindspore.himindspore.net;
|
||||
|
||||
public class UpdateInfoBean {
|
||||
|
||||
private int versionCode;
|
||||
private String versionName;
|
||||
private int forceUpdate;
|
||||
private String create_time;
|
||||
private String url;
|
||||
private String md5;
|
||||
private String message;
|
||||
|
||||
public int getVersionCode() {
|
||||
return versionCode;
|
||||
}
|
||||
|
||||
public void setVersionCode(int versionCode) {
|
||||
this.versionCode = versionCode;
|
||||
}
|
||||
|
||||
public String getVersionName() {
|
||||
return versionName;
|
||||
}
|
||||
|
||||
public void setVersionName(String versionName) {
|
||||
this.versionName = versionName;
|
||||
}
|
||||
|
||||
public int getForceUpdate() {
|
||||
return forceUpdate;
|
||||
}
|
||||
|
||||
public void setForceUpdate(int forceUpdate) {
|
||||
this.forceUpdate = forceUpdate;
|
||||
}
|
||||
|
||||
public String getCreate_time() {
|
||||
return create_time;
|
||||
}
|
||||
|
||||
public void setCreate_time(String create_time) {
|
||||
this.create_time = create_time;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getMd5() {
|
||||
return md5;
|
||||
}
|
||||
|
||||
public void setMd5(String md5) {
|
||||
this.md5 = md5;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
|
@ -15,7 +15,7 @@ public class RecognitionObjectBean {
|
|||
private float right;
|
||||
private float bottom;
|
||||
|
||||
private RecognitionObjectBean(Builder builder){
|
||||
private RecognitionObjectBean(Builder builder) {
|
||||
this.rectID = builder.rectID;
|
||||
this.imgID = builder.imgID;
|
||||
this.objectName = builder.objectName;
|
||||
|
@ -37,7 +37,7 @@ public class RecognitionObjectBean {
|
|||
private float right;
|
||||
private float bottom;
|
||||
|
||||
public RecognitionObjectBean build(){
|
||||
public RecognitionObjectBean build() {
|
||||
return new RecognitionObjectBean(this);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import android.content.res.AssetManager;
|
|||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mindspore.himindspore.track.TrackListener;
|
||||
import com.mindspore.himindspore.base.TrackListener;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
@ -40,24 +40,24 @@ public class ObjectTrackingMobile implements TrackListener {
|
|||
* jni Load model
|
||||
*
|
||||
* @param assetManager assetManager
|
||||
* @param buffer buffer
|
||||
* @param numThread numThread
|
||||
* @param buffer buffer
|
||||
* @param numThread numThread
|
||||
* @return Load model data
|
||||
*/
|
||||
public native long loadModel(AssetManager assetManager, ByteBuffer buffer, int numThread);
|
||||
|
||||
/**
|
||||
* jni Run model
|
||||
*
|
||||
*
|
||||
* @param netEnv Load model data
|
||||
* @param img Current picture
|
||||
* @param img Current picture
|
||||
* @return Run model data
|
||||
*/
|
||||
public native String runNet(long netEnv, Bitmap img);
|
||||
|
||||
/**
|
||||
* Unbind model data
|
||||
*
|
||||
*
|
||||
* @param netEnv model data
|
||||
* @return Unbound state
|
||||
*/
|
||||
|
@ -65,7 +65,7 @@ public class ObjectTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* C++ encapsulated as a method of the msnetworks class
|
||||
*
|
||||
*
|
||||
* @param assetManager Model file location
|
||||
* @return Loading model file status
|
||||
*/
|
||||
|
@ -79,7 +79,7 @@ public class ObjectTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* Run Mindspore
|
||||
*
|
||||
*
|
||||
* @param img Current image recognition
|
||||
* @return Recognized text information
|
||||
*/
|
||||
|
@ -90,6 +90,7 @@ public class ObjectTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* Unbound model
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
public boolean unloadModel() {
|
||||
|
@ -99,8 +100,9 @@ public class ObjectTrackingMobile implements TrackListener {
|
|||
|
||||
/**
|
||||
* Load model file stream
|
||||
*
|
||||
* @param modelPath Model file path
|
||||
* @return Load model file stream
|
||||
* @return Load model file stream
|
||||
*/
|
||||
public ByteBuffer loadModelFile(String modelPath) {
|
||||
InputStream is = null;
|
||||
|
|
|
@ -8,7 +8,6 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||
|
||||
import com.mindspore.himindspore.R;
|
||||
import com.mindspore.himindspore.camera.CameraPreview;
|
||||
import com.mindspore.himindspore.imageclassification.help.ImageTrackingMobile;
|
||||
import com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean;
|
||||
import com.mindspore.himindspore.objectdetection.help.ObjectTrackingMobile;
|
||||
|
||||
|
@ -20,7 +19,7 @@ import static com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBe
|
|||
|
||||
/**
|
||||
* main page of entrance
|
||||
*
|
||||
* <p>
|
||||
* Pass in pictures to JNI, test mindspore model, load reasoning, etc
|
||||
*/
|
||||
|
||||
|
@ -29,7 +28,7 @@ public class ObjectCameraActivity extends AppCompatActivity implements CameraPre
|
|||
private final String TAG = "ObjectCameraActivity";
|
||||
|
||||
private CameraPreview cameraPreview;
|
||||
|
||||
|
||||
private ObjectTrackingMobile mTrackingMobile;
|
||||
|
||||
private ObjectRectView mObjectRectView;
|
||||
|
@ -64,7 +63,7 @@ public class ObjectCameraActivity extends AppCompatActivity implements CameraPre
|
|||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
cameraPreview.onResume(this,CameraPreview.OPEN_TYPE_OBJECT,mTrackingMobile);
|
||||
cameraPreview.onResume(this, CameraPreview.OPEN_TYPE_OBJECT, mTrackingMobile);
|
||||
|
||||
}
|
||||
|
||||
|
@ -80,7 +79,7 @@ public class ObjectCameraActivity extends AppCompatActivity implements CameraPre
|
|||
mObjectRectView.clearCanvas();
|
||||
return;
|
||||
}
|
||||
Log.d(TAG,result);
|
||||
Log.d(TAG, result);
|
||||
recognitionObjectBeanList = getRecognitionList(result);
|
||||
mObjectRectView.setInfo(recognitionObjectBeanList);
|
||||
}
|
||||
|
|
|
@ -17,11 +17,10 @@ import android.widget.ImageView;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
|
||||
import com.mindspore.himindspore.R;
|
||||
import com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean;
|
||||
import com.mindspore.himindspore.objectdetection.help.BitmapUtils;
|
||||
import com.mindspore.himindspore.objectdetection.help.ObjectTrackingMobile;
|
||||
import com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean;
|
||||
import com.mindspore.himindspore.utils.DisplayUtil;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
@ -33,7 +32,7 @@ import static com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBe
|
|||
public class ObjectPhotoActivity extends AppCompatActivity {
|
||||
|
||||
private static final String TAG = "ObjectPhotoActivity";
|
||||
private static final int[] COLORS ={R.color.white,R.color.text_blue,R.color.text_yellow,R.color.text_orange,R.color.text_green};
|
||||
private static final int[] COLORS = {R.color.white, R.color.text_blue, R.color.text_yellow, R.color.text_orange, R.color.text_green};
|
||||
|
||||
private static final int RC_CHOOSE_PHOTO = 1;
|
||||
|
||||
|
@ -61,7 +60,7 @@ public class ObjectPhotoActivity extends AppCompatActivity {
|
|||
|
||||
}
|
||||
|
||||
private void openGallay(){
|
||||
private void openGallay() {
|
||||
Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
|
||||
intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
|
||||
startActivityForResult(intentToPickPic, RC_CHOOSE_PHOTO);
|
||||
|
@ -76,14 +75,16 @@ public class ObjectPhotoActivity extends AppCompatActivity {
|
|||
this.imageUri = data.getData();
|
||||
showOriginImage();
|
||||
}
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
private void showOriginImage(){
|
||||
private void showOriginImage() {
|
||||
Pair<Integer, Integer> targetedSize = this.getTargetSize();
|
||||
int targetWidth = targetedSize.first;
|
||||
int maxHeight = targetedSize.second;
|
||||
originBitmap = BitmapUtils.loadFromPath(ObjectPhotoActivity.this, imageUri, targetWidth, maxHeight);
|
||||
originBitmap = BitmapUtils.loadFromPath(ObjectPhotoActivity.this, imageUri, targetWidth, maxHeight);
|
||||
// Determine how much to scale down the image.
|
||||
Log.i(ObjectPhotoActivity.TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight());
|
||||
|
||||
|
@ -113,8 +114,8 @@ public class ObjectPhotoActivity extends AppCompatActivity {
|
|||
String result = trackingMobile.MindSpore_runnet(bitmap);
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
Log.d(TAG, "RUNNET CONSUMING:"+(endTime-startTime)+"ms");
|
||||
Log.d(TAG, "result:"+ result);
|
||||
Log.d(TAG, "RUNNET CONSUMING:" + (endTime - startTime) + "ms");
|
||||
Log.d(TAG, "result:" + result);
|
||||
|
||||
recognitionObjectBeanList = getRecognitionList(result);
|
||||
|
||||
|
@ -126,29 +127,31 @@ public class ObjectPhotoActivity extends AppCompatActivity {
|
|||
private void drawRect(Bitmap bitmap) {
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mPaint.setTextSize(DisplayUtil.sp2px(this,16));
|
||||
mPaint.setTextSize(DisplayUtil.sp2px(this, 16));
|
||||
//Draw only outline (stroke)
|
||||
mPaint.setStyle(Paint.Style.STROKE);
|
||||
mPaint.setStrokeWidth(DisplayUtil.dip2px(this,2));
|
||||
mPaint.setStrokeWidth(DisplayUtil.dip2px(this, 2));
|
||||
|
||||
for (int i = 0; i < recognitionObjectBeanList.size(); i++) {
|
||||
RecognitionObjectBean objectBean = recognitionObjectBeanList.get(i);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(objectBean.getRectID()).append("_").append(objectBean.getObjectName()).append("_").append(String.format("%.2f", (100 * objectBean.getScore())) + "%");
|
||||
|
||||
int paintColor =getResources().getColor(COLORS[i % COLORS.length]);
|
||||
int paintColor = getResources().getColor(COLORS[i % COLORS.length]);
|
||||
mPaint.setColor(paintColor);
|
||||
|
||||
RectF rectF = new RectF(objectBean.getLeft(), objectBean.getTop(), objectBean.getRight(), objectBean.getBottom());
|
||||
canvas.drawRect(rectF, mPaint);
|
||||
canvas.drawText(sb.toString(),objectBean.getLeft(), objectBean.getTop()-10,mPaint);
|
||||
canvas.drawText(sb.toString(), objectBean.getLeft(), objectBean.getTop() - 10, mPaint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
trackingMobile.unloadModel();
|
||||
if (trackingMobile != null) {
|
||||
trackingMobile.unloadModel();
|
||||
}
|
||||
BitmapUtils.recycleBitmap(originBitmap);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.mindspore.himindspore.objectdetection.ui;
|
|||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Paint.Style;
|
||||
import android.graphics.RectF;
|
||||
|
@ -10,7 +9,6 @@ import android.util.AttributeSet;
|
|||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
|
||||
import com.mindspore.himindspore.R;
|
||||
import com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean;
|
||||
import com.mindspore.himindspore.utils.DisplayUtil;
|
||||
|
@ -20,7 +18,7 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* Rectangle drawing class for object detection
|
||||
*
|
||||
* <p>
|
||||
* 1. Canvas:Represents the canvas attached to the specified view and uses its method to draw various graphics
|
||||
* 2. Paint:Represents the brush on canvas and is used to set brush color, brush thickness, fill style, etc
|
||||
*/
|
||||
|
@ -38,31 +36,31 @@ public class ObjectRectView extends View {
|
|||
private Context context;
|
||||
|
||||
public ObjectRectView(Context context) {
|
||||
this(context,null);
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public ObjectRectView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs,0);
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public ObjectRectView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
this.context =context;
|
||||
this.context = context;
|
||||
initialize();
|
||||
}
|
||||
|
||||
|
||||
private static final int[] MyColor ={R.color.white,R.color.text_blue,R.color.text_yellow,R.color.text_orange,R.color.text_green};
|
||||
private static final int[] MyColor = {R.color.white, R.color.text_blue, R.color.text_yellow, R.color.text_orange, R.color.text_green};
|
||||
|
||||
|
||||
private void initialize() {
|
||||
mObjRectF = new RectF();
|
||||
|
||||
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mPaint.setTextSize(DisplayUtil.sp2px(context,16));
|
||||
mPaint.setTextSize(DisplayUtil.sp2px(context, 16));
|
||||
//Draw only outline (stroke)
|
||||
mPaint.setStyle(Style.STROKE);
|
||||
mPaint.setStrokeWidth(DisplayUtil.dip2px(context,2));
|
||||
mPaint.setStrokeWidth(DisplayUtil.dip2px(context, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,14 +69,14 @@ public class ObjectRectView extends View {
|
|||
* @param recognitions
|
||||
*/
|
||||
public void setInfo(List<RecognitionObjectBean> recognitions) {
|
||||
Log.i(TAG, "setInfo: "+recognitions.size());
|
||||
Log.i(TAG, "setInfo: " + recognitions.size());
|
||||
|
||||
mRecognitions.clear();
|
||||
mRecognitions.addAll(recognitions);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void clearCanvas(){
|
||||
public void clearCanvas() {
|
||||
mRecognitions.clear();
|
||||
invalidate();
|
||||
}
|
||||
|
@ -90,7 +88,7 @@ public class ObjectRectView extends View {
|
|||
if (mRecognitions == null || mRecognitions.size() == 0) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0;i<mRecognitions.size();i++){
|
||||
for (int i = 0; i < mRecognitions.size(); i++) {
|
||||
RecognitionObjectBean bean = mRecognitions.get(i);
|
||||
mPaint.setColor(context.getResources().getColor(MyColor[i % MyColor.length]));
|
||||
drawRect(bean, canvas);
|
||||
|
@ -98,14 +96,13 @@ public class ObjectRectView extends View {
|
|||
}
|
||||
|
||||
|
||||
|
||||
public void drawRect(RecognitionObjectBean bean, Canvas canvas) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(bean.getRectID()).append("_").append(bean.getObjectName()).append("_").append(String.format("%.2f", (100 * bean.getScore())) + "%");
|
||||
|
||||
mObjRectF = new RectF(bean.getLeft(), bean.getTop(), bean.getRight(), bean.getBottom());
|
||||
canvas.drawRect(mObjRectF, mPaint);
|
||||
canvas.drawText(sb.toString(), mObjRectF.left, mObjRectF.top - DisplayUtil.dip2px(context,10) , mPaint);
|
||||
canvas.drawText(sb.toString(), mObjRectF.left, mObjRectF.top - DisplayUtil.dip2px(context, 10), mPaint);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@ import android.content.Context;
|
|||
public class DisplayUtil {
|
||||
|
||||
private DisplayUtil() {
|
||||
/* cannot be instantiated */
|
||||
throw new UnsupportedOperationException("cannot be instantiated");
|
||||
}
|
||||
/* cannot be instantiated */
|
||||
throw new UnsupportedOperationException("cannot be instantiated");
|
||||
}
|
||||
|
||||
|
||||
public static int px2dip(Context context, float pxValue) {
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<external-path path="Android/data/com.mindspore.himindspore/" name="files_root" />
|
||||
|
||||
<files-path
|
||||
name="Android/data/com.mindspore.himindspore/"
|
||||
path="files_root">
|
||||
</files-path>
|
||||
</paths>
|
Loading…
Reference in New Issue