Capture - Face detection

转载 2016年06月01日 10:43:38
package opencvTest;

/**
 * Created by lwc on 6/1/16.
 */
import 	java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import javax.swing.*;
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.core.Size;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;


class My_Panel extends JPanel{
    private static final long serialVersionUID = 1L;
    private BufferedImage image;
    // Create a constructor method
    public My_Panel(){
        super();
    }
    /**
     * Converts/writes a Mat into a BufferedImage.
     *
//     * @param matrix Mat of type CV_8UC3 or CV_8UC1
     * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
     */
    public boolean MatToBufferedImage(Mat matBGR){
        long startTime = System.nanoTime();
        int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels() ;
        byte[] sourcePixels = new byte[width * height * channels];
        matBGR.get(0, 0, sourcePixels);
        // create new image and get reference to backing data
        image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);
        long endTime = System.nanoTime();
        System.out.println(String.format("Elapsed time: %.2f ms", (float)(endTime - startTime)/1000000));
        return true;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if (this.image==null) return;
        g.drawImage(this.image,10,10,this.image.getWidth(),this.image.getHeight(), null);
        //g.drawString("This is my custom Panel!",10,20);
    }
}
class processor {
    private CascadeClassifier face_cascade;
    // Create a constructor method
    public processor(){
        face_cascade=new CascadeClassifier("/home/lwc/Documents/opencv-3.1.0/data/haarcascades/haarcascade_frontalface_alt.xml");
        if(face_cascade.empty())
        {
            System.out.println("--(!)Error loading A\n");
            return;
        }
        else
        {
            System.out.println("Face classifier loaded up");
        }
    }
    public Mat detect(Mat inputframe){
        Mat mRgba=new Mat();
        Mat mGrey=new Mat();
        MatOfRect faces = new MatOfRect();
        inputframe.copyTo(mRgba);
        inputframe.copyTo(mGrey);
        Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);
        Imgproc.equalizeHist( mGrey, mGrey );
        face_cascade.detectMultiScale(mGrey, faces);
        System.out.println(String.format("Detected %s faces", faces.toArray().length));
        for(Rect rect:faces.toArray())
        {
            Point center= new Point(rect.x + rect.width*0.5, rect.y + rect.height*0.5 );
            Imgproc.ellipse( mRgba, center, new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 255, 0, 255 ), 4, 8, 0 );
        }
        return mRgba;
    }
}
public class Demo1 {
    public static void main(String arg[]){
        // Load the native library.
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        String window_name = "Capture - Face detection";
        JFrame frame = new JFrame(window_name);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        processor my_processor=new processor();
        My_Panel my_panel = new My_Panel();
        frame.setContentPane(my_panel);
        frame.setVisible(true);
        //-- 2. Read the video stream
        Mat webcam_image=new Mat();
        VideoCapture capture =new VideoCapture(0);
        if( capture.isOpened())
        {
            while( true )
            {
                capture.read(webcam_image);
                if( !webcam_image.empty() )
                {
                    frame.setSize(webcam_image.width()+40,webcam_image.height()+60);
                    //-- 3. Apply the classifier to the captured image
                    webcam_image=my_processor.detect(webcam_image);
                    //-- 4. Display the image
                    my_panel.MatToBufferedImage(webcam_image); // We could look at the error...
                    my_panel.repaint();
                }
                else
                {
                    System.out.println(" --(!) No captured frame -- Break!");
                    break;
                }
            }
        }
        return;
    }
}

opencv学习篇(2)haar人脸检测

在这节教程中您将学到: 使用 CascadeClassifier 类来检测视频流中的物体. 特别地, 我们将使用函数: load 来加载一个 .xml 分类器文件. 它既可以是Haar...
  • qq_27789527
  • qq_27789527
  • 2015-11-04 09:38:25
  • 623

Opencv face detection人脸检测

opencv face detection code人脸检测代码
  • hao529good
  • hao529good
  • 2015-04-23 16:40:28
  • 1213

【论文学习笔记】Joint Cascade Face Detection and Alignment

第一节: 关键思想是将人脸检测和人脸标点结合起来。 一个应用比较广泛的人脸检测方法,Viola-Jones检测器是基于以下两个原则进行检测的:1,逐步提升的级联结构;2,简单的特征。这种方法在...
  • shixiangyun2
  • shixiangyun2
  • 2016-03-05 15:30:11
  • 5255

论文《Joint Cascade Face Detection and Alignment》笔记

论文:Joint Cascade Face Detection and Alignment.pdf 实现:https://github.com/FaceDetect/jointCascade_py...
  • u010333076
  • u010333076
  • 2016-02-05 11:11:24
  • 3433

人脸检测--Scale-Aware Face Detection

Scale-Aware Face Detection CVPR2017针对人脸检测中的人脸多尺度问题,本文首先用一个 Scale Proposal Network (SPN) 估计出图像中人脸的尺...
  • zhangjunhit
  • zhangjunhit
  • 2017-08-21 11:06:29
  • 1397

10年后再看Robust Real-Time Face Detection(一)

这篇论文是人脸检测上的jingdian
  • a130737
  • a130737
  • 2014-05-30 19:40:44
  • 3822

人脸检测之Joint Cascade Face Detection and Alignment 笔记---ECCV2014

人脸检测(detection)在opencv中早就有直接能拿来用的haar分类器,基于Viola-Jones算法。但是毕竟是老掉牙的技术,Precision/Recall曲线渣到不行,在实际工程中根本...
  • zwlq1314521
  • zwlq1314521
  • 2016-05-13 14:12:09
  • 3736

Face Detection paper 1st season

关于目标检测,怎么去衡量一个目标检测器的好坏? 显然这个答案是检测速度和检测精度,这是最主要的两个指标,其它的包括训练时间,算法复杂度,算法的适应能力(不同场景,不同类别)。速度的评价指标按时间计算...
  • xx116213
  • xx116213
  • 2016-08-06 11:59:57
  • 3736

Face2Face: Real-time Face Capture and Reenactment of RGB Videos

Face2Face: Real-time Face Capture and Reenactment of RGB Videos CVPR 2016 (Oral) J. Thie...
  • Real_Myth
  • Real_Myth
  • 2016-06-15 09:48:04
  • 1814

经典计算机视觉论文笔记——《Robust Real-Time Face Detection》

第一次读这篇传奇之作大概是九年前了,也就是2007年,而那时距论文正式发表(2004年)也已经有四年之久了。现在读来,一些想法,在深度学习大行其道的今天仍然具有借鉴意义,让人敬佩不已。        ...
  • u010318961
  • u010318961
  • 2016-06-01 20:40:00
  • 1360
收藏助手
不良信息举报
您举报文章:Capture - Face detection
举报原因:
原因补充:

(最多只允许输入30个字)