| @@ -87,10 +87,8 @@ public class ObjectPhotoActivity extends AppCompatActivity { | |||
| super.onActivityResult(requestCode, resultCode, data); | |||
| if (RC_CHOOSE_PHOTO == requestCode && null != data && null != data.getData()) { | |||
| if (data != null) { | |||
| this.imageUri = data.getData(); | |||
| showOriginImage(); | |||
| } | |||
| } else { | |||
| finish(); | |||
| } | |||
| @@ -204,7 +202,7 @@ public class ObjectPhotoActivity extends AppCompatActivity { | |||
| Integer maxHeight = this.getMaxHeightOfImage(); | |||
| targetWidth = this.isLandScape ? maxHeight : maxWidth; | |||
| targetHeight = this.isLandScape ? maxWidth : maxHeight; | |||
| Log.i(ObjectPhotoActivity.TAG, "height:" + targetHeight + ",width:" + targetWidth); | |||
| Log.i(TAG, "height:" + targetHeight + ",width:" + targetWidth); | |||
| return new Pair<>(targetWidth, targetHeight); | |||
| } | |||
| } | |||
| @@ -88,12 +88,12 @@ public class ObjectRectView extends View { | |||
| mRecognitions.clear(); | |||
| mRecognitions.addAll(recognitions); | |||
| invalidate(); | |||
| postInvalidate(); | |||
| } | |||
| public void clearCanvas() { | |||
| mRecognitions.clear(); | |||
| invalidate(); | |||
| postInvalidate(); | |||
| } | |||
| @Override | |||
| @@ -60,8 +60,8 @@ public class Posenet { | |||
| } | |||
| public class Position { | |||
| int x; | |||
| int y; | |||
| float x; | |||
| float y; | |||
| } | |||
| public class KeyPoint { | |||
| @@ -151,7 +151,6 @@ public class Posenet { | |||
| int[] intValues = new int[bitmap.getWidth() * bitmap.getHeight()]; | |||
| bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); | |||
| int pixel = 0; | |||
| for (int y = 0; y < bitmap.getHeight(); y++) { | |||
| for (int x = 0; x < bitmap.getWidth(); x++) { | |||
| @@ -281,16 +280,16 @@ public class Posenet { | |||
| } | |||
| // Calculating the x and y coordinates of the keypoints with offset adjustment. | |||
| int[] xCoords = new int[numKeypoints]; | |||
| int[] yCoords = new int[numKeypoints]; | |||
| float[] xCoords = new float[numKeypoints]; | |||
| float[] yCoords = new float[numKeypoints]; | |||
| float[] confidenceScores = new float[numKeypoints]; | |||
| for (int i = 0; i < keypointPositions.length; i++) { | |||
| Pair position = keypointPositions[i]; | |||
| int positionY = (int) position.first; | |||
| int positionX = (int) position.second; | |||
| yCoords[i] = (int) ((float) positionY / (float) (height - 1) * bitmap.getHeight() + offsets[0][positionY][positionX][i]); | |||
| xCoords[i] = (int) ((float) positionX / (float) (width - 1) * bitmap.getWidth() + offsets[0][positionY][positionX][i + numKeypoints]); | |||
| yCoords[i] = ((float) positionY / (float) (height - 1) * bitmap.getHeight() + offsets[0][positionY][positionX][i]); | |||
| xCoords[i] = ((float) positionX / (float) (width - 1) * bitmap.getWidth() + offsets[0][positionY][positionX][i + numKeypoints]); | |||
| confidenceScores[i] = sigmoid(heatmaps[0][positionY][positionX][i]); | |||
| } | |||
| @@ -52,7 +52,14 @@ import static com.mindspore.posenet.Posenet.BodyPart.RIGHT_WRIST; | |||
| @Route(path = "/posenet/PosenetMainActivity") | |||
| public class PosenetMainActivity extends AppCompatActivity implements CameraDataDealListener { | |||
| private final List bodyJoints; | |||
| private final List bodyJoints = Arrays.asList( | |||
| new Pair(LEFT_WRIST, LEFT_ELBOW), new Pair(LEFT_ELBOW, LEFT_SHOULDER), | |||
| new Pair(LEFT_SHOULDER, RIGHT_SHOULDER), new Pair(RIGHT_SHOULDER, RIGHT_ELBOW), | |||
| new Pair(RIGHT_ELBOW, RIGHT_WRIST), new Pair(LEFT_SHOULDER, LEFT_HIP), | |||
| new Pair(LEFT_HIP, RIGHT_HIP), new Pair(RIGHT_HIP, RIGHT_SHOULDER), | |||
| new Pair(LEFT_HIP, LEFT_KNEE), new Pair(LEFT_KNEE, LEFT_ANKLE), | |||
| new Pair(RIGHT_HIP, RIGHT_KNEE), new Pair(RIGHT_KNEE, RIGHT_ANKLE)); | |||
| ; | |||
| /** | |||
| @@ -74,16 +81,6 @@ public class PosenetMainActivity extends AppCompatActivity implements CameraData | |||
| private int lensFacing = CameraCharacteristics.LENS_FACING_BACK; | |||
| private PoseNetFragment poseNetFragment; | |||
| public PosenetMainActivity() { | |||
| bodyJoints = Arrays.asList( | |||
| new Pair(LEFT_WRIST, LEFT_ELBOW), new Pair(LEFT_ELBOW, LEFT_SHOULDER), | |||
| new Pair(LEFT_SHOULDER, RIGHT_SHOULDER), new Pair(RIGHT_SHOULDER, RIGHT_ELBOW), | |||
| new Pair(RIGHT_ELBOW, RIGHT_WRIST), new Pair(LEFT_SHOULDER, LEFT_HIP), | |||
| new Pair(LEFT_HIP, RIGHT_HIP), new Pair(RIGHT_HIP, RIGHT_SHOULDER), | |||
| new Pair(LEFT_HIP, LEFT_KNEE), new Pair(LEFT_KNEE, LEFT_ANKLE), | |||
| new Pair(RIGHT_HIP, RIGHT_KNEE), new Pair(RIGHT_KNEE, RIGHT_ANKLE)); | |||
| } | |||
| @Override | |||
| protected void onCreate(Bundle savedInstanceState) { | |||
| super.onCreate(savedInstanceState); | |||
| @@ -211,8 +208,8 @@ public class PosenetMainActivity extends AppCompatActivity implements CameraData | |||
| new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), | |||
| new Rect(left, top, right, bottom), paint); | |||
| float widthRatio = screenWidth / MODEL_WIDTH; | |||
| float heightRatio = screenHeight / MODEL_HEIGHT; | |||
| float widthRatio = (float) screenWidth / MODEL_WIDTH; | |||
| float heightRatio = (float) screenHeight / MODEL_HEIGHT; | |||
| for (Posenet.KeyPoint keyPoint : person.keyPoints) { | |||
| if (keyPoint.score > minConfidence) { | |||
| @@ -14,18 +14,22 @@ | |||
| android:roundIcon="@mipmap/ic_launcher_round" | |||
| android:supportsRtl="true" | |||
| android:theme="@style/AppTheme"> | |||
| <activity android:name=".TestActivity" | |||
| <activity | |||
| android:name=".TestActivity" | |||
| android:screenOrientation="portrait" | |||
| android:theme="@style/Theme.AppCompat.NoActionBar"> | |||
| <intent-filter> | |||
| <action android:name="android.intent.action.MAIN" /> | |||
| <category android:name="android.intent.category.LAUNCHER" /> | |||
| </intent-filter> | |||
| </activity> | |||
| <activity android:name=".MainActivity" | |||
| <activity | |||
| android:name=".MainActivity" | |||
| android:screenOrientation="portrait" | |||
| android:theme="@style/Theme.AppCompat.NoActionBar"> | |||
| <intent-filter> | |||
| <action android:name="android.intent.action.MAIN" /> | |||
| <category android:name="android.intent.category.LAUNCHER" /> | |||
| </intent-filter> | |||
| </activity> | |||
| </application> | |||
| @@ -204,8 +204,8 @@ public class MainActivity extends AppCompatActivity implements CameraDataDealLis | |||
| new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), | |||
| new Rect(left, top, right, bottom), paint); | |||
| float widthRatio = screenWidth / MODEL_WIDTH; | |||
| float heightRatio = screenHeight / MODEL_HEIGHT; | |||
| float widthRatio = (float) screenWidth / MODEL_WIDTH; | |||
| float heightRatio = (float) screenHeight / MODEL_HEIGHT; | |||
| for (Posenet.KeyPoint keyPoint : person.keyPoints) { | |||
| if (keyPoint.score > minConfidence) { | |||
| @@ -60,8 +60,8 @@ public class Posenet { | |||
| } | |||
| public class Position { | |||
| int x; | |||
| int y; | |||
| float x; | |||
| float y; | |||
| } | |||
| public class KeyPoint { | |||
| @@ -151,7 +151,6 @@ public class Posenet { | |||
| int[] intValues = new int[bitmap.getWidth() * bitmap.getHeight()]; | |||
| bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); | |||
| int pixel = 0; | |||
| for (int y = 0; y < bitmap.getHeight(); y++) { | |||
| for (int x = 0; x < bitmap.getWidth(); x++) { | |||
| @@ -281,16 +280,16 @@ public class Posenet { | |||
| } | |||
| // Calculating the x and y coordinates of the keypoints with offset adjustment. | |||
| int[] xCoords = new int[numKeypoints]; | |||
| int[] yCoords = new int[numKeypoints]; | |||
| float[] xCoords = new float[numKeypoints]; | |||
| float[] yCoords = new float[numKeypoints]; | |||
| float[] confidenceScores = new float[numKeypoints]; | |||
| for (int i = 0; i < keypointPositions.length; i++) { | |||
| Pair position = keypointPositions[i]; | |||
| int positionY = (int) position.first; | |||
| int positionX = (int) position.second; | |||
| yCoords[i] = (int) ((float) positionY / (float) (height - 1) * bitmap.getHeight() + offsets[0][positionY][positionX][i]); | |||
| xCoords[i] = (int) ((float) positionX / (float) (width - 1) * bitmap.getWidth() + offsets[0][positionY][positionX][i + numKeypoints]); | |||
| yCoords[i] = ((float) positionY / (float) (height - 1) * bitmap.getHeight() + offsets[0][positionY][positionX][i]); | |||
| xCoords[i] = ((float) positionX / (float) (width - 1) * bitmap.getWidth() + offsets[0][positionY][positionX][i + numKeypoints]); | |||
| confidenceScores[i] = sigmoid(heatmaps[0][positionY][positionX][i]); | |||
| } | |||
| @@ -17,13 +17,29 @@ package com.mindspore.posenetdemo; | |||
| import android.graphics.Bitmap; | |||
| import android.graphics.Canvas; | |||
| import android.graphics.Color; | |||
| import android.graphics.Paint; | |||
| import android.graphics.drawable.Drawable; | |||
| import android.os.Bundle; | |||
| import android.widget.ImageView; | |||
| import androidx.appcompat.app.AppCompatActivity; | |||
| import androidx.core.util.Pair; | |||
| import java.util.Arrays; | |||
| import java.util.List; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.LEFT_ANKLE; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.LEFT_ELBOW; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.LEFT_HIP; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.LEFT_KNEE; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.LEFT_SHOULDER; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.LEFT_WRIST; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.RIGHT_ANKLE; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.RIGHT_ELBOW; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.RIGHT_HIP; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.RIGHT_KNEE; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.RIGHT_SHOULDER; | |||
| import static com.mindspore.posenetdemo.Posenet.BodyPart.RIGHT_WRIST; | |||
| public class TestActivity extends AppCompatActivity { | |||
| @@ -41,16 +57,33 @@ public class TestActivity extends AppCompatActivity { | |||
| // Draw the keypoints over the image. | |||
| Paint paint = new Paint(); | |||
| paint.setColor(Color.RED); | |||
| paint.setColor(getResources().getColor(R.color.text_blue)); | |||
| paint.setTextSize(80.0f); | |||
| paint.setStrokeWidth(5.0f); | |||
| Bitmap mutableBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888, true); | |||
| Canvas canvas = new Canvas(mutableBitmap); | |||
| for (Posenet.KeyPoint keypoint : person.keyPoints) { | |||
| canvas.drawCircle( | |||
| keypoint.position.x, | |||
| keypoint.position.y, 2.0f, paint); | |||
| } | |||
| for (int i = 0; i < bodyJoints.size(); i++) { | |||
| Pair line = (Pair) bodyJoints.get(i); | |||
| Posenet.BodyPart first = (Posenet.BodyPart) line.first; | |||
| Posenet.BodyPart second = (Posenet.BodyPart) line.second; | |||
| if (person.keyPoints.get(first.ordinal()).score > minConfidence & | |||
| person.keyPoints.get(second.ordinal()).score > minConfidence) { | |||
| canvas.drawLine( | |||
| person.keyPoints.get(first.ordinal()).position.x, | |||
| person.keyPoints.get(first.ordinal()).position.y, | |||
| person.keyPoints.get(second.ordinal()).position.x, | |||
| person.keyPoints.get(second.ordinal()).position.y, paint); | |||
| } | |||
| } | |||
| sampleImageView.setAdjustViewBounds(true); | |||
| sampleImageView.setImageBitmap(mutableBitmap); | |||
| @@ -64,4 +97,17 @@ public class TestActivity extends AppCompatActivity { | |||
| drawable.draw(canvas); | |||
| return bitmap; | |||
| } | |||
| private final static int MODEL_WIDTH = 257; | |||
| private final static int MODEL_HEIGHT = 257; | |||
| private final double minConfidence = 0.5; | |||
| private final float circleRadius = 8.0f; | |||
| private final List bodyJoints = Arrays.asList( | |||
| new Pair(LEFT_WRIST, LEFT_ELBOW), new Pair(LEFT_ELBOW, LEFT_SHOULDER), | |||
| new Pair(LEFT_SHOULDER, RIGHT_SHOULDER), new Pair(RIGHT_SHOULDER, RIGHT_ELBOW), | |||
| new Pair(RIGHT_ELBOW, RIGHT_WRIST), new Pair(LEFT_SHOULDER, LEFT_HIP), | |||
| new Pair(LEFT_HIP, RIGHT_HIP), new Pair(RIGHT_HIP, RIGHT_SHOULDER), | |||
| new Pair(LEFT_HIP, LEFT_KNEE), new Pair(LEFT_KNEE, LEFT_ANKLE), | |||
| new Pair(RIGHT_HIP, RIGHT_KNEE), new Pair(RIGHT_KNEE, RIGHT_ANKLE)); | |||
| } | |||