Advertisement
Guest User

Untitled

a guest
Aug 1st, 2019
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.DataBufferByte;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8. import javax.imageio.ImageIO;
  9.  
  10. import org.opencv.core.Core;
  11. import org.opencv.core.Mat;
  12. import org.opencv.core.MatOfRect;
  13. import org.opencv.core.Point;
  14. import org.opencv.core.Rect;
  15. import org.opencv.core.Scalar;
  16. import org.opencv.core.Size;
  17. import org.opencv.imgcodecs.Imgcodecs;
  18. import org.opencv.imgproc.Imgproc;
  19. import org.opencv.objdetect.CascadeClassifier;
  20. import org.opencv.videoio.VideoCapture;
  21.  
  22. public class App {
  23.    
  24.     public static void main(String[] args) {
  25.    
  26.         // doAll does the face detection algorithm for EVERY frame.
  27.         //doAll();
  28.        
  29.         // doLess does NOT do the face detection algorithm for every frame. It does less work
  30.         // 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
  31.         //doLess();
  32.        
  33.         System.out.println("in");
  34.         justPrintImageToScreen();
  35.        
  36.     }
  37.    
  38.  
  39.     public static void justPrintImageToScreen() {
  40.        
  41.         // Load the OpenCV native library.
  42.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  43.         // 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.
  44.         // 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.
  45.        
  46.  
  47. //      System.out.println(Core.getBuildInformation());
  48. //      System.out.println();
  49. //      System.exit(-2);
  50.        
  51.        
  52.         GUI appGUI = new GUI();
  53.         //appGUI.paintImage(img);
  54.         appGUI.setVisible(true);
  55.        
  56.        
  57.         // VideoCapture object is used to read the frames from the stream URI
  58.         VideoCapture videoCapture = new VideoCapture("rtsp://admin:admin@192.168.15.20/media/video2");
  59.        
  60.        
  61.         Mat webcamImage = new Mat(); // Matrics ("Mat") are used to store images in OpenCV (logically enough)
  62.        
  63.         int i = 1;
  64.         while (videoCapture.isOpened()) {
  65.             videoCapture.read(webcamImage); // read in the image (it's put in the webcamImage Matrix)
  66.            
  67.             if (i % 25 == 0) System.out.println(webcamImage);
  68.            
  69.             // resize the image (due this BEFORE running the face detection algorithm! Might as well make it easier on the algo)
  70.             Mat resizedWebcamImage = new Mat();
  71.             Imgproc.resize(webcamImage, resizedWebcamImage, new Size(900, 700));
  72.            
  73.            
  74.             //System.out.println(resizedWebcamImage);
  75.            
  76.             BufferedImage imageFromCamera = mat2BufferedImage(resizedWebcamImage);
  77.            
  78.            
  79.             appGUI.paintImage(imageFromCamera);
  80.            
  81.                        
  82.             i++;
  83.            
  84.         }
  85.        
  86.     }
  87.    
  88.     public static BufferedImage mat2BufferedImage(Mat m) {
  89.         int type = BufferedImage.TYPE_BYTE_GRAY;
  90.         if (m.channels() > 1) {
  91.             type = BufferedImage.TYPE_3BYTE_BGR;
  92.         }
  93.         int bufferSize = m.channels() * m.cols() * m.rows();
  94.         byte[] b = new byte[bufferSize];
  95.         m.get(0, 0, b); // get all the pixels
  96.         BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
  97.         final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
  98.         System.arraycopy(b, 0, targetPixels, 0, b.length);
  99.         return img;
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement