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-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-17 02:12:53 -04:00
|
|
|
import org.apache.commons.math3.stat.descriptive.moment.Mean;
|
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
|
|
|
|
|
|
|
|
private CameraServer cs = CameraServer.getInstance();
|
2019-09-17 11:22:54 +03:00
|
|
|
private NetworkTableEntry ntPipelineEntry, ntDriverModeEntry,ntYawEntry,ntPitchEntry,ntDistanceEntry,ntTimeStampEntry;
|
2019-09-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
private MemoryManager memManager = new MemoryManager(125);
|
|
|
|
|
|
|
|
|
|
private int imgWidth, imgHeight;
|
2019-09-17 11:22:54 +03:00
|
|
|
private void ChangeCameraValues(int Exposure, int Brightness){
|
|
|
|
|
SettingsManager.getInstance().UsbCameras.get(CameraName).setBrightness(Brightness);
|
|
|
|
|
SettingsManager.getInstance().UsbCameras.get(CameraName).setExposureManual(Exposure);
|
|
|
|
|
}
|
|
|
|
|
private void DriverModeListener(EntryNotification entryNotification){
|
|
|
|
|
if (entryNotification.value.getBoolean()){
|
|
|
|
|
ChangeCameraValues(25,15);
|
|
|
|
|
} else{
|
|
|
|
|
Pipeline pipeline = SettingsManager.Cameras.get(CameraName).pipelines.get(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
|
|
|
|
ChangeCameraValues(pipeline.exposure, pipeline.brightness);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void PipelineListener(EntryNotification entryNotification){
|
|
|
|
|
if (SettingsManager.Cameras.get(CameraName).pipelines.containsKey(entryNotification.value.getString())){
|
|
|
|
|
SettingsManager.CamerasCurrentPipeline.put(CameraName,entryNotification.value.getString());
|
|
|
|
|
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
|
|
|
|
|
} else{
|
|
|
|
|
ntPipelineEntry.setString(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-17 02:12:53 -04:00
|
|
|
public CameraProcess(String CameraName) {
|
2019-09-14 17:14:49 +03:00
|
|
|
this.CameraName = CameraName;
|
2019-09-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
// add pipeline
|
|
|
|
|
SettingsManager.CamerasCurrentPipeline.put(CameraName, SettingsManager.Cameras.get(CameraName).pipelines.keySet().toArray()[0].toString());
|
|
|
|
|
|
|
|
|
|
// NetworkTables
|
|
|
|
|
NetworkTable ntTable = NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + CameraName);
|
|
|
|
|
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");
|
|
|
|
|
ntDriverModeEntry.addListener(this::DriverModeListener, EntryListenerFlags.kUpdate);
|
|
|
|
|
ntPipelineEntry.addListener(this::PipelineListener, EntryListenerFlags.kUpdate);
|
|
|
|
|
ntDriverModeEntry.setBoolean(false);
|
|
|
|
|
ntPipelineEntry.setString(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
2019-09-16 04:08:23 -04:00
|
|
|
imgWidth = SettingsManager.Cameras.get(CameraName).camVideoMode.width;
|
|
|
|
|
imgHeight = SettingsManager.Cameras.get(CameraName).camVideoMode.height;
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2019-09-17 02:12:53 -04:00
|
|
|
// camera values
|
2019-09-16 04:08:23 -04:00
|
|
|
var cv_sink = cs.getVideo(SettingsManager.UsbCameras.get(CameraName));
|
|
|
|
|
var cv_publish = cs.putVideo(CameraName, imgWidth, imgHeight);
|
|
|
|
|
double fov = SettingsManager.Cameras.get(CameraName).FOV;
|
|
|
|
|
CameraValues camVals = new CameraValues(imgWidth, imgHeight, fov);
|
|
|
|
|
VisionProcess visionProcess = new VisionProcess(camVals);
|
|
|
|
|
Pipeline currentPipeline;
|
2019-09-14 20:38:50 +03:00
|
|
|
|
2019-09-17 02:12:53 -04:00
|
|
|
// actual OpenCV objects
|
2019-09-16 04:08:23 -04:00
|
|
|
List<MatOfPoint> FoundContours = new ArrayList<>();
|
|
|
|
|
List<MatOfPoint> FilteredContours = new ArrayList<>();
|
2019-09-16 21:45:04 +03:00
|
|
|
List<RotatedRect> GroupedContours = new ArrayList<>();
|
2019-09-16 04:08:23 -04:00
|
|
|
Mat inputMat = new Mat();
|
|
|
|
|
Mat hsvThreshMat = new Mat();
|
|
|
|
|
Mat outputMat = new Mat();
|
|
|
|
|
Scalar contourColor = new Scalar(255, 0, 0);
|
|
|
|
|
|
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-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
currentPipeline = SettingsManager.Cameras.get(CameraName).pipelines.get(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
2019-09-16 21:45:04 +03:00
|
|
|
|
2019-09-17 02:12:53 -04:00
|
|
|
// start fps counter right before grabbing input frame
|
|
|
|
|
startTime = System.nanoTime();
|
2019-09-16 04:08:23 -04:00
|
|
|
cv_sink.grabFrame(inputMat);
|
2019-09-17 02:12:53 -04:00
|
|
|
if (inputMat.cols() == 0 && inputMat.rows() == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-09-16 21:45:04 +03:00
|
|
|
|
|
|
|
|
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(inputMat, hsvThreshMat, hsvLower, hsvUpper, currentPipeline.erode, currentPipeline.dilate);
|
|
|
|
|
|
|
|
|
|
if (currentPipeline.is_binary == 1) {
|
2019-09-17 02:12:53 -04:00
|
|
|
Imgproc.cvtColor(hsvThreshMat, outputMat, Imgproc.COLOR_GRAY2BGR, 3);
|
2019-09-16 21:45:04 +03:00
|
|
|
} else {
|
|
|
|
|
outputMat = inputMat;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 02:12:53 -04:00
|
|
|
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);
|
2019-09-17 11:22:54 +03:00
|
|
|
// TODO Add calibration calc
|
|
|
|
|
//TODO Calc Pitch Yaw And Distance Send it them using networktables
|
|
|
|
|
// TODO Send pitch yaw distance and Raw Point using websockets to client for calic calc
|
2019-09-17 02:12:53 -04:00
|
|
|
if (finalRect != null) {
|
|
|
|
|
List<MatOfPoint> a = new ArrayList<>();
|
|
|
|
|
Point[] vertices = new Point[4];
|
|
|
|
|
finalRect.points(vertices);
|
|
|
|
|
a.add(new MatOfPoint(vertices));
|
|
|
|
|
Imgproc.drawContours(outputMat, a, 0, contourColor, 3);
|
|
|
|
|
}
|
2019-09-17 11:22:54 +03:00
|
|
|
|
2019-09-17 02:12:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-16 04:08:23 -04:00
|
|
|
}
|
2019-09-17 02:12:53 -04:00
|
|
|
|
2019-09-16 21:45:04 +03:00
|
|
|
cv_publish.putFrame(outputMat);
|
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-17 11:22:54 +03: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-16 21:45:04 +03:00
|
|
|
inputMat.release();
|
|
|
|
|
hsvThreshMat.release();
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|