Skip to content

Commit

Permalink
Run lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Oct 1, 2023
1 parent 62ee656 commit 8da409f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CVPipelineResult, DnnPipelineSettings> {
private final CalculateFPSPipe calculateFPSPipe = new CalculateFPSPipe();
Expand All @@ -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<String> 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<String> names = new ArrayList<>();

List<Integer> outLayers = net.getUnconnectedOutLayers().toList();
List<String> 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;
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.photonvision.vision.pipeline;


public class DnnPipelineSettings extends CVPipelineSettings {
public DnnPipelineSettings() {
this.pipelineType = PipelineType.Dnn;
Expand Down

0 comments on commit 8da409f

Please sign in to comment.