Please setup Your IDE For OpenCV
package com.test.motion.detection;
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.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class MyTest {
static OpenCVWindow openCVWindow;
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
openCVWindow=new OpenCVWindow(1000,600);
readImgFromCam();
}
public static void readImgFromCam() {
CascadeClassifier faceDetector = new CascadeClassifier("D:/javaTech/OpenCV/opencv/sources/data/lbpcascades/lbpcascade_frontalface.xml");
VideoCapture cam=new VideoCapture(0);
while(true){
if(!cam.isOpened()){
cam.open(0);
System.out.println("No cam Found....");
}else {
}
Mat frameMat=new Mat();
Mat newmMat=new Mat();
MatOfRect matOfRect=new MatOfRect();
cam.read(frameMat);
Imgproc.resize(frameMat, newmMat, new Size(1000,600));
faceDetector.detectMultiScale(newmMat, matOfRect);
for(Rect rect:matOfRect.toArray()){
// Core.rectangle(new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
Core.rectangle(newmMat, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
openCVWindow.showImage(newmMat);
}
//cam.release();
}
}
package com.test.motion.detection;
import java.awt.Dimension;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.highgui.Highgui;
public class OpenCVWindow extends JFrame{
private static final long serialVersionUID = 1L;
Sheet sheet;
int height, width;
public OpenCVWindow( int length, int breadth)
{
width = length;
height = breadth;
sheet = new Sheet(breadth,length);
this.setSize(new Dimension(length, breadth));
this.add(sheet);
this.setFocusable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void showImage(Mat m)
{
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", m, matOfByte);
byte[] byteArray = matOfByte.toArray();
try
{
InputStream in = new ByteArrayInputStream(byteArray);
sheet.paintSheet(ImageIO.read(in));
} catch (Exception e)
{
e.printStackTrace();
}
}
}
package com.test.motion.detection;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class Sheet extends JPanel
{
private static final long serialVersionUID = 1L;
BufferedImage image;
int width, height;
public Sheet( int h, int w)
{
width = w;
height = h;
setSize(w, h);
}
public void paintSheet(BufferedImage img)
{
image = img;
repaint();
}
public void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, width, height, this);
}
}
No comments:
Post a Comment