initial working build!

This commit is contained in:
rattatwinko
2025-05-23 19:08:56 +02:00
commit f2eb9be5d7
7 changed files with 1202 additions and 0 deletions

42
.gitignore vendored Normal file
View File

@@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

20
build.gradle.kts Normal file
View File

@@ -0,0 +1,20 @@
plugins {
id("java")
}
group = "org.jdetect"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation("org.openpnp:opencv:4.5.1-2")
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.test {
useJUnitPlatform()
}

BIN
gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

234
gradlew vendored Executable file
View File

@@ -0,0 +1,234 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

89
gradlew.bat vendored Normal file
View File

@@ -0,0 +1,89 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

1
settings.gradle.kts Normal file
View File

@@ -0,0 +1 @@
rootProject.name = "CameraApp"

View 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);
});
}
}