experimental #1
@@ -663,12 +663,14 @@ class DetectionThread extends SwingWorker<Mat, Void> {
|
||||
return frame;
|
||||
}
|
||||
|
||||
try {
|
||||
Size frameSize = frame.size();
|
||||
int height = (int) frameSize.height;
|
||||
int width = (int) frameSize.width;
|
||||
|
||||
// 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
|
||||
net.setInput(blob);
|
||||
@@ -683,33 +685,46 @@ class DetectionThread extends SwingWorker<Mat, Void> {
|
||||
List<Rect2d> boxes = new ArrayList<>();
|
||||
|
||||
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++) {
|
||||
Mat row = out.row(i);
|
||||
Mat scores = row.colRange(5, row.cols());
|
||||
Core.MinMaxLocResult mmr = Core.minMaxLoc(scores);
|
||||
double confidence = mmr.maxVal;
|
||||
int offset = i * out.cols();
|
||||
float confidence = data[offset + 4];
|
||||
|
||||
if (confidence > 0.5) {
|
||||
Point classIdPoint = mmr.maxLoc;
|
||||
int classId = (int) classIdPoint.x;
|
||||
// Find class with maximum score
|
||||
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);
|
||||
double centerX = detection[0] * width;
|
||||
double centerY = detection[1] * height;
|
||||
double boxWidth = detection[2] * width;
|
||||
double boxHeight = detection[3] * height;
|
||||
// Only add detection if classId is valid
|
||||
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;
|
||||
|
||||
double x = centerX - (boxWidth / 2);
|
||||
double y = centerY - (boxHeight / 2);
|
||||
float x = centerX - (boxWidth / 2);
|
||||
float y = centerY - (boxHeight / 2);
|
||||
|
||||
boxes.add(new Rect2d(x, y, boxWidth, boxHeight));
|
||||
confidences.add((float) confidence);
|
||||
confidences.add(confidence);
|
||||
classIds.add(classId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply Non-Maximum Suppression
|
||||
if (!boxes.isEmpty()) {
|
||||
MatOfRect2d boxesMat = new MatOfRect2d();
|
||||
boxesMat.fromList(boxes);
|
||||
MatOfFloat confidencesMat = new MatOfFloat();
|
||||
@@ -721,7 +736,7 @@ class DetectionThread extends SwingWorker<Mat, Void> {
|
||||
// Draw detections
|
||||
int[] indicesArray = indices.toArray();
|
||||
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);
|
||||
String label = classes.get(classIds.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);
|
||||
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;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return frame; // Return original frame if detection fails
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user