加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

opencv – java中的CascadeClassifier找不到带网络摄像头的面孔

发布时间:2020-12-15 04:11:42 所属栏目:Java 来源:网络整理
导读:我试图将 OpenCV CascadeClassifier tutorial从C翻译成Java.在C中工作得很好.此 java tutorial工作正常. 但翻译根本就没有检测到脸部.我没有明确的错误.我可以看到从网络摄像头(灰度/直方图……)和视频显示器处理视频输入.级联负载不会产生错误.但CascadeCla
我试图将 OpenCV CascadeClassifier tutorial从C翻译成Java.在C中工作得很好.此 java tutorial工作正常.

但翻译根本就没有检测到脸部.我没有明确的错误.我可以看到从网络摄像头(灰度/直方图……)和视频显示器处理视频输入.级联负载不会产生错误.但CascadeClassifier调用只是没有返回任何面…所以,你可能可以跳过所有代码,只是转到我的CascadeClassifier调用,直到公共Mat检测(Mat输入帧).由于我是Java和OpenCV的新手,我粘贴其余部分(我删除了任何我觉得可能不重要的东西),以防万一,但并不意味着你要调试……

我也用很多不同的方式尝试过这个电话(以及其他部分),没有任何东西……想法不足……

谢谢!!

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.highgui.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;
    private CascadeClassifier face_cascade;

    // Create a constructor method
    public My_Panel(){
        super(); 
        String face_cascade_name = "/haarcascade_frontalface_alt.xml";
        //String face_cascade_name = "/lbpcascade_frontalface.xml";
        //-- 1. Load the cascades

        String str;
        str = getClass().getResource(face_cascade_name).getPath();
        str = str.replace("/C:","C:");
        face_cascade_name=str;

        face_cascade=new CascadeClassifier(face_cascade_name);
        if( !face_cascade.empty())
        {
            System.out.println("--(!)Error loading An");
            return;
        }
        else
        {
                System.out.println("Face classifier loooaaaaaded up");
        }
    }

    private BufferedImage getimage(){
        return image;
    }

    public void setimage(BufferedImage newimage){
        image=newimage;
        return;
    }

    /**
     * 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 BufferedImage matToBufferedImage(Mat matrix) {
        int cols = matrix.cols();
        int rows = matrix.rows();
        int elemSize = (int)matrix.elemSize();
        byte[] data = new byte[cols * rows * elemSize];
        int type;

        matrix.get(0,data);

        switch (matrix.channels()) {
            case 1:
                type = BufferedImage.TYPE_BYTE_GRAY;
                break;

            case 3: 
                type = BufferedImage.TYPE_3BYTE_BGR;

                // bgr to rgb
                byte b;
                for(int i=0; i<data.length; i=i+3) {
                    b = data[i];
                    data[i] = data[i+2];
                    data[i+2] = b;
                }
                break;

            default:
                return null;
        }

        BufferedImage image2 = new BufferedImage(cols,rows,type);
        image2.getRaster().setDataElements(0,cols,data);

        return image2;
    }

    public void paintComponent(Graphics g){
         BufferedImage temp=getimage();
         g.drawImage(temp,10,temp.getWidth(),temp.getHeight(),this); 
    }

    public Mat detect(Mat inputframe){
        Mat mRgba=new Mat();
        Mat mGrey=new Mat();
        MatOfRect faces = new MatOfRect();
        //MatOfRect eyes = new MatOfRect();

        inputframe.copyTo(mRgba);
        inputframe.copyTo(mGrey);
        Imgproc.cvtColor( mRgba,mGrey,Imgproc.COLOR_BGR2GRAY);
        Imgproc.equalizeHist( mGrey,mGrey );

        face_cascade.detectMultiScale(mGrey,faces);
        //face_cascade.detectMultiScale(mGrey,faces,1.1,2,0|Objdetect.CASCADE_SCALE_IMAGE,new Size(30,30),new Size(200,200) );
        //face_cascade.detectMultiScale(mGrey,2//CV_HAAR_SCALE_IMAGE,//,200) );

        System.out.println(String.format("Detected %s faces",faces.toArray().length));

        return mGrey;
        }
}

public class window {
    public static void main(String arg[]){
     // Load the native library.
     System.loadLibrary("opencv_java245");  

     String window_name = "Capture - Face detection";

     JFrame frame = new JFrame(window_name);
     frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(400,400);

     My_Panel my_panel = new My_Panel();
     frame.setContentPane(my_panel);          
     frame.setVisible(true);           

     //-- 2. Read the video stream
     BufferedImage temp;
     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
                   // At this point I was wondering where this should be done.
                   // I put it within the panel class,but maybe one could actually
                   // create a processor object...
                   webcam_image=my_panel.detect(webcam_image);

                 //-- 4. Display the image
                   temp=my_panel.matToBufferedImage(webcam_image);
                   my_panel.setimage(temp);
                   my_panel.repaint(); 
               }
               else
               { 
                   System.out.println(" --(!) No captured frame -- Break!"); 
                   break; 
               }
              }
           }
           return;
    }
}

PS.:其他信息,以防万一:

> mGrey是:Mat [480 * 640 * CV_8UC1,isCont = true,isSubmat = false,nativeObj = 0x19d9af48,dataAddr = 0x19dc3430]
> face是:Mat [0 * 0 * CV_8UC1,isCont = false,nativeObj = 0x194bb048,dataAddr = 0x0]

解决方法

我已经尝试过你的代码,它运行正常! haarcascade_frontalface_alt.xml文件位置只有一个问题.尝试使用文件的完整路径: face_cascade = new CascadeClassifier(“D:/HelloCV/src/haarcascade_frontalface_alt.xml”);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读