-1

I'm using Viola-Jones method to detect face, whenever the face is tilted there will be possibility of algorithm won't work properly.

I want to detect that movements, when it can not able to detect the face.

Can I implement this using motion detection or is there any other methods to find the movements.

Thanks in advance,

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Punith K
  • 656
  • 1
  • 9
  • 26

1 Answers1

1

Yes you can do it by capture and compare them.

This will help you to detect them and then you can compare the x and y positions.

class DetectNose extends JPanel implements KeyListener, ActionListener {
private static final long serialVersionUID = 1L;
private static JFrame frame;
private BufferedImage image;
private CascadeClassifier face_cascade;
private Point center;
private JLabel label;
private Image scalledItemImage;
private double customY = 0;
private double customX = 0;
private Iterator<InputStream> iterator;
private ArrayList<BufferedImage> listOfCachedImages;
private int imageIndex = 1;
private int customZ = 0;
private Size size;
private Image scalledItemImageBackup;
private Point center1;
private int imgSize = 35;
private boolean isLocked;


public DetectNose(JFrame frame, List<Long> listOfOrnaments) {
    super();
    this.frame = frame;
    this.frame.setFocusable(true);
    this.frame.requestFocusInWindow();
    this.frame.addKeyListener(this);

    File f = null;
    try {
        System.out.println(System.getProperty("os.name"));
        if (System.getProperty("os.name").contains("Windows")) {
            f = new File("res/opencv_lib_win/opencv_java249.dll");
            System.load(f.getAbsolutePath());
            System.out.println("Loaded :" + f.getAbsolutePath());
        } else {
            f = new File("res/opencv_lib/libopencv_java246.so");
            System.load(f.getAbsolutePath());
            System.out.println("Loaded :" + f.getAbsolutePath());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    List<InputStream> ornaments = DatabaseHandler
            .getOrnamentsImagesByListOfOrnaments(listOfOrnaments);

    iterator = ornaments.iterator();

    listOfCachedImages = new ArrayList<BufferedImage>();

    try {
        while (iterator.hasNext()) {
            InputStream inputStream = iterator.next();
            listOfCachedImages.add(ImageIO.read(inputStream));
        }

        setFirstOrnament();

    } catch (IOException e) {
        e.printStackTrace();
    }
    label = new JLabel(new ImageIcon(scalledItemImage));
    add(label);
    face_cascade = new CascadeClassifier(
            "res/cascades/haarcascade_frontalface_alt_tree.xml");
    if (face_cascade.empty()) {
        System.out.println("--(!)Error loading A\n");
        return;
    } else {
        System.out.println("Face classifier loaded up");
    }
}

private void setFirstOrnament() {
    scalledItemImage = listOfCachedImages.get(imageIndex - 1);
    scalledItemImageBackup = scalledItemImage.getScaledInstance(700, 700,
            BufferedImage.TYPE_INT_RGB);
    scalledItemImage = scalledItemImage.getScaledInstance(imgSize, imgSize,
            BufferedImage.TYPE_INT_RGB);
    repaint();
    System.out.println("imageIndex = " + imageIndex);
}

private void setPrevOrnament() {
    if (imageIndex > 1) {
        imageIndex--;
        scalledItemImage = listOfCachedImages.get(imageIndex - 1);
        scalledItemImageBackup = scalledItemImage.getScaledInstance(700,
                700, BufferedImage.TYPE_INT_RGB);
        scalledItemImage = scalledItemImage.getScaledInstance(imgSize,
                imgSize, BufferedImage.TYPE_INT_RGB);
        GoLiveIntermediator.nextButton.setEnabled(true);
        repaint();
        revalidate();
        System.out.println("imageIndex = " + imageIndex);
    } else {
        GoLiveIntermediator.prevButton.setEnabled(false);
    }
}

private void setNextOrnament() {
    if (listOfCachedImages.size() > imageIndex) {
        imageIndex++;
        scalledItemImage = listOfCachedImages.get(imageIndex - 1);
        scalledItemImageBackup = scalledItemImage.getScaledInstance(700,
                700, BufferedImage.TYPE_INT_RGB);
        scalledItemImage = scalledItemImage.getScaledInstance(imgSize,
                imgSize, BufferedImage.TYPE_INT_RGB);
        GoLiveIntermediator.prevButton.setEnabled(true);
        repaint();
        revalidate();
        System.out.println("imageIndex = " + imageIndex);
    } else {
        GoLiveIntermediator.nextButton.setEnabled(false);
    }
}

private BufferedImage getimage() {
    return image;
}

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

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, 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, 0, cols, rows, data);
    return image2;
}

public void paintComponent(Graphics g) {
    try {
        this.frame.requestFocusInWindow();
        BufferedImage temp = getimage();
        g.drawImage(temp, 0, 0, temp.getWidth(), temp.getHeight() + 50,
                this);
    } catch (Exception ex) {
        System.out.print("Trying to load images...");
    }
}

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);
    try {
        face_cascade.detectMultiScale(mGrey, faces);
    } catch (Exception e) {
        System.out.print(".");
    }
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);

    for (Rect rect : faces.toArray()) {
        center = new Point(rect.x + rect.width * 0.5, rect.y + rect.height
                * 0.5); // You can use this to point out as first detection and last detection
        size = new Size(rect.width * 0.5, rect.height * 0.5);
        Core.ellipse(mRgba, center, size, 0, 0, 360, new Scalar(255, 0,
                255), 1, 8, 0);

        repaint();
    }
    return mRgba;
}

Here center is the first detection point same way you can find the last detection point from the image.

Gopal00005
  • 2,061
  • 4
  • 35
  • 54
  • The tutorial link you sent was for face detection in Java. send me the link to compare x and y positions. – Punith K Sep 09 '14 at 07:56
  • Is there any method to detect changes from one frame to another in OpenCV. for ex, if a person moves then detection of face fails, I want to detect that change from initial state (face detection) to face not detected. – Punith K Sep 09 '14 at 08:09
  • You can track the detection and save last detection and then measure the points. – Gopal00005 Sep 09 '14 at 08:50
  • If you got the solution then you can select my answer as the answer and can vote it up. – Gopal00005 Sep 09 '14 at 09:20
  • would please give me a sample example code or link to follow. – Punith K Sep 09 '14 at 09:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60882/discussion-between-gops-and-punith-k). – Gopal00005 Sep 09 '14 at 10:14