-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
162 lines (128 loc) · 5.64 KB
/
Copy pathMainActivity.java
File metadata and controls
162 lines (128 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package org.tensorflow.lite.examples.detection;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import org.tensorflow.lite.examples.detection.customview.OverlayView;
import org.tensorflow.lite.examples.detection.env.ImageUtils;
import org.tensorflow.lite.examples.detection.env.Logger;
import org.tensorflow.lite.examples.detection.env.Utils;
import org.tensorflow.lite.examples.detection.tflite.Classifier;
import org.tensorflow.lite.examples.detection.tflite.YoloV4Classifier;
import org.tensorflow.lite.examples.detection.tracking.MultiBoxTracker;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final float MINIMUM_CONFIDENCE_TF_OD_API = 0.5f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraButton = findViewById(R.id.cameraButton);
detectButton = findViewById(R.id.detectButton);
imageView = findViewById(R.id.imageView);
cameraButton.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, DetectorActivity.class)));
detectButton.setOnClickListener(v -> {
Handler handler = new Handler();
new Thread(() -> {
final List<Classifier.Recognition> results = detector.recognizeImage(cropBitmap);
handler.post(new Runnable() {
@Override
public void run() {
handleResult(cropBitmap, results);
}
});
}).start();
});
this.sourceBitmap = Utils.getBitmapFromAsset(MainActivity.this, "a1.jpg");
this.cropBitmap = Utils.processBitmap(sourceBitmap, TF_OD_API_INPUT_SIZE);
this.imageView.setImageBitmap(cropBitmap);
initBox();
}
private static final Logger LOGGER = new Logger();
public static final int TF_OD_API_INPUT_SIZE = 416;
private static final boolean TF_OD_API_IS_QUANTIZED = false;
private static final String TF_OD_API_MODEL_FILE = "yolov4-416-fp32.tflite";
private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/coco.txt";
// Minimum detection confidence to track a detection.
private static final boolean MAINTAIN_ASPECT = false;
private Integer sensorOrientation = 90;
private Classifier detector;
private Matrix frameToCropTransform;
private Matrix cropToFrameTransform;
private MultiBoxTracker tracker;
private OverlayView trackingOverlay;
protected int previewWidth = 0;
protected int previewHeight = 0;
private Bitmap sourceBitmap;
private Bitmap cropBitmap;
private Button cameraButton, detectButton;
private ImageView imageView;
private void initBox() {
previewHeight = TF_OD_API_INPUT_SIZE;
previewWidth = TF_OD_API_INPUT_SIZE;
frameToCropTransform =
ImageUtils.getTransformationMatrix(
previewWidth, previewHeight,
TF_OD_API_INPUT_SIZE, TF_OD_API_INPUT_SIZE,
sensorOrientation, MAINTAIN_ASPECT);
cropToFrameTransform = new Matrix();
frameToCropTransform.invert(cropToFrameTransform);
tracker = new MultiBoxTracker(this);
trackingOverlay = findViewById(R.id.tracking_overlay);
trackingOverlay.addCallback(
canvas -> tracker.draw(canvas));
tracker.setFrameConfiguration(TF_OD_API_INPUT_SIZE, TF_OD_API_INPUT_SIZE, sensorOrientation);
try {
detector =
YoloV4Classifier.create(
getAssets(),
TF_OD_API_MODEL_FILE,
TF_OD_API_LABELS_FILE,
TF_OD_API_IS_QUANTIZED);
} catch (final IOException e) {
e.printStackTrace();
LOGGER.e(e, "Exception initializing classifier!");
Toast toast =
Toast.makeText(
getApplicationContext(), "Classifier could not be initialized", Toast.LENGTH_SHORT);
toast.show();
finish();
}
}
private void handleResult(Bitmap bitmap, List<Classifier.Recognition> results) {
final Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2.0f);
final List<Classifier.Recognition> mappedRecognitions =
new LinkedList<Classifier.Recognition>();
for (final Classifier.Recognition result : results) {
final RectF location = result.getLocation();
if (location != null && result.getConfidence() >= MINIMUM_CONFIDENCE_TF_OD_API) {
canvas.drawRect(location, paint);
// cropToFrameTransform.mapRect(location);
//
// result.setLocation(location);
// mappedRecognitions.add(result);
}
}
// tracker.trackResults(mappedRecognitions, new Random().nextInt());
// trackingOverlay.postInvalidate();
imageView.setImageBitmap(bitmap);
}
}