experimental #1

Merged
rattatwinko merged 4 commits from experimental into master 2025-05-24 17:29:25 +00:00
Showing only changes of commit 1b6595a700 - Show all commits

View File

@@ -663,12 +663,14 @@ class DetectionThread extends SwingWorker<Mat, Void> {
return frame; return frame;
} }
try {
Size frameSize = frame.size(); Size frameSize = frame.size();
int height = (int) frameSize.height; int height = (int) frameSize.height;
int width = (int) frameSize.width; int width = (int) frameSize.width;
// Create blob from image // Create blob from image
Mat blob = Dnn.blobFromImage(frame, 1.0/255.0, new Size(416, 416), new Scalar(0), true, false); Mat blob = Dnn.blobFromImage(frame, 1.0/255.0, new Size(416, 416),
new Scalar(0), true, false);
// Set input to the network // Set input to the network
net.setInput(blob); net.setInput(blob);
@@ -683,33 +685,46 @@ class DetectionThread extends SwingWorker<Mat, Void> {
List<Rect2d> boxes = new ArrayList<>(); List<Rect2d> boxes = new ArrayList<>();
for (Mat out : outs) { for (Mat out : outs) {
// Convert Mat to float array
float[] data = new float[(int) out.total() * out.channels()];
out.get(0, 0, data);
// Process each detection (85 elements per detection: [x, y, w, h, confidence, class_probs...])
for (int i = 0; i < out.rows(); i++) { for (int i = 0; i < out.rows(); i++) {
Mat row = out.row(i); int offset = i * out.cols();
Mat scores = row.colRange(5, row.cols()); float confidence = data[offset + 4];
Core.MinMaxLocResult mmr = Core.minMaxLoc(scores);
double confidence = mmr.maxVal;
if (confidence > 0.5) { if (confidence > 0.5) {
Point classIdPoint = mmr.maxLoc; // Find class with maximum score
int classId = (int) classIdPoint.x; int classId = 0;
float maxScore = 0;
for (int j = 5; j < out.cols(); j++) {
if (data[offset + j] > maxScore) {
maxScore = data[offset + j];
classId = j - 5;
}
}
double[] detection = row.get(0, 0); // Only add detection if classId is valid
double centerX = detection[0] * width; if (classId >= 0 && classId < classes.size()) {
double centerY = detection[1] * height; float centerX = data[offset] * width;
double boxWidth = detection[2] * width; float centerY = data[offset + 1] * height;
double boxHeight = detection[3] * height; float boxWidth = data[offset + 2] * width;
float boxHeight = data[offset + 3] * height;
double x = centerX - (boxWidth / 2); float x = centerX - (boxWidth / 2);
double y = centerY - (boxHeight / 2); float y = centerY - (boxHeight / 2);
boxes.add(new Rect2d(x, y, boxWidth, boxHeight)); boxes.add(new Rect2d(x, y, boxWidth, boxHeight));
confidences.add((float) confidence); confidences.add(confidence);
classIds.add(classId); classIds.add(classId);
} }
} }
} }
}
// Apply Non-Maximum Suppression // Apply Non-Maximum Suppression
if (!boxes.isEmpty()) {
MatOfRect2d boxesMat = new MatOfRect2d(); MatOfRect2d boxesMat = new MatOfRect2d();
boxesMat.fromList(boxes); boxesMat.fromList(boxes);
MatOfFloat confidencesMat = new MatOfFloat(); MatOfFloat confidencesMat = new MatOfFloat();
@@ -721,7 +736,7 @@ class DetectionThread extends SwingWorker<Mat, Void> {
// Draw detections // Draw detections
int[] indicesArray = indices.toArray(); int[] indicesArray = indices.toArray();
for (int i : indicesArray) { for (int i : indicesArray) {
if (i < boxes.size() && i < classIds.size() && i < confidences.size()) { if (i >= 0 && i < boxes.size() && i < classIds.size() && i < confidences.size()) {
Rect2d box = boxes.get(i); Rect2d box = boxes.get(i);
String label = classes.get(classIds.get(i)); String label = classes.get(classIds.get(i));
float confidence = confidences.get(i); float confidence = confidences.get(i);
@@ -734,11 +749,17 @@ class DetectionThread extends SwingWorker<Mat, Void> {
String text = String.format("%s: %.2f", label, confidence); String text = String.format("%s: %.2f", label, confidence);
Point textPoint = new Point(box.x, box.y - 5); Point textPoint = new Point(box.x, box.y - 5);
Imgproc.putText(frame, text, textPoint, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, color, 2); Imgproc.putText(frame, text, textPoint,
Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, color, 2);
}
} }
} }
return frame; return frame;
} catch (Exception e) {
e.printStackTrace();
return frame; // Return original frame if detection fails
}
} }
@Override @Override