ArrayList<Classifier.Recognition> detections = new ArrayList<Classifier.Recognition>();
float[][][] out = new float[1][10647][85];
Log.d("YoloV5Classifier", "out[0] detect start");
for (int i = 0; i < 10647; ++i) {
for (int j = 0; j < 85; ++j) {
out[0][j] = byteBuffer.getFloat();
}
// Denormalize xywh
for (int j = 0; j < 4; ++j) {
out[0][j] *= 416;
}
}
for (int i = 0; i < 10647; ++i) {
final int offset = 0;
final float confidence = out[0][4];
int detectedClass = -1;
float maxClass = 0;
final float[] classes = new float[80];
for (int c = 0; c < 80; ++c) {
classes[c] = out[0][5 + c];
}
for (int c = 0; c < 80; ++c) {
if (classes[c] > maxClass) {
detectedClass = c;
maxClass = classes[c];
}
}
final float confidenceInClass = maxClass * confidence;
Log.e("====", "====maxClass =" + maxClass);
Log.e("====", "====confidence =" + confidence);
Log.e("====", "====confidenceInClass =" + confidenceInClass);
if (confidenceInClass > 0.3f) {
final float xPos = out[0][0];
final float yPos = out[0][1];
final float w = out[0][2];
final float h = out[0][3];
Log.d("====YoloV5Classifier",
Float.toString(xPos) + ',' + yPos + ',' + w + ',' + h);
final RectF rect =
new RectF(
Math.max(0, xPos - w / 2),
Math.max(0, yPos - h / 2),
Math.min(bitmap.getWidth() - 1, xPos + w / 2),
Math.min(bitmap.getHeight() - 1, yPos + h / 2));
//non maximum suppression
protected ArrayList<Recognition> nms(ArrayList<Recognition> list) {
ArrayList<Recognition> nmsList = new ArrayList<Recognition>();
for (int k = 0; k < labels.size(); k++) {
//1.find max confidence per class
PriorityQueue<Recognition> pq =
new PriorityQueue<Recognition>(
50,
new Comparator<Recognition>() {
@Override
public int compare(final Recognition lhs, final Recognition rhs) {
// Intentionally reversed to put high confidence at the head of the queue.
return Float.compare(rhs.getConfidence(), lhs.getConfidence());
}
});
for (int i = 0; i < list.size(); ++i) {
if (list.get(i).getDetectedClass() == k) {
pq.add(list.get(i));
}
}
//2.do non maximum suppression
while (pq.size() > 0) {
//insert detection with max confidence
Recognition[] a = new Recognition[pq.size()];
Recognition[] detections = pq.toArray(a);
Recognition max = detections[0];
nmsList.add(max);
pq.clear();
for (int j = 1; j < detections.length; j++) {
Recognition detection = detections[j];
RectF b = detection.getLocation();
if (box_iou(max.getLocation(), b) < mNmsThresh) {
pq.add(detection);
}
}
}
}
return nmsList;
}
protected float mNmsThresh = 0.6f;
protected float box_iou(RectF a, RectF b) {
return box_intersection(a, b) / box_union(a, b);
}
protected float box_intersection(RectF a, RectF b) {
float w = overlap((a.left + a.right) / 2, a.right - a.left,
(b.left + b.right) / 2, b.right - b.left);
float h = overlap((a.top + a.bottom) / 2, a.bottom - a.top,
(b.top + b.bottom) / 2, b.bottom - b.top);
if (w < 0 || h < 0) return 0;
float area = w * h;
return area;
}
protected float box_union(RectF a, RectF b) {
float i = box_intersection(a, b);
float u = (a.right - a.left) * (a.bottom - a.top) + (b.right - b.left) * (b.bottom - b.top) - i;
return u;
}