Last active
July 18, 2022 09:36
-
-
Save AliZafar120/a3aace7f3fb1f4837727e93bc49e2f72 to your computer and use it in GitHub Desktop.
Java Haar Cascade car detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Opencv; | |
import static org.opencv.imgcodecs.Imgcodecs.imwrite; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayInputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import javax.imageio.ImageIO; | |
import javax.swing.ImageIcon; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import org.opencv.core.Core; | |
import org.opencv.core.Mat; | |
import org.opencv.core.MatOfByte; | |
import org.opencv.core.MatOfRect; | |
import org.opencv.core.Point; | |
import org.opencv.core.Rect; | |
import org.opencv.core.Scalar; | |
import org.opencv.imgcodecs.Imgcodecs; | |
import org.opencv.imgproc.Imgproc; | |
import org.opencv.objdetect.CascadeClassifier; | |
import org.opencv.videoio.VideoCapture; | |
public class Snippet { | |
public static void main(String[] args) { | |
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |
Mat frame = new Mat(); | |
VideoCapture camera = new VideoCapture("C:\\Users\\User\\Desktop\\Road and Cars\\test.mp4"); | |
JFrame jframe = new JFrame("Video"); | |
JLabel vidpanel = new JLabel(); | |
jframe.setContentPane(vidpanel); | |
jframe.setSize(600, 600); | |
jframe.setUndecorated(true); | |
jframe.setVisible(true); | |
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
while (true) { | |
if (camera.read(frame)) { | |
frame=detectCars(frame); | |
ImageIcon image = new ImageIcon(Mat2bufferedImage(frame)); | |
vidpanel.setIcon(image); | |
vidpanel.repaint(); | |
} | |
} | |
} | |
private static BufferedImage Mat2bufferedImage(Mat image) { | |
MatOfByte bytemat = new MatOfByte(); | |
Imgcodecs.imencode(".jpg", image, bytemat); | |
byte[] bytes = bytemat.toArray(); | |
InputStream in = new ByteArrayInputStream(bytes); | |
BufferedImage img = null; | |
try { | |
img = ImageIO.read(in); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return img; | |
} | |
public static Mat detectCars(Mat image){ | |
CascadeClassifier cardetector = new CascadeClassifier("E:\\Haar Training\\Haar Training\\cascade2xml\\car1.xml"); | |
MatOfRect faceDetections = new MatOfRect(); | |
cardetector.detectMultiScale(image, faceDetections); | |
System.out.println(String.format("Detected %s cars", faceDetections.toArray().length)); | |
for (Rect rect : faceDetections.toArray()) { | |
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), | |
new Scalar(0, 255, 0)); | |
} | |
/* String filename = "C:\\Users\\User\\Desktop\\Road and Cars\\Outputs\\"; | |
System.out.println(String.format("Writing %s", filename));*/ | |
return image; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment