2019-09-15 15:03:44 -04:00
|
|
|
package com.chameleonvision.vision.process;
|
2019-09-14 17:14:49 +03:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
import com.chameleonvision.MemoryManager;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
2019-09-16 04:08:23 -04:00
|
|
|
import com.chameleonvision.vision.CameraValues;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.vision.Pipeline;
|
2019-09-18 03:08:06 -04:00
|
|
|
import edu.wpi.cscore.CvSink;
|
|
|
|
|
import edu.wpi.cscore.CvSource;
|
2019-09-17 11:22:54 +03:00
|
|
|
import edu.wpi.first.networktables.*;
|
2019-09-14 17:14:49 +03:00
|
|
|
import edu.wpi.first.cameraserver.CameraServer;
|
2019-09-16 04:08:23 -04:00
|
|
|
import org.opencv.core.*;
|
|
|
|
|
import org.opencv.imgproc.Imgproc;
|
2019-09-14 17:14:49 +03:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class CameraProcess implements Runnable {
|
2019-09-14 17:14:49 +03:00
|
|
|
private String CameraName;
|
2019-09-16 04:08:23 -04:00
|
|
|
|
2019-09-18 03:08:06 -04:00
|
|
|
// NetworkTables
|
|
|
|
|
private NetworkTableEntry ntPipelineEntry;
|
|
|
|
|
private NetworkTableEntry ntDriverModeEntry;
|
|
|
|
|
private NetworkTableEntry ntYawEntry;
|
|
|
|
|
private NetworkTableEntry ntPitchEntry;
|
|
|
|
|
private NetworkTableEntry ntDistanceEntry;
|
|
|
|
|
private NetworkTableEntry ntTimeStampEntry;
|
|
|
|
|
private NetworkTableEntry ntValidEntry;
|
2019-09-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
private MemoryManager memManager = new MemoryManager(125);
|
|
|
|
|
|
2019-09-18 03:08:06 -04:00
|
|
|
// chameleon specific
|
|
|
|
|
private Pipeline currentPipeline;
|
|
|
|
|
private VisionProcess visionProcess;
|
|
|
|
|
private CameraValues camVals;
|
|
|
|
|
|
|
|
|
|
// cscore
|
|
|
|
|
private CvSink cvSink;
|
|
|
|
|
private CvSource cvPublish;
|
|
|
|
|
|
|
|
|
|
// pipeline process items
|
|
|
|
|
private List<MatOfPoint> FoundContours = new ArrayList<>();
|
|
|
|
|
private List<MatOfPoint> FilteredContours = new ArrayList<>();
|
|
|
|
|
private List<RotatedRect> GroupedContours = new ArrayList<>();
|
|
|
|
|
private Mat cameraInputMat = new Mat();
|
|
|
|
|
private Mat hsvThreshMat = new Mat();
|
|
|
|
|
private Mat streamOutputMat = new Mat();
|
|
|
|
|
private Scalar contourRectColor = new Scalar(255, 0, 0);
|
|
|
|
|
|
|
|
|
|
private void ChangeCameraValues(int exposure, int brightness) {
|
|
|
|
|
SettingsManager.UsbCameras.get(CameraName).setBrightness(brightness);
|
|
|
|
|
SettingsManager.UsbCameras.get(CameraName).setExposureManual(exposure);
|
2019-09-17 11:22:54 +03:00
|
|
|
}
|
2019-09-18 03:04:48 +03:00
|
|
|
|
|
|
|
|
private void DriverModeListener(EntryNotification entryNotification) {
|
|
|
|
|
if (entryNotification.value.getBoolean()) {
|
|
|
|
|
ChangeCameraValues(25, 15);
|
|
|
|
|
} else {
|
2019-09-17 11:22:54 +03:00
|
|
|
Pipeline pipeline = SettingsManager.Cameras.get(CameraName).pipelines.get(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
|
|
|
|
ChangeCameraValues(pipeline.exposure, pipeline.brightness);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-18 03:04:48 +03:00
|
|
|
|
|
|
|
|
private void PipelineListener(EntryNotification entryNotification) {
|
|
|
|
|
if (SettingsManager.Cameras.get(CameraName).pipelines.containsKey(entryNotification.value.getString())) {
|
|
|
|
|
SettingsManager.CamerasCurrentPipeline.put(CameraName, entryNotification.value.getString());
|
2019-09-17 11:22:54 +03:00
|
|
|
Pipeline pipeline = SettingsManager.Cameras.get(CameraName).pipelines.get(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
|
|
|
|
ChangeCameraValues(pipeline.exposure, pipeline.brightness);
|
|
|
|
|
//TODO Send Pipeline change using websocket to client
|
2019-09-18 03:04:48 +03:00
|
|
|
} else {
|
2019-09-17 11:22:54 +03:00
|
|
|
ntPipelineEntry.setString(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-18 03:04:48 +03:00
|
|
|
|
2019-09-18 03:08:06 -04:00
|
|
|
public CameraProcess(String cameraName) {
|
|
|
|
|
CameraName = cameraName;
|
2019-09-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
// NetworkTables
|
2019-09-18 03:08:06 -04:00
|
|
|
NetworkTable ntTable = NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + cameraName);
|
2019-09-16 04:08:23 -04:00
|
|
|
ntPipelineEntry = ntTable.getEntry("Pipeline");
|
2019-09-17 02:12:53 -04:00
|
|
|
ntDriverModeEntry = ntTable.getEntry("Driver_Mode");
|
2019-09-17 11:22:54 +03:00
|
|
|
ntPitchEntry = ntTable.getEntry("Pitch");
|
|
|
|
|
ntYawEntry = ntTable.getEntry("Yaw");
|
|
|
|
|
ntDistanceEntry = ntTable.getEntry("Distance");
|
|
|
|
|
ntTimeStampEntry = ntTable.getEntry("TimeStamp");
|
2019-09-17 13:59:53 +03:00
|
|
|
ntValidEntry = ntTable.getEntry("Valid");
|
2019-09-17 11:22:54 +03:00
|
|
|
ntDriverModeEntry.addListener(this::DriverModeListener, EntryListenerFlags.kUpdate);
|
|
|
|
|
ntPipelineEntry.addListener(this::PipelineListener, EntryListenerFlags.kUpdate);
|
|
|
|
|
ntDriverModeEntry.setBoolean(false);
|
2019-09-18 03:08:06 -04:00
|
|
|
ntPipelineEntry.setString(SettingsManager.CamerasCurrentPipeline.get(cameraName));
|
|
|
|
|
|
|
|
|
|
// camera settings
|
|
|
|
|
camVals = new CameraValues(SettingsManager.Cameras.get(cameraName));
|
|
|
|
|
visionProcess = new VisionProcess(camVals);
|
|
|
|
|
|
|
|
|
|
// cscore setup
|
|
|
|
|
CameraServer cs = CameraServer.getInstance();
|
|
|
|
|
cvSink = cs.getVideo(SettingsManager.UsbCameras.get(cameraName));
|
|
|
|
|
cvPublish = cs.putVideo(cameraName, camVals.ImageWidth, camVals.ImageHeight);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void drawContour(Mat inputMat, RotatedRect contourRect) {
|
|
|
|
|
if (contourRect == null) return;
|
|
|
|
|
List<MatOfPoint> drawnContour = new ArrayList<>();
|
|
|
|
|
Point[] vertices = new Point[4];
|
|
|
|
|
contourRect.points(vertices);
|
|
|
|
|
drawnContour.add(new MatOfPoint(vertices));
|
|
|
|
|
Imgproc.drawContours(inputMat, drawnContour, 0, contourRectColor, 3);
|
|
|
|
|
Imgproc.circle(inputMat, contourRect.center, 3, contourRectColor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Separate video output, contour drawing, data output to separate function, maybe even second thread
|
|
|
|
|
private PipelineResult runVisionProcess(Mat inputImage, Mat outputImage) {
|
|
|
|
|
var pipelineResult = new PipelineResult();
|
|
|
|
|
|
|
|
|
|
Scalar hsvLower = new Scalar(currentPipeline.hue.get(0), currentPipeline.saturation.get(0), currentPipeline.value.get(0));
|
|
|
|
|
Scalar hsvUpper = new Scalar(currentPipeline.hue.get(1), currentPipeline.saturation.get(1), currentPipeline.value.get(1));
|
|
|
|
|
|
|
|
|
|
visionProcess.HSVThreshold(inputImage, hsvThreshMat, hsvLower, hsvUpper, currentPipeline.erode, currentPipeline.dilate);
|
|
|
|
|
|
|
|
|
|
if (currentPipeline.is_binary == 1) {
|
|
|
|
|
Imgproc.cvtColor(hsvThreshMat, outputImage, Imgproc.COLOR_GRAY2BGR, 3);
|
|
|
|
|
} else {
|
|
|
|
|
inputImage.copyTo(outputImage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FoundContours = visionProcess.FindContours(hsvThreshMat);
|
|
|
|
|
if (FoundContours.size() > 0) {
|
|
|
|
|
FilteredContours = visionProcess.FilterContours(FoundContours, currentPipeline.area, currentPipeline.ratio, currentPipeline.extent, currentPipeline.sort_mode, currentPipeline.target_intersection, currentPipeline.target_group);
|
|
|
|
|
if (FilteredContours.size() > 0) {
|
|
|
|
|
GroupedContours = visionProcess.GroupTargets(FilteredContours, currentPipeline.target_intersection, currentPipeline.target_group);
|
|
|
|
|
if (GroupedContours.size() > 0) {
|
|
|
|
|
var finalRect = visionProcess.SortTargetsToOne(GroupedContours, currentPipeline.sort_mode);
|
|
|
|
|
pipelineResult.IsValid = true;
|
|
|
|
|
if (!currentPipeline.is_calibrated) {
|
|
|
|
|
pipelineResult.CalibratedX = camVals.CenterX;
|
|
|
|
|
pipelineResult.CalibratedY = camVals.CenterY;
|
|
|
|
|
} else {
|
|
|
|
|
pipelineResult.CalibratedX = (finalRect.center.y - currentPipeline.B) / currentPipeline.M;
|
|
|
|
|
pipelineResult.CalibratedY = finalRect.center.x * currentPipeline.M + currentPipeline.B;
|
|
|
|
|
pipelineResult.Pitch = camVals.CalculatePitch(finalRect.center.y, pipelineResult.CalibratedY);
|
|
|
|
|
pipelineResult.Yaw = camVals.CalculateYaw(finalRect.center.x, pipelineResult.CalibratedX);
|
|
|
|
|
}
|
|
|
|
|
// Send calc using networktables
|
|
|
|
|
// TODO Send pitch yaw distance and Raw Point using websockets to client for calib calc
|
|
|
|
|
drawContour(outputImage, finalRect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pipelineResult;
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2019-09-17 02:12:53 -04:00
|
|
|
// processing time tracking
|
|
|
|
|
long startTime;
|
|
|
|
|
double processTimeMs;
|
|
|
|
|
double fps;
|
|
|
|
|
|
|
|
|
|
while (!Thread.interrupted()) {
|
2019-09-16 04:08:23 -04:00
|
|
|
FoundContours.clear();
|
|
|
|
|
FilteredContours.clear();
|
2019-09-17 02:12:53 -04:00
|
|
|
GroupedContours.clear();
|
2019-09-18 10:36:29 +03:00
|
|
|
SettingsManager.CamerasCurrentPipeline.put(CameraName,SettingsManager.Cameras.get(CameraName).pipelines.keySet().stream().findFirst().toString());
|
2019-09-16 04:08:23 -04:00
|
|
|
currentPipeline = SettingsManager.Cameras.get(CameraName).pipelines.get(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
2019-09-18 10:36:29 +03:00
|
|
|
|
2019-09-18 03:04:48 +03:00
|
|
|
// System.out.println(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
2019-09-17 02:12:53 -04:00
|
|
|
// start fps counter right before grabbing input frame
|
|
|
|
|
startTime = System.nanoTime();
|
2019-09-18 03:08:06 -04:00
|
|
|
cvSink.grabFrame(cameraInputMat);
|
|
|
|
|
if (cameraInputMat.cols() == 0 && cameraInputMat.rows() == 0) {
|
2019-09-17 02:12:53 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-09-16 21:45:04 +03:00
|
|
|
|
2019-09-18 03:08:06 -04:00
|
|
|
// get vision data
|
|
|
|
|
var pipelineResult = runVisionProcess(cameraInputMat, streamOutputMat);
|
2019-09-16 21:45:04 +03:00
|
|
|
|
2019-09-18 03:08:06 -04:00
|
|
|
cvPublish.putFrame(streamOutputMat);
|
2019-09-17 02:12:53 -04:00
|
|
|
// calculate FPS after publishing output frame
|
|
|
|
|
processTimeMs = (System.nanoTime() - startTime) * 1e-6;
|
|
|
|
|
fps = 1000 / processTimeMs;
|
2019-09-18 03:08:06 -04:00
|
|
|
System.out.printf("%s - Process time: %fms, FPS: %.2f, FoundContours: %d, FilteredContours: %d, GroupedContours: %d\n",CameraName ,processTimeMs, fps, FoundContours.size(), FilteredContours.size(), GroupedContours.size());
|
2019-09-17 02:12:53 -04:00
|
|
|
|
2019-09-18 03:08:06 -04:00
|
|
|
cameraInputMat.release();
|
2019-09-16 21:45:04 +03:00
|
|
|
hsvThreshMat.release();
|
2019-09-18 03:08:06 -04:00
|
|
|
|
|
|
|
|
memManager.run(true);
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|