|
| 1 | +package ch.bildspur.vision; |
| 2 | + |
| 3 | +import ch.bildspur.vision.network.ObjectDetectionNetwork; |
| 4 | +import ch.bildspur.vision.result.ObjectDetectionResult; |
| 5 | +import ch.bildspur.vision.result.ResultList; |
| 6 | +import ch.bildspur.vision.util.MathUtils; |
| 7 | +import org.bytedeco.javacpp.FloatPointer; |
| 8 | +import org.bytedeco.javacpp.IntPointer; |
| 9 | +import org.bytedeco.javacpp.indexer.FloatIndexer; |
| 10 | +import org.bytedeco.opencv.global.opencv_dnn; |
| 11 | +import org.bytedeco.opencv.opencv_core.*; |
| 12 | +import org.bytedeco.opencv.opencv_dnn.Net; |
| 13 | +import org.bytedeco.opencv.opencv_text.FloatVector; |
| 14 | + |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.bytedeco.opencv.global.opencv_core.CV_32F; |
| 20 | +import static org.bytedeco.opencv.global.opencv_dnn.*; |
| 21 | + |
| 22 | +/** |
| 23 | + * Based on https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/blob/master/caffe/ultra_face_opencvdnn_inference.py |
| 24 | + * Adapted and improved a lot. |
| 25 | + */ |
| 26 | +public class MediaPipeBlazeFaceNetwork extends ObjectDetectionNetwork { |
| 27 | + private Path modelPath; |
| 28 | + protected Net net; |
| 29 | + |
| 30 | + private int width; |
| 31 | + private int height; |
| 32 | + |
| 33 | + private Scalar imageMean = Scalar.all(127); |
| 34 | + private float imageStd = 128.0f; |
| 35 | + |
| 36 | + public MediaPipeBlazeFaceNetwork(Path modelPath, int width, int height) { |
| 37 | + this.modelPath = modelPath; |
| 38 | + this.width = width; |
| 39 | + this.height = height; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean setup() { |
| 44 | + net = readNetFromONNX(modelPath.toAbsolutePath().toString()); |
| 45 | + |
| 46 | + if (DeepVision.ENABLE_CUDA_BACKEND) { |
| 47 | + net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); |
| 48 | + net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); |
| 49 | + } |
| 50 | + |
| 51 | + if (net.empty()) { |
| 52 | + System.out.println("Can't load network!"); |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public ResultList<ObjectDetectionResult> run(Mat frame) { |
| 61 | + // convert image into batch of images |
| 62 | + Mat inputBlob = blobFromImage(frame, |
| 63 | + 1 / imageStd, |
| 64 | + new Size(width, height), |
| 65 | + imageMean, |
| 66 | + false, false, CV_32F); |
| 67 | + |
| 68 | + // set input |
| 69 | + net.setInput(inputBlob); |
| 70 | + |
| 71 | + // create output layers |
| 72 | + StringVector outNames = net.getUnconnectedOutLayersNames(); |
| 73 | + MatVector outs = new MatVector(outNames.size()); |
| 74 | + |
| 75 | + // run detection |
| 76 | + net.forward(outs, outNames); |
| 77 | + |
| 78 | + // extract boxes and scores |
| 79 | + Mat boxesOut = outs.get(0); |
| 80 | + Mat confidencesOut = outs.get(1); |
| 81 | + |
| 82 | + // boxes |
| 83 | + Mat boxes = boxesOut.reshape(0, boxesOut.size(1)); |
| 84 | + |
| 85 | + // class confidences (BACKGROUND, face) |
| 86 | + Mat confidences = confidencesOut.reshape(0, confidencesOut.size(1)); |
| 87 | + |
| 88 | + return new ResultList<>(); |
| 89 | + } |
| 90 | +} |
0 commit comments