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-20 02:17:22 -04:00
|
|
|
import com.chameleonvision.vision.Pipeline;
|
2019-09-19 14:07:42 -04:00
|
|
|
import com.chameleonvision.vision.camera.Camera;
|
|
|
|
|
import com.chameleonvision.vision.camera.CameraValues;
|
2019-09-18 13:48:34 +03:00
|
|
|
import com.chameleonvision.web.Server;
|
2019-09-18 03:08:06 -04:00
|
|
|
import edu.wpi.cscore.CvSink;
|
|
|
|
|
import edu.wpi.cscore.CvSource;
|
2019-09-14 17:14:49 +03:00
|
|
|
import edu.wpi.first.cameraserver.CameraServer;
|
2019-09-20 02:17:22 -04:00
|
|
|
import edu.wpi.first.networktables.*;
|
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;
|
2019-09-18 13:48:34 +03:00
|
|
|
import java.util.HashMap;
|
2019-09-16 04:08:23 -04:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class CameraProcess implements Runnable {
|
2019-09-19 14:07:42 -04:00
|
|
|
|
|
|
|
|
private final Camera camera;
|
|
|
|
|
private final 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;
|
|
|
|
|
|
|
|
|
|
// 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);
|
2019-09-19 23:35:01 +03:00
|
|
|
private long TimeStamp = 0;
|
2019-09-18 03:08:06 -04:00
|
|
|
|
2019-09-18 03:04:48 +03:00
|
|
|
private void DriverModeListener(EntryNotification entryNotification) {
|
|
|
|
|
if (entryNotification.value.getBoolean()) {
|
2019-09-19 14:07:42 -04:00
|
|
|
camera.setExposure(25);
|
|
|
|
|
camera.setBrightness(15);
|
2019-09-18 03:04:48 +03:00
|
|
|
} else {
|
2019-09-19 14:07:42 -04:00
|
|
|
Pipeline pipeline = camera.getCurrentPipeline();
|
|
|
|
|
camera.setExposure(pipeline.exposure);
|
|
|
|
|
camera.setBrightness(pipeline.brightness);
|
2019-09-17 11:22:54 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-18 03:04:48 +03:00
|
|
|
|
|
|
|
|
private void PipelineListener(EntryNotification entryNotification) {
|
2019-09-19 14:07:42 -04:00
|
|
|
if (camera.getPipelines().containsKey(entryNotification.value.getString())) {
|
|
|
|
|
// camera.setPntryNotification.value.getString());
|
|
|
|
|
Pipeline pipeline = camera.getCurrentPipeline();
|
|
|
|
|
|
|
|
|
|
camera.setExposure(pipeline.exposure);
|
|
|
|
|
camera.setBrightness(pipeline.brightness);
|
2019-09-17 11:22:54 +03:00
|
|
|
//TODO Send Pipeline change using websocket to client
|
2019-09-18 03:04:48 +03:00
|
|
|
} else {
|
2019-09-19 14:07:42 -04:00
|
|
|
ntPipelineEntry.setString("pipeline" + camera.getCurrentPipelineIndex());
|
2019-09-17 11:22:54 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-18 03:04:48 +03:00
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
public CameraProcess(Camera processCam) {
|
|
|
|
|
camera = processCam;
|
|
|
|
|
this.cameraName = camera.name;
|
|
|
|
|
|
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-19 14:07:42 -04:00
|
|
|
ntPipelineEntry.setString("pipeline" + camera.getCurrentPipelineIndex());
|
2019-09-18 03:08:06 -04:00
|
|
|
|
|
|
|
|
// camera settings
|
2019-09-19 14:07:42 -04:00
|
|
|
camVals = new CameraValues(camera);
|
2019-09-18 03:08:06 -04:00
|
|
|
visionProcess = new VisionProcess(camVals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
private void updateNetworkTables(PipelineResult pipelineResult) {
|
2019-09-19 23:35:01 +03:00
|
|
|
ntValidEntry.setBoolean(pipelineResult.IsValid);
|
|
|
|
|
if (pipelineResult.IsValid){
|
|
|
|
|
ntYawEntry.setNumber(pipelineResult.Yaw);
|
|
|
|
|
ntPitchEntry.setNumber(pipelineResult.Pitch);
|
|
|
|
|
}
|
|
|
|
|
ntTimeStampEntry.setNumber(TimeStamp);
|
2019-09-19 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Separate video output to separate function, maybe even second thread
|
2019-09-18 03:08:06 -04:00
|
|
|
private PipelineResult runVisionProcess(Mat inputImage, Mat outputImage) {
|
|
|
|
|
var pipelineResult = new PipelineResult();
|
|
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
if (currentPipeline == null) {
|
|
|
|
|
return pipelineResult;
|
|
|
|
|
}
|
2019-09-19 23:35:01 +03:00
|
|
|
if (ntDriverModeEntry.getBoolean(false)){
|
|
|
|
|
inputImage.copyTo(outputImage);
|
|
|
|
|
return pipelineResult;
|
|
|
|
|
}
|
2019-09-18 03:08:06 -04: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(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);
|
2019-09-18 13:48:34 +03:00
|
|
|
pipelineResult.RawPoint = finalRect;
|
2019-09-18 03:08:06 -04:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
// 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
|
2019-09-19 23:35:01 +03:00
|
|
|
long startTime;
|
2019-09-17 02:12:53 -04:00
|
|
|
double processTimeMs;
|
2019-09-18 13:48:34 +03:00
|
|
|
double fps = 0;
|
2019-09-17 02:12:53 -04:00
|
|
|
|
|
|
|
|
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-19 14:07:42 -04:00
|
|
|
|
|
|
|
|
currentPipeline = camera.getCurrentPipeline();
|
2019-09-17 02:12:53 -04:00
|
|
|
// start fps counter right before grabbing input frame
|
|
|
|
|
startTime = System.nanoTime();
|
2019-09-20 02:17:22 -04:00
|
|
|
TimeStamp = camera.grabFrame(cameraInputMat);
|
2019-09-18 03:08:06 -04:00
|
|
|
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-19 23:35:01 +03:00
|
|
|
updateNetworkTables(pipelineResult);
|
2019-09-19 14:07:42 -04:00
|
|
|
if (cameraName.equals(SettingsManager.GeneralSettings.curr_camera) && pipelineResult.IsValid) {
|
2019-09-18 13:48:34 +03:00
|
|
|
HashMap<String,Object> WebSend = new HashMap<>();
|
|
|
|
|
HashMap<String,Object> point = new HashMap<>();
|
2019-09-19 14:07:42 -04:00
|
|
|
List<Double> center = new ArrayList<>();
|
2019-09-18 13:48:34 +03:00
|
|
|
center.add(pipelineResult.RawPoint.center.x);
|
|
|
|
|
center.add(pipelineResult.RawPoint.center.y);
|
|
|
|
|
point.put("pitch", pipelineResult.Pitch);
|
|
|
|
|
point.put("yaw", pipelineResult.Yaw);
|
|
|
|
|
point.put("fps", fps);
|
|
|
|
|
WebSend.put("point", point);
|
2019-09-20 02:17:22 -04:00
|
|
|
WebSend.put("raw_point", center);
|
2019-09-18 13:48:34 +03:00
|
|
|
Server.broadcastMessage(WebSend);
|
|
|
|
|
}
|
2019-09-20 02:17:22 -04:00
|
|
|
camera.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-19 14:07:42 -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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|