Skip to content

Commit 2eae01f

Browse files
committed
added blazeface test
1 parent e9037bb commit 2eae01f

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ch.bildspur.vision.test;
2+
3+
4+
import ch.bildspur.vision.DeepVisionPreview;
5+
import ch.bildspur.vision.MediaPipeBlazeFaceNetwork;
6+
import ch.bildspur.vision.TextBoxesNetwork;
7+
import ch.bildspur.vision.result.ObjectDetectionResult;
8+
import processing.core.PApplet;
9+
import processing.core.PImage;
10+
11+
import java.nio.file.Paths;
12+
import java.util.List;
13+
14+
public class BlazeFaceTest extends PApplet {
15+
16+
public static void main(String... args) {
17+
BlazeFaceTest sketch = new BlazeFaceTest();
18+
sketch.runSketch();
19+
}
20+
21+
public void settings() {
22+
size(640, 480, FX2D);
23+
}
24+
25+
PImage testImage;
26+
27+
DeepVisionPreview vision = new DeepVisionPreview(this);
28+
MediaPipeBlazeFaceNetwork network;
29+
List<ObjectDetectionResult> detections;
30+
31+
public void setup() {
32+
colorMode(HSB, 360, 100, 100);
33+
34+
testImage = loadImage(sketchPath("data/faces.png"));
35+
36+
println("creating network...");
37+
network = new MediaPipeBlazeFaceNetwork(Paths.get("networks/face_detection_back_256x256_barracuda.onnx"), 256, 256);
38+
39+
println("loading model...");
40+
network.setup();
41+
42+
//network.setConfidenceThreshold(0.2f);
43+
44+
println("inferencing...");
45+
detections = network.run(testImage);
46+
println("done!");
47+
48+
for (ObjectDetectionResult detection : detections) {
49+
System.out.println(detection.getClassName() + "\t[" + detection.getConfidence() + "]");
50+
}
51+
52+
println("found " + detections.size() + " texts!");
53+
}
54+
55+
public void draw() {
56+
background(55);
57+
58+
image(testImage, 0, 0);
59+
60+
noFill();
61+
strokeWeight(2f);
62+
63+
stroke(200, 80, 100);
64+
for (ObjectDetectionResult detection : detections) {
65+
rect(detection.getX(), detection.getY(), detection.getWidth(), detection.getHeight());
66+
}
67+
68+
surface.setTitle("BlazeFace Test - FPS: " + Math.round(frameRate));
69+
}
70+
}

0 commit comments

Comments
 (0)