Search in sources :

Example 1 with IntVector

use of org.bytedeco.opencv.opencv_text.IntVector in project javacv by bytedeco.

the class YOLONet method postprocess.

/**
 * Remove the bounding boxes with low confidence using non-maxima suppression
 *
 * @param frame Input frame
 * @param outs  Network outputs
 * @return List of objects
 */
private List<ObjectDetectionResult> postprocess(Mat frame, MatVector outs) {
    final IntVector classIds = new IntVector();
    final FloatVector confidences = new FloatVector();
    final RectVector boxes = new RectVector();
    for (int i = 0; i < outs.size(); ++i) {
        // extract the bounding boxes that have a high enough score
        // and assign their highest confidence class prediction.
        Mat result = outs.get(i);
        FloatIndexer data = result.createIndexer();
        for (int j = 0; j < result.rows(); j++) {
            // minMaxLoc implemented in java because it is 1D
            int maxIndex = -1;
            float maxScore = Float.MIN_VALUE;
            for (int k = 5; k < result.cols(); k++) {
                float score = data.get(j, k);
                if (score > maxScore) {
                    maxScore = score;
                    maxIndex = k - 5;
                }
            }
            if (maxScore > confidenceThreshold) {
                int centerX = (int) (data.get(j, 0) * frame.cols());
                int centerY = (int) (data.get(j, 1) * frame.rows());
                int width = (int) (data.get(j, 2) * frame.cols());
                int height = (int) (data.get(j, 3) * frame.rows());
                int left = centerX - width / 2;
                int top = centerY - height / 2;
                classIds.push_back(maxIndex);
                confidences.push_back(maxScore);
                boxes.push_back(new Rect(left, top, width, height));
            }
        }
        data.release();
        result.release();
    }
    // remove overlapping bounding boxes with NMS
    IntPointer indices = new IntPointer(confidences.size());
    FloatPointer confidencesPointer = new FloatPointer(confidences.size());
    confidencesPointer.put(confidences.get());
    NMSBoxes(boxes, confidencesPointer, confidenceThreshold, nmsThreshold, indices, 1.f, 0);
    // create result list
    List<ObjectDetectionResult> detections = new ArrayList<>();
    for (int i = 0; i < indices.limit(); ++i) {
        final int idx = indices.get(i);
        final Rect box = boxes.get(idx);
        final int clsId = classIds.get(idx);
        detections.add(new ObjectDetectionResult() {

            {
                classId = clsId;
                className = names.get(clsId);
                confidence = confidences.get(idx);
                x = box.x();
                y = box.y();
                width = box.width();
                height = box.height();
            }
        });
        box.releaseReference();
    }
    // cleanup
    indices.releaseReference();
    confidencesPointer.releaseReference();
    classIds.releaseReference();
    confidences.releaseReference();
    boxes.releaseReference();
    return detections;
}
Also used : IntVector(org.bytedeco.opencv.opencv_text.IntVector) ArrayList(java.util.ArrayList) FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) FloatVector(org.bytedeco.opencv.opencv_text.FloatVector) FloatPointer(org.bytedeco.javacpp.FloatPointer) IntPointer(org.bytedeco.javacpp.IntPointer)

Aggregations

ArrayList (java.util.ArrayList)1 FloatPointer (org.bytedeco.javacpp.FloatPointer)1 IntPointer (org.bytedeco.javacpp.IntPointer)1 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)1 FloatVector (org.bytedeco.opencv.opencv_text.FloatVector)1 IntVector (org.bytedeco.opencv.opencv_text.IntVector)1