• Java+opencv实现人脸检测


    版本

    Java1.8

    opencv3.4

    代码:

    import java.awt.Graphics;
    
    import java.awt.image.BufferedImage;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import org.opencv.core.Mat;
    import org.opencv.core.MatOfDouble;
    import org.opencv.core.MatOfRect;
    import org.opencv.core.Point;
    import org.opencv.core.Rect;
    import org.opencv.core.Scalar;
    import org.opencv.imgproc.Imgproc;
    import org.opencv.objdetect.CascadeClassifier;
    import org.opencv.objdetect.HOGDescriptor;
    import org.opencv.videoio.VideoCapture;
    import org.opencv.videoio.Videoio;
      
    public class FaceDetection extends JPanel {  
          
        private BufferedImage mImg;  
        private static JFrame jFrame;
        //Mat转成image
        private BufferedImage matToImage(Mat mat){ 
            int dataSize =(int) (mat.total()*mat.channels()); 
            byte[] data=new byte[dataSize];  
            mat.get(0, 0,data);  
            int type=mat.channels()==1? BufferedImage.TYPE_BYTE_GRAY:BufferedImage.TYPE_3BYTE_BGR;  
            
            if(type==BufferedImage.TYPE_3BYTE_BGR){ 
                for(int i=0;i<dataSize;i+=3){  
                    byte blue=data[i+0]; 
                    data[i+0]=data[i+2];  
                    data[i+2]=blue;  
                }  
            }  
            //创建空的image
            BufferedImage image=new BufferedImage(mat.cols(),mat.rows(),type);  
            //帧数据拷贝
            image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);    
            return image;  
        }  
      /**
       * Jpanel调用repaint的时候会自动调用该绘图方法
       */
        public void paintComponent(Graphics g){  
            if(mImg!=null){  
                g.drawImage(mImg, 0, 0, mImg.getWidth(),mImg.getHeight(),this);  
            }  
        }  
        static{
         //System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.load(
    "D:/opencv/build/java/x64/opencv_java320.dll"); } public static void creatFrame(JPanel jPanel,int width,int height){ jFrame = new JFrame("camera"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setVisible(true); //将内容添加到JFrame jFrame.setSize(width,height); jFrame.setContentPane(jPanel); } public static void main(String[] args) throws Exception { //开启摄像头 VideoCapture capture=new VideoCapture(0); FaceDetection jPanel=new FaceDetection(); creatFrame(jPanel,(int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH),(int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT)); Mat mat=new Mat(); while(true){ if(!capture.isOpened()){ System.out.println("camera is not open"); System.exit(0); } capture.read(mat); jPanel.mImg=jPanel.matToImage(detectFace(mat)); jPanel.repaint(); } } /** * opencv实现人脸识别 * @param img */ public static Mat detectFace(Mat mat) throws Exception { // 从配置文件lbpcascade_frontalface.xml中创建一个人脸识别器,该文件位于opencv安装目录中 CascadeClassifier faceDetector = new CascadeClassifier("D:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml"); // 在图片中检测人脸 MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(mat, faceDetections); Rect[] rects = faceDetections.toArray(); for (Rect rect : rects) { //标出脸 Imgproc.rectangle(mat, new Point(rect.x, rect.y), //标记框的左下点坐标 new Point(rect.x + rect.width, rect.y + rect.height), //标记框的右上点坐标 new Scalar(0, 255, 0), //颜色 2); //粗细 } return mat; } }
  • 相关阅读:
    面试90%都会翻车的高并发分布式事务,我劝你好好啃透!
    JVM最多支持多少个线程?你知道吗?
    利用注解 + 反射消除重复代码(Java项目)
    ASP.NET HTTP模拟提交通用类 GET POST
    UPW学习资料整理 .NET C# 转
    前端引擎初步设计稿 -通过配置生成动态页面 ,LandaSugar平台 .NET-C#-MVC
    分享一个ASP.NET 文件压缩解压类 C#
    验证码的种类与实现 C#封装类
    ASP.NET MVC 使用 IOC框架 AutoFac 自动释放数据库资源
    ASP.NET MVC权限验证 封装类
  • 原文地址:https://www.cnblogs.com/timeTraveler/p/9916671.html
Copyright © 2020-2023  润新知