Here is a simple Java program that demonstrates how to perform facial recognition using the Java OpenCV library:
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; public class FacialRecognition { public static void main(String[] args) { // Load the OpenCV library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Load the image file Mat image = Imgcodecs.imread("image.jpg"); // Load the classifier file CascadeClassifier classifier = new CascadeClassifier("haarcascade_frontalface_default.xml"); // Detect faces in the image MatOfRect faceDetections = new MatOfRect(); classifier.detectMultiScale(image, faceDetections); // Draw a bounding box around each detected face for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); } // Save the image with the detected faces Imgcodecs.imwrite("image_with_faces.jpg", image); } }
This program uses the Java OpenCV library to load an image file, load a classifier file that is trained to recognize faces, detect faces in the image, and then draw a bounding box around each detected face.
Note that this program assumes that you have already installed the Java OpenCV library and have the necessary classifier files. You can download the Java OpenCV library and classifier files from the OpenCV website (https://opencv.org/).