Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Detect a face using OpenCV in Java

//Detect a face using OpenCV in Java
/* Put haarcascade_frontalface_alt.xml provided by OpenCV(GitHub link attached) 
in Resource folder for easy access */
				Mat imageMatrix =  Imgcodecs.imread(imagePath);
				MatOfRect facesDetected = new MatOfRect();		
				CascadeClassifier cascadeClassifier = new CascadeClassifier(); 
				int minFaceSize = Math.round(imageMatrix.rows() * 0.1f); 
				cascadeClassifier.load("./src/main/resources/haarcascades/haarcascade_frontalface_alt.xml"); 
				cascadeClassifier.detectMultiScale(imageMatrix, 
				  facesDetected, 
				  1.1, 
				  3, 
				  Objdetect.CASCADE_SCALE_IMAGE, 
				  new Size(minFaceSize, minFaceSize), 
				  new Size() 
				);
				//SAVE image
				Rect[] facesArray = facesDetected.toArray(); 
				for(Rect face : facesArray) { 
				    Imgproc.rectangle(imageMatrix, face.tl(), face.br(), new Scalar(0, 0, 255), 3); 
				}
				Imgcodecs.imwrite(imagePath, imageMatrix);
Source by github.com #
 
PREVIOUS NEXT
Tagged: #Detect #face #OpenCV #Java
ADD COMMENT
Topic
Name
4+5 =