Java写一段人脸识别代码
时间: 2023-03-14 22:12:04 浏览: 146
andriod人脸识别代码,用于课程设计毕业设计
可以这样写:import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;public class FaceRecognition {
public static void main(String[] args) {
// 从文件中读取图像
BufferedImage image = ImageIO.read(new File("image.jpg"));
// 将图像转换成IplImage格式
IplImage iplImage = IplImage.createFrom(image);
// 加载人脸检测器
CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad("haarcascade_frontalface_alt.xml"));
// 运行人脸检测
CvSeq faces = cvHaarDetectObjects(iplImage, cascade, new CvMemStorage(), 1.1, 3, 0);
// 在图像中画出矩形框
for (int i = 0; i < faces.total(); i++) {
CvRect r = new CvRect(cvGetSeqElem(faces, i));
cvRectangle(iplImage, cvPoint(r.x(), r.y()), cvPoint(r.width() + r.x(), r.height() + r.y()), CvScalar.RED, 2, CV_AA, 0);
}
// 保存图像
cvSaveImage("result.jpg", iplImage);
}
}
阅读全文