-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathReal-time_Object_Tracking.java
More file actions
130 lines (112 loc) · 4.65 KB
/
Real-time_Object_Tracking.java
File metadata and controls
130 lines (112 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.*;
import org.opencv.videoio.VideoCapture;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
public class ObjectTracking {
private static JFrame frame;
private static JLabel imageLabel;
private static Rect2d bbox;
private static boolean startTracking = false;
private static boolean cancelTracking = false;
private static boolean isTracking = false;
private static boolean isLost = false;
private static boolean isQuit = false;
public static void main(String[] args) {
// Load the OpenCV library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Create the video capture
VideoCapture capture = new VideoCapture(0);
if (!capture.isOpened()) {
System.out.println("Failed to open video capture.");
return;
}
// Create the frame and image label for displaying the video
frame = new JFrame("Object Tracking");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
imageLabel = new JLabel();
frame.getContentPane().add(imageLabel, BorderLayout.CENTER);
frame.setVisible(true);
// Add key listener to the frame for capturing key events
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
startTracking = true;
} else if (e.getKeyChar() == KeyEvent.VK_C) {
cancelTracking = true;
} else if (e.getKeyChar() == KeyEvent.VK_Q) {
isQuit = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
// Create the tracker
Tracker tracker = TrackerKCF.create();
// Main loop for video processing
while (true) {
// Read a frame from the video
Mat frameMat = new Mat();
if (capture.read(frameMat)) {
// Create a copy of the frame for drawing
Mat drawMat = frameMat.clone();
// Start or cancel tracking based on user input
if (startTracking) {
bbox = Imgproc.selectROI("Tracking", frameMat, false);
tracker.init(frameMat, bbox);
isTracking = true;
startTracking = false;
} else if (cancelTracking) {
tracker.clear();
isTracking = false;
cancelTracking = false;
}
// Update the tracker with the current frame
if (isTracking) {
isLost = !tracker.update(frameMat, bbox);
}
// Draw bounding box and status on the image
if (isTracking) {
Imgproc.rectangle(drawMat, bbox.tl(), bbox.br(), new Scalar(0, 255, 255), 3);
Imgproc.putText(drawMat, "Tracking", bbox.tl(), Imgproc.FONT_HERSHEY_SIMPLEX, 0.9, new Scalar(0, 255, 255), 2);
} else if (isLost) {
Imgproc.putText(drawMat, "Lost", new Point(20, 40), Imgproc.FONT_HERSHEY_SIMPLEX, 0.9, new Scalar(0, 0, 255), 2);
}
// Display the image
BufferedImage image = MatToBufferedImage(drawMat);
ImageIcon imageIcon = new ImageIcon(image);
imageLabel.setIcon(imageIcon);
frame.pack();
// Exit the loop if 'q' is pressed or window is closed
if (isQuit || frame.isVisible()) {
break;
}
}
}
// Release the video capture and close windows
capture.release();
frame.dispose();
}
// Convert OpenCV Mat to BufferedImage
private static BufferedImage MatToBufferedImage(Mat mat) {
int width = mat.cols();
int height = mat.rows();
int channels = mat.channels();
byte[] sourceData = new byte[width * height * channels];
mat.get(0, 0, sourceData);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
byte[] targetData = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(sourceData, 0, targetData, 0, sourceData.length);
return image;
}
}