!12860 [MS][LITE]android pet classsification add english and chinese language

From: @sishuikang
Reviewed-by: @zhanghaibo5,@zhang_xue_tong
Signed-off-by: @zhang_xue_tong
pull/12860/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit 9134e7b2d4

@ -56,7 +56,7 @@ char *CreateLocalModelBuffer(JNIEnv *env, jobject modelBuffer) {
* @param msOutputs
* @return
*/
std::string ProcessRunnetResult(const int RET_CATEGORY_SUM, const char *const labels_name_map[],
std::string ProcessRunnetResult(const int RET_CATEGORY_SUM,
std::unordered_map<std::string, mindspore::tensor::MSTensor *> msOutputs) {
// Get the branch of the model output.
// Use iterators to get map elements.
@ -80,7 +80,7 @@ std::string ProcessRunnetResult(const int RET_CATEGORY_SUM, const char *const la
// Score for each category.
// Converted to text information that needs to be displayed in the APP.
std::string categoryScore = "";
categoryScore += labels_name_map[maxIndex];
categoryScore += std::to_string(maxIndex);
return categoryScore;
}
@ -272,8 +272,7 @@ Java_com_mindspore_classificationforpet_gallery_classify_TrackingMobile_runNet(J
msOutputs.insert(std::pair<std::string, mindspore::tensor::MSTensor *>{name, temp_dat});
}
std::string resultStr = ProcessRunnetResult(::RET_PET_DETAILED_SUM,
::labels_name_pet_detailed_map, msOutputs);
std::string resultStr = ProcessRunnetResult(::RET_PET_DETAILED_SUM, msOutputs);
const char *resultCharData = resultStr.c_str();
return (env)->NewStringUTF(resultCharData);

@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mindspore.classificationforpet.gallery.classify;
import android.util.Size;

@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mindspore.classificationforpet.gallery.classify;
import android.content.Context;

@ -116,7 +116,6 @@ public class TrackingMobile {
InputStream is = null;
try {
is = new FileInputStream(modelPath);
// is = mActivity.getAssets().open(modelPath);
byte[] bytes = new byte[is.available()];
is.read(bytes);
return ByteBuffer.allocateDirect(bytes.length).put(bytes);

@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mindspore.classificationforpet.widget;
import android.content.Context;

@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mindspore.classificationforpet.widget;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
@ -54,7 +54,6 @@ public class CameraActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.activity_camera);
filePath = getIntent().getStringExtra("FILEPATH");
@ -96,7 +95,9 @@ public class CameraActivity extends AppCompatActivity {
long startTime = System.currentTimeMillis();
String result = trackingMobile.MindSpore_runnet(bitmap);
long endTime = System.currentTimeMillis();
resultText.setText(TextUtils.isEmpty(result) ? "正在识别..." : result);
String[] IMAGECONTENT = getResources().getStringArray(R.array.image_category_pet);
int nameIndex = Integer.parseInt(result);
resultText.setText(IMAGECONTENT[nameIndex]);
Log.d(TAG, "RUNNET CONSUMING" + (endTime - startTime) + "ms");
Log.d(TAG, "result" + result);
} else {
@ -111,13 +112,15 @@ public class CameraActivity extends AppCompatActivity {
long endTime = System.currentTimeMillis();
Log.d(TAG, "RUNNET CONSUMING" + (endTime - startTime) + "ms");
Log.d(TAG, "result" + result);
String[] IMAGECONTENT = getResources().getStringArray(R.array.image_category);
if (!TextUtils.isEmpty(result)) {
String[] resultArray = result.split(";");
for (String singleRecognitionResult : resultArray) {
String[] singleResult = singleRecognitionResult.split(":");
int nameIndex = Integer.parseInt(singleResult[0]);
float score = Float.parseFloat(singleResult[1]);
if (score > 0.5) {
recognitionObjectBeanList.add(new RecognitionImageBean(singleResult[0], score));
recognitionObjectBeanList.add(new RecognitionImageBean(IMAGECONTENT[nameIndex], score));
}
}
Collections.sort(recognitionObjectBeanList, (t1, t2) -> Float.compare(t2.getScore(), t1.getScore()));

@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mindspore.classificationforpet.widget;
import android.annotation.SuppressLint;

@ -133,21 +133,18 @@ public class MainActivity extends AppCompatActivity implements OnBackgroundImage
private void openAppDetails() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("HiMindSpore需要访问 “相机” 和 “外部存储器”,请到 “应用信息 -> 权限” 中授予!");
builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
}
builder.setMessage(getString(R.string.app_choose_authority));
builder.setPositiveButton(getString(R.string.app_choose_authority_manual), (dialog, which) -> {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
});
builder.setNegativeButton("取消", null);
builder.setNegativeButton(getString(R.string.app_choose_cancle), null);
builder.show();
}
@ -247,8 +244,6 @@ public class MainActivity extends AppCompatActivity implements OnBackgroundImage
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void initMindspore(Bitmap bitmap) {
@ -265,8 +260,10 @@ public class MainActivity extends AppCompatActivity implements OnBackgroundImage
long startTime = System.currentTimeMillis();
String result = trackingMobile.MindSpore_runnet(bitmap);
long endTime = System.currentTimeMillis();
String[] IMAGECONTENT = getResources().getStringArray(R.array.image_category_pet);
int nameIndex = Integer.parseInt(result);
progressBar.setVisibility(View.GONE);
textResult.setText(result);
textResult.setText(IMAGECONTENT[nameIndex]);
Log.d(TAG, "RUNNET CONSUMING" + (endTime - startTime) + "ms");
Log.d(TAG, "result" + result);
} else {
@ -289,14 +286,16 @@ public class MainActivity extends AppCompatActivity implements OnBackgroundImage
progressBar.setVisibility(View.GONE);
Log.d(TAG, "RUNNET CONSUMING" + (endTime - startTime) + "ms");
Log.d(TAG, "result" + result);
String[] IMAGECONTENT = getResources().getStringArray(R.array.image_category);
if (!TextUtils.isEmpty(result)) {
String[] resultArray = result.split(";");
for (String singleRecognitionResult : resultArray) {
String[] singleResult = singleRecognitionResult.split(":");
int nameIndex = Integer.parseInt(singleResult[0]);
float score = Float.parseFloat(singleResult[1]);
if (score > 0.5) {
recognitionObjectBeanList.add(new RecognitionImageBean(singleResult[0], score));
recognitionObjectBeanList.add(new RecognitionImageBean(IMAGECONTENT[nameIndex], score));
}
}
Collections.sort(recognitionObjectBeanList, (t1, t2) -> Float.compare(t2.getScore(), t1.getScore()));

@ -1,9 +1,21 @@
/**
* Copyright 2021 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.classificationforpet.widget;
import android.view.View;
public interface OnBackgroundImageListener {
void onBackImageSelected(int position);
// void onImageAdd(View view);
}

@ -57,11 +57,7 @@ public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapte
view.setTag(IMAGES[position]);
view.setOnClickListener(view1 -> {
if (mListener != null) {
// if (IMAGES.length - 1 == position) {
// mListener.onImageAdd(holder.getImageView());
// } else {
mListener.onBackImageSelected(position);
// }
mListener.onBackImageSelected(position);
}
});
}

@ -18,7 +18,6 @@
android:background="@android:color/black"
tools:context="com.mindspore.classificationforpet.widget.CameraActivity" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
@ -33,7 +32,7 @@
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:maxLines="1"
android:text="MS Pet Classification"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
@ -47,7 +46,7 @@
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:text="正在识别..."
android:text="@string/app_result_identifying"
android:textColor="@color/black"
android:textSize="25sp" />
</RelativeLayout>

@ -19,7 +19,7 @@
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:maxLines="1"
android:text="Pet Classification"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
@ -53,7 +53,7 @@
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="Choose an image"
android:text="@string/app_choose_an_image"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="20sp" />
@ -73,7 +73,7 @@
android:background="@color/gray_btn"
android:gravity="center"
android:onClick="onClickPhoto"
android:text="PHOTO"
android:text="@string/app_photo"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="12sp" />
@ -87,7 +87,7 @@
android:background="@color/gray_btn"
android:gravity="center"
android:onClick="onClickCamera"
android:text="CAMERA"
android:text="@string/app_camera"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="12sp" />
@ -101,12 +101,10 @@
android:background="@color/gray_btn"
android:gravity="center"
android:onClick="onClickScene"
android:text="SCAN"
android:text="@string/app_scan"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
@ -114,7 +112,7 @@
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="Choose a sample"
android:text="@string/app_choose_a_sample"
android:textColor="@color/white"
android:textSize="20sp" />

Loading…
Cancel
Save