Face embedding calculation from Java
Hi, is there a way to calculate face embedding from the Java port of OpenCV?
I have browsed the JavaDoc but I cannot find it.
Thanks in advance.
Hi, is there a way to calculate face embedding from the Java port of OpenCV?
I have browsed the JavaDoc but I cannot find it.
Thanks in advance.
you're lucky, here's a complete example:
import org.opencv.core.*;
import org.opencv.dnn.*;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.*;
public class FaceRecognition {
public static Mat process(Net net, Mat img) {
Mat inputBlob =
Dnn.blobFromImage(img, 1./255, new Size(96,96), new Scalar(0,0,0), true, false);
net.setInput(inputBlob);
return net.forward().clone();
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Net net = Dnn.readNetFromTorch("openface.nn4.small2.v1.t7");
Mat feature1 = process(net, Imgcodecs.imread("Abdullah_Gul_0004.jpg"));
Mat feature2 = process(net, Imgcodecs.imread("Abdullah_Gul_0007.jpg"));
double dist = Core.norm(feature1, feature2);
if (dist < 0.6)
System.out.println("SAME !");
}
}
Hi. I keep having strange results. I am running the above code verbatim (no changes).I am using OpenCV 4.1.1 I downloaded the Java libraries from Sourceforge and I am running under Windows 10 64 bit.
I downloaded the model from here: https://storage.cmusatyalab.org/openface-models/nn4.small2.v1.t7
I downloaded test images from here: https://github.com/muxspace/facial_expressions/tree/master/images
When I run the code, it gives me a distance of 0.9325596751685123 between the two faces. Clerly the embedding is not working :(
Thanks a lot for the quick reply! However, let me add a note fro beginners like me :)
When I ran the above code, I got the following error:
cannot open <openface.nn4.small2.v1.t7>
This because, after installing OpenCV for Java as explained here, you need to download the model separately from here . Then, you need to pass the full file name for the downloaded model in:
Dnn.readNetFromTorch( [fullFileNameHere] );
uhhhmm -- maybe you found something !
code was adapted from the js sample but it looks like the js code is working on RGBA, not BGR
Asked: 2019-10-21 23:26:25 -0600
Seen: 595 times
Last updated: Oct 22 '19
Java: Example app not detecting faces
Unresolved inclusion in OpenCV+Android tutorial
How to convert Floating point image to 32-bit single-channel?
How to translate this to Java?
android: how to put a column into Mat
OpenCV Tutorial 1 - Add OpenCV on API 8
Running the OpenCV4Android application on my PC