Gesichtserkennung unter Verwendung von Java openCV 4.10.0 und facenet.onnxJava

Java-Forum
Anonymous
 Gesichtserkennung unter Verwendung von Java openCV 4.10.0 und facenet.onnx

Post by Anonymous »

Ich versuche nach Gesichtserkennung. Ich muss überprüfen, ob zwei Gesichter ähnlich sind, indem die Einbettungspunkte der beiden Bilddateien abgerufen werden. Verwenden Sie das Modell von Spacenet.onnx. Ich habe openCV-java410.dll in run configurations
hinzugefügt. Ich habe den folgenden Code ausprobiert. < /P>
public class FaceRecognition {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private static final String MODEL_PATH = "D:\\FaceRecognition\\face-
recognition\\src\\main\\resources\\facenet.onnx";
private static final String HAAR_CASCADE_PATH = "D:\\FaceRecognition\\face-recognition\\src\\main\\resources\\haarcascade_frontalface_default.xml";

public static void main(String[] args) {
System.out.println("----------------------------");
String imagePath1 = "D:\\FaceRecognition\\face-recognition\\src\\main\\resources\\image1.jpg";
String imagePath2 = "D:\\FaceRecognition\\face-recognition\\src\\main\\resources\\image1.jpg"; // Use a different image for testing

Mat img1 = Imgcodecs.imread(imagePath1);
Mat img2 = Imgcodecs.imread(imagePath2);

Mat embeddings1 = extractFaceEmbeddings(img1);
Mat embeddings2 = extractFaceEmbeddings(img2);

if (embeddings1 == null || embeddings2 == null) {
System.out.println("Error: One or both images did not produce embeddings.");
return;
}

double match = cosineSimilarity(embeddings1, embeddings2);
System.out.println("Match Score: " + match);
}

private static Mat extractFaceEmbeddings(Mat img) {
// Step 1: Detect faces
CascadeClassifier faceDetector = new CascadeClassifier(HAAR_CASCADE_PATH);
MatOfRect faces = new MatOfRect();
faceDetector.detectMultiScale(img, faces);

if (faces.toArray().length == 0) return null;

// Step 2: Extract the first detected face
Rect face = faces.toArray()[0];
Mat faceImg = new Mat(img, face);
System.out.println("Face Image Channels (Before Conversion): " + faceImg.channels());

// Step 3: Convert to BGR if the image is grayscale
if (faceImg.channels() == 1) {
Imgproc.cvtColor(faceImg, faceImg, Imgproc.COLOR_GRAY2BGR);
}
System.out.println("Face Image Channels (After Conversion): " + faceImg.channels());

// Step 4: Resize the face image to 160x160
Imgproc.resize(faceImg, faceImg, new Size(160, 160));

// Step 5: Load the ONNX model
Net net = Dnn.readNetFromONNX(MODEL_PATH);

// Step 6: Create the input blob
Mat blob = Dnn.blobFromImage(faceImg, 1.0 / 255.0, new Size(160, 160), new Scalar(127, 127, 127), true, false);

// Step 7: Debugging Blob Info
System.out.println("Blob Shape: " + blob.size());
System.out.println("Blob Channels: " + blob.channels());
System.out.println("Blob Type: " + blob.type());
System.out.println("Blob Dimensions: " + blob.size(0) + "x" + blob.size(1) + "x" + blob.size(2) + "x" + blob.size(3));

// Step 8: Check if the blob has the correct number of channels
if (blob.size(1) != 3) {
System.err.println("❌ Error: Blob has incorrect number of channels.");
return null;
}
Mat trans = new Mat();
Core.transpose(blob, trans);
// Step 9: Set the input blob
net.setInput(trans);
// Step 10: Forward pass to get the embeddings
return net.forward();
}
< /code>
Erhalten Sie den Fehler < /p>
Exception in thread "main" CvException [org.opencv.core.CvException:
cv::Exception: OpenCV(4.10.0) C:\GHA-OCV-1\_work\ci-gha-workflow\ci-gha-
workflow\opencv\modules\core\src\matrix_transform.cpp:249: error: (-215:Assertion
failed) _src.dims()

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post