|
| 1 | +import org.opencv.core.*; |
| 2 | +import org.opencv.core.Rect; |
| 3 | +import org.opencv.video.*; |
| 4 | +import org.opencv.videoio.VideoCapture; |
| 5 | +import org.opencv.imgproc.Imgproc; |
| 6 | +import org.opencv.highgui.HighGui; |
| 7 | +import org.opencv.core.Scalar; |
| 8 | +import org.opencv.core.Point; |
| 9 | + |
| 10 | +public class ObjectTracking { |
| 11 | + private static Rect2d bbox; |
| 12 | + private static boolean startTracking = false; |
| 13 | + private static boolean cancelTracking = false; |
| 14 | + private static boolean isTracking = false; |
| 15 | + private static boolean isLost = false; |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + // Load the OpenCV library |
| 19 | + System.loadLibrary(Core.NATIVE_LIBRARY_NAME); |
| 20 | + |
| 21 | + // Create the video capture |
| 22 | + VideoCapture cap = new VideoCapture(0); // Adjust the camera index if needed |
| 23 | + |
| 24 | + if (!cap.isOpened()) { |
| 25 | + System.out.println("Failed to open video capture."); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + // Create a tracker object |
| 30 | + Tracker tracker = TrackerKCF.create(); |
| 31 | + |
| 32 | + // Read the first frame from the video |
| 33 | + Mat frame = new Mat(); |
| 34 | + if (!cap.read(frame)) { |
| 35 | + System.out.println("Failed to read frame."); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Select the region of interest (ROI) for tracking |
| 40 | + bbox = HighGui.selectROI("Tracking", frame, false); |
| 41 | + |
| 42 | + // Initialize the tracker with the selected ROI |
| 43 | + tracker.init(frame, bbox); |
| 44 | + isTracking = true; |
| 45 | + |
| 46 | + // Main loop for video processing |
| 47 | + while (true) { |
| 48 | + // Read a frame from the video |
| 49 | + if (!cap.read(frame)) { |
| 50 | + System.out.println("Failed to read frame."); |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + // Start or cancel tracking based on user input |
| 55 | + if (startTracking) { |
| 56 | + bbox = HighGui.selectROI("Tracking", frame, false); |
| 57 | + tracker.init(frame, bbox); |
| 58 | + isTracking = true; |
| 59 | + startTracking = false; |
| 60 | + } else if (cancelTracking) { |
| 61 | + tracker.clear(); |
| 62 | + isTracking = false; |
| 63 | + cancelTracking = false; |
| 64 | + } |
| 65 | + |
| 66 | + // Update the tracker with the current frame |
| 67 | + if (isTracking) { |
| 68 | + isLost = !tracker.update(frame, bbox); |
| 69 | + } |
| 70 | + |
| 71 | + // If tracking is successful, draw the bounding box |
| 72 | + if (isTracking) { |
| 73 | + Imgproc.rectangle(frame, bbox.tl(), bbox.br(), new Scalar(255, 0, 255), 3); |
| 74 | + Imgproc.putText(frame, "Tracking", bbox.tl(), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(0, 255, 0), 2); |
| 75 | + } else if (isLost) { |
| 76 | + Imgproc.putText(frame, "Lost", new Point(100, 75), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(0, 0, 255), 2); |
| 77 | + } |
| 78 | + |
| 79 | + // Calculate and display FPS |
| 80 | + double fps = HighGui.getTickFrequency() / (HighGui.getTickCount() - frame.nativeObjAddr); |
| 81 | + Scalar myColor; |
| 82 | + if (fps > 60) { |
| 83 | + myColor = new Scalar(20, 230, 20); |
| 84 | + } else if (fps > 20) { |
| 85 | + myColor = new Scalar(230, 20, 20); |
| 86 | + } else { |
| 87 | + myColor = new Scalar(20, 20, 230); |
| 88 | + } |
| 89 | + Imgproc.rectangle(frame, new Point(15, 15), new Point(200, 90), new Scalar(255, 0, 255), 2); |
| 90 | + Imgproc.putText(frame, "fps:", new Point(20, 40), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 0, 255), 2); |
| 91 | + Imgproc.putText(frame, String.valueOf((int) fps), new Point(75, 40), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, myColor, 2); |
| 92 | + |
| 93 | + // Display the frame |
| 94 | + HighGui.imshow("Tracking", frame); |
| 95 | + |
| 96 | + // Exit the loop if 'q' is pressed |
| 97 | + if (HighGui.waitKey(1) == 'q') { |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Release the video capture and close windows |
| 103 | + cap.release(); |
| 104 | + HighGui.destroyAllWindows(); |
| 105 | + } |
| 106 | +} |
0 commit comments