Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferByte;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- 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.imgcodecs.Imgcodecs;
- import org.opencv.imgproc.Imgproc;
- import org.opencv.objdetect.CascadeClassifier;
- import org.opencv.videoio.VideoCapture;
- public class App {
- public static void main(String[] args) {
- // doAll does the face detection algorithm for EVERY frame.
- //doAll();
- // doLess does NOT do the face detection algorithm for every frame. It does less work
- // Only checks for faces every 25 frames actually, so approximately every second. Also pauses for a second after it's drawn the box around the face, just to let the user see the box
- //doLess();
- System.out.println("in");
- justPrintImageToScreen();
- }
- public static void justPrintImageToScreen() {
- // Load the OpenCV native library.
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
- // Note the call to System.loadLibrary(Core.NATIVE_LIBRARY_NAME). This command MUST be executed exactly once per Java process prior to using any native OpenCV methods.
- // If you don't call it, you will get UnsatisfiedLink errors. You will also get errors if you try to load OpenCV when it has already been loaded.
- // System.out.println(Core.getBuildInformation());
- // System.out.println();
- // System.exit(-2);
- GUI appGUI = new GUI();
- //appGUI.paintImage(img);
- appGUI.setVisible(true);
- // VideoCapture object is used to read the frames from the stream URI
- VideoCapture videoCapture = new VideoCapture("rtsp://admin:admin@192.168.15.20/media/video2");
- Mat webcamImage = new Mat(); // Matrics ("Mat") are used to store images in OpenCV (logically enough)
- int i = 1;
- while (videoCapture.isOpened()) {
- videoCapture.read(webcamImage); // read in the image (it's put in the webcamImage Matrix)
- if (i % 25 == 0) System.out.println(webcamImage);
- // resize the image (due this BEFORE running the face detection algorithm! Might as well make it easier on the algo)
- Mat resizedWebcamImage = new Mat();
- Imgproc.resize(webcamImage, resizedWebcamImage, new Size(900, 700));
- //System.out.println(resizedWebcamImage);
- BufferedImage imageFromCamera = mat2BufferedImage(resizedWebcamImage);
- appGUI.paintImage(imageFromCamera);
- i++;
- }
- }
- public static BufferedImage mat2BufferedImage(Mat m) {
- int type = BufferedImage.TYPE_BYTE_GRAY;
- if (m.channels() > 1) {
- type = BufferedImage.TYPE_3BYTE_BGR;
- }
- int bufferSize = m.channels() * m.cols() * m.rows();
- byte[] b = new byte[bufferSize];
- m.get(0, 0, b); // get all the pixels
- BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
- final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
- System.arraycopy(b, 0, targetPixels, 0, b.length);
- return img;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement