From 8da409fa80c4bd02adcd358b9d6a4a06fe7f887b Mon Sep 17 00:00:00 2001 From: Matthew Morley Date: Sun, 1 Oct 2023 15:21:44 -0400 Subject: [PATCH] Run lint --- .../vision/aruco/ArucoDetectorParams.java | 1 - .../vision/aruco/PhotonArucoDetector.java | 2 +- .../pipe/impl/ArucoDetectionPipeParams.java | 1 - .../vision/pipeline/DnnPipeline.java | 149 ++++++++++++++---- .../vision/pipeline/DnnPipelineSettings.java | 1 - 5 files changed, 116 insertions(+), 38 deletions(-) diff --git a/photon-core/src/main/java/org/photonvision/vision/aruco/ArucoDetectorParams.java b/photon-core/src/main/java/org/photonvision/vision/aruco/ArucoDetectorParams.java index cdcb9c0d9b..810afe0536 100644 --- a/photon-core/src/main/java/org/photonvision/vision/aruco/ArucoDetectorParams.java +++ b/photon-core/src/main/java/org/photonvision/vision/aruco/ArucoDetectorParams.java @@ -19,7 +19,6 @@ import org.opencv.objdetect.ArucoDetector; import org.opencv.objdetect.DetectorParameters; -import org.opencv.objdetect.Dictionary; import org.photonvision.common.logging.LogGroup; import org.photonvision.common.logging.Logger; diff --git a/photon-core/src/main/java/org/photonvision/vision/aruco/PhotonArucoDetector.java b/photon-core/src/main/java/org/photonvision/vision/aruco/PhotonArucoDetector.java index 8aae0b2472..0963a2812a 100644 --- a/photon-core/src/main/java/org/photonvision/vision/aruco/PhotonArucoDetector.java +++ b/photon-core/src/main/java/org/photonvision/vision/aruco/PhotonArucoDetector.java @@ -23,9 +23,9 @@ import edu.wpi.first.math.geometry.Translation3d; import edu.wpi.first.math.util.Units; import java.util.ArrayList; -import org.opencv.objdetect.ArucoDetector; import org.opencv.aruco.Aruco; import org.opencv.core.Mat; +import org.opencv.objdetect.ArucoDetector; import org.photonvision.common.logging.LogGroup; import org.photonvision.common.logging.Logger; import org.photonvision.vision.calibration.CameraCalibrationCoefficients; diff --git a/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ArucoDetectionPipeParams.java b/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ArucoDetectionPipeParams.java index 276005f8bd..62d3214030 100644 --- a/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ArucoDetectionPipeParams.java +++ b/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ArucoDetectionPipeParams.java @@ -18,7 +18,6 @@ package org.photonvision.vision.pipe.impl; import java.util.Objects; - import org.opencv.objdetect.ArucoDetector; import org.photonvision.vision.calibration.CameraCalibrationCoefficients; diff --git a/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipeline.java b/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipeline.java index 5135a295fe..0a3ad28cbf 100644 --- a/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipeline.java +++ b/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipeline.java @@ -19,33 +19,25 @@ import java.util.ArrayList; import java.util.List; - -import org.photonvision.common.util.ColorHelper; -import org.photonvision.vision.frame.Frame; -import org.photonvision.vision.frame.FrameThresholdType; -import org.photonvision.vision.pipe.impl.*; -import org.photonvision.vision.pipeline.result.CVPipelineResult; -import org.photonvision.vision.target.TrackedTarget; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfFloat; import org.opencv.core.MatOfInt; -import org.opencv.core.MatOfRect; import org.opencv.core.MatOfRect2d; import org.opencv.core.Point; -import org.opencv.core.Rect; import org.opencv.core.Rect2d; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.dnn.Dnn; import org.opencv.dnn.Net; -import org.opencv.dnn.SegmentationModel; -import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.utils.Converters; - -import static java.lang.Math.min; -import static java.lang.Math.max; +import org.photonvision.common.util.ColorHelper; +import org.photonvision.vision.frame.Frame; +import org.photonvision.vision.frame.FrameThresholdType; +import org.photonvision.vision.pipe.impl.*; +import org.photonvision.vision.pipeline.result.CVPipelineResult; +import org.photonvision.vision.target.TrackedTarget; public class DnnPipeline extends CVPipeline { private final CalculateFPSPipe calculateFPSPipe = new CalculateFPSPipe(); @@ -71,33 +63,115 @@ public DnnPipeline(DnnPipelineSettings settings) { try { // this.net = Dnn.readNetFromONNX("/home/matt/Downloads/best_1.onnx"); // this.net = Dnn.readNet("/home/matt/Downloads/yolov7_post_640x640.onnx"); - this.net = Dnn.readNetFromDarknet( - "/home/matt/Downloads/yolov4-csp-swish.cfg", - "/home/matt/Downloads/yolov4-csp-swish.weights" - ); + this.net = + Dnn.readNetFromDarknet( + "/home/matt/Downloads/yolov4-csp-swish.cfg", + "/home/matt/Downloads/yolov4-csp-swish.weights"); Core.setNumThreads(4); } catch (Exception e) { System.out.println(e); } this.outBlobNames = getOutputNames(net); - this.coco_names = List.of("person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"); + this.coco_names = + List.of( + "person", + "bicycle", + "car", + "motorcycle", + "airplane", + "bus", + "train", + "truck", + "boat", + "traffic light", + "fire hydrant", + "stop sign", + "parking meter", + "bench", + "bird", + "cat", + "dog", + "horse", + "sheep", + "cow", + "elephant", + "bear", + "zebra", + "giraffe", + "backpack", + "umbrella", + "handbag", + "tie", + "suitcase", + "frisbee", + "skis", + "snowboard", + "sports ball", + "kite", + "baseball bat", + "baseball glove", + "skateboard", + "surfboard", + "tennis racket", + "bottle", + "wine glass", + "cup", + "fork", + "knife", + "spoon", + "bowl", + "banana", + "apple", + "sandwich", + "orange", + "broccoli", + "carrot", + "hot dog", + "pizza", + "donut", + "cake", + "chair", + "couch", + "potted plant", + "bed", + "dining table", + "toilet", + "tv", + "laptop", + "mouse", + "remote", + "keyboard", + "cell phone", + "microwave", + "oven", + "toaster", + "sink", + "refrigerator", + "book", + "clock", + "vase", + "scissors", + "teddy bear", + "hair drier", + "toothbrush"); } @Override - protected void setPipeParamsImpl() { - } + protected void setPipeParamsImpl() {} private static List getOutputNames(Net net) { - // return new ArrayList<>(List.of("person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush")); + // return new + // ArrayList<>(List.of("person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush")); List names = new ArrayList<>(); List outLayers = net.getUnconnectedOutLayers().toList(); List layersNames = net.getLayerNames(); - outLayers.forEach((item) -> names.add(layersNames.get(item - 1)));// unfold and create R-CNN layers from the - // loaded YOLO model// + outLayers.forEach( + (item) -> names.add(layersNames.get(item - 1))); // unfold and create R-CNN layers from the + // loaded YOLO model// return names; } @@ -109,9 +183,8 @@ protected CVPipelineResult process(Frame input_frame, DnnPipelineSettings settin // ====================== - var frame = input_frame.colorImage.getMat(); - + if (frame.empty()) { return new CVPipelineResult(sumPipeNanosElapsed, 0, targetList, input_frame); } @@ -140,7 +213,7 @@ protected CVPipelineResult process(Frame input_frame, DnnPipelineSettings settin Point classIdPoint = mm.maxLoc; if (confidence > confThreshold) { // scaling for drawing the bounding boxes// - int centerX = (int) (row.get(0, 0)[0] * frame.cols()); + int centerX = (int) (row.get(0, 0)[0] * frame.cols()); int centerY = (int) (row.get(0, 1)[0] * frame.rows()); int width = (int) (row.get(0, 2)[0] * frame.cols()); int height = (int) (row.get(0, 3)[0] * frame.rows()); @@ -158,19 +231,27 @@ protected CVPipelineResult process(Frame input_frame, DnnPipelineSettings settin Rect2d[] boxesArray = rects.toArray(new Rect2d[0]); MatOfRect2d boxes = new MatOfRect2d(boxesArray); MatOfInt indices = new MatOfInt(); - Dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThresh, indices); // We draw the bounding boxes for objects - // here// + Dnn.NMSBoxes( + boxes, + confidences, + confThreshold, + nmsThresh, + indices); // We draw the bounding boxes for objects + // here// int[] ind = indices.toArray(); for (int i = 0; i < ind.length; ++i) { int idx = ind[i]; var box = boxesArray[idx]; Imgproc.rectangle(frame, box.tl(), box.br(), new Scalar(0, 0, 255), 2); - Imgproc.putText(frame, coco_names.get(clsIds.get(idx)), box.br(), - 0, - 0.6, - ColorHelper.colorToScalar(java.awt.Color.white), - 2); + Imgproc.putText( + frame, + coco_names.get(clsIds.get(idx)), + box.br(), + 0, + 0.6, + ColorHelper.colorToScalar(java.awt.Color.white), + 2); // System.out.println(idx); } diff --git a/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipelineSettings.java b/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipelineSettings.java index 0256a2ee80..e5ae95a7bc 100644 --- a/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipelineSettings.java +++ b/photon-core/src/main/java/org/photonvision/vision/pipeline/DnnPipelineSettings.java @@ -17,7 +17,6 @@ package org.photonvision.vision.pipeline; - public class DnnPipelineSettings extends CVPipelineSettings { public DnnPipelineSettings() { this.pipelineType = PipelineType.Dnn;