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-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;
|
|
|
|
|
|
|
|
|
|
// 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-20 21:25:54 -04:00
|
|
|
private final StreamProcess streamProcess;
|
|
|
|
|
|
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-20 15:42:13 -04:00
|
|
|
var ntPipelineIndex = Integer.parseInt(entryNotification.value.getString().replace("pipeline", ""));
|
|
|
|
|
if (camera.getPipelines().containsKey(ntPipelineIndex)) {
|
|
|
|
|
// camera.setEntryNotification.value.getString());
|
|
|
|
|
var pipeline = camera.getCurrentPipeline();
|
2019-09-19 14:07:42 -04:00
|
|
|
|
|
|
|
|
camera.setExposure(pipeline.exposure);
|
|
|
|
|
camera.setBrightness(pipeline.brightness);
|
2019-09-21 18:52:28 +03:00
|
|
|
HashMap<String,Object> pipeChange = new HashMap<>();
|
|
|
|
|
pipeChange.put("curr_pipeline",ntPipelineIndex);
|
|
|
|
|
Server.handler.broadcastMessage(pipeChange);
|
|
|
|
|
|
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-21 04:43:13 -04:00
|
|
|
visionProcess = new VisionProcess(camera.getCamVals());
|
2019-09-20 21:25:54 -04:00
|
|
|
streamProcess = new StreamProcess(camera);
|
2019-09-18 03:08:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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-21 08:06:27 -07:00
|
|
|
if (!currentPipeline.orientation.equals("Normal")){
|
|
|
|
|
Core.flip(inputImage,inputImage,-1);
|
|
|
|
|
}
|
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) {
|
2019-09-21 09:42:22 -07:00
|
|
|
FilteredContours = visionProcess.FilterContours(FoundContours, currentPipeline.area, currentPipeline.ratio, currentPipeline.extent);
|
2019-09-18 03:08:06 -04:00
|
|
|
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) {
|
2019-09-21 04:43:13 -04:00
|
|
|
pipelineResult.CalibratedX = camera.getCamVals().CenterX;
|
|
|
|
|
pipelineResult.CalibratedY = camera.getCamVals().CenterY;
|
2019-09-18 03:08:06 -04:00
|
|
|
} else {
|
|
|
|
|
pipelineResult.CalibratedX = (finalRect.center.y - currentPipeline.B) / currentPipeline.M;
|
|
|
|
|
pipelineResult.CalibratedY = finalRect.center.x * currentPipeline.M + currentPipeline.B;
|
|
|
|
|
}
|
2019-09-21 08:06:27 -07:00
|
|
|
pipelineResult.Pitch = camera.getCamVals().CalculatePitch(finalRect.center.y, pipelineResult.CalibratedY);
|
|
|
|
|
pipelineResult.Yaw = camera.getCamVals().CalculateYaw(finalRect.center.x, pipelineResult.CalibratedX);
|
2019-09-18 03:08:06 -04:00
|
|
|
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
|
|
|
|
2019-09-20 21:25:54 -04:00
|
|
|
new Thread(streamProcess).start();
|
|
|
|
|
|
2019-09-21 04:43:13 -04:00
|
|
|
long lastFrameEndNanosec = 0;
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-21 04:43:13 -04:00
|
|
|
while (!Thread.interrupted()) {
|
2019-09-17 02:12:53 -04:00
|
|
|
startTime = System.nanoTime();
|
2019-09-21 04:43:13 -04:00
|
|
|
if ((startTime - lastFrameEndNanosec) * 1e-6 >= 1000.0/camera.getVideoMode().fps) {
|
2019-09-16 21:45:04 +03:00
|
|
|
|
2019-09-21 04:43:13 -04:00
|
|
|
|
|
|
|
|
FoundContours.clear();
|
|
|
|
|
FilteredContours.clear();
|
|
|
|
|
GroupedContours.clear();
|
|
|
|
|
|
|
|
|
|
currentPipeline = camera.getCurrentPipeline();
|
|
|
|
|
// start fps counter right before grabbing input frame
|
|
|
|
|
startTime = System.nanoTime();
|
|
|
|
|
TimeStamp = camera.grabFrame(cameraInputMat);
|
|
|
|
|
if (cameraInputMat.cols() == 0 && cameraInputMat.rows() == 0) {
|
|
|
|
|
continue;
|
2019-09-20 21:25:54 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-21 04:43:13 -04:00
|
|
|
// get vision data
|
|
|
|
|
var pipelineResult = runVisionProcess(cameraInputMat, streamOutputMat);
|
|
|
|
|
updateNetworkTables(pipelineResult);
|
|
|
|
|
if (cameraName.equals(SettingsManager.GeneralSettings.curr_camera)) {
|
|
|
|
|
HashMap<String, Object> WebSend = new HashMap<>();
|
|
|
|
|
HashMap<String, Object> point = new HashMap<>();
|
|
|
|
|
List<Double> center = new ArrayList<>();
|
|
|
|
|
if (pipelineResult.IsValid) {
|
|
|
|
|
center.add(pipelineResult.RawPoint.center.x);
|
|
|
|
|
center.add(pipelineResult.RawPoint.center.y);
|
|
|
|
|
point.put("pitch", pipelineResult.Pitch);
|
|
|
|
|
point.put("yaw", pipelineResult.Yaw);
|
|
|
|
|
} else {
|
|
|
|
|
center.add(0.0);
|
|
|
|
|
center.add(0.0);
|
|
|
|
|
point.put("pitch", 0);
|
|
|
|
|
point.put("yaw", 0);
|
|
|
|
|
}
|
|
|
|
|
point.put("fps", fps);
|
|
|
|
|
WebSend.put("point", point);
|
|
|
|
|
WebSend.put("raw_point", center);
|
2019-09-21 16:04:58 +03:00
|
|
|
Server.handler.broadcastMessage(WebSend);
|
2019-09-21 04:43:13 -04:00
|
|
|
}
|
2019-09-20 21:25:54 -04:00
|
|
|
|
2019-09-21 04:43:13 -04:00
|
|
|
//camera.putFrame(streamOutputMat);
|
|
|
|
|
streamProcess.updateFrame(streamOutputMat);
|
2019-09-17 02:12:53 -04:00
|
|
|
|
2019-09-21 04:43:13 -04:00
|
|
|
cameraInputMat.release();
|
|
|
|
|
hsvThreshMat.release();
|
|
|
|
|
|
|
|
|
|
// calculate FPS
|
|
|
|
|
lastFrameEndNanosec = System.nanoTime();
|
|
|
|
|
processTimeMs = (lastFrameEndNanosec - startTime) * 1e-6;
|
|
|
|
|
fps = 1000 / processTimeMs;
|
|
|
|
|
System.out.printf("%s - Process time: %.2fms, FPS: %.2f, FoundContours: %d, FilteredContours: %d, GroupedContours: %d\n", cameraName, processTimeMs, fps, FoundContours.size(), FilteredContours.size(), GroupedContours.size());
|
|
|
|
|
}
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|