diff --git a/model_zoo/official/lite/Himindspore/app/build.gradle b/model_zoo/official/lite/Himindspore/app/build.gradle
index 0d86b0e828..6ff55793c1 100644
--- a/model_zoo/official/lite/Himindspore/app/build.gradle
+++ b/model_zoo/official/lite/Himindspore/app/build.gradle
@@ -8,16 +8,15 @@ android {
applicationId "com.mindspore.himindspore"
minSdkVersion 21
targetSdkVersion 30
- versionCode 3
- versionName "1.1.1"
+ versionCode 4
+ versionName "1.1.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
- arguments = [moduleName: project.getName()]
+ arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
-
}
buildTypes {
@@ -69,10 +68,11 @@ dependencies {
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
implementation 'org.greenrobot:eventbus:3.0.0'
- implementation 'com.alibaba:arouter-api:1.2.1'
- annotationProcessor 'com.alibaba:arouter-compiler:1.1.2'
+ implementation 'com.alibaba:arouter-api:1.5.1'
+ annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'
implementation project(':posenet')
implementation project(':imageObject')
implementation project(':styletransfer')
+ implementation project(':segmentation')
}
diff --git a/model_zoo/official/lite/Himindspore/app/src/androidTest/java/com/mindspore/himindspore/ExampleInstrumentedTest.java b/model_zoo/official/lite/Himindspore/app/src/androidTest/java/com/mindspore/himindspore/ExampleInstrumentedTest.java
deleted file mode 100644
index 840c4d2eb4..0000000000
--- a/model_zoo/official/lite/Himindspore/app/src/androidTest/java/com/mindspore/himindspore/ExampleInstrumentedTest.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.mindspore.himindspore;
-
-import android.content.Context;
-
-import androidx.test.ext.junit.runners.AndroidJUnit4;
-import androidx.test.platform.app.InstrumentationRegistry;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import static org.junit.Assert.assertEquals;
-
-/**
- * Instrumented test, which will execute on an Android device.
- *
- * @see Testing documentation
- */
-@RunWith(AndroidJUnit4.class)
-public class ExampleInstrumentedTest {
- @Test
- public void useAppContext() {
- // Context of the app under test.
- Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
- assertEquals("com.mindspore.himindspore", appContext.getPackageName());
- }
-}
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/SplashActivity.java b/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/SplashActivity.java
index eb5fce60e8..a6669d60aa 100644
--- a/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/SplashActivity.java
+++ b/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/SplashActivity.java
@@ -206,6 +206,14 @@ public class SplashActivity extends BaseActivity implements MainC
}
}
+ public void onClickSegmentation(View view) {
+ if (isAllGranted) {
+ ARouter.getInstance().build("/segmentation/SegmentationMainActivity").navigation(this);
+ } else {
+ requestPermissions();
+ }
+ }
+
public void onClickSouceCode(View view) {
openBrowser(CODE_URL);
}
@@ -343,4 +351,5 @@ public class SplashActivity extends BaseActivity implements MainC
}
+
}
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_splash.xml b/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_splash.xml
index 20eda83547..764d97d1d6 100644
--- a/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_splash.xml
+++ b/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_splash.xml
@@ -14,7 +14,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:drawableTop="@drawable/logo"
- android:drawablePadding="30dp"
+ android:drawablePadding="20dp"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="@string/app_name"
@@ -133,11 +133,28 @@
app:layout_constraintEnd_toEndOf="@+id/btn_image_garbage"
app:layout_constraintWidth_percent="0.43" />
+
+
Style Transfer
Source CodeHelp And FeedBack
+ Image SegmentationPhotoCamera
diff --git a/model_zoo/official/lite/Himindspore/imageObject/build.gradle b/model_zoo/official/lite/Himindspore/imageObject/build.gradle
index c2825f0cd3..0aecb8a8f2 100644
--- a/model_zoo/official/lite/Himindspore/imageObject/build.gradle
+++ b/model_zoo/official/lite/Himindspore/imageObject/build.gradle
@@ -16,7 +16,7 @@ android {
consumerProguardFiles "consumer-rules.pro"
javaCompileOptions {
annotationProcessorOptions {
- arguments = [moduleName: project.getName()]
+ arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
externalNativeBuild {
@@ -77,6 +77,6 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
- implementation 'com.alibaba:arouter-api:1.2.1'
- annotationProcessor 'com.alibaba:arouter-compiler:1.1.2'
+ implementation 'com.alibaba:arouter-api:1.5.1'
+ annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'
}
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/posenet/build.gradle b/model_zoo/official/lite/Himindspore/posenet/build.gradle
index 72cd79902f..81cca371f7 100644
--- a/model_zoo/official/lite/Himindspore/posenet/build.gradle
+++ b/model_zoo/official/lite/Himindspore/posenet/build.gradle
@@ -13,7 +13,7 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
- arguments = [moduleName: project.getName()]
+ arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
@@ -59,13 +59,13 @@ apply from: 'download.gradle'
dependencies {
implementation project(':mindsporelibrary')
- implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
+
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
- implementation 'com.alibaba:arouter-api:1.2.1'
- annotationProcessor 'com.alibaba:arouter-compiler:1.1.2'
+ implementation 'com.alibaba:arouter-api:1.5.1'
+ annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'
}
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/posenet/download.gradle b/model_zoo/official/lite/Himindspore/posenet/download.gradle
index ee1f547fe0..f7a4ae2021 100644
--- a/model_zoo/official/lite/Himindspore/posenet/download.gradle
+++ b/model_zoo/official/lite/Himindspore/posenet/download.gradle
@@ -69,4 +69,4 @@ class DownloadUrlTask extends DefaultTask {
void download() {
ant.get(src: sourceUrl, dest: target)
}
-}
+}
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/mindsporelibrary/.gitignore b/model_zoo/official/lite/Himindspore/segmentation/.gitignore
similarity index 100%
rename from model_zoo/official/lite/Himindspore/mindsporelibrary/.gitignore
rename to model_zoo/official/lite/Himindspore/segmentation/.gitignore
diff --git a/model_zoo/official/lite/Himindspore/segmentation/build.gradle b/model_zoo/official/lite/Himindspore/segmentation/build.gradle
new file mode 100644
index 0000000000..6639bec178
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/build.gradle
@@ -0,0 +1,75 @@
+plugins {
+ id 'com.android.library'
+}
+
+android {
+ compileSdkVersion 30
+ buildToolsVersion "30.0.1"
+
+ defaultConfig {
+ minSdkVersion 21
+ targetSdkVersion 30
+ versionCode 1
+ versionName "1.0"
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
+ javaCompileOptions {
+ annotationProcessorOptions {
+ arguments = [AROUTER_MODULE_NAME: project.getName()]
+ }
+ }
+ }
+
+ aaptOptions {
+ noCompress "ms"
+ }
+
+ buildTypes {
+ release {
+ minifyEnabled false
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+ }
+ }
+
+ lintOptions {
+ checkReleaseBuilds false
+ // Or, if you prefer, you can continue to check for errors in release builds,
+ // but continue the build even when errors are found:
+ abortOnError false
+ }
+
+ repositories {
+ google()
+ jcenter()
+ mavenCentral()
+ maven { url "https://jitpack.io" }
+
+ flatDir {
+ dirs 'libs', '../mindsporelibrary/libs'
+ }
+ }
+
+
+ compileOptions {
+ sourceCompatibility JavaVersion.VERSION_1_8
+ targetCompatibility JavaVersion.VERSION_1_8
+ }
+}
+
+apply from: 'download.gradle'
+
+dependencies {
+ implementation 'androidx.appcompat:appcompat:1.2.0'
+ implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
+ implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
+ implementation 'com.google.android.material:material:1.2.1'
+ implementation 'androidx.legacy:legacy-support-v4:1.0.0'
+ androidTestImplementation 'androidx.test.ext:junit:1.1.2'
+ androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
+ implementation 'com.github.bumptech.glide:glide:4.11.0'
+ annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
+
+ implementation 'com.alibaba:arouter-api:1.5.1'
+ annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'
+ implementation project(':mindsporelibrary')
+
+}
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/download.gradle b/model_zoo/official/lite/Himindspore/segmentation/download.gradle
new file mode 100644
index 0000000000..f52f3cd3fc
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/download.gradle
@@ -0,0 +1,34 @@
+/**
+ * To download necessary library from HuaWei server.
+ * Including mindspore-lite .so file, minddata-lite .so file and model file.
+ * The libraries can be downloaded manually.
+ */
+def targetModelFile = "src/main/assets/segment_model.ms"
+def modelDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/mobile_segment_lite/segment_model.ms"
+
+task downloadModelFile(type: DownloadUrlTask) {
+ doFirst {
+ println "Downloading ${modelDownloadUrl}"
+ }
+ sourceUrl = "${modelDownloadUrl}"
+ target = file("${targetModelFile}")
+}
+
+if (file("src/main/assets/segment_model.ms").exists()) {
+ downloadModelFile.enabled = false
+}
+
+preBuild.dependsOn downloadModelFile
+
+class DownloadUrlTask extends DefaultTask {
+ @Input
+ String sourceUrl
+
+ @OutputFile
+ File target
+
+ @TaskAction
+ void download() {
+ ant.get(src: sourceUrl, dest: target)
+ }
+}
diff --git a/model_zoo/official/lite/Himindspore/segmentation/proguard-rules.pro b/model_zoo/official/lite/Himindspore/segmentation/proguard-rules.pro
new file mode 100644
index 0000000000..481bb43481
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/proguard-rules.pro
@@ -0,0 +1,21 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/AndroidManifest.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/AndroidManifest.xml
new file mode 100644
index 0000000000..0832464fed
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/AndroidManifest.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/OnBackgroundImageListener.java b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/OnBackgroundImageListener.java
new file mode 100644
index 0000000000..c50f900aba
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/OnBackgroundImageListener.java
@@ -0,0 +1,25 @@
+/**
+ * Copyright 2020 Huawei Technologies Co., Ltd
+ *
+ * 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.mindspore.imagesegmentation;
+
+import android.view.View;
+
+public interface OnBackgroundImageListener {
+ void onBackImageSelected(int position);
+
+ void onImageAdd(View view);
+
+}
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/SegmentationMainActivity.java b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/SegmentationMainActivity.java
new file mode 100644
index 0000000000..30987ada9b
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/SegmentationMainActivity.java
@@ -0,0 +1 @@
+/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mindspore.imagesegmentation;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.mindspore.imagesegmentation.help.BitmapUtils;
import com.mindspore.imagesegmentation.help.ModelTrackingResult;
import com.mindspore.imagesegmentation.help.TrackingMobile;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.ref.WeakReference;
@Route(path = "/segmentation/SegmentationMainActivity")
public class SegmentationMainActivity extends AppCompatActivity implements OnBackgroundImageListener {
private static final String TAG = "SegmentationMainActivity";
private static final int[] IMAGES = {R.drawable.img_001, R.drawable.img_002, R.drawable.img_003, R.drawable.img_004, R.drawable.img_005,
R.drawable.img_006, R.drawable.img_007, R.drawable.img_008, R.drawable.add};
private static final int RC_CHOOSE_PHOTO = 1;
private static final int RC_CHOOSE_PHOTO_FOR_BACKGROUND = 11;
private static final int RC_CHOOSE_CAMERA = 2;
private ImageView imgPreview;
private TextView textOriginImage;
public ProgressBar progressBar;
private Uri imageUri;
private Bitmap originBitmap, lastOriginBitmap;
private Integer maxWidthOfImage;
private Integer maxHeightOfImage;
private boolean isLandScape;
private TrackingMobile trackingMobile;
private int selectedPosition;
public boolean isRunningModel;
private Handler mHandler;
public ModelTrackingResult modelTrackingResult;
private Bitmap processedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.isLandScape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
setContentView(R.layout.activity_main);
init();
trackingMobile = new TrackingMobile(this);
}
private void init() {
imgPreview = findViewById(R.id.img_origin);
textOriginImage = findViewById(R.id.tv_image);
progressBar = findViewById(R.id.progress);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(new StyleRecyclerViewAdapter(this, IMAGES, this));
}
public void onClickPhoto(View view) {
openGallay(RC_CHOOSE_PHOTO);
textOriginImage.setVisibility(View.GONE);
}
public void onClickCamera(View view) {
openCamera();
textOriginImage.setVisibility(View.GONE);
}
public void onClickRecovery(View view) {
if (originBitmap != null) {
Glide.with(this).load(originBitmap).into(imgPreview);
} else {
Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("LongLogTag")
public void onClickSave(View view) {
if (this.processedImage == null) {
Log.e(TAG, "null processed image");
Toast.makeText(this.getApplicationContext(), R.string.no_pic_neededSave, Toast.LENGTH_SHORT).show();
} else {
BitmapUtils.saveToAlbum(getApplicationContext(), this.processedImage);
Toast.makeText(this.getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show();
}
}
private void openGallay(int request) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, request);
}
private void openCamera() {
Intent intentToTakePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String mTempPhotoPath = Environment.getExternalStorageDirectory() + File.separator + "photo.jpeg";
imageUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".fileprovider", new File(mTempPhotoPath));
intentToTakePhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intentToTakePhoto, RC_CHOOSE_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (RC_CHOOSE_PHOTO == requestCode) {
if (null != data && null != data.getData()) {
this.imageUri = data.getData();
showOriginImage();
} else {
finish();
}
} else if (RC_CHOOSE_PHOTO_FOR_BACKGROUND == requestCode) {
if (null != data && null != data.getData()) {
showCustomBack(data.getData());
} else {
finish();
}
} else if (RC_CHOOSE_CAMERA == requestCode) {
if (null != data && null != data.getData()) {
this.imageUri = data.getData();
showOriginImage();
}
showOriginCamera();
}
}
}
private void showOriginImage() {
Pair targetedSize = this.getTargetSize();
int targetWidth = targetedSize.first;
int maxHeight = targetedSize.second;
originBitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight);
// Determine how much to scale down the image.
Log.i(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight());
if (originBitmap != null) {
Glide.with(this).load(originBitmap).into(imgPreview);
}
}
private void showOriginCamera() {
try {
originBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Determine how much to scale down the image.
if (originBitmap != null) {
Glide.with(this).load(originBitmap).into(imgPreview);
}
}
private BitmapDrawable customBack;
private void showCustomBack(Uri imageUri) {
Pair targetedSize = this.getTargetSize();
int targetWidth = targetedSize.first;
int maxHeight = targetedSize.second;
Bitmap bitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight);
customBack = new BitmapDrawable(getResources(), bitmap);
changeBackground(false);
}
// Returns max width of image.
private Integer getMaxWidthOfImage() {
if (this.maxWidthOfImage == null) {
if (this.isLandScape) {
this.maxWidthOfImage = ((View) this.imgPreview.getParent()).getHeight();
} else {
this.maxWidthOfImage = ((View) this.imgPreview.getParent()).getWidth();
}
}
return this.maxWidthOfImage;
}
// Returns max height of image.
private Integer getMaxHeightOfImage() {
if (this.maxHeightOfImage == null) {
if (this.isLandScape) {
this.maxHeightOfImage = ((View) this.imgPreview.getParent()).getWidth();
} else {
this.maxHeightOfImage = ((View) this.imgPreview.getParent()).getHeight();
}
}
return this.maxHeightOfImage;
}
// Gets the targeted size(width / height).
private Pair getTargetSize() {
Integer targetWidth;
Integer targetHeight;
Integer maxWidth = this.getMaxWidthOfImage();
Integer maxHeight = this.getMaxHeightOfImage();
targetWidth = this.isLandScape ? maxHeight : maxWidth;
targetHeight = this.isLandScape ? maxWidth : maxHeight;
Log.i(TAG, "height:" + targetHeight + ",width:" + targetWidth);
return new Pair<>(targetWidth, targetHeight);
}
private void startRunningModel(boolean isDemo) {
if (!isRunningModel) {
if (originBitmap == null) {
Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
new Thread(() -> {
isRunningModel = true;
modelTrackingResult = trackingMobile.execut(originBitmap);
if (modelTrackingResult != null) {
isRunningModel = false;
lastOriginBitmap = originBitmap;
}
Looper.prepare();
mHandler = new MyHandler(SegmentationMainActivity.this, isDemo);
mHandler.sendEmptyMessage(1);
Looper.loop();
}).start();
} else {
Toast.makeText(this, "Previous Model still running", Toast.LENGTH_SHORT).show();
}
}
private static class MyHandler extends Handler {
private final WeakReference weakReference;
private final boolean isDemo;
public MyHandler(SegmentationMainActivity activity, boolean demo) {
weakReference = new WeakReference<>(activity);
this.isDemo = demo;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.i(TAG, "handleMessage: load");
SegmentationMainActivity activity = weakReference.get();
if (msg.what == 1) {
if (null != activity && null != activity.modelTrackingResult) {
activity.changeBackground(isDemo);
}
}
}
}
public void changeBackground(boolean isDemo) {
runOnUiThread(() -> {
if(null != modelTrackingResult && null !=modelTrackingResult.getBitmapMaskOnly()){
if (progressBar.getVisibility() == View.VISIBLE) {
progressBar.setVisibility(View.INVISIBLE);
}
Bitmap foreground = modelTrackingResult.getBitmapMaskOnly();
Matrix matrix = new Matrix();
matrix.setScale(0.7f, 0.7f);
foreground = Bitmap.createBitmap(foreground, 0, 0, foreground.getWidth(), foreground.getHeight(), matrix, false);
SegmentationMainActivity.this.imgPreview.setImageBitmap(foreground);
SegmentationMainActivity.this.imgPreview.setDrawingCacheEnabled(true);
SegmentationMainActivity.this.imgPreview.setBackground(isDemo ? getDrawable(IMAGES[selectedPosition]) : customBack);
SegmentationMainActivity.this.imgPreview.setImageBitmap(foreground);
SegmentationMainActivity. this.processedImage = Bitmap.createBitmap( SegmentationMainActivity.this.imgPreview.getDrawingCache());
SegmentationMainActivity.this.imgPreview.setDrawingCacheEnabled(false);
}else{
Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onBackImageSelected(int position) {
selectedPosition = position;
if (lastOriginBitmap == originBitmap) {
changeBackground(true);
} else {
startRunningModel(true);
}
}
@Override
public void onImageAdd(View view) {
if (originBitmap == null) {
Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show();
return;
}
if (lastOriginBitmap == originBitmap) {
openGallay(RC_CHOOSE_PHOTO_FOR_BACKGROUND);
} else {
startRunningModel(false);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
trackingMobile.free();
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
BitmapUtils.recycleBitmap(this.originBitmap, this.lastOriginBitmap, this.processedImage);
}
}
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/StyleRecyclerViewAdapter.java b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/StyleRecyclerViewAdapter.java
new file mode 100644
index 0000000000..1c1da01626
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/StyleRecyclerViewAdapter.java
@@ -0,0 +1,97 @@
+/**
+ * Copyright 2020 Huawei Technologies Co., Ltd
+ *
+ * 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.mindspore.imagesegmentation;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+
+import androidx.annotation.NonNull;
+import androidx.recyclerview.widget.RecyclerView;
+
+import com.bumptech.glide.Glide;
+
+public class StyleRecyclerViewAdapter extends RecyclerView.Adapter {
+
+ private final int[] IMAGES;
+ private final Context context;
+ private final OnBackgroundImageListener mListener;
+
+ public StyleRecyclerViewAdapter(Context context, int[] IMAGES, OnBackgroundImageListener mListener) {
+ this.IMAGES = IMAGES;
+ this.context = context;
+ this.mListener = mListener;
+ }
+
+ @NonNull
+ @Override
+ public StyleItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
+ View view = LayoutInflater.from(context)
+ .inflate(R.layout.image_item, parent, false);
+ return new StyleItemViewHolder(view);
+ }
+
+ @Override
+ public void onBindViewHolder(@NonNull StyleItemViewHolder holder, int position) {
+ Glide.with(context).
+ load(IMAGES[position]).
+ into(holder.getImageView());
+
+ View view = holder.getMView();
+ view.setTag(IMAGES[position]);
+ view.setOnClickListener(view1 -> {
+ if (mListener != null) {
+ if (IMAGES.length - 1 == position) {
+ mListener.onImageAdd(holder.getImageView());
+ } else {
+ mListener.onBackImageSelected(position);
+ }
+ }
+ });
+ }
+
+
+ @Override
+ public int getItemCount() {
+ return IMAGES == null ? 0 : IMAGES.length;
+ }
+
+
+ public class StyleItemViewHolder extends RecyclerView.ViewHolder {
+ private ImageView imageView;
+ private final View mView;
+
+ public final ImageView getImageView() {
+ return this.imageView;
+ }
+
+ public final void setImageView(ImageView imageView) {
+ this.imageView = imageView;
+ }
+
+ public final View getMView() {
+ return this.mView;
+ }
+
+ public StyleItemViewHolder(View mView) {
+ super(mView);
+ this.mView = mView;
+ this.imageView = mView.findViewById(R.id.image_view);
+ }
+ }
+}
diff --git a/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/help/ImageUtils.java b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/BitmapUtils.java
similarity index 53%
rename from model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/help/ImageUtils.java
rename to model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/BitmapUtils.java
index 04e4038b0d..68eab1fdb1 100644
--- a/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/help/ImageUtils.java
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/BitmapUtils.java
@@ -15,22 +15,22 @@
*/
package com.mindspore.imagesegmentation.help;
+import android.app.Activity;
import android.content.Context;
import android.content.Intent;
+import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
-import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.RectF;
+import android.media.ExifInterface;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
+import android.provider.MediaStore;
import android.util.Log;
-import androidx.annotation.NonNull;
-import androidx.exifinterface.media.ExifInterface;
-
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@@ -39,37 +39,154 @@ import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-public class ImageUtils {
+public class BitmapUtils {
+ private static final String TAG = "BitmapUtils";
+
+ public static void recycleBitmap(Bitmap... bitmaps) {
+ for (Bitmap bitmap : bitmaps) {
+ if (bitmap != null && !bitmap.isRecycled()) {
+ bitmap.recycle();
+ bitmap = null;
+ }
+ }
+ }
+
+ private static String getImagePath(Activity activity, Uri uri) {
+ String[] projection = {MediaStore.Images.Media.DATA};
+ Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
+ int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
+ cursor.moveToFirst();
+ return cursor.getString(columnIndex);
+ }
+
+ public static Bitmap loadFromPath(Activity activity, int id, int width, int height) {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ InputStream is = activity.getResources().openRawResource(id);
+ int sampleSize = calculateInSampleSize(options, width, height);
+ options.inSampleSize = sampleSize;
+ options.inJustDecodeBounds = false;
+ return zoomImage(BitmapFactory.decodeStream(is), width, height);
+ }
+
+ public static Bitmap loadFromPath(Activity activity, Uri uri, int width, int height) {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+
+ String path = getImagePath(activity, uri);
+ BitmapFactory.decodeFile(path, options);
+ int sampleSize = calculateInSampleSize(options, width, height);
+ options.inSampleSize = sampleSize;
+ options.inJustDecodeBounds = false;
+
+ Bitmap bitmap = zoomImage(BitmapFactory.decodeFile(path, options), width, height);
+ return rotateBitmap(bitmap, getRotationAngle(path));
+ }
+
+ private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
+ final int width = options.outWidth;
+ final int height = options.outHeight;
+ int inSampleSize = 1;
- private static final String TAG = "ImageUtils";
+ if (height > reqHeight || width > reqWidth) {
+ // Calculate height and required height scale.
+ final int heightRatio = Math.round((float) height / (float) reqHeight);
+ // Calculate width and required width scale.
+ final int widthRatio = Math.round((float) width / (float) reqWidth);
+ // Take the larger of the values.
+ inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
+ }
+ return inSampleSize;
+ }
+
+ // Scale pictures to screen width.
+ private static Bitmap zoomImage(Bitmap imageBitmap, int targetWidth, int maxHeight) {
+ float scaleFactor =
+ Math.max(
+ (float) imageBitmap.getWidth() / (float) targetWidth,
+ (float) imageBitmap.getHeight() / (float) maxHeight);
+ Bitmap resizedBitmap =
+ Bitmap.createScaledBitmap(
+ imageBitmap,
+ (int) (imageBitmap.getWidth() / scaleFactor),
+ (int) (imageBitmap.getHeight() / scaleFactor),
+ true);
+
+ return resizedBitmap;
+ }
+
+ /**
+ * Get the rotation angle of the photo.
+ *
+ * @param path photo path.
+ * @return angle.
+ */
+ public static int getRotationAngle(String path) {
+ int rotation = 0;
+ try {
+ ExifInterface exifInterface = new ExifInterface(path);
+ int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
+ switch (orientation) {
+ case ExifInterface.ORIENTATION_ROTATE_90:
+ rotation = 90;
+ break;
+ case ExifInterface.ORIENTATION_ROTATE_180:
+ rotation = 180;
+ break;
+ case ExifInterface.ORIENTATION_ROTATE_270:
+ rotation = 270;
+ break;
+ default:
+ break;
+ }
+ } catch (IOException e) {
+ Log.e(TAG, "Failed to get rotation: " + e.getMessage());
+ }
+ return rotation;
+ }
+
+ public static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
+ Matrix matrix = new Matrix();
+ matrix.postRotate(angle);
+ Bitmap result = null;
+ try {
+ result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
+ } catch (OutOfMemoryError e) {
+ Log.e(TAG, "Failed to rotate bitmap: " + e.getMessage());
+ }
+ if (result == null) {
+ return bitmap;
+ }
+ return result;
+ }
private static Matrix decodeExifOrientation(int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
- case ExifInterface.ORIENTATION_NORMAL:
- case ExifInterface.ORIENTATION_UNDEFINED:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_NORMAL:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_UNDEFINED:
break;
- case ExifInterface.ORIENTATION_ROTATE_90:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90F);
break;
- case ExifInterface.ORIENTATION_ROTATE_180:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180F);
break;
- case ExifInterface.ORIENTATION_ROTATE_270:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270F);
break;
- case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.postScale(-1F, 1F);
break;
- case ExifInterface.ORIENTATION_FLIP_VERTICAL:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.postScale(1F, -1F);
break;
- case ExifInterface.ORIENTATION_TRANSPOSE:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSPOSE:
matrix.postScale(-1F, 1F);
matrix.postRotate(270F);
break;
- case ExifInterface.ORIENTATION_TRANSVERSE:
+ case androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSVERSE:
matrix.postScale(-1F, 1F);
matrix.postRotate(90F);
break;
@@ -84,55 +201,6 @@ public class ImageUtils {
return matrix;
}
- public void setExifOrientation(@NonNull String filePath, @NonNull String value) {
- try {
- ExifInterface exif = new ExifInterface(filePath);
- exif.setAttribute(ExifInterface.TAG_ORIENTATION, value);
- exif.saveAttributes();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public int computeExifOrientation(int rotationDegrees, boolean mirrored) {
- if (rotationDegrees == 0 && !mirrored) {
- return ExifInterface.ORIENTATION_NORMAL;
- } else if (rotationDegrees == 0 && mirrored) {
- return ExifInterface.ORIENTATION_FLIP_HORIZONTAL;
- } else if (rotationDegrees == 180 && !mirrored) {
- return ExifInterface.ORIENTATION_ROTATE_180;
- } else if (rotationDegrees == 180 && mirrored) {
- return ExifInterface.ORIENTATION_FLIP_VERTICAL;
- } else if (rotationDegrees == 90 && !mirrored) {
- return ExifInterface.ORIENTATION_ROTATE_90;
- } else if (rotationDegrees == 90 && mirrored) {
- return ExifInterface.ORIENTATION_TRANSPOSE;
- } else if (rotationDegrees == 270 && !mirrored) {
- return ExifInterface.ORIENTATION_ROTATE_270;
- } else if (rotationDegrees == 270 && mirrored) {
- return ExifInterface.ORIENTATION_TRANSVERSE;
- } else {
- return ExifInterface.ORIENTATION_UNDEFINED;
- }
- }
-
- public static Bitmap decodeBitmap(@NonNull File file) {
- Bitmap finalBitmap = null;
- try {
- ExifInterface exif = new ExifInterface(file.getAbsolutePath());
-
- Matrix transformation = decodeExifOrientation(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_ROTATE_90));
-
- BitmapFactory.Options options = new BitmapFactory.Options();
- Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
- finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), transformation, true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return finalBitmap;
- }
-
-
public static Bitmap scaleBitmapAndKeepRatio(Bitmap targetBmp, int reqHeightInPixels, int reqWidthInPixels) {
if (targetBmp.getHeight() == reqHeightInPixels && targetBmp.getWidth() == reqWidthInPixels) {
return targetBmp;
@@ -154,16 +222,6 @@ public class ImageUtils {
);
}
- public static Bitmap loadBitmapFromResources(Context context, String path) {
- try {
- InputStream inputStream = context.getAssets().open(path);
- return BitmapFactory.decodeStream(inputStream);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
public static ByteBuffer bitmapToByteBuffer(Bitmap bitmapIn, int width, int height, float mean, float std) {
Bitmap bitmap = scaleBitmapAndKeepRatio(bitmapIn, width, height);
ByteBuffer inputImage = ByteBuffer.allocateDirect(1 * width * height * 3 * 4);
@@ -185,30 +243,6 @@ public class ImageUtils {
}
- public static Bitmap convertArrayToBitmap(float[][][][] imageArray, int imageWidth, int imageHeight) {
- Bitmap styledImage = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
-
- for (int x = 0; x < imageArray[0].length; x++) {
- for (int y = 0; y < imageArray[0][0].length; y++) {
-
- int color = Color.rgb((int) (imageArray[0][x][y][0] * (float) 255),
- (int) (imageArray[0][x][y][1] * (float) 255),
- (int) (imageArray[0][x][y][2] * (float) 255));
- // this y, x is in the correct order!!!
- styledImage.setPixel(y, x, color);
- }
- }
- return styledImage;
- }
-
- public Bitmap createEmptyBitmap(int imageWidth, int imageHeigth, int color) {
- Bitmap ret = Bitmap.createBitmap(imageWidth, imageHeigth, Bitmap.Config.RGB_565);
- if (color != 0) {
- ret.eraseColor(color);
- }
- return ret;
- }
-
// Save the picture to the system album and refresh it.
public static void saveToAlbum(final Context context, Bitmap bitmap) {
File file = null;
@@ -263,4 +297,4 @@ public class ImageUtils {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile())));
}
}
-}
\ No newline at end of file
+}
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/ModelTrackingResult.java b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/ModelTrackingResult.java
new file mode 100644
index 0000000000..8a3301d6b8
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/ModelTrackingResult.java
@@ -0,0 +1,81 @@
+/**
+ * Copyright 2020 Huawei Technologies Co., Ltd
+ *
+ * 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.mindspore.imagesegmentation.help;
+
+import android.graphics.Bitmap;
+
+import java.util.Set;
+
+public class ModelTrackingResult {
+ private Bitmap bitmapResult;
+ private Bitmap bitmapOriginal;
+ private Bitmap bitmapMaskOnly;
+ private String executionLog;
+ private Set itemsFound;
+
+ public ModelTrackingResult(Bitmap bitmapResult, Bitmap bitmapOriginal, Bitmap bitmapMaskOnly, String executionLog, Set itemsFound) {
+ this.bitmapResult = bitmapResult;
+ this.bitmapOriginal = bitmapOriginal;
+ this.bitmapMaskOnly = bitmapMaskOnly;
+ this.executionLog = executionLog;
+ this.itemsFound = itemsFound;
+ }
+
+ public Bitmap getBitmapResult() {
+ return bitmapResult;
+ }
+
+ public ModelTrackingResult setBitmapResult(Bitmap bitmapResult) {
+ this.bitmapResult = bitmapResult;
+ return this;
+ }
+
+ public Bitmap getBitmapOriginal() {
+ return bitmapOriginal;
+ }
+
+ public ModelTrackingResult setBitmapOriginal(Bitmap bitmapOriginal) {
+ this.bitmapOriginal = bitmapOriginal;
+ return this;
+ }
+
+ public Bitmap getBitmapMaskOnly() {
+ return bitmapMaskOnly;
+ }
+
+ public ModelTrackingResult setBitmapMaskOnly(Bitmap bitmapMaskOnly) {
+ this.bitmapMaskOnly = bitmapMaskOnly;
+ return this;
+ }
+
+ public String getExecutionLog() {
+ return executionLog;
+ }
+
+ public ModelTrackingResult setExecutionLog(String executionLog) {
+ this.executionLog = executionLog;
+ return this;
+ }
+
+ public Set getItemsFound() {
+ return itemsFound;
+ }
+
+ public ModelTrackingResult setItemsFound(Set itemsFound) {
+ this.itemsFound = itemsFound;
+ return this;
+ }
+}
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/TrackingMobile.java b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/TrackingMobile.java
new file mode 100644
index 0000000000..e892381c8e
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/java/com/mindspore/imagesegmentation/help/TrackingMobile.java
@@ -0,0 +1,218 @@
+/**
+ * Copyright 2020 Huawei Technologies Co., Ltd
+ *
+ * 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.mindspore.imagesegmentation.help;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.util.Log;
+
+import androidx.core.graphics.ColorUtils;
+
+import com.mindspore.lite.LiteSession;
+import com.mindspore.lite.MSTensor;
+import com.mindspore.lite.Model;
+import com.mindspore.lite.config.CpuBindMode;
+import com.mindspore.lite.config.DeviceType;
+import com.mindspore.lite.config.MSConfig;
+
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+
+public class TrackingMobile {
+ private static final String TAG = "TrackingMobile";
+
+ private static final String IMAGESEGMENTATIONMODEL = "segment_model.ms";
+ private static final int imageSize = 257;
+ public static final int NUM_CLASSES = 21;
+ private static final float IMAGE_MEAN = 127.5F;
+ private static final float IMAGE_STD = 127.5F;
+
+ public static final int[] segmentColors = new int[2];
+
+ private Bitmap maskBitmap;
+ private Bitmap resultBitmap;
+ private HashSet itemsFound = new HashSet();
+
+ private final Context mContext;
+
+ private MSConfig msConfig;
+ private LiteSession session;
+ private Model model;
+
+ public TrackingMobile(Context context) {
+ mContext = context;
+ init();
+ }
+
+ public void init() {
+ // Load the .ms model.
+ model = new Model();
+ if (!model.loadModel(mContext, IMAGESEGMENTATIONMODEL)) {
+ Log.e(TAG, "Load Model failed");
+ return;
+ }
+
+ // Create and init config.
+ msConfig = new MSConfig();
+ if (!msConfig.init(DeviceType.DT_CPU, 2, CpuBindMode.MID_CPU)) {
+ Log.e(TAG, "Init context failed");
+ return;
+ }
+
+ // Create the MindSpore lite session.
+ session = new LiteSession();
+ if (!session.init(msConfig)) {
+ Log.e(TAG, "Create session failed");
+ msConfig.free();
+ return;
+ }
+ msConfig.free();
+
+ // Complile graph.
+ if (!session.compileGraph(model)) {
+ Log.e(TAG, "Compile graph failed");
+ model.freeBuffer();
+ return;
+ }
+
+ // Note: when use model.freeBuffer(), the model can not be complile graph again.
+ model.freeBuffer();
+
+ }
+
+ public ModelTrackingResult execut(Bitmap bitmap) {
+ // Set input tensor values.
+ List inputs = session.getInputs();
+ if (inputs.size() != 1) {
+ Log.e(TAG, "inputs.size() != 1");
+ return null;
+ }
+
+ float resource_height = bitmap.getHeight();
+ float resource_weight = bitmap.getWidth();
+
+ Bitmap scaledBitmap = BitmapUtils.scaleBitmapAndKeepRatio(bitmap, imageSize, imageSize);
+ ByteBuffer contentArray = BitmapUtils.bitmapToByteBuffer(scaledBitmap, imageSize, imageSize, IMAGE_MEAN, IMAGE_STD);
+
+ MSTensor inTensor = inputs.get(0);
+ // int byteLen = (int) inTensor.size();
+ inTensor.setData(contentArray);
+
+ // Run graph to infer results.
+ if (!session.runGraph()) {
+ Log.e(TAG, "Run graph failed");
+ return null;
+ }
+
+ // Get output tensor values.
+ List tensorNames = session.getOutputTensorNames();
+ Map outputs = session.getOutputMapByTensor();
+ for (String tensorName : tensorNames) {
+ MSTensor output = outputs.get(tensorName);
+ if (output == null) {
+ Log.e(TAG, "Can not find output " + tensorName);
+ return null;
+ }
+ float[] results = output.getFloatData();
+ float[] result = new float[output.elementsNum()];
+
+ int batch = output.getShape()[0];
+ int channel = output.getShape()[1];
+ int weight = output.getShape()[2];
+ int hight = output.getShape()[3];
+ int plane = weight * hight;
+
+ for (int n = 0; n < batch; n++) {
+ for (int c = 0; c < channel; c++) {
+ for (int hw = 0; hw < plane; hw++) {
+ result[n * channel * plane + hw * channel + c] = results[n * channel * plane + c * plane + hw];
+ }
+ }
+ }
+ ByteBuffer bytebuffer_results = floatArrayToByteArray(result);
+
+ convertBytebufferMaskToBitmap(
+ bytebuffer_results, imageSize, imageSize, scaledBitmap,
+ segmentColors
+ );
+ //scaledBitmap resize成resource_height,resource_weight
+ scaledBitmap = BitmapUtils.scaleBitmapAndKeepRatio(scaledBitmap, (int) resource_height, (int) resource_weight);
+ resultBitmap = BitmapUtils.scaleBitmapAndKeepRatio(resultBitmap, (int) resource_height, (int) resource_weight);
+ maskBitmap = BitmapUtils.scaleBitmapAndKeepRatio(maskBitmap, (int) resource_height, (int) resource_weight);
+ }
+ return new ModelTrackingResult(resultBitmap, scaledBitmap, maskBitmap, this.formatExecutionLog(), itemsFound);
+ }
+
+ private static ByteBuffer floatArrayToByteArray(float[] floats) {
+ ByteBuffer buffer = ByteBuffer.allocate(4 * floats.length);
+ FloatBuffer floatBuffer = buffer.asFloatBuffer();
+ floatBuffer.put(floats);
+ return buffer;
+ }
+
+ private void convertBytebufferMaskToBitmap(ByteBuffer inputBuffer, int imageWidth,
+ int imageHeight, Bitmap backgroundImage, int[] colors) {
+ Bitmap.Config conf = Bitmap.Config.ARGB_8888;
+ maskBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf);
+ resultBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf);
+ Bitmap scaledBackgroundImage =
+ BitmapUtils.scaleBitmapAndKeepRatio(backgroundImage, imageWidth, imageHeight);
+ int[][] mSegmentBits = new int[imageWidth][imageHeight];
+ inputBuffer.rewind();
+ for (int y = 0; y < imageHeight; y++) {
+ for (int x = 0; x < imageWidth; x++) {
+ float maxVal = 0f;
+ mSegmentBits[x][y] = 0;
+ for (int i = 0; i < NUM_CLASSES; i++) {
+ float value = inputBuffer.getFloat((y * imageWidth * NUM_CLASSES + x * NUM_CLASSES + i) * 4);
+ if (i == 0 || value > maxVal) {
+ maxVal = value;
+ if (i == 15) {
+ mSegmentBits[x][y] = i;
+ } else {
+ mSegmentBits[x][y] = 0;
+ }
+ }
+ }
+ itemsFound.add(mSegmentBits[x][y]);
+
+ int newPixelColor = ColorUtils.compositeColors(
+ colors[mSegmentBits[x][y] == 0 ? 0 : 1],
+ scaledBackgroundImage.getPixel(x, y)
+ );
+ resultBitmap.setPixel(x, y, newPixelColor);
+ maskBitmap.setPixel(x, y, mSegmentBits[x][y] == 0 ? colors[0] : scaledBackgroundImage.getPixel(x, y));
+ }
+ }
+ }
+
+ // Note: we must release the memory at the end, otherwise it will cause the memory leak.
+ public void free() {
+ session.free();
+ model.free();
+ }
+
+
+ private final String formatExecutionLog() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Input Image Size: " + imageSize * imageSize + '\n');
+ return sb.toString();
+ }
+
+}
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-v24/ic_launcher_foreground.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-v24/ic_launcher_foreground.xml
new file mode 100644
index 0000000000..2b068d1146
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-v24/ic_launcher_foreground.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/add.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/add.jpg
new file mode 100644
index 0000000000..bdfe6d7518
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/add.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_001.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_001.jpg
new file mode 100644
index 0000000000..4b69593e25
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_001.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_002.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_002.jpg
new file mode 100644
index 0000000000..1a3d5cc100
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_002.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_003.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_003.jpg
new file mode 100644
index 0000000000..1d958a0d1f
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_003.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_004.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_004.jpg
new file mode 100644
index 0000000000..8d0cab6a99
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_004.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_005.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_005.jpg
new file mode 100644
index 0000000000..5337b50dad
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_005.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_006.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_006.jpg
new file mode 100644
index 0000000000..e28061a2ba
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_006.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_007.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_007.jpg
new file mode 100644
index 0000000000..74b046d50b
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_007.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_008.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_008.jpg
new file mode 100644
index 0000000000..3a5c7fdcc8
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_008.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_009.jpg b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_009.jpg
new file mode 100644
index 0000000000..d84b9fe27a
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/img_009.jpg differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/logo2.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/logo2.png
new file mode 100644
index 0000000000..c90f1dda43
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable-xxhdpi/logo2.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable/ic_launcher_background.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable/ic_launcher_background.xml
new file mode 100644
index 0000000000..07d5da9cbf
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable/ic_launcher_background.xml
@@ -0,0 +1,170 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable/progressbar.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable/progressbar.xml
new file mode 100644
index 0000000000..d48a929d88
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/drawable/progressbar.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/layout/activity_main.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/layout/activity_main.xml
new file mode 100644
index 0000000000..9b9305416a
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/layout/activity_main.xml
@@ -0,0 +1,141 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/layout/image_item.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/layout/image_item.xml
new file mode 100644
index 0000000000..37ccdaad8f
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/layout/image_item.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-hdpi/ic_launcher.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000000..a571e60098
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-hdpi/ic_launcher.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-hdpi/ic_launcher_round.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-hdpi/ic_launcher_round.png
new file mode 100644
index 0000000000..61da551c55
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-hdpi/ic_launcher_round.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-mdpi/ic_launcher.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000000..c41dd28531
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-mdpi/ic_launcher.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-mdpi/ic_launcher_round.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-mdpi/ic_launcher_round.png
new file mode 100644
index 0000000000..db5080a752
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-mdpi/ic_launcher_round.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xhdpi/ic_launcher.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000000..6dba46dab1
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xhdpi/ic_launcher.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xhdpi/ic_launcher_round.png
new file mode 100644
index 0000000000..da31a871c8
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxhdpi/ic_launcher.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000000..15ac681720
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxhdpi/ic_launcher.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
new file mode 100644
index 0000000000..b216f2d313
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000000..f25a419744
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
new file mode 100644
index 0000000000..e96783ccce
Binary files /dev/null and b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/colors.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/colors.xml
new file mode 100644
index 0000000000..a0bf027ccc
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/colors.xml
@@ -0,0 +1,21 @@
+
+
+ #303030
+ #3700B3
+ #03DAC5
+
+ #A69D9D
+ #424242
+
+ #6DA7FF
+ #F8E71C
+ #FF844D
+ #66B50A
+ #FFBB86FC
+ #FF6200EE
+ #FF3700B3
+ #FF03DAC5
+ #FF018786
+ #FF000000
+ #FFFFFFFF
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/strings.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/strings.xml
new file mode 100644
index 0000000000..4197f5c7ab
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/strings.xml
@@ -0,0 +1,5 @@
+
+ ImageSegmentation
+ Null Image needed to save
+ Save success
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/themes.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/themes.xml
new file mode 100644
index 0000000000..e470697d9b
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/values/themes.xml
@@ -0,0 +1,5 @@
+
+
+
+
\ No newline at end of file
diff --git a/model_zoo/official/lite/Himindspore/segmentation/src/main/res/xml/file_paths.xml b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/xml/file_paths.xml
new file mode 100644
index 0000000000..a075ef96b4
--- /dev/null
+++ b/model_zoo/official/lite/Himindspore/segmentation/src/main/res/xml/file_paths.xml
@@ -0,0 +1,6 @@
+
+
+
+
diff --git a/model_zoo/official/lite/Himindspore/settings.gradle b/model_zoo/official/lite/Himindspore/settings.gradle
index 02f6392ec5..8b4b9f4f74 100644
--- a/model_zoo/official/lite/Himindspore/settings.gradle
+++ b/model_zoo/official/lite/Himindspore/settings.gradle
@@ -1,4 +1,5 @@
include ':mindsporelibrary'
+include ':segmentation'
include ':imageObject'
include ':styletransfer'
include ':posenet'
diff --git a/model_zoo/official/lite/Himindspore/styletransfer/build.gradle b/model_zoo/official/lite/Himindspore/styletransfer/build.gradle
index 9e605d6d25..4737a4f747 100644
--- a/model_zoo/official/lite/Himindspore/styletransfer/build.gradle
+++ b/model_zoo/official/lite/Himindspore/styletransfer/build.gradle
@@ -15,7 +15,7 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
- arguments = [moduleName: project.getName()]
+ arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
@@ -61,7 +61,6 @@ android {
apply from: 'download.gradle'
dependencies {
- implementation project(':mindsporelibrary')
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
@@ -73,6 +72,7 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
- implementation 'com.alibaba:arouter-api:1.2.1'
- annotationProcessor 'com.alibaba:arouter-compiler:1.1.2'
+ implementation 'com.alibaba:arouter-api:1.5.1'
+ annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'
+ implementation project(':mindsporelibrary')
}
\ No newline at end of file
diff --git a/model_zoo/official/lite/image_segmentation/app/download.gradle b/model_zoo/official/lite/image_segmentation/app/download.gradle
index fb2b09b787..83026ca645 100644
--- a/model_zoo/official/lite/image_segmentation/app/download.gradle
+++ b/model_zoo/official/lite/image_segmentation/app/download.gradle
@@ -4,8 +4,8 @@
* The libraries can be downloaded manually.
*/
def mindsporeLite_Version = "mindspore-lite-maven-1.0.1"
-def targetModelFile = "src/main/assets/mobile_segment.ms"
-def modelDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/mobile_segment_lite/mobile_segment.ms"
+def targetModelFile = "src/main/assets/segment_model.ms"
+def modelDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/mobile_segment_lite/segment_model.ms"
def mindsporeLiteDownloadUrl = "https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.0.1/lite/java/${mindsporeLite_Version}.zip"
def mindSporeLibrary = "libs/${mindsporeLite_Version}.zip"
def cleantargetMindSporeInclude = "libs"
@@ -50,7 +50,7 @@ if (file("libs/mindspore-lite-1.0.1.aar").exists()) {
}
-if (file("src/main/assets/mobile_segment.ms").exists()) {
+if (file("src/main/assets/segment_model.ms").exists()) {
downloadModelFile.enabled = false
}
diff --git a/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/BitmapUtils.java b/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/BitmapUtils.java
deleted file mode 100644
index 807e656d47..0000000000
--- a/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/BitmapUtils.java
+++ /dev/null
@@ -1,151 +0,0 @@
-/**
- * Copyright 2020 Huawei Technologies Co., Ltd
- *
- * 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
- *
- * 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.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.mindspore.imagesegmentation;
-
-import android.app.Activity;
-import android.database.Cursor;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.graphics.Matrix;
-import android.media.ExifInterface;
-import android.net.Uri;
-import android.provider.MediaStore;
-import android.util.Log;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-public class BitmapUtils {
- private static final String TAG = "BitmapUtils";
-
- public static void recycleBitmap(Bitmap... bitmaps) {
- for (Bitmap bitmap : bitmaps) {
- if (bitmap != null && !bitmap.isRecycled()) {
- bitmap.recycle();
- bitmap = null;
- }
- }
- }
-
- private static String getImagePath(Activity activity, Uri uri) {
- String[] projection = {MediaStore.Images.Media.DATA};
- Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
- int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- return cursor.getString(columnIndex);
- }
-
- public static Bitmap loadFromPath(Activity activity, int id, int width, int height) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- InputStream is = activity.getResources().openRawResource(id);
- int sampleSize = calculateInSampleSize(options, width, height);
- options.inSampleSize = sampleSize;
- options.inJustDecodeBounds = false;
- return zoomImage(BitmapFactory.decodeStream(is), width, height);
- }
-
- public static Bitmap loadFromPath(Activity activity, Uri uri, int width, int height) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
-
- String path = getImagePath(activity, uri);
- BitmapFactory.decodeFile(path, options);
- int sampleSize = calculateInSampleSize(options, width, height);
- options.inSampleSize = sampleSize;
- options.inJustDecodeBounds = false;
-
- Bitmap bitmap = zoomImage(BitmapFactory.decodeFile(path, options), width, height);
- return rotateBitmap(bitmap, getRotationAngle(path));
- }
-
- private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
- final int width = options.outWidth;
- final int height = options.outHeight;
- int inSampleSize = 1;
-
- if (height > reqHeight || width > reqWidth) {
- // Calculate height and required height scale.
- final int heightRatio = Math.round((float) height / (float) reqHeight);
- // Calculate width and required width scale.
- final int widthRatio = Math.round((float) width / (float) reqWidth);
- // Take the larger of the values.
- inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
- }
- return inSampleSize;
- }
-
- // Scale pictures to screen width.
- private static Bitmap zoomImage(Bitmap imageBitmap, int targetWidth, int maxHeight) {
- float scaleFactor =
- Math.max(
- (float) imageBitmap.getWidth() / (float) targetWidth,
- (float) imageBitmap.getHeight() / (float) maxHeight);
- Bitmap resizedBitmap =
- Bitmap.createScaledBitmap(
- imageBitmap,
- (int) (imageBitmap.getWidth() / scaleFactor),
- (int) (imageBitmap.getHeight() / scaleFactor),
- true);
-
- return resizedBitmap;
- }
-
- /**
- * Get the rotation angle of the photo.
- *
- * @param path photo path.
- * @return angle.
- */
- public static int getRotationAngle(String path) {
- int rotation = 0;
- try {
- ExifInterface exifInterface = new ExifInterface(path);
- int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
- switch (orientation) {
- case ExifInterface.ORIENTATION_ROTATE_90:
- rotation = 90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- rotation = 180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- rotation = 270;
- break;
- default:
- break;
- }
- } catch (IOException e) {
- Log.e(TAG, "Failed to get rotation: " + e.getMessage());
- }
- return rotation;
- }
-
- public static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
- Matrix matrix = new Matrix();
- matrix.postRotate(angle);
- Bitmap result = null;
- try {
- result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
- } catch (OutOfMemoryError e) {
- Log.e(TAG, "Failed to rotate bitmap: " + e.getMessage());
- }
- if (result == null) {
- return bitmap;
- }
- return result;
- }
-}
diff --git a/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/MainActivity.java b/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/MainActivity.java
index b0e537fcc7..6eaa36f950 100644
--- a/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/MainActivity.java
+++ b/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/MainActivity.java
@@ -47,7 +47,7 @@ import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
-import com.mindspore.imagesegmentation.help.ImageUtils;
+import com.mindspore.imagesegmentation.help.BitmapUtils;
import com.mindspore.imagesegmentation.help.ModelTrackingResult;
import com.mindspore.imagesegmentation.help.TrackingMobile;
@@ -160,7 +160,7 @@ public class MainActivity extends AppCompatActivity implements OnBackgroundImage
Log.e(TAG, "null processed image");
Toast.makeText(this.getApplicationContext(), R.string.no_pic_neededSave, Toast.LENGTH_SHORT).show();
} else {
- ImageUtils.saveToAlbum(getApplicationContext(), this.processedImage);
+ BitmapUtils.saveToAlbum(getApplicationContext(), this.processedImage);
Toast.makeText(this.getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show();
}
}
diff --git a/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/help/BitmapUtils.java b/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/help/BitmapUtils.java
new file mode 100644
index 0000000000..0fdf670800
--- /dev/null
+++ b/model_zoo/official/lite/image_segmentation/app/src/main/java/com/mindspore/imagesegmentation/help/BitmapUtils.java
@@ -0,0 +1,299 @@
+/**
+ * Copyright 2020 Huawei Technologies Co., Ltd
+ *
+ * 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
+ *