diff --git a/mindspore/lite/test/models_onnx.cfg b/mindspore/lite/test/models_onnx.cfg index b0355d84a5..fb7af1cae1 100644 --- a/mindspore/lite/test/models_onnx.cfg +++ b/mindspore/lite/test/models_onnx.cfg @@ -2,7 +2,7 @@ mtk_detect-mbv2-shortcut-400-400-simplified.onnx mtk_emotions-d2012-75.8%.onnx mtk_face_features_v3.onnx emotion-ferplus-8.onnx -#rcnn-ilsvrc13-9.onnx +rcnn-ilsvrc13-9.onnx efficientnet-lite4-11.onnx mobilenetv2-7.onnx shufflenet-v2-10.onnx @@ -11,7 +11,7 @@ densenet-9.onnx googlenet-9.onnx inception-v1-9.onnx inception-v2-9.onnx -#shufflenet-9.onnx +shufflenet-9.onnx squeezenet1.0-9.onnx ml_face_3d.onnx gts_version-RFB-320_simplified.onnx diff --git a/model_zoo/official/lite/Himindspore/app/src/main/AndroidManifest.xml b/model_zoo/official/lite/Himindspore/app/src/main/AndroidManifest.xml index d8be253874..781d6d6fcb 100644 --- a/model_zoo/official/lite/Himindspore/app/src/main/AndroidManifest.xml +++ b/model_zoo/official/lite/Himindspore/app/src/main/AndroidManifest.xml @@ -46,7 +46,7 @@ android:screenOrientation="portrait" android:theme="@style/Theme.AppCompat.NoActionBar" /> reqHeight || width > reqWidth) { + // Calculate height and required height scale. + final int heightRatio = Math.round((float) height / (float) reqHeight); + // Calculate width and required width scale. + final int widthRatio = Math.round((float) width / (float) reqWidth); + // Take the larger of the values. + inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio; + } + return inSampleSize; + } + + // Scale pictures to screen width. + private static Bitmap zoomImage(Bitmap imageBitmap, int targetWidth, int maxHeight) { + float scaleFactor = + Math.max( + (float) imageBitmap.getWidth() / (float) targetWidth, + (float) imageBitmap.getHeight() / (float) maxHeight); + Bitmap resizedBitmap = + Bitmap.createScaledBitmap( + imageBitmap, + (int) (imageBitmap.getWidth() / scaleFactor), + (int) (imageBitmap.getHeight() / scaleFactor), + true); + + return resizedBitmap; + } + + /** + * Get the rotation angle of the photo. + * + * @param path photo path. + * @return angle. + */ + public static int getRotationAngle(String path) { + int rotation = 0; + try { + ExifInterface exifInterface = new ExifInterface(path); + int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + switch (orientation) { + case ExifInterface.ORIENTATION_ROTATE_90: + rotation = 90; + break; + case ExifInterface.ORIENTATION_ROTATE_180: + rotation = 180; + break; + case ExifInterface.ORIENTATION_ROTATE_270: + rotation = 270; + break; + default: + break; + } + } catch (IOException e) { + Log.e(TAG, "Failed to get rotation: " + e.getMessage()); + } + return rotation; + } + + public static Bitmap rotateBitmap(Bitmap bitmap, int angle) { + Matrix matrix = new Matrix(); + matrix.postRotate(angle); + Bitmap result = null; + try { + result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + } catch (OutOfMemoryError e) { + Log.e(TAG, "Failed to rotate bitmap: " + e.getMessage()); + } + if (result == null) { + return bitmap; + } + return result; + } +} diff --git a/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/ObjectDetectionMainActivity.java b/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/ObjectDetectionMainActivity.java index d415d72488..4589340260 100644 --- a/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/ObjectDetectionMainActivity.java +++ b/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/ObjectDetectionMainActivity.java @@ -3,12 +3,10 @@ package com.mindspore.himindspore.objectdetection.ui; import android.Manifest; import android.content.Intent; import android.os.Bundle; -import android.provider.MediaStore; import android.view.View; import android.widget.Button; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; @@ -16,8 +14,6 @@ import com.mindspore.himindspore.R; public class ObjectDetectionMainActivity extends AppCompatActivity implements View.OnClickListener { - private static final int RC_CHOOSE_PHOTO = 1; - private static final int REQUEST_CAMERA_PERMISSION = 2; private static final int REQUEST_PHOTO_PERMISSION = 3; @@ -63,25 +59,13 @@ public class ObjectDetectionMainActivity extends AppCompatActivity implements Vi private void choosePhoto() { - Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null); - intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); - startActivityForResult(intentToPickPic, RC_CHOOSE_PHOTO); + Intent intent = new Intent(ObjectDetectionMainActivity.this, ObjectPhotoActivity.class); + startActivity(intent); } private void chooseCamera() { Intent intent = new Intent(ObjectDetectionMainActivity.this, ObjectCameraActivity.class); startActivity(intent); } - - @Override - protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { - super.onActivityResult(requestCode, resultCode, data); - - if (RC_CHOOSE_PHOTO == requestCode && null != data && null != data.getData()) { - Intent intent = new Intent(ObjectDetectionMainActivity.this, PhotoActivity.class); - intent.setData(data.getData()); - startActivity(intent); - } - } } diff --git a/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/ObjectPhotoActivity.java b/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/ObjectPhotoActivity.java new file mode 100644 index 0000000000..a03013596b --- /dev/null +++ b/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/ObjectPhotoActivity.java @@ -0,0 +1,191 @@ +package com.mindspore.himindspore.objectdetection.ui; + +import android.content.Intent; +import android.content.res.Configuration; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.RectF; +import android.net.Uri; +import android.os.Bundle; +import android.provider.MediaStore; +import android.util.Log; +import android.util.Pair; +import android.view.View; +import android.widget.ImageView; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + + +import com.mindspore.himindspore.R; +import com.mindspore.himindspore.objectdetection.help.BitmapUtils; +import com.mindspore.himindspore.objectdetection.help.ObjectTrackingMobile; +import com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean; +import com.mindspore.himindspore.utils.DisplayUtil; + +import java.io.FileNotFoundException; +import java.util.List; + +import static com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean.getRecognitionList; + + +public class ObjectPhotoActivity extends AppCompatActivity { + + private static final String TAG = "ObjectPhotoActivity"; + private static final int[] COLORS ={R.color.white,R.color.text_blue,R.color.text_yellow,R.color.text_orange,R.color.text_green}; + + private static final int RC_CHOOSE_PHOTO = 1; + + private ImageView preview; + private ObjectTrackingMobile trackingMobile; + private List recognitionObjectBeanList; + + + private Integer maxWidthOfImage; + private Integer maxHeightOfImage; + boolean isLandScape; + private Bitmap originBitmap; + + Uri imageUri; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_object_photo); + + preview = findViewById(R.id.img_photo); + + this.isLandScape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; + openGallay(); + + } + + private void openGallay(){ + Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null); + intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); + startActivityForResult(intentToPickPic, RC_CHOOSE_PHOTO); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (RC_CHOOSE_PHOTO == requestCode && null != data && null != data.getData()) { + if (data != null) { + this.imageUri = data.getData(); + showOriginImage(); + } + } + } + + private void showOriginImage(){ + Pair targetedSize = this.getTargetSize(); + int targetWidth = targetedSize.first; + int maxHeight = targetedSize.second; + originBitmap = BitmapUtils.loadFromPath(ObjectPhotoActivity.this, imageUri, targetWidth, maxHeight); + // Determine how much to scale down the image. + Log.i(ObjectPhotoActivity.TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); + + if (originBitmap != null) { + initMindspore(originBitmap); + preview.setImageBitmap(originBitmap); + } + } + + + private void initMindspore(Bitmap bitmap) { + try { + trackingMobile = new ObjectTrackingMobile(this); + } catch (FileNotFoundException e) { + Log.e(TAG, Log.getStackTraceString(e)); + e.printStackTrace(); + } + // 加载模型 + boolean ret = trackingMobile.loadModelFromBuf(getAssets()); + + if (!ret) { + Log.e(TAG, "Load model error."); + return; + } + // run net. + long startTime = System.currentTimeMillis(); + String result = trackingMobile.MindSpore_runnet(bitmap); + long endTime = System.currentTimeMillis(); + + Log.d(TAG, "RUNNET 耗时:"+(endTime-startTime)+"ms"); + Log.d(TAG, "result:"+ result); + + recognitionObjectBeanList = getRecognitionList(result); + + if (recognitionObjectBeanList != null && recognitionObjectBeanList.size() > 0) { + drawRect(bitmap); + } + } + + private void drawRect(Bitmap bitmap) { + Canvas canvas = new Canvas(bitmap); + Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mPaint.setTextSize(DisplayUtil.sp2px(this,16)); + //只绘制图形轮廓(描边) + mPaint.setStyle(Paint.Style.STROKE); + mPaint.setStrokeWidth(DisplayUtil.dip2px(this,2)); + + for (int i = 0; i < recognitionObjectBeanList.size(); i++) { + RecognitionObjectBean objectBean = recognitionObjectBeanList.get(i); + StringBuilder sb = new StringBuilder(); + sb.append(objectBean.getRectID()).append("_").append(objectBean.getObjectName()).append("_").append(String.format("%.2f", (100 * objectBean.getScore())) + "%"); + + int paintColor =getResources().getColor(COLORS[i % COLORS.length]); + mPaint.setColor(paintColor); + + RectF rectF = new RectF(objectBean.getLeft(), objectBean.getTop(), objectBean.getRight(), objectBean.getBottom()); + canvas.drawRect(rectF, mPaint); + canvas.drawText(sb.toString(),objectBean.getLeft(), objectBean.getTop()-10,mPaint); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + trackingMobile.unloadModel(); + BitmapUtils.recycleBitmap(originBitmap); + } + + + // Returns max width of image. + private Integer getMaxWidthOfImage() { + if (this.maxWidthOfImage == null) { + if (this.isLandScape) { + this.maxWidthOfImage = ((View) this.preview.getParent()).getHeight(); + } else { + this.maxWidthOfImage = ((View) this.preview.getParent()).getWidth(); + } + } + return this.maxWidthOfImage; + } + + // Returns max height of image. + private Integer getMaxHeightOfImage() { + if (this.maxHeightOfImage == null) { + if (this.isLandScape) { + this.maxHeightOfImage = ((View) this.preview.getParent()).getWidth(); + } else { + this.maxHeightOfImage = ((View) this.preview.getParent()).getHeight(); + } + } + return this.maxHeightOfImage; + } + + // Gets the targeted size(width / height). + private Pair getTargetSize() { + Integer targetWidth; + Integer targetHeight; + Integer maxWidth = this.getMaxWidthOfImage(); + Integer maxHeight = this.getMaxHeightOfImage(); + targetWidth = this.isLandScape ? maxHeight : maxWidth; + targetHeight = this.isLandScape ? maxWidth : maxHeight; + Log.i(ObjectPhotoActivity.TAG, "height:" + targetHeight + ",width:" + targetWidth); + return new Pair<>(targetWidth, targetHeight); + } +} \ No newline at end of file diff --git a/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/PhotoActivity.java b/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/PhotoActivity.java deleted file mode 100644 index fa48f83ea7..0000000000 --- a/model_zoo/official/lite/Himindspore/app/src/main/java/com/mindspore/himindspore/objectdetection/ui/PhotoActivity.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.mindspore.himindspore.objectdetection.ui; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.RectF; -import android.net.Uri; -import android.os.Bundle; -import android.util.Log; -import android.widget.ImageView; - -import androidx.appcompat.app.AppCompatActivity; - - -import com.mindspore.himindspore.R; -import com.mindspore.himindspore.objectdetection.help.ObjectTrackingMobile; -import com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean; -import com.mindspore.himindspore.objectdetection.help.ImageDegreeHelper; -import com.mindspore.himindspore.utils.DisplayUtil; - -import java.io.FileNotFoundException; -import java.util.List; - -import static com.mindspore.himindspore.objectdetection.bean.RecognitionObjectBean.getRecognitionList; - - -public class PhotoActivity extends AppCompatActivity { - - private static final String TAG = "PhotoActivity"; - private static final int[] COLORS ={R.color.white,R.color.text_blue,R.color.text_yellow,R.color.text_orange,R.color.text_green}; - - private ImageView imgPhoto; - private ObjectTrackingMobile trackingMobile; - private List recognitionObjectBeanList; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_object_photo); - - imgPhoto = findViewById(R.id.img_photo); - - Uri uri = getIntent().getData(); - String imgPath = ImageDegreeHelper.getPath(this,uri); - int degree = ImageDegreeHelper.readPictureDegree(imgPath); - Bitmap originBitmap = BitmapFactory.decodeFile(imgPath); - if (originBitmap != null) { - Bitmap bitmap = ImageDegreeHelper.rotaingImageView(degree, originBitmap.copy(Bitmap.Config.ARGB_8888, true)); - if (bitmap != null) { - imgPhoto.setImageBitmap(bitmap); - initMindspore(bitmap); - } - } - } - - private void initMindspore(Bitmap bitmap) { - try { - trackingMobile = new ObjectTrackingMobile(this); - } catch (FileNotFoundException e) { - Log.e(TAG, Log.getStackTraceString(e)); - e.printStackTrace(); - } - // 加载模型 - boolean ret = trackingMobile.loadModelFromBuf(getAssets()); - - if (!ret) { - Log.e(TAG, "Load model error."); - return; - } - // run net. - long startTime = System.currentTimeMillis(); - String result = trackingMobile.MindSpore_runnet(bitmap); - long endTime = System.currentTimeMillis(); - - Log.d(TAG, "RUNNET 耗时:"+(endTime-startTime)+"ms"); - Log.d(TAG, "result:"+ result); - - recognitionObjectBeanList = getRecognitionList(result); - - if (recognitionObjectBeanList != null && recognitionObjectBeanList.size() > 0) { - drawRect(bitmap); - } - } - - private void drawRect(Bitmap bitmap) { - Canvas canvas = new Canvas(bitmap); - Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); - mPaint.setTextSize(DisplayUtil.sp2px(this,30)); - //只绘制图形轮廓(描边) - mPaint.setStyle(Paint.Style.STROKE); - mPaint.setStrokeWidth(DisplayUtil.dip2px(this,2)); - - for (int i = 0; i < recognitionObjectBeanList.size(); i++) { - RecognitionObjectBean objectBean = recognitionObjectBeanList.get(i); - StringBuilder sb = new StringBuilder(); - sb.append(objectBean.getRectID()).append("_").append(objectBean.getObjectName()).append("_").append(String.format("%.2f", (100 * objectBean.getScore())) + "%"); - - int paintColor =getResources().getColor(COLORS[i % COLORS.length]); - mPaint.setColor(paintColor); - - RectF rectF = new RectF(objectBean.getLeft(), objectBean.getTop(), objectBean.getRight(), objectBean.getBottom()); - canvas.drawRect(rectF, mPaint); - canvas.drawText(sb.toString(),objectBean.getLeft(), objectBean.getTop()-10,mPaint); - } - } - - @Override - protected void onDestroy() { - super.onDestroy(); - trackingMobile.unloadModel(); - } - -} \ No newline at end of file diff --git a/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_object_photo.xml b/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_object_photo.xml index c29d0fa7f9..f029ddc500 100644 --- a/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_object_photo.xml +++ b/model_zoo/official/lite/Himindspore/app/src/main/res/layout/activity_object_photo.xml @@ -1,18 +1,16 @@ - + android:background="@color/colorPrimary" + android:fitsSystemWindows="true" + android:keepScreenOn="true" + android:orientation="vertical"> - - \ No newline at end of file + + \ No newline at end of file