| @@ -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 | |||
| @@ -46,7 +46,7 @@ | |||
| android:screenOrientation="portrait" | |||
| android:theme="@style/Theme.AppCompat.NoActionBar" /> | |||
| <activity | |||
| android:name=".objectdetection.ui.PhotoActivity" | |||
| android:name=".objectdetection.ui.ObjectPhotoActivity" | |||
| android:screenOrientation="portrait" | |||
| android:theme="@style/Theme.AppCompat.NoActionBar" /> | |||
| <activity | |||
| @@ -0,0 +1,136 @@ | |||
| package com.mindspore.himindspore.objectdetection.help; | |||
| 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; | |||
| } | |||
| } | |||
| @@ -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); | |||
| } | |||
| } | |||
| } | |||
| @@ -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<RecognitionObjectBean> 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<Integer, Integer> 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<Integer, Integer> 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); | |||
| } | |||
| } | |||
| @@ -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<RecognitionObjectBean> 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(); | |||
| } | |||
| } | |||
| @@ -1,18 +1,16 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||
| xmlns:app="http://schemas.android.com/apk/res-auto" | |||
| xmlns:tools="http://schemas.android.com/tools" | |||
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||
| android:layout_width="match_parent" | |||
| android:layout_height="match_parent" | |||
| tools:context=".objectdetection.ui.PhotoActivity"> | |||
| android:background="@color/colorPrimary" | |||
| android:fitsSystemWindows="true" | |||
| android:keepScreenOn="true" | |||
| android:orientation="vertical"> | |||
| <ImageView | |||
| android:id="@+id/img_photo" | |||
| android:layout_width="match_parent" | |||
| android:layout_height="match_parent" | |||
| android:scaleType="fitCenter" | |||
| app:layout_constraintBottom_toBottomOf="parent" | |||
| app:layout_constraintEnd_toEndOf="parent" | |||
| app:layout_constraintStart_toStartOf="parent" | |||
| app:layout_constraintTop_toTopOf="parent" /> | |||
| </androidx.constraintlayout.widget.ConstraintLayout> | |||
| <ImageView | |||
| android:id="@+id/img_photo" | |||
| android:layout_width="wrap_content" | |||
| android:layout_height="wrap_content" | |||
| android:layout_centerInParent="true" | |||
| android:scaleType="fitXY"/> | |||
| </RelativeLayout> | |||