From: @sishuikang Reviewed-by: @zhanghaibo5,@zhang_xue_tong Signed-off-by: @zhanghaibo5,@zhang_xue_tongtags/v1.1.0
| @@ -8,16 +8,15 @@ android { | |||||
| applicationId "com.mindspore.himindspore" | applicationId "com.mindspore.himindspore" | ||||
| minSdkVersion 21 | minSdkVersion 21 | ||||
| targetSdkVersion 30 | targetSdkVersion 30 | ||||
| versionCode 3 | |||||
| versionName "1.1.1" | |||||
| versionCode 4 | |||||
| versionName "1.1.2" | |||||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||||
| javaCompileOptions { | javaCompileOptions { | ||||
| annotationProcessorOptions { | annotationProcessorOptions { | ||||
| arguments = [moduleName: project.getName()] | |||||
| arguments = [AROUTER_MODULE_NAME: project.getName()] | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| buildTypes { | buildTypes { | ||||
| @@ -69,10 +68,11 @@ dependencies { | |||||
| implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0' | implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0' | ||||
| implementation 'org.greenrobot:eventbus:3.0.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(':posenet') | ||||
| implementation project(':imageObject') | implementation project(':imageObject') | ||||
| implementation project(':styletransfer') | implementation project(':styletransfer') | ||||
| implementation project(':segmentation') | |||||
| } | } | ||||
| @@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a> | |||||
| */ | |||||
| @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()); | |||||
| } | |||||
| } | |||||
| @@ -206,6 +206,14 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } | } | ||||
| } | } | ||||
| public void onClickSegmentation(View view) { | |||||
| if (isAllGranted) { | |||||
| ARouter.getInstance().build("/segmentation/SegmentationMainActivity").navigation(this); | |||||
| } else { | |||||
| requestPermissions(); | |||||
| } | |||||
| } | |||||
| public void onClickSouceCode(View view) { | public void onClickSouceCode(View view) { | ||||
| openBrowser(CODE_URL); | openBrowser(CODE_URL); | ||||
| } | } | ||||
| @@ -343,4 +351,5 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } | } | ||||
| } | } | ||||
| @@ -14,7 +14,7 @@ | |||||
| android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
| android:layout_marginTop="68dp" | android:layout_marginTop="68dp" | ||||
| android:drawableTop="@drawable/logo" | android:drawableTop="@drawable/logo" | ||||
| android:drawablePadding="30dp" | |||||
| android:drawablePadding="20dp" | |||||
| android:gravity="center_horizontal" | android:gravity="center_horizontal" | ||||
| android:maxLines="1" | android:maxLines="1" | ||||
| android:text="@string/app_name" | android:text="@string/app_name" | ||||
| @@ -133,11 +133,28 @@ | |||||
| app:layout_constraintEnd_toEndOf="@+id/btn_image_garbage" | app:layout_constraintEnd_toEndOf="@+id/btn_image_garbage" | ||||
| app:layout_constraintWidth_percent="0.43" /> | app:layout_constraintWidth_percent="0.43" /> | ||||
| <Button | |||||
| android:layout_marginTop="16dp" | |||||
| android:id="@+id/btn_segmentation" | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:background="@color/gray_btn" | |||||
| android:drawableStart="@drawable/btn_image" | |||||
| android:drawablePadding="5dp" | |||||
| android:gravity="left|center_vertical" | |||||
| android:onClick="onClickSegmentation" | |||||
| android:paddingLeft="4dp" | |||||
| android:text="@string/title_segmentation" | |||||
| android:textAllCaps="false" | |||||
| android:textSize="12sp" | |||||
| app:layout_constraintStart_toStartOf="@+id/btn_posenet" | |||||
| app:layout_constraintTop_toBottomOf="@+id/btn_posenet" | |||||
| app:layout_constraintWidth_percent="0.43" /> | |||||
| <Button | <Button | ||||
| android:id="@+id/btn_source" | android:id="@+id/btn_source" | ||||
| android:layout_width="0dp" | android:layout_width="0dp" | ||||
| android:layout_height="48dp" | android:layout_height="48dp" | ||||
| android:layout_marginTop="16dp" | |||||
| android:background="@color/gray_btn" | android:background="@color/gray_btn" | ||||
| android:drawableStart="@drawable/btn_code" | android:drawableStart="@drawable/btn_code" | ||||
| android:drawablePadding="5dp" | android:drawablePadding="5dp" | ||||
| @@ -147,14 +164,15 @@ | |||||
| android:text="@string/title_source" | android:text="@string/title_source" | ||||
| android:textAllCaps="false" | android:textAllCaps="false" | ||||
| android:textSize="12sp" | android:textSize="12sp" | ||||
| app:layout_constraintStart_toStartOf="@+id/btn_posenet" | |||||
| app:layout_constraintTop_toBottomOf="@+id/btn_posenet" | |||||
| app:layout_constraintBottom_toBottomOf="@+id/btn_segmentation" | |||||
| app:layout_constraintEnd_toEndOf="@+id/btn_image_garbage" | |||||
| app:layout_constraintWidth_percent="0.43" /> | app:layout_constraintWidth_percent="0.43" /> | ||||
| <Button | <Button | ||||
| android:id="@+id/btn_help" | android:id="@+id/btn_help" | ||||
| android:layout_width="0dp" | android:layout_width="0dp" | ||||
| android:layout_height="48dp" | android:layout_height="48dp" | ||||
| android:layout_marginTop="16dp" | |||||
| android:background="@color/gray_btn" | android:background="@color/gray_btn" | ||||
| android:drawableStart="@drawable/btn_help" | android:drawableStart="@drawable/btn_help" | ||||
| android:drawablePadding="5dp" | android:drawablePadding="5dp" | ||||
| @@ -164,8 +182,8 @@ | |||||
| android:text="@string/title_help" | android:text="@string/title_help" | ||||
| android:textAllCaps="false" | android:textAllCaps="false" | ||||
| android:textSize="12sp" | android:textSize="12sp" | ||||
| app:layout_constraintBottom_toBottomOf="@+id/btn_source" | |||||
| app:layout_constraintEnd_toEndOf="@+id/btn_image_garbage" | |||||
| app:layout_constraintStart_toStartOf="@+id/btn_segmentation" | |||||
| app:layout_constraintTop_toBottomOf="@+id/btn_segmentation" | |||||
| app:layout_constraintWidth_percent="0.43" /> | app:layout_constraintWidth_percent="0.43" /> | ||||
| <TextView | <TextView | ||||
| @@ -11,6 +11,7 @@ | |||||
| <string name="title_style_transfer">Style Transfer</string> | <string name="title_style_transfer">Style Transfer</string> | ||||
| <string name="title_source">Source Code</string> | <string name="title_source">Source Code</string> | ||||
| <string name="title_help">Help And FeedBack</string> | <string name="title_help">Help And FeedBack</string> | ||||
| <string name="title_segmentation">Image Segmentation</string> | |||||
| <string name="title_photo">Photo</string> | <string name="title_photo">Photo</string> | ||||
| <string name="title_camera">Camera</string> | <string name="title_camera">Camera</string> | ||||
| @@ -16,7 +16,7 @@ android { | |||||
| consumerProguardFiles "consumer-rules.pro" | consumerProguardFiles "consumer-rules.pro" | ||||
| javaCompileOptions { | javaCompileOptions { | ||||
| annotationProcessorOptions { | annotationProcessorOptions { | ||||
| arguments = [moduleName: project.getName()] | |||||
| arguments = [AROUTER_MODULE_NAME: project.getName()] | |||||
| } | } | ||||
| } | } | ||||
| externalNativeBuild { | externalNativeBuild { | ||||
| @@ -77,6 +77,6 @@ dependencies { | |||||
| androidTestImplementation 'androidx.test.ext:junit:1.1.2' | androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | 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' | |||||
| } | } | ||||
| @@ -13,7 +13,7 @@ android { | |||||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||||
| javaCompileOptions { | javaCompileOptions { | ||||
| annotationProcessorOptions { | annotationProcessorOptions { | ||||
| arguments = [moduleName: project.getName()] | |||||
| arguments = [AROUTER_MODULE_NAME: project.getName()] | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -59,13 +59,13 @@ apply from: 'download.gradle' | |||||
| dependencies { | dependencies { | ||||
| implementation project(':mindsporelibrary') | implementation project(':mindsporelibrary') | ||||
| implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) | |||||
| implementation 'androidx.appcompat:appcompat:1.2.0' | implementation 'androidx.appcompat:appcompat:1.2.0' | ||||
| implementation 'com.google.android.material:material:1.2.1' | implementation 'com.google.android.material:material:1.2.1' | ||||
| implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||||
| androidTestImplementation 'androidx.test.ext:junit:1.1.2' | androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | 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' | |||||
| } | } | ||||
| @@ -69,4 +69,4 @@ class DownloadUrlTask extends DefaultTask { | |||||
| void download() { | void download() { | ||||
| ant.get(src: sourceUrl, dest: target) | ant.get(src: sourceUrl, dest: target) | ||||
| } | } | ||||
| } | |||||
| } | |||||
| @@ -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') | |||||
| } | |||||
| @@ -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) | |||||
| } | |||||
| } | |||||
| @@ -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 | |||||
| @@ -0,0 +1,30 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| xmlns:tools="http://schemas.android.com/tools" | |||||
| package="com.mindspore.imagesegmentation"> | |||||
| <application | |||||
| tools:replace="android:label" | |||||
| android:allowBackup="true" | |||||
| android:label="@string/app_name" | |||||
| android:supportsRtl="true"> | |||||
| <activity android:name=".SegmentationMainActivity" | |||||
| android:screenOrientation="portrait" | |||||
| android:theme="@style/Theme.AppCompat.NoActionBar"> | |||||
| </activity> | |||||
| <provider | |||||
| android:name="androidx.core.content.FileProvider" | |||||
| android:authorities="com.mindspore.imagesegmentation.fileprovider" | |||||
| android:exported="false" | |||||
| tools:replace="android:authorities" | |||||
| android:grantUriPermissions="true"> | |||||
| <meta-data | |||||
| tools:replace="android:resource" | |||||
| android:name="android.support.FILE_PROVIDER_PATHS" | |||||
| android:resource="@xml/file_paths"/> | |||||
| </provider> | |||||
| </application> | |||||
| </manifest> | |||||
| @@ -0,0 +1,25 @@ | |||||
| /** | |||||
| * 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. | |||||
| * 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); | |||||
| } | |||||
| @@ -0,0 +1,97 @@ | |||||
| /** | |||||
| * 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. | |||||
| * 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<StyleRecyclerViewAdapter.StyleItemViewHolder> { | |||||
| 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); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -15,22 +15,22 @@ | |||||
| */ | */ | ||||
| package com.mindspore.imagesegmentation.help; | package com.mindspore.imagesegmentation.help; | ||||
| import android.app.Activity; | |||||
| import android.content.Context; | import android.content.Context; | ||||
| import android.content.Intent; | import android.content.Intent; | ||||
| import android.database.Cursor; | |||||
| import android.graphics.Bitmap; | import android.graphics.Bitmap; | ||||
| import android.graphics.BitmapFactory; | import android.graphics.BitmapFactory; | ||||
| import android.graphics.Color; | |||||
| import android.graphics.Matrix; | import android.graphics.Matrix; | ||||
| import android.graphics.RectF; | import android.graphics.RectF; | ||||
| import android.media.ExifInterface; | |||||
| import android.media.MediaScannerConnection; | import android.media.MediaScannerConnection; | ||||
| import android.net.Uri; | import android.net.Uri; | ||||
| import android.os.Build; | import android.os.Build; | ||||
| import android.os.Environment; | import android.os.Environment; | ||||
| import android.provider.MediaStore; | |||||
| import android.util.Log; | import android.util.Log; | ||||
| import androidx.annotation.NonNull; | |||||
| import androidx.exifinterface.media.ExifInterface; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||
| import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||
| @@ -39,37 +39,154 @@ import java.io.InputStream; | |||||
| import java.nio.ByteBuffer; | import java.nio.ByteBuffer; | ||||
| import java.nio.ByteOrder; | 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) { | private static Matrix decodeExifOrientation(int orientation) { | ||||
| Matrix matrix = new Matrix(); | Matrix matrix = new Matrix(); | ||||
| switch (orientation) { | 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; | break; | ||||
| case ExifInterface.ORIENTATION_ROTATE_90: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_90: | |||||
| matrix.postRotate(90F); | matrix.postRotate(90F); | ||||
| break; | break; | ||||
| case ExifInterface.ORIENTATION_ROTATE_180: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_180: | |||||
| matrix.postRotate(180F); | matrix.postRotate(180F); | ||||
| break; | break; | ||||
| case ExifInterface.ORIENTATION_ROTATE_270: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_270: | |||||
| matrix.postRotate(270F); | matrix.postRotate(270F); | ||||
| break; | break; | ||||
| case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_HORIZONTAL: | |||||
| matrix.postScale(-1F, 1F); | matrix.postScale(-1F, 1F); | ||||
| break; | break; | ||||
| case ExifInterface.ORIENTATION_FLIP_VERTICAL: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_VERTICAL: | |||||
| matrix.postScale(1F, -1F); | matrix.postScale(1F, -1F); | ||||
| break; | break; | ||||
| case ExifInterface.ORIENTATION_TRANSPOSE: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSPOSE: | |||||
| matrix.postScale(-1F, 1F); | matrix.postScale(-1F, 1F); | ||||
| matrix.postRotate(270F); | matrix.postRotate(270F); | ||||
| break; | break; | ||||
| case ExifInterface.ORIENTATION_TRANSVERSE: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSVERSE: | |||||
| matrix.postScale(-1F, 1F); | matrix.postScale(-1F, 1F); | ||||
| matrix.postRotate(90F); | matrix.postRotate(90F); | ||||
| break; | break; | ||||
| @@ -84,55 +201,6 @@ public class ImageUtils { | |||||
| return matrix; | 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) { | public static Bitmap scaleBitmapAndKeepRatio(Bitmap targetBmp, int reqHeightInPixels, int reqWidthInPixels) { | ||||
| if (targetBmp.getHeight() == reqHeightInPixels && targetBmp.getWidth() == reqWidthInPixels) { | if (targetBmp.getHeight() == reqHeightInPixels && targetBmp.getWidth() == reqWidthInPixels) { | ||||
| return targetBmp; | 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) { | public static ByteBuffer bitmapToByteBuffer(Bitmap bitmapIn, int width, int height, float mean, float std) { | ||||
| Bitmap bitmap = scaleBitmapAndKeepRatio(bitmapIn, width, height); | Bitmap bitmap = scaleBitmapAndKeepRatio(bitmapIn, width, height); | ||||
| ByteBuffer inputImage = ByteBuffer.allocateDirect(1 * width * height * 3 * 4); | 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. | // Save the picture to the system album and refresh it. | ||||
| public static void saveToAlbum(final Context context, Bitmap bitmap) { | public static void saveToAlbum(final Context context, Bitmap bitmap) { | ||||
| File file = null; | File file = null; | ||||
| @@ -263,4 +297,4 @@ public class ImageUtils { | |||||
| context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile()))); | context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile()))); | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| } | |||||
| @@ -0,0 +1,81 @@ | |||||
| /** | |||||
| * 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. | |||||
| * 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<Integer> itemsFound; | |||||
| public ModelTrackingResult(Bitmap bitmapResult, Bitmap bitmapOriginal, Bitmap bitmapMaskOnly, String executionLog, Set<Integer> 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<Integer> getItemsFound() { | |||||
| return itemsFound; | |||||
| } | |||||
| public ModelTrackingResult setItemsFound(Set<Integer> itemsFound) { | |||||
| this.itemsFound = itemsFound; | |||||
| return this; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,218 @@ | |||||
| /** | |||||
| * 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. | |||||
| * 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<MSTensor> 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<String> tensorNames = session.getOutputTensorNames(); | |||||
| Map<String, MSTensor> 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(); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,30 @@ | |||||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| xmlns:aapt="http://schemas.android.com/aapt" | |||||
| android:width="108dp" | |||||
| android:height="108dp" | |||||
| android:viewportWidth="108" | |||||
| android:viewportHeight="108"> | |||||
| <path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z"> | |||||
| <aapt:attr name="android:fillColor"> | |||||
| <gradient | |||||
| android:endX="85.84757" | |||||
| android:endY="92.4963" | |||||
| android:startX="42.9492" | |||||
| android:startY="49.59793" | |||||
| android:type="linear"> | |||||
| <item | |||||
| android:color="#44000000" | |||||
| android:offset="0.0" /> | |||||
| <item | |||||
| android:color="#00000000" | |||||
| android:offset="1.0" /> | |||||
| </gradient> | |||||
| </aapt:attr> | |||||
| </path> | |||||
| <path | |||||
| android:fillColor="#FFFFFF" | |||||
| android:fillType="nonZero" | |||||
| android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z" | |||||
| android:strokeWidth="1" | |||||
| android:strokeColor="#00000000" /> | |||||
| </vector> | |||||
| @@ -0,0 +1,170 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| android:width="108dp" | |||||
| android:height="108dp" | |||||
| android:viewportWidth="108" | |||||
| android:viewportHeight="108"> | |||||
| <path | |||||
| android:fillColor="#3DDC84" | |||||
| android:pathData="M0,0h108v108h-108z" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M9,0L9,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M19,0L19,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M29,0L29,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M39,0L39,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M49,0L49,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M59,0L59,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M69,0L69,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M79,0L79,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M89,0L89,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M99,0L99,108" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,9L108,9" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,19L108,19" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,29L108,29" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,39L108,39" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,49L108,49" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,59L108,59" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,69L108,69" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,79L108,79" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,89L108,89" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M0,99L108,99" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M19,29L89,29" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M19,39L89,39" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M19,49L89,49" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M19,59L89,59" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M19,69L89,69" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M19,79L89,79" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M29,19L29,89" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M39,19L39,89" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M49,19L49,89" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M59,19L59,89" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M69,19L69,89" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| <path | |||||
| android:fillColor="#00000000" | |||||
| android:pathData="M79,19L79,89" | |||||
| android:strokeWidth="0.8" | |||||
| android:strokeColor="#33FFFFFF" /> | |||||
| </vector> | |||||
| @@ -0,0 +1,20 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| android:fromDegrees="0" | |||||
| android:pivotX="50%" | |||||
| android:pivotY="50%" | |||||
| android:toDegrees="360"> | |||||
| <shape | |||||
| android:innerRadiusRatio="3" | |||||
| android:shape="ring" | |||||
| android:thicknessRatio="8" | |||||
| android:useLevel="false"> | |||||
| <gradient | |||||
| android:centerColor="#62AEEC" | |||||
| android:centerY="0.50" | |||||
| android:endColor="#1063A5" | |||||
| android:startColor="#61C2EC" | |||||
| android:type="sweep" | |||||
| android:useLevel="false" /> | |||||
| </shape> | |||||
| </animated-rotate> | |||||
| @@ -0,0 +1,141 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| android:layout_width="match_parent" | |||||
| android:layout_height="match_parent" | |||||
| android:background="@color/colorPrimary" | |||||
| android:orientation="vertical"> | |||||
| <androidx.appcompat.widget.Toolbar | |||||
| android:id="@+id/toolbar" | |||||
| android:layout_width="match_parent" | |||||
| android:layout_height="?attr/actionBarSize" | |||||
| android:layout_alignParentTop="true" | |||||
| android:background="#66000000"> | |||||
| <TextView | |||||
| android:layout_width="match_parent" | |||||
| android:layout_height="wrap_content" | |||||
| android:drawableStart="@drawable/logo2" | |||||
| android:drawablePadding="5dp" | |||||
| android:gravity="center_vertical" | |||||
| android:maxLines="1" | |||||
| android:text="MS ImageSegmentation" | |||||
| android:textColor="#ffffff" | |||||
| android:textSize="20sp" /> | |||||
| </androidx.appcompat.widget.Toolbar> | |||||
| <FrameLayout | |||||
| android:layout_width="match_parent" | |||||
| android:layout_height="wrap_content"> | |||||
| <ImageView | |||||
| android:id="@+id/img_origin" | |||||
| android:layout_width="256dp" | |||||
| android:layout_height="256dp" | |||||
| android:layout_gravity="center" | |||||
| android:layout_margin="10dp" | |||||
| android:scaleType="fitXY" /> | |||||
| <TextView | |||||
| android:id="@+id/tv_image" | |||||
| android:layout_width="206dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_gravity="center" | |||||
| android:gravity="center" | |||||
| android:paddingLeft="4dp" | |||||
| android:text="Choose a Image" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="20sp" /> | |||||
| <ProgressBar | |||||
| android:id="@+id/progress" | |||||
| android:layout_width="80dp" | |||||
| android:layout_height="80dp" | |||||
| android:layout_gravity="center" | |||||
| android:indeterminateDrawable="@drawable/progressbar" | |||||
| android:visibility="invisible" /> | |||||
| </FrameLayout> | |||||
| <LinearLayout | |||||
| android:layout_width="match_parent" | |||||
| android:layout_height="wrap_content" | |||||
| android:orientation="horizontal"> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="20dp" | |||||
| android:layout_marginRight="5dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickPhoto" | |||||
| android:text="PHOTO" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="5dp" | |||||
| android:layout_marginRight="5dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickCamera" | |||||
| android:text="CAMERA" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="5dp" | |||||
| android:layout_marginRight="5dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickRecovery" | |||||
| android:text="RECOVERY" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="5dp" | |||||
| android:layout_marginRight="20dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickSave" | |||||
| android:text="SAVE" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| </LinearLayout> | |||||
| <TextView | |||||
| android:layout_width="wrap_content" | |||||
| android:layout_height="wrap_content" | |||||
| android:layout_marginLeft="20dp" | |||||
| android:layout_marginTop="10dp" | |||||
| android:text="Choose a Background" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="20sp" /> | |||||
| <androidx.recyclerview.widget.RecyclerView | |||||
| android:id="@+id/recyclerview" | |||||
| android:layout_width="match_parent" | |||||
| android:layout_height="wrap_content" | |||||
| android:layout_margin="20dp" | |||||
| android:fadeScrollbars="false" | |||||
| android:scrollbarSize="6dp" | |||||
| android:scrollbarStyle="outsideInset" | |||||
| android:scrollbarThumbVertical="@color/gray" | |||||
| android:scrollbars="vertical" /> | |||||
| </LinearLayout> | |||||
| @@ -0,0 +1,14 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| xmlns:tools="http://schemas.android.com/tools" | |||||
| android:layout_width="wrap_content" | |||||
| android:layout_height="wrap_content" | |||||
| android:orientation="vertical"> | |||||
| <ImageView | |||||
| android:id="@+id/image_view" | |||||
| android:layout_width="wrap_content" | |||||
| android:layout_height="120dp" | |||||
| android:scaleType="fitXY" | |||||
| tools:srcCompat="@drawable/add" /> | |||||
| </LinearLayout> | |||||
| @@ -0,0 +1,21 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <resources> | |||||
| <color name="colorPrimary">#303030</color> | |||||
| <color name="colorPrimaryDark">#3700B3</color> | |||||
| <color name="colorAccent">#03DAC5</color> | |||||
| <color name="gray">#A69D9D</color> | |||||
| <color name="gray_btn">#424242</color> | |||||
| <color name="text_blue">#6DA7FF</color> | |||||
| <color name="text_yellow">#F8E71C</color> | |||||
| <color name="text_orange">#FF844D</color> | |||||
| <color name="text_green">#66B50A</color> | |||||
| <color name="purple_200">#FFBB86FC</color> | |||||
| <color name="purple_500">#FF6200EE</color> | |||||
| <color name="purple_700">#FF3700B3</color> | |||||
| <color name="teal_200">#FF03DAC5</color> | |||||
| <color name="teal_700">#FF018786</color> | |||||
| <color name="black">#FF000000</color> | |||||
| <color name="white">#FFFFFFFF</color> | |||||
| </resources> | |||||
| @@ -0,0 +1,5 @@ | |||||
| <resources> | |||||
| <string name="app_name">ImageSegmentation</string> | |||||
| <string name="no_pic_neededSave">Null Image needed to save</string> | |||||
| <string name="save_success">Save success</string> | |||||
| </resources> | |||||
| @@ -0,0 +1,5 @@ | |||||
| <resources xmlns:tools="http://schemas.android.com/tools"> | |||||
| <!-- Base application theme. --> | |||||
| <style name="Theme.ImageSegmentation" parent="Theme.AppCompat.Light.NoActionBar"> | |||||
| </style> | |||||
| </resources> | |||||
| @@ -0,0 +1,6 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <paths> | |||||
| <external-path | |||||
| name="external_files" | |||||
| path="." /> | |||||
| </paths> | |||||
| @@ -1,4 +1,5 @@ | |||||
| include ':mindsporelibrary' | include ':mindsporelibrary' | ||||
| include ':segmentation' | |||||
| include ':imageObject' | include ':imageObject' | ||||
| include ':styletransfer' | include ':styletransfer' | ||||
| include ':posenet' | include ':posenet' | ||||
| @@ -15,7 +15,7 @@ android { | |||||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||||
| javaCompileOptions { | javaCompileOptions { | ||||
| annotationProcessorOptions { | annotationProcessorOptions { | ||||
| arguments = [moduleName: project.getName()] | |||||
| arguments = [AROUTER_MODULE_NAME: project.getName()] | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -61,7 +61,6 @@ android { | |||||
| apply from: 'download.gradle' | apply from: 'download.gradle' | ||||
| dependencies { | dependencies { | ||||
| implementation project(':mindsporelibrary') | |||||
| implementation 'androidx.legacy:legacy-support-v4:1.0.0' | implementation 'androidx.legacy:legacy-support-v4:1.0.0' | ||||
| implementation 'androidx.recyclerview:recyclerview:1.1.0' | implementation 'androidx.recyclerview:recyclerview:1.1.0' | ||||
| implementation 'androidx.appcompat:appcompat:1.2.0' | implementation 'androidx.appcompat:appcompat:1.2.0' | ||||
| @@ -73,6 +72,7 @@ dependencies { | |||||
| implementation 'com.github.bumptech.glide:glide:4.11.0' | implementation 'com.github.bumptech.glide:glide:4.11.0' | ||||
| annotationProcessor 'com.github.bumptech.glide:compiler: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') | |||||
| } | } | ||||
| @@ -4,8 +4,8 @@ | |||||
| * The libraries can be downloaded manually. | * The libraries can be downloaded manually. | ||||
| */ | */ | ||||
| def mindsporeLite_Version = "mindspore-lite-maven-1.0.1" | 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 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 mindSporeLibrary = "libs/${mindsporeLite_Version}.zip" | ||||
| def cleantargetMindSporeInclude = "libs" | 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 | downloadModelFile.enabled = false | ||||
| } | } | ||||
| @@ -1,151 +0,0 @@ | |||||
| /** | |||||
| * 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. | |||||
| * 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; | |||||
| } | |||||
| } | |||||
| @@ -47,7 +47,7 @@ import androidx.recyclerview.widget.GridLayoutManager; | |||||
| import androidx.recyclerview.widget.RecyclerView; | import androidx.recyclerview.widget.RecyclerView; | ||||
| import com.bumptech.glide.Glide; | 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.ModelTrackingResult; | ||||
| import com.mindspore.imagesegmentation.help.TrackingMobile; | import com.mindspore.imagesegmentation.help.TrackingMobile; | ||||
| @@ -160,7 +160,7 @@ public class MainActivity extends AppCompatActivity implements OnBackgroundImage | |||||
| Log.e(TAG, "null processed image"); | Log.e(TAG, "null processed image"); | ||||
| Toast.makeText(this.getApplicationContext(), R.string.no_pic_neededSave, Toast.LENGTH_SHORT).show(); | Toast.makeText(this.getApplicationContext(), R.string.no_pic_neededSave, Toast.LENGTH_SHORT).show(); | ||||
| } else { | } else { | ||||
| ImageUtils.saveToAlbum(getApplicationContext(), this.processedImage); | |||||
| BitmapUtils.saveToAlbum(getApplicationContext(), this.processedImage); | |||||
| Toast.makeText(this.getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show(); | Toast.makeText(this.getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show(); | ||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,299 @@ | |||||
| /** | |||||
| * 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. | |||||
| * See the License for the specific language governing permissions and | |||||
| * limitations under the License. | |||||
| */ | |||||
| 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.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 java.io.File; | |||||
| import java.io.FileNotFoundException; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | |||||
| import java.nio.ByteBuffer; | |||||
| import java.nio.ByteOrder; | |||||
| 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; | |||||
| } | |||||
| private static Matrix decodeExifOrientation(int orientation) { | |||||
| Matrix matrix = new Matrix(); | |||||
| switch (orientation) { | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_NORMAL: | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_UNDEFINED: | |||||
| break; | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_90: | |||||
| matrix.postRotate(90F); | |||||
| break; | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_180: | |||||
| matrix.postRotate(180F); | |||||
| break; | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_270: | |||||
| matrix.postRotate(270F); | |||||
| break; | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_HORIZONTAL: | |||||
| matrix.postScale(-1F, 1F); | |||||
| break; | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_FLIP_VERTICAL: | |||||
| matrix.postScale(1F, -1F); | |||||
| break; | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSPOSE: | |||||
| matrix.postScale(-1F, 1F); | |||||
| matrix.postRotate(270F); | |||||
| break; | |||||
| case androidx.exifinterface.media.ExifInterface.ORIENTATION_TRANSVERSE: | |||||
| matrix.postScale(-1F, 1F); | |||||
| matrix.postRotate(90F); | |||||
| break; | |||||
| default: | |||||
| try { | |||||
| new IllegalArgumentException("Invalid orientation: " + orientation); | |||||
| } catch (Throwable throwable) { | |||||
| throwable.printStackTrace(); | |||||
| } | |||||
| } | |||||
| return matrix; | |||||
| } | |||||
| public static Bitmap scaleBitmapAndKeepRatio(Bitmap targetBmp, int reqHeightInPixels, int reqWidthInPixels) { | |||||
| if (targetBmp.getHeight() == reqHeightInPixels && targetBmp.getWidth() == reqWidthInPixels) { | |||||
| return targetBmp; | |||||
| } | |||||
| Matrix matrix = new Matrix(); | |||||
| matrix.setRectToRect(new RectF(0f, 0f, | |||||
| targetBmp.getWidth(), | |||||
| targetBmp.getHeight() | |||||
| ), new RectF(0f, 0f, | |||||
| reqWidthInPixels, | |||||
| reqHeightInPixels | |||||
| ), Matrix.ScaleToFit.FILL); | |||||
| return Bitmap.createBitmap( | |||||
| targetBmp, 0, 0, | |||||
| targetBmp.getWidth(), | |||||
| targetBmp.getHeight(), matrix, true | |||||
| ); | |||||
| } | |||||
| 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); | |||||
| inputImage.order(ByteOrder.nativeOrder()); | |||||
| inputImage.rewind(); | |||||
| int[] intValues = new int[width * height]; | |||||
| bitmap.getPixels(intValues, 0, width, 0, 0, width, height); | |||||
| int pixel = 0; | |||||
| for (int y = 0; y < height; y++) { | |||||
| for (int x = 0; x < width; x++) { | |||||
| int value = intValues[pixel++]; | |||||
| inputImage.putFloat(((float) (value >> 16 & 255) - mean) / std); | |||||
| inputImage.putFloat(((float) (value >> 8 & 255) - mean) / std); | |||||
| inputImage.putFloat(((float) (value & 255) - mean) / std); | |||||
| } | |||||
| } | |||||
| inputImage.rewind(); | |||||
| return inputImage; | |||||
| } | |||||
| // Save the picture to the system album and refresh it. | |||||
| public static void saveToAlbum(final Context context, Bitmap bitmap) { | |||||
| File file = null; | |||||
| String fileName = System.currentTimeMillis() + ".jpg"; | |||||
| File root = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), context.getPackageName()); | |||||
| File dir = new File(root, "image"); | |||||
| if (dir.mkdirs() || dir.isDirectory()) { | |||||
| file = new File(dir, fileName); | |||||
| } | |||||
| FileOutputStream os = null; | |||||
| try { | |||||
| os = new FileOutputStream(file); | |||||
| bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); | |||||
| os.flush(); | |||||
| } catch (FileNotFoundException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } catch (IOException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } finally { | |||||
| try { | |||||
| if (os != null) { | |||||
| os.close(); | |||||
| } | |||||
| } catch (IOException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } | |||||
| } | |||||
| if (file == null) { | |||||
| return; | |||||
| } | |||||
| // Gallery refresh. | |||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |||||
| String path = null; | |||||
| try { | |||||
| path = file.getCanonicalPath(); | |||||
| } catch (IOException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } | |||||
| MediaScannerConnection.scanFile(context, new String[]{path}, null, | |||||
| new MediaScannerConnection.OnScanCompletedListener() { | |||||
| @Override | |||||
| public void onScanCompleted(String path, Uri uri) { | |||||
| Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); | |||||
| mediaScanIntent.setData(uri); | |||||
| context.sendBroadcast(mediaScanIntent); | |||||
| } | |||||
| }); | |||||
| } else { | |||||
| String relationDir = file.getParent(); | |||||
| File file1 = new File(relationDir); | |||||
| context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile()))); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -37,7 +37,7 @@ import java.util.Map; | |||||
| public class TrackingMobile { | public class TrackingMobile { | ||||
| private static final String TAG = "TrackingMobile"; | private static final String TAG = "TrackingMobile"; | ||||
| private static final String IMAGESEGMENTATIONMODEL = "mobile_segment.ms"; | |||||
| private static final String IMAGESEGMENTATIONMODEL = "segment_model.ms"; | |||||
| private static final int imageSize = 257; | private static final int imageSize = 257; | ||||
| public static final int NUM_CLASSES = 21; | public static final int NUM_CLASSES = 21; | ||||
| private static final float IMAGE_MEAN = 127.5F; | private static final float IMAGE_MEAN = 127.5F; | ||||
| @@ -107,8 +107,8 @@ public class TrackingMobile { | |||||
| float resource_height = bitmap.getHeight(); | float resource_height = bitmap.getHeight(); | ||||
| float resource_weight = bitmap.getWidth(); | float resource_weight = bitmap.getWidth(); | ||||
| Bitmap scaledBitmap = ImageUtils.scaleBitmapAndKeepRatio(bitmap, imageSize, imageSize); | |||||
| ByteBuffer contentArray = ImageUtils.bitmapToByteBuffer(scaledBitmap, imageSize, imageSize, IMAGE_MEAN, IMAGE_STD); | |||||
| Bitmap scaledBitmap = BitmapUtils.scaleBitmapAndKeepRatio(bitmap, imageSize, imageSize); | |||||
| ByteBuffer contentArray = BitmapUtils.bitmapToByteBuffer(scaledBitmap, imageSize, imageSize, IMAGE_MEAN, IMAGE_STD); | |||||
| MSTensor inTensor = inputs.get(0); | MSTensor inTensor = inputs.get(0); | ||||
| // int byteLen = (int) inTensor.size(); | // int byteLen = (int) inTensor.size(); | ||||
| @@ -130,17 +130,31 @@ public class TrackingMobile { | |||||
| return null; | return null; | ||||
| } | } | ||||
| float[] results = output.getFloatData(); | float[] results = output.getFloatData(); | ||||
| ByteBuffer bytebuffer_results = floatArrayToByteArray(results); | |||||
| 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( | convertBytebufferMaskToBitmap( | ||||
| bytebuffer_results, imageSize, imageSize, scaledBitmap, | bytebuffer_results, imageSize, imageSize, scaledBitmap, | ||||
| segmentColors | segmentColors | ||||
| ); | ); | ||||
| //scaledBitmap resize成resource_height,resource_weight | //scaledBitmap resize成resource_height,resource_weight | ||||
| scaledBitmap = ImageUtils.scaleBitmapAndKeepRatio(scaledBitmap, (int) resource_height, (int) resource_weight); | |||||
| resultBitmap = ImageUtils.scaleBitmapAndKeepRatio(resultBitmap, (int) resource_height, (int) resource_weight); | |||||
| maskBitmap = ImageUtils.scaleBitmapAndKeepRatio(maskBitmap, (int) resource_height, (int) 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); | return new ModelTrackingResult(resultBitmap, scaledBitmap, maskBitmap, this.formatExecutionLog(), itemsFound); | ||||
| } | } | ||||
| @@ -158,7 +172,7 @@ public class TrackingMobile { | |||||
| maskBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf); | maskBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf); | ||||
| resultBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf); | resultBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf); | ||||
| Bitmap scaledBackgroundImage = | Bitmap scaledBackgroundImage = | ||||
| ImageUtils.scaleBitmapAndKeepRatio(backgroundImage, imageWidth, imageHeight); | |||||
| BitmapUtils.scaleBitmapAndKeepRatio(backgroundImage, imageWidth, imageHeight); | |||||
| int[][] mSegmentBits = new int[imageWidth][imageHeight]; | int[][] mSegmentBits = new int[imageWidth][imageHeight]; | ||||
| inputBuffer.rewind(); | inputBuffer.rewind(); | ||||
| for (int y = 0; y < imageHeight; y++) { | for (int y = 0; y < imageHeight; y++) { | ||||
| @@ -5,7 +5,7 @@ buildscript { | |||||
| jcenter() | jcenter() | ||||
| } | } | ||||
| dependencies { | dependencies { | ||||
| classpath "com.android.tools.build:gradle:4.1.1" | |||||
| classpath 'com.android.tools.build:gradle:4.0.1' | |||||
| // NOTE: Do not place your application dependencies here; they belong | // NOTE: Do not place your application dependencies here; they belong | ||||
| // in the individual module build.gradle files | // in the individual module build.gradle files | ||||
| @@ -1,6 +1,6 @@ | |||||
| #Fri Nov 27 21:53:22 CST 2020 | |||||
| #Mon Dec 07 09:58:04 CST 2020 | |||||
| distributionBase=GRADLE_USER_HOME | distributionBase=GRADLE_USER_HOME | ||||
| distributionPath=wrapper/dists | distributionPath=wrapper/dists | ||||
| zipStoreBase=GRADLE_USER_HOME | zipStoreBase=GRADLE_USER_HOME | ||||
| zipStorePath=wrapper/dists | zipStorePath=wrapper/dists | ||||
| distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip | |||||
| distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip | |||||