diff --git a/model_zoo/official/lite/Himindspore/imageObject/src/main/java/com/mindspore/imageobject/objectdetection/ui/ObjectPhotoActivity.java b/model_zoo/official/lite/Himindspore/imageObject/src/main/java/com/mindspore/imageobject/objectdetection/ui/ObjectPhotoActivity.java index d64d883a14..ad4b3cbc68 100644 --- a/model_zoo/official/lite/Himindspore/imageObject/src/main/java/com/mindspore/imageobject/objectdetection/ui/ObjectPhotoActivity.java +++ b/model_zoo/official/lite/Himindspore/imageObject/src/main/java/com/mindspore/imageobject/objectdetection/ui/ObjectPhotoActivity.java @@ -28,6 +28,7 @@ import android.util.Log; import android.util.Pair; import android.view.View; import android.widget.ImageView; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; @@ -92,6 +93,7 @@ public class ObjectPhotoActivity extends AppCompatActivity { showOriginImage(); } } else { + Toast.makeText(this, R.string.image_invalid, Toast.LENGTH_LONG).show(); finish(); } } @@ -107,10 +109,11 @@ public class ObjectPhotoActivity extends AppCompatActivity { if (originBitmap != null) { initMindspore(originBitmap); preview.setImageBitmap(originBitmap); + } else { + Toast.makeText(this, R.string.image_invalid, Toast.LENGTH_LONG).show(); } } - private void initMindspore(Bitmap bitmap) { try { trackingMobile = new ObjectTrackingMobile(this); @@ -118,9 +121,7 @@ public class ObjectPhotoActivity extends AppCompatActivity { Log.e(TAG, Log.getStackTraceString(e)); e.printStackTrace(); } - // 加载模型 boolean ret = trackingMobile.loadModelFromBuf(getAssets()); - if (!ret) { Log.e(TAG, "Load model error."); return; @@ -137,6 +138,8 @@ public class ObjectPhotoActivity extends AppCompatActivity { if (recognitionObjectBeanList != null && recognitionObjectBeanList.size() > 0) { drawRect(bitmap); + } else { + Toast.makeText(this, R.string.train_invalid, Toast.LENGTH_LONG).show(); } } diff --git a/model_zoo/official/lite/Himindspore/imageObject/src/main/res/values/strings.xml b/model_zoo/official/lite/Himindspore/imageObject/src/main/res/values/strings.xml index 69bc2200ca..858ec34a27 100644 --- a/model_zoo/official/lite/Himindspore/imageObject/src/main/res/values/strings.xml +++ b/model_zoo/official/lite/Himindspore/imageObject/src/main/res/values/strings.xml @@ -20,4 +20,6 @@ Inference Time + The image path you selected is not valid. Please choose again + Sorry, there is no object identified in this picture. Try another picture \ No newline at end of file diff --git a/model_zoo/official/lite/object_detection/app/build.gradle b/model_zoo/official/lite/object_detection/app/build.gradle index a7a88d896a..8bfecefa11 100644 --- a/model_zoo/official/lite/object_detection/app/build.gradle +++ b/model_zoo/official/lite/object_detection/app/build.gradle @@ -13,7 +13,7 @@ android { minSdkVersion 21 targetSdkVersion 30 versionCode 1 - versionName "1.0" + versionName "1.0.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { @@ -23,12 +23,10 @@ android { } } ndk { - //abiFilters 'arm64-v8a', 'armeabi-v7a' abiFilters 'arm64-v8a' } } aaptOptions { -// noCompress '.so', ".tflite", ".mnn", ".cambricon", '.mslite', 'om', 'ms' noCompress '.so', 'ms' } diff --git a/model_zoo/official/lite/object_detection/app/src/main/AndroidManifest.xml b/model_zoo/official/lite/object_detection/app/src/main/AndroidManifest.xml index 8061839645..d3b3d37153 100644 --- a/model_zoo/official/lite/object_detection/app/src/main/AndroidManifest.xml +++ b/model_zoo/official/lite/object_detection/app/src/main/AndroidManifest.xml @@ -20,10 +20,6 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - diff --git a/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/SplashActivity.java b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/SplashActivity.java index 6c69aa6b12..2c1c19d559 100644 --- a/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/SplashActivity.java +++ b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/SplashActivity.java @@ -1,52 +1,75 @@ package com.mindspore.hiobject; import android.Manifest; +import android.app.ProgressDialog; +import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; +import android.provider.Settings; import android.view.View; import android.widget.Button; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import com.mindspore.hiobject.objectdetect.CameraActivity; import com.mindspore.hiobject.objectdetect.PhotoActivity; -public class SplashActivity extends AppCompatActivity implements View.OnClickListener { +public class SplashActivity extends AppCompatActivity { + private static final String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA}; + private static final int REQUEST_PERMISSION = 1; - private static final int RC_CHOOSE_PHOTO = 1; - private static final int REQUEST_CAMERA_PERMISSION = 2; - - private Button btnPhoto, btnCamera; + private boolean isAllGranted; + private TextView versionText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); + versionText = findViewById(R.id.tv_vision); + showPackageInfo(); + requestPermissions(); + } - btnPhoto = findViewById(R.id.btn_photo); - btnCamera = findViewById(R.id.btn_camera); - - btnPhoto.setOnClickListener(this); - btnCamera.setOnClickListener(this); - + private void showPackageInfo() { + try { + PackageManager packageManager = this.getPackageManager(); + PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0); + versionText.setText("Version: " + packageInfo.versionName); + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + } } + private void requestPermissions() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + isAllGranted = checkPermissionAllGranted(PERMISSIONS); + if (!isAllGranted) { + ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_PERMISSION); + } + } else { + isAllGranted = true; + } + } - @Override - public void onClick(View view) { - if (R.id.btn_photo == view.getId()) { - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, - Manifest.permission.READ_PHONE_STATE}, RC_CHOOSE_PHOTO); - } else if (R.id.btn_camera == view.getId()) { - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, - Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION); + private boolean checkPermissionAllGranted(String[] permissions) { + for (String permission : permissions) { + if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { + return false; + } } + return true; } /** @@ -54,33 +77,56 @@ public class SplashActivity extends AppCompatActivity implements View.OnClickLis */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - if (RC_CHOOSE_PHOTO == requestCode) { - choosePhoto(); - } else if (REQUEST_CAMERA_PERMISSION == requestCode) { - chooseCamera(); + if (REQUEST_PERMISSION == requestCode) { + isAllGranted = true; + for (int grant : grantResults) { + if (grant != PackageManager.PERMISSION_GRANTED) { + isAllGranted = false; + break; + } + } + if (!isAllGranted) { + openAppDetails(); + } } } - - 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); - } - - private void chooseCamera() { - Intent intent = new Intent(SplashActivity.this, CameraActivity.class); - startActivity(intent); + 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.setNegativeButton("取消", null); + builder.show(); } - @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()) { + public void onClickPhoto(View view) { + if (isAllGranted){ Intent intent = new Intent(SplashActivity.this, PhotoActivity.class); - intent.setData(data.getData()); startActivity(intent); + }else{ + requestPermissions(); + } + } + + public void onClickCamera(View view) { + if (isAllGranted){ + Intent intent = new Intent(SplashActivity.this, CameraActivity.class); + startActivity(intent); + }else{ + requestPermissions(); } } } diff --git a/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/help/BitmapUtils.java b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/help/BitmapUtils.java new file mode 100644 index 0000000000..f8321c25f2 --- /dev/null +++ b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/help/BitmapUtils.java @@ -0,0 +1,151 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mindspore.hiobject.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; + } +} diff --git a/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/help/DisplayUtil.java b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/help/DisplayUtil.java new file mode 100644 index 0000000000..2f657127c4 --- /dev/null +++ b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/help/DisplayUtil.java @@ -0,0 +1,48 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mindspore.hiobject.help; + +import android.content.Context; + +public class DisplayUtil { + + private DisplayUtil() { + /* cannot be instantiated */ + throw new UnsupportedOperationException("cannot be instantiated"); + } + + + public static int px2dip(Context context, float pxValue) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (pxValue / scale + 0.5f); + } + + + public static int dip2px(Context context, float dipValue) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (dipValue * scale + 0.5f); + } + + public static int px2sp(Context context, float pxValue) { + final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; + return (int) (pxValue / fontScale + 0.5f); + } + + public static int sp2px(Context context, float spValue) { + final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; + return (int) (spValue * fontScale + 0.5f); + } +} \ No newline at end of file diff --git a/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/objectdetect/ObjectRectView.java b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/objectdetect/ObjectRectView.java index 4b5306f07d..16b9e4bd97 100644 --- a/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/objectdetect/ObjectRectView.java +++ b/model_zoo/official/lite/object_detection/app/src/main/java/com/mindspore/hiobject/objectdetect/ObjectRectView.java @@ -2,7 +2,6 @@ package com.mindspore.hiobject.objectdetect; import android.content.Context; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; @@ -10,6 +9,8 @@ import android.util.AttributeSet; import android.util.Log; import android.view.View; +import com.mindspore.hiobject.R; +import com.mindspore.hiobject.help.DisplayUtil; import com.mindspore.hiobject.help.RecognitionObjectBean; import java.util.ArrayList; @@ -32,34 +33,34 @@ public class ObjectRectView extends View { // Frame area private RectF mObjRectF; + private Context context; public ObjectRectView(Context context) { - super(context); - initialize(); + this(context, null); } public ObjectRectView(Context context, AttributeSet attrs) { - super(context, attrs); - initialize(); + this(context, attrs, 0); } public ObjectRectView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + this.context = context; initialize(); } - public int[] MyColor = {Color.RED, Color.WHITE, Color.YELLOW, Color.GREEN, Color.LTGRAY, Color.MAGENTA, Color.BLACK, Color.BLUE, Color.CYAN}; + private static final int[] MyColor = {R.color.white, R.color.text_blue, R.color.text_yellow, R.color.text_orange, R.color.text_green}; private void initialize() { mObjRectF = new RectF(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); - mPaint.setTextSize(50); + mPaint.setTextSize(DisplayUtil.sp2px(context, 16)); //Draw only outline (stroke) mPaint.setStyle(Style.STROKE); - mPaint.setStrokeWidth(5); + mPaint.setStrokeWidth(DisplayUtil.dip2px(context, 2)); } /** @@ -68,15 +69,14 @@ public class ObjectRectView extends View { * @param recognitions */ public void setInfo(List recognitions) { - Log.i(TAG, "setInfo: "+recognitions.size()); + Log.i(TAG, "setInfo: " + recognitions.size()); mRecognitions.clear(); mRecognitions.addAll(recognitions); - invalidate(); } - public void clearCanvas(){ + public void clearCanvas() { mRecognitions.clear(); invalidate(); } @@ -88,21 +88,21 @@ public class ObjectRectView extends View { if (mRecognitions == null || mRecognitions.size() == 0) { return; } - for (int i = 0;i recognitionObjectBeanList; + + private Integer maxWidthOfImage; + private Integer maxHeightOfImage; + boolean isLandScape; + private Bitmap originBitmap; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo); + preview = findViewById(R.id.img_photo); + this.isLandScape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; + openGallay(); - 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) { - Matrix matrix = new Matrix(); - matrix.setScale(0.7f, 0.7f); - bitmap = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); - - imgPhoto.setImageBitmap(bitmap); - initMindspore(bitmap); + 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) { + showOriginImage(data.getData()); } + } else { + Toast.makeText(this, R.string.image_invalid, Toast.LENGTH_LONG).show(); + finish(); + } + } + + private void showOriginImage(Uri imageUri) { + Pair targetedSize = this.getTargetSize(); + int targetWidth = targetedSize.first; + int maxHeight = targetedSize.second; + originBitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight); + // Determine how much to scale down the image. + Log.i(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); + if (originBitmap != null) { + initMindspore(originBitmap); + preview.setImageBitmap(originBitmap); + } else { + Toast.makeText(this, R.string.image_invalid, Toast.LENGTH_LONG).show(); } } @@ -64,9 +98,7 @@ public class PhotoActivity extends AppCompatActivity { Log.e(TAG, Log.getStackTraceString(e)); e.printStackTrace(); } - // 加载模型 boolean ret = trackingMobile.loadModelFromBuf(getAssets()); - if (!ret) { Log.e(TAG, "Load model error."); return; @@ -76,46 +108,83 @@ public class PhotoActivity extends AppCompatActivity { String result = trackingMobile.MindSpore_runnet(bitmap); long endTime = System.currentTimeMillis(); - Log.d(TAG, "RUNNET CONSUMING:"+(endTime-startTime)+"ms"); - Log.d(TAG, "result:"+ result); + Log.d(TAG, "RUNNET CONSUMING:" + (endTime - startTime) + "ms"); + Log.d(TAG, "result:" + result); recognitionObjectBeanList = getRecognitionList(result); if (recognitionObjectBeanList != null && recognitionObjectBeanList.size() > 0) { drawRect(bitmap); + } else { + Toast.makeText(this, R.string.train_invalid, Toast.LENGTH_LONG).show(); } } private void drawRect(Bitmap bitmap) { Canvas canvas = new Canvas(bitmap); - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setTextSize(dip2px(15)); - paint.setStyle(Paint.Style.STROKE); - paint.setStrokeWidth(3); + Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mPaint.setTextSize(DisplayUtil.sp2px(this, 16)); + //Draw only outline (stroke) + 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 = COLORS[i % COLORS.length]; - paint.setColor(paintColor); + 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, paint); - canvas.drawText(sb.toString(),objectBean.getLeft(), objectBean.getTop()-10,paint); + canvas.drawRect(rectF, mPaint); + canvas.drawText(sb.toString(), objectBean.getLeft(), objectBean.getTop() - 10, mPaint); } } @Override protected void onDestroy() { super.onDestroy(); - trackingMobile.unloadModel(); + if (trackingMobile != null) { + trackingMobile.unloadModel(); + } + BitmapUtils.recycleBitmap(originBitmap); } - public int dip2px(float dipValue){ - float scale = getResources().getDisplayMetrics().density; - return (int) (dipValue*scale+0.5f); + // 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(TAG, "height:" + targetHeight + ",width:" + targetWidth); + return new Pair<>(targetWidth, targetHeight); } } \ No newline at end of file diff --git a/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/btn_image.png b/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/btn_image.png new file mode 100644 index 0000000000..a5e78d02d2 Binary files /dev/null and b/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/btn_image.png differ diff --git a/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/btn_object.png b/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/btn_object.png new file mode 100644 index 0000000000..5fce148070 Binary files /dev/null and b/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/btn_object.png differ diff --git a/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/logo.png b/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/logo.png new file mode 100644 index 0000000000..4dd2da3ecb Binary files /dev/null and b/model_zoo/official/lite/object_detection/app/src/main/res/drawable-xxhdpi/logo.png differ diff --git a/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_photo.xml b/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_photo.xml index c24b18ebc7..cad3459d7a 100644 --- a/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_photo.xml +++ b/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_photo.xml @@ -1,13 +1,15 @@ - + android:fitsSystemWindows="true" + android:keepScreenOn="true" + android:orientation="vertical"> - \ No newline at end of file + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_centerInParent="true" + android:scaleType="fitXY" /> + \ No newline at end of file diff --git a/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_splash.xml b/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_splash.xml index a0cb447982..7dbabeacc4 100644 --- a/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_splash.xml +++ b/model_zoo/official/lite/object_detection/app/src/main/res/layout/activity_splash.xml @@ -7,46 +7,76 @@ android:background="@color/colorPrimary" tools:context=".SplashActivity"> - +