initial working build!
This commit is contained in:
816
src/main/java/org/jdetect/CameraApp.java
Normal file
816
src/main/java/org/jdetect/CameraApp.java
Normal file
@@ -0,0 +1,816 @@
|
||||
package org.jdetect;
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.dnn.Dnn;
|
||||
import org.opencv.dnn.Net;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.videoio.VideoCapture;
|
||||
import org.opencv.videoio.Videoio;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBufferByte;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
// Custom class to hold camera information
|
||||
class CameraInfo {
|
||||
public int index;
|
||||
public int backend;
|
||||
public String name;
|
||||
|
||||
public CameraInfo(int index, int backend, String name) {
|
||||
this.index = index;
|
||||
this.backend = backend;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
// Download progress dialog class
|
||||
class DownloadProgressBar extends JDialog {
|
||||
private JLabel label;
|
||||
private JProgressBar progress;
|
||||
private JButton cancelButton;
|
||||
private AtomicBoolean cancelled = new AtomicBoolean(false);
|
||||
|
||||
public DownloadProgressBar(JFrame parent) {
|
||||
super(parent, "Downloading YOLO Files", true);
|
||||
setSize(400, 150);
|
||||
setLocationRelativeTo(parent);
|
||||
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
|
||||
|
||||
JPanel panel = new JPanel(new BorderLayout(10, 10));
|
||||
panel.setBorder(new EmptyBorder(20, 20, 20, 20));
|
||||
|
||||
label = new JLabel("Downloading YOLO files...");
|
||||
panel.add(label, BorderLayout.NORTH);
|
||||
|
||||
progress = new JProgressBar(0, 100);
|
||||
progress.setStringPainted(true);
|
||||
panel.add(progress, BorderLayout.CENTER);
|
||||
|
||||
cancelButton = new JButton("Cancel");
|
||||
cancelButton.addActionListener(e -> {
|
||||
cancelled.set(true);
|
||||
dispose();
|
||||
});
|
||||
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.add(cancelButton);
|
||||
panel.add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
add(panel);
|
||||
}
|
||||
|
||||
public void updateProgress(long current, long total) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
int percentage = (int) ((current * 100) / total);
|
||||
progress.setValue(percentage);
|
||||
progress.setString(percentage + "%");
|
||||
});
|
||||
}
|
||||
|
||||
public void updateLabel(String filename) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
label.setText("Downloading " + filename + "...");
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return cancelled.get();
|
||||
}
|
||||
}
|
||||
|
||||
// Download progress handler class
|
||||
class DownloadProgressHandler {
|
||||
private DownloadProgressBar progressDialog;
|
||||
private AtomicLong currentSize = new AtomicLong(0);
|
||||
private AtomicLong totalSize = new AtomicLong(0);
|
||||
private AtomicLong lastUpdate = new AtomicLong(0);
|
||||
|
||||
public DownloadProgressHandler(DownloadProgressBar progressDialog) {
|
||||
this.progressDialog = progressDialog;
|
||||
}
|
||||
|
||||
public void handleProgress(long current, long total) {
|
||||
this.totalSize.set(total);
|
||||
this.currentSize.set(current);
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (currentTime - lastUpdate.get() > 100) { // Update every 100ms
|
||||
progressDialog.updateProgress(current, total);
|
||||
lastUpdate.set(currentTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Detection thread class
|
||||
class DetectionThread extends Thread {
|
||||
private Net net;
|
||||
private List<String> classes;
|
||||
private List<String> outputLayers;
|
||||
private Mat frame;
|
||||
private AtomicBoolean running = new AtomicBoolean(true);
|
||||
private CompletableFuture<Mat> detectionDone;
|
||||
|
||||
public DetectionThread(Net net, List<String> classes, List<String> outputLayers, Mat frame) {
|
||||
this.net = net;
|
||||
this.classes = classes;
|
||||
this.outputLayers = outputLayers;
|
||||
this.frame = frame.clone();
|
||||
this.detectionDone = new CompletableFuture<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!running.get() || net == null || classes == null) {
|
||||
detectionDone.complete(frame);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
int height = frame.rows();
|
||||
int width = frame.cols();
|
||||
|
||||
// Create blob from image
|
||||
Mat blob = Dnn.blobFromImage(frame, 1.0/255.0, new Size(416, 416), new Scalar(0), true, false);
|
||||
|
||||
// Set input to the network
|
||||
net.setInput(blob);
|
||||
|
||||
// Run forward pass
|
||||
List<Mat> outs = new ArrayList<>();
|
||||
net.forward(outs, outputLayers);
|
||||
|
||||
List<Integer> classIds = new ArrayList<>();
|
||||
List<Float> confidences = new ArrayList<>();
|
||||
List<Rect2d> boxes = new ArrayList<>();
|
||||
|
||||
// Process detections
|
||||
for (Mat out : outs) {
|
||||
for (int i = 0; i < out.rows(); ++i) {
|
||||
Mat row = out.row(i);
|
||||
Mat scores = row.colRange(5, out.cols());
|
||||
Core.MinMaxLocResult mm = Core.minMaxLoc(scores);
|
||||
float confidence = (float) mm.maxVal;
|
||||
Point classIdPoint = mm.maxLoc;
|
||||
|
||||
if (confidence > 0.5) {
|
||||
double[] detection = row.get(0, 0);
|
||||
double centerX = detection[0] * width;
|
||||
double centerY = detection[1] * height;
|
||||
double boxWidth = detection[2] * width;
|
||||
double boxHeight = detection[3] * height;
|
||||
|
||||
double x = centerX - (boxWidth / 2);
|
||||
double y = centerY - (boxHeight / 2);
|
||||
|
||||
boxes.add(new Rect2d(x, y, boxWidth, boxHeight));
|
||||
confidences.add(confidence);
|
||||
classIds.add((int) classIdPoint.x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply Non-Maximum Suppression
|
||||
MatOfFloat confidencesMat = new MatOfFloat();
|
||||
confidencesMat.fromList(confidences);
|
||||
|
||||
MatOfRect2d boxesMat = new MatOfRect2d();
|
||||
boxesMat.fromArray(boxes.toArray(new Rect2d[0]));
|
||||
|
||||
MatOfInt indices = new MatOfInt();
|
||||
Dnn.NMSBoxes(boxesMat, confidencesMat, 0.5f, 0.4f, indices);
|
||||
|
||||
// Draw bounding boxes
|
||||
int[] indicesArray = indices.toArray();
|
||||
for (int i = 0; i < indicesArray.length; ++i) {
|
||||
int idx = indicesArray[i];
|
||||
Rect2d box = boxes.get(idx);
|
||||
String label = classes.get(classIds.get(idx));
|
||||
float confidence = confidences.get(idx);
|
||||
|
||||
Scalar color = new Scalar(0, 255, 0);
|
||||
Point topLeft = new Point(box.x, box.y);
|
||||
Point bottomRight = new Point(box.x + box.width, box.y + box.height);
|
||||
|
||||
Imgproc.rectangle(frame, topLeft, bottomRight, color, 2);
|
||||
|
||||
String text = String.format("%s: %.2f", label, confidence);
|
||||
Point textPoint = new Point(box.x, box.y - 5);
|
||||
Imgproc.putText(frame, text, textPoint, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, color, 2);
|
||||
}
|
||||
|
||||
detectionDone.complete(frame);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
detectionDone.complete(frame);
|
||||
}
|
||||
}
|
||||
|
||||
public void stopDetection() {
|
||||
running.set(false);
|
||||
try {
|
||||
this.join(1000); // Wait up to 1 second
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Mat> getDetectionResult() {
|
||||
return detectionDone;
|
||||
}
|
||||
}
|
||||
|
||||
// Main camera application class
|
||||
public class CameraApp extends JFrame {
|
||||
// UI Components
|
||||
private JLabel videoLabel;
|
||||
private JComboBox<CameraInfo> cameraSelect;
|
||||
private JCheckBox detectionCheckbox;
|
||||
private JButton startButton;
|
||||
private JButton stopButton;
|
||||
private JButton snapshotButton;
|
||||
private JLabel statusLabel;
|
||||
|
||||
// Camera and detection attributes
|
||||
private int cameraIndex = 0;
|
||||
private VideoCapture cap;
|
||||
private Timer timer;
|
||||
private List<CameraInfo> availableCameras;
|
||||
private DetectionThread detectionThread;
|
||||
private Net net;
|
||||
private List<String> classes;
|
||||
private List<String> outputLayers;
|
||||
private AtomicBoolean detectionEnabled = new AtomicBoolean(true);
|
||||
private AtomicBoolean modelLoaded = new AtomicBoolean(false);
|
||||
private double fps = 0;
|
||||
private int frameCount = 0;
|
||||
private long lastFpsUpdate = System.currentTimeMillis();
|
||||
private ExecutorService executorService;
|
||||
|
||||
// Load OpenCV native library
|
||||
static {
|
||||
nu.pattern.OpenCV.loadLocally();
|
||||
}
|
||||
|
||||
public CameraApp() {
|
||||
super("Advanced Object Detection Camera Viewer");
|
||||
|
||||
// Initialize attributes
|
||||
availableCameras = new ArrayList<>();
|
||||
executorService = Executors.newCachedThreadPool();
|
||||
|
||||
// Initialize UI
|
||||
initUI();
|
||||
|
||||
// Load YOLO after UI is ready
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
Timer loadTimer = new Timer(100, e -> {
|
||||
loadYolo();
|
||||
((Timer) e.getSource()).stop();
|
||||
});
|
||||
loadTimer.setRepeats(false);
|
||||
loadTimer.start();
|
||||
});
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(1000, 800);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
// Main panel
|
||||
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
|
||||
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
||||
|
||||
// Video display
|
||||
videoLabel = new JLabel();
|
||||
videoLabel.setHorizontalAlignment(JLabel.CENTER);
|
||||
videoLabel.setVerticalAlignment(JLabel.CENTER);
|
||||
videoLabel.setPreferredSize(new Dimension(640, 480));
|
||||
videoLabel.setBorder(BorderFactory.createLoweredBevelBorder());
|
||||
videoLabel.setText("Camera feed will appear here");
|
||||
mainPanel.add(videoLabel, BorderLayout.CENTER);
|
||||
|
||||
// Control panel
|
||||
JPanel controlPanel = new JPanel(new GridBagLayout());
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.insets = new Insets(5, 5, 5, 5);
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||
|
||||
// Camera selection
|
||||
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
|
||||
cameraSelect = new JComboBox<>();
|
||||
cameraSelect.addActionListener(this::changeCamera);
|
||||
controlPanel.add(cameraSelect, gbc);
|
||||
|
||||
// Detection checkbox
|
||||
gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2;
|
||||
detectionCheckbox = new JCheckBox("Enable Object Detection", true);
|
||||
detectionCheckbox.addActionListener(this::toggleDetection);
|
||||
controlPanel.add(detectionCheckbox, gbc);
|
||||
|
||||
// Buttons
|
||||
gbc.gridwidth = 1;
|
||||
gbc.gridx = 0; gbc.gridy = 2;
|
||||
startButton = new JButton("Start Camera");
|
||||
startButton.addActionListener(e -> startCamera());
|
||||
controlPanel.add(startButton, gbc);
|
||||
|
||||
gbc.gridx = 1; gbc.gridy = 2;
|
||||
stopButton = new JButton("Stop Camera");
|
||||
stopButton.addActionListener(e -> stopCamera());
|
||||
controlPanel.add(stopButton, gbc);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2;
|
||||
snapshotButton = new JButton("Take Snapshot");
|
||||
snapshotButton.addActionListener(e -> takeSnapshot());
|
||||
controlPanel.add(snapshotButton, gbc);
|
||||
|
||||
mainPanel.add(controlPanel, BorderLayout.EAST);
|
||||
|
||||
// Status bar
|
||||
statusLabel = new JLabel("Initializing...");
|
||||
statusLabel.setBorder(new EmptyBorder(5, 10, 5, 10));
|
||||
mainPanel.add(statusLabel, BorderLayout.SOUTH);
|
||||
|
||||
add(mainPanel);
|
||||
|
||||
// Set up timer
|
||||
timer = new Timer(30, this::updateFrame);
|
||||
|
||||
// Detect cameras after UI is ready
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
Timer detectTimer = new Timer(100, e -> {
|
||||
detectCameras();
|
||||
((Timer) e.getSource()).stop();
|
||||
});
|
||||
detectTimer.setRepeats(false);
|
||||
detectTimer.start();
|
||||
});
|
||||
|
||||
// Window close event
|
||||
addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
closeEvent();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void toggleDetection(ActionEvent e) {
|
||||
detectionEnabled.set(detectionCheckbox.isSelected());
|
||||
statusLabel.setText("Detection " + (detectionEnabled.get() ? "enabled" : "disabled"));
|
||||
}
|
||||
|
||||
private void detectCameras() {
|
||||
cameraSelect.removeAllItems();
|
||||
availableCameras.clear();
|
||||
|
||||
// Check for available cameras
|
||||
int[] backends = {Videoio.CAP_DSHOW, Videoio.CAP_V4L2, Videoio.CAP_ANY};
|
||||
int maxCamerasToCheck = 10;
|
||||
|
||||
for (int backend : backends) {
|
||||
for (int i = 0; i < maxCamerasToCheck; i++) {
|
||||
try {
|
||||
VideoCapture testCap = new VideoCapture(i, backend);
|
||||
if (testCap.isOpened()) {
|
||||
// Get camera properties
|
||||
int width = (int) testCap.get(Videoio.CAP_PROP_FRAME_WIDTH);
|
||||
int height = (int) testCap.get(Videoio.CAP_PROP_FRAME_HEIGHT);
|
||||
double fps = testCap.get(Videoio.CAP_PROP_FPS);
|
||||
|
||||
String name = String.format("Camera %d (%dx%d)", i, width, height);
|
||||
if (fps > 0) {
|
||||
name += String.format(" @ %.1fFPS", fps);
|
||||
}
|
||||
|
||||
CameraInfo camInfo = new CameraInfo(i, backend, name);
|
||||
availableCameras.add(camInfo);
|
||||
cameraSelect.addItem(camInfo);
|
||||
testCap.release();
|
||||
break; // Only add once per camera index
|
||||
}
|
||||
testCap.release();
|
||||
} catch (Exception ex) {
|
||||
// Ignore errors for non-existent cameras
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (availableCameras.isEmpty()) {
|
||||
statusLabel.setText("No cameras detected");
|
||||
JOptionPane.showMessageDialog(this, "No cameras detected!", "Warning", JOptionPane.WARNING_MESSAGE);
|
||||
} else {
|
||||
statusLabel.setText(String.format("Found %d cameras", availableCameras.size()));
|
||||
}
|
||||
}
|
||||
|
||||
private void changeCamera(ActionEvent e) {
|
||||
CameraInfo selected = (CameraInfo) cameraSelect.getSelectedItem();
|
||||
if (selected != null) {
|
||||
cameraIndex = selected.index;
|
||||
if (cap != null && cap.isOpened()) {
|
||||
stopCamera();
|
||||
startCamera();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startCamera() {
|
||||
if (cap != null) {
|
||||
stopCamera();
|
||||
}
|
||||
|
||||
if (availableCameras.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "No cameras available", "Error", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
CameraInfo selected = (CameraInfo) cameraSelect.getSelectedItem();
|
||||
if (selected == null) {
|
||||
selected = availableCameras.get(0);
|
||||
}
|
||||
|
||||
cap = new VideoCapture(selected.index, selected.backend);
|
||||
if (!cap.isOpened()) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
String.format("Cannot open camera %d", selected.index),
|
||||
"Error", JOptionPane.ERROR_MESSAGE);
|
||||
statusLabel.setText(String.format("Failed to open camera %d", selected.index));
|
||||
return;
|
||||
}
|
||||
|
||||
// Set camera properties
|
||||
cap.set(Videoio.CAP_PROP_FRAME_WIDTH, 1280);
|
||||
cap.set(Videoio.CAP_PROP_FRAME_HEIGHT, 720);
|
||||
cap.set(Videoio.CAP_PROP_FPS, 60);
|
||||
cap.set(Videoio.CAP_PROP_AUTOFOCUS, 0);
|
||||
|
||||
timer.start();
|
||||
statusLabel.setText(String.format("Camera %d started", selected.index));
|
||||
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"Error starting camera: " + ex.getMessage(),
|
||||
"Error", JOptionPane.ERROR_MESSAGE);
|
||||
statusLabel.setText("Error: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void stopCamera() {
|
||||
if (cap != null) {
|
||||
timer.stop();
|
||||
cap.release();
|
||||
cap = null;
|
||||
videoLabel.setIcon(null);
|
||||
videoLabel.setText("Camera feed will appear here");
|
||||
statusLabel.setText("Camera stopped");
|
||||
}
|
||||
|
||||
if (detectionThread != null) {
|
||||
detectionThread.stopDetection();
|
||||
detectionThread = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadYolo() {
|
||||
String weightsUrl = "https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights";
|
||||
String configUrl = "https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny.cfg";
|
||||
String classesUrl = "https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/coco.names";
|
||||
|
||||
String weightsPath = "yolov4-tiny.weights";
|
||||
String configPath = "yolov4-tiny.cfg";
|
||||
String classesPath = "coco.names";
|
||||
|
||||
boolean filesValid = true;
|
||||
|
||||
// Check if files exist and have minimum sizes
|
||||
if (!Files.exists(Paths.get(weightsPath)) || getFileSize(weightsPath) < 10000000) {
|
||||
filesValid = false;
|
||||
}
|
||||
if (!Files.exists(Paths.get(configPath)) || getFileSize(configPath) < 10000) {
|
||||
filesValid = false;
|
||||
}
|
||||
if (!Files.exists(Paths.get(classesPath)) || getFileSize(classesPath) < 1000) {
|
||||
filesValid = false;
|
||||
}
|
||||
|
||||
if (!filesValid) {
|
||||
int reply = JOptionPane.showConfirmDialog(this,
|
||||
"YOLO model files need to be downloaded (~25MB). Continue?",
|
||||
"Download Files",
|
||||
JOptionPane.YES_NO_OPTION);
|
||||
|
||||
if (reply != JOptionPane.YES_OPTION) {
|
||||
statusLabel.setText("Object detection disabled - model not loaded");
|
||||
return;
|
||||
}
|
||||
|
||||
downloadYoloFiles(weightsUrl, configUrl, classesUrl);
|
||||
}
|
||||
|
||||
try {
|
||||
net = Dnn.readNetFromDarknet(configPath, weightsPath);
|
||||
|
||||
// Try to use CUDA if available (OpenCV Java bindings may not support this fully)
|
||||
try {
|
||||
net.setPreferableBackend(Dnn.DNN_BACKEND_CUDA);
|
||||
net.setPreferableTarget(Dnn.DNN_TARGET_CUDA);
|
||||
statusLabel.setText("Attempting CUDA acceleration");
|
||||
} catch (Exception e) {
|
||||
net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
|
||||
net.setPreferableTarget(Dnn.DNN_TARGET_CPU);
|
||||
statusLabel.setText("Using CPU (no CUDA available)");
|
||||
}
|
||||
|
||||
// Load class names
|
||||
classes = new ArrayList<>();
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(classesPath))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
classes.add(line.trim());
|
||||
}
|
||||
}
|
||||
|
||||
// Get output layer names
|
||||
List<String> layerNames = net.getLayerNames();
|
||||
MatOfInt unconnectedOutLayers = net.getUnconnectedOutLayers();
|
||||
int[] indices = unconnectedOutLayers.toArray();
|
||||
outputLayers = new ArrayList<>();
|
||||
for (int index : indices) {
|
||||
outputLayers.add(layerNames.get(index - 1));
|
||||
}
|
||||
|
||||
modelLoaded.set(true);
|
||||
statusLabel.setText("YOLOv4 model loaded successfully");
|
||||
|
||||
} catch (Exception e) {
|
||||
modelLoaded.set(false);
|
||||
statusLabel.setText("Error loading model: " + e.getMessage());
|
||||
JOptionPane.showMessageDialog(this, "Failed to load object detection model", "Error", JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
private long getFileSize(String filePath) {
|
||||
try {
|
||||
return Files.size(Paths.get(filePath));
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadYoloFiles(String weightsUrl, String configUrl, String classesUrl) {
|
||||
String[][] files = {
|
||||
{"yolov4-tiny.weights", weightsUrl},
|
||||
{"yolov4-tiny.cfg", configUrl},
|
||||
{"coco.names", classesUrl}
|
||||
};
|
||||
|
||||
DownloadProgressBar progressDialog = new DownloadProgressBar(this);
|
||||
progressDialog.setVisible(true);
|
||||
|
||||
// Download in background thread
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
for (String[] file : files) {
|
||||
String fileName = file[0];
|
||||
String url = file[1];
|
||||
|
||||
if (Files.exists(Paths.get(fileName))) {
|
||||
try {
|
||||
long size = getFileSize(fileName);
|
||||
if ((fileName.equals("yolov4-tiny.weights") && size >= 10000000) ||
|
||||
(fileName.equals("yolov4-tiny.cfg") && size >= 10000) ||
|
||||
(fileName.equals("coco.names") && size >= 1000)) {
|
||||
continue;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Existing file " + fileName + " is invalid: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (progressDialog.isCancelled()) {
|
||||
break;
|
||||
}
|
||||
|
||||
System.out.println("Downloading " + fileName + "...");
|
||||
progressDialog.updateLabel(fileName);
|
||||
|
||||
try {
|
||||
downloadFile(url, fileName, progressDialog);
|
||||
System.out.println("Downloaded " + fileName);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error downloading " + fileName + ": " + e.getMessage());
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Failed to download " + fileName, "Error", JOptionPane.ERROR_MESSAGE);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
SwingUtilities.invokeLater(() -> progressDialog.dispose());
|
||||
}
|
||||
}, executorService);
|
||||
}
|
||||
|
||||
private void downloadFile(String urlString, String fileName, DownloadProgressBar progressDialog) throws Exception {
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.connect();
|
||||
|
||||
int contentLength = connection.getContentLength();
|
||||
|
||||
try (InputStream inputStream = connection.getInputStream();
|
||||
FileOutputStream outputStream = new FileOutputStream(fileName)) {
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
long totalBytesRead = 0;
|
||||
long lastUpdate = 0;
|
||||
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
if (progressDialog.isCancelled()) {
|
||||
throw new InterruptedException("Download cancelled by user");
|
||||
}
|
||||
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
totalBytesRead += bytesRead;
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (currentTime - lastUpdate > 100) { // Update every 100ms
|
||||
progressDialog.updateProgress(totalBytesRead, contentLength);
|
||||
lastUpdate = currentTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateFrame(ActionEvent e) {
|
||||
if (cap == null || !cap.isOpened()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Mat frame = new Mat();
|
||||
boolean ret = cap.read(frame);
|
||||
if (!ret || frame.empty()) {
|
||||
statusLabel.setText("Failed to capture frame");
|
||||
return;
|
||||
}
|
||||
|
||||
frameCount++;
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (currentTime - lastFpsUpdate >= 1000) {
|
||||
fps = frameCount * 1000.0 / (currentTime - lastFpsUpdate);
|
||||
lastFpsUpdate = currentTime;
|
||||
frameCount = 0;
|
||||
statusLabel.setText(String.format("Running at %.1f FPS", fps));
|
||||
}
|
||||
|
||||
// Convert BGR to RGB
|
||||
Mat rgbFrame = new Mat();
|
||||
Imgproc.cvtColor(frame, rgbFrame, Imgproc.COLOR_BGR2RGB);
|
||||
|
||||
if (detectionEnabled.get() && modelLoaded.get()) {
|
||||
if (detectionThread == null || !detectionThread.isAlive()) {
|
||||
detectionThread = new DetectionThread(net, classes, outputLayers, rgbFrame);
|
||||
detectionThread.start();
|
||||
|
||||
// Handle detection result asynchronously
|
||||
detectionThread.getDetectionResult().thenAccept(this::displayFrame);
|
||||
} else {
|
||||
displayFrame(rgbFrame);
|
||||
}
|
||||
} else {
|
||||
displayFrame(rgbFrame);
|
||||
}
|
||||
}
|
||||
|
||||
private void displayFrame(Mat frame) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
BufferedImage bufferedImage = matToBufferedImage(frame);
|
||||
if (bufferedImage != null) {
|
||||
// Scale image to fit label while maintaining aspect ratio
|
||||
Dimension labelSize = videoLabel.getSize();
|
||||
ImageIcon scaledIcon = new ImageIcon(bufferedImage.getScaledInstance(
|
||||
labelSize.width, labelSize.height, Image.SCALE_SMOOTH));
|
||||
videoLabel.setIcon(scaledIcon);
|
||||
videoLabel.setText(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private BufferedImage matToBufferedImage(Mat mat) {
|
||||
try {
|
||||
int type = BufferedImage.TYPE_BYTE_GRAY;
|
||||
if (mat.channels() > 1) {
|
||||
type = BufferedImage.TYPE_3BYTE_BGR;
|
||||
}
|
||||
|
||||
int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
mat.get(0, 0, buffer); // get all pixels
|
||||
|
||||
BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
||||
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
||||
System.arraycopy(buffer, 0, targetPixels, 0, buffer.length);
|
||||
|
||||
return image;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void takeSnapshot() {
|
||||
if (cap == null || !cap.isOpened()) {
|
||||
JOptionPane.showMessageDialog(this, "No active camera to take snapshot from", "Warning", JOptionPane.WARNING_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
Mat frame = new Mat();
|
||||
boolean ret = cap.read(frame);
|
||||
if (!ret || frame.empty()) {
|
||||
JOptionPane.showMessageDialog(this, "Failed to capture frame", "Warning", JOptionPane.WARNING_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Save Snapshot");
|
||||
fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JPEG Images", "jpg", "jpeg"));
|
||||
|
||||
int result = fileChooser.showSaveDialog(this);
|
||||
if (result == JFileChooser.APPROVE_OPTION) {
|
||||
String fileName = fileChooser.getSelectedFile().getAbsolutePath();
|
||||
if (!fileName.toLowerCase().endsWith(".jpg") && !fileName.toLowerCase().endsWith(".jpeg")) {
|
||||
fileName += ".jpg";
|
||||
}
|
||||
|
||||
// Convert BGR to RGB for saving
|
||||
Mat rgbFrame = new Mat();
|
||||
Imgproc.cvtColor(frame, rgbFrame, Imgproc.COLOR_BGR2RGB);
|
||||
|
||||
boolean success = Imgcodecs.imwrite(fileName, rgbFrame);
|
||||
if (success) {
|
||||
statusLabel.setText("Snapshot saved to " + fileName);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Failed to save snapshot", "Error", JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void closeEvent() {
|
||||
stopCamera();
|
||||
if (detectionThread != null) {
|
||||
detectionThread.stopDetection();
|
||||
}
|
||||
if (executorService != null) {
|
||||
executorService.shutdown();
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
CameraApp app = new CameraApp();
|
||||
app.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user