diff --git a/src/main/java/org/jdetect/CameraApp.java b/src/main/java/org/jdetect/CameraApp.java index ad54282..0985d92 100644 --- a/src/main/java/org/jdetect/CameraApp.java +++ b/src/main/java/org/jdetect/CameraApp.java @@ -663,82 +663,103 @@ class DetectionThread extends SwingWorker { return frame; } - Size frameSize = frame.size(); - int height = (int) frameSize.height; - int width = (int) frameSize.width; + 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); + // 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); + // Set input to the network + net.setInput(blob); - // Run forward pass - List outs = new ArrayList<>(); - net.forward(outs, outputLayers); + // Run forward pass + List outs = new ArrayList<>(); + net.forward(outs, outputLayers); - // Parse detections - List classIds = new ArrayList<>(); - List confidences = new ArrayList<>(); - List boxes = new ArrayList<>(); + // Parse detections + List classIds = new ArrayList<>(); + List confidences = new ArrayList<>(); + List boxes = new ArrayList<>(); - for (Mat out : outs) { - 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; + for (Mat out : outs) { + // Convert Mat to float array + float[] data = new float[(int) out.total() * out.channels()]; + out.get(0, 0, data); - if (confidence > 0.5) { - Point classIdPoint = mmr.maxLoc; - int classId = (int) classIdPoint.x; + // Process each detection (85 elements per detection: [x, y, w, h, confidence, class_probs...]) + for (int i = 0; i < out.rows(); i++) { + int offset = i * out.cols(); + float confidence = data[offset + 4]; - 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; + if (confidence > 0.5) { + // 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 x = centerX - (boxWidth / 2); - double y = centerY - (boxHeight / 2); + // 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; - boxes.add(new Rect2d(x, y, boxWidth, boxHeight)); - confidences.add((float) confidence); - classIds.add(classId); + float x = centerX - (boxWidth / 2); + float y = centerY - (boxHeight / 2); + + boxes.add(new Rect2d(x, y, boxWidth, boxHeight)); + confidences.add(confidence); + classIds.add(classId); + } + } } } - } - // Apply Non-Maximum Suppression - MatOfRect2d boxesMat = new MatOfRect2d(); - boxesMat.fromList(boxes); - MatOfFloat confidencesMat = new MatOfFloat(); - confidencesMat.fromList(confidences); - MatOfInt indices = new MatOfInt(); + // Apply Non-Maximum Suppression + if (!boxes.isEmpty()) { + MatOfRect2d boxesMat = new MatOfRect2d(); + boxesMat.fromList(boxes); + MatOfFloat confidencesMat = new MatOfFloat(); + 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 - int[] indicesArray = indices.toArray(); - for (int i : indicesArray) { - if (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); + // Draw detections + int[] indicesArray = indices.toArray(); + for (int i : indicesArray) { + 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); - 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); + 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); + 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); + 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); + } + } } - } - return frame; + return frame; + } catch (Exception e) { + e.printStackTrace(); + return frame; // Return original frame if detection fails + } } @Override