FUCKING SHIT RENDERS!!!!!!!!!! THE OBJECT DETECTION WORKSSS!!!

This commit is contained in:
rattatwinko
2025-05-24 19:21:52 +02:00
parent 201d8013be
commit 1b6595a700

View File

@@ -663,82 +663,103 @@ class DetectionThread extends SwingWorker<Mat, Void> {
return frame; return frame;
} }
Size frameSize = frame.size(); try {
int height = (int) frameSize.height; Size frameSize = frame.size();
int width = (int) frameSize.width; int height = (int) frameSize.height;
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);
// Run forward pass // Run forward pass
List<Mat> outs = new ArrayList<>(); List<Mat> outs = new ArrayList<>();
net.forward(outs, outputLayers); net.forward(outs, outputLayers);
// Parse detections // Parse detections
List<Integer> classIds = new ArrayList<>(); List<Integer> classIds = new ArrayList<>();
List<Float> confidences = new ArrayList<>(); List<Float> confidences = new ArrayList<>();
List<Rect2d> boxes = new ArrayList<>(); List<Rect2d> boxes = new ArrayList<>();
for (Mat out : outs) { for (Mat out : outs) {
for (int i = 0; i < out.rows(); i++) { // Convert Mat to float array
Mat row = out.row(i); float[] data = new float[(int) out.total() * out.channels()];
Mat scores = row.colRange(5, row.cols()); out.get(0, 0, data);
Core.MinMaxLocResult mmr = Core.minMaxLoc(scores);
double confidence = mmr.maxVal;
if (confidence > 0.5) { // Process each detection (85 elements per detection: [x, y, w, h, confidence, class_probs...])
Point classIdPoint = mmr.maxLoc; for (int i = 0; i < out.rows(); i++) {
int classId = (int) classIdPoint.x; int offset = i * out.cols();
float confidence = data[offset + 4];
double[] detection = row.get(0, 0); if (confidence > 0.5) {
double centerX = detection[0] * width; // Find class with maximum score
double centerY = detection[1] * height; int classId = 0;
double boxWidth = detection[2] * width; float maxScore = 0;
double boxHeight = detection[3] * height; for (int j = 5; j < out.cols(); j++) {
if (data[offset + j] > maxScore) {
maxScore = data[offset + j];
classId = j - 5;
}
}
double x = centerX - (boxWidth / 2); // Only add detection if classId is valid
double y = centerY - (boxHeight / 2); if (classId >= 0 && classId < classes.size()) {
float centerX = data[offset] * width;
float centerY = data[offset + 1] * height;
float boxWidth = data[offset + 2] * width;
float boxHeight = data[offset + 3] * height;
boxes.add(new Rect2d(x, y, boxWidth, boxHeight)); float x = centerX - (boxWidth / 2);
confidences.add((float) confidence); float y = centerY - (boxHeight / 2);
classIds.add(classId);
boxes.add(new Rect2d(x, y, boxWidth, boxHeight));
confidences.add(confidence);
classIds.add(classId);
}
}
} }
} }
}
// Apply Non-Maximum Suppression // Apply Non-Maximum Suppression
MatOfRect2d boxesMat = new MatOfRect2d(); if (!boxes.isEmpty()) {
boxesMat.fromList(boxes); MatOfRect2d boxesMat = new MatOfRect2d();
MatOfFloat confidencesMat = new MatOfFloat(); boxesMat.fromList(boxes);
confidencesMat.fromList(confidences); MatOfFloat confidencesMat = new MatOfFloat();
MatOfInt indices = new MatOfInt(); confidencesMat.fromList(confidences);
MatOfInt indices = new MatOfInt();
Dnn.NMSBoxes(boxesMat, confidencesMat, 0.5f, 0.4f, indices); Dnn.NMSBoxes(boxesMat, confidencesMat, 0.5f, 0.4f, indices);
// 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);
Scalar color = new Scalar(0, 255, 0); Scalar color = new Scalar(0, 255, 0);
Point topLeft = new Point(box.x, box.y); Point topLeft = new Point(box.x, box.y);
Point bottomRight = new Point(box.x + box.width, box.y + box.height); Point bottomRight = new Point(box.x + box.width, box.y + box.height);
Imgproc.rectangle(frame, topLeft, bottomRight, color, 2); Imgproc.rectangle(frame, topLeft, bottomRight, color, 2);
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