2019-11-10 11:47:56 -05:00
|
|
|
package com.chameleonvision.classabstraction;
|
|
|
|
|
|
|
|
|
|
import com.chameleonvision.classabstraction.camera.CameraProcess;
|
2019-11-16 10:17:33 -08:00
|
|
|
import com.chameleonvision.classabstraction.camera.CameraStreamer;
|
2019-11-10 11:47:56 -05:00
|
|
|
import com.chameleonvision.classabstraction.pipeline.CVPipeline;
|
2019-11-15 16:01:50 -05:00
|
|
|
import com.chameleonvision.classabstraction.pipeline.CVPipelineResult;
|
2019-11-10 11:47:56 -05:00
|
|
|
import com.chameleonvision.classabstraction.pipeline.CVPipelineSettings;
|
|
|
|
|
import com.chameleonvision.classabstraction.pipeline.DriverVisionPipeline;
|
2019-11-15 16:01:50 -05:00
|
|
|
import org.opencv.core.Mat;
|
2019-11-10 11:47:56 -05:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class VisionProcess {
|
|
|
|
|
|
|
|
|
|
private final CameraProcess cameraProcess;
|
|
|
|
|
private final List<CVPipeline> pipelines = new ArrayList<>();
|
2019-11-16 10:17:33 -08:00
|
|
|
private final CameraFrameRunnable cameraRunnable;
|
|
|
|
|
private final CameraStramerRunnable streamRunnable;
|
|
|
|
|
private final VisionProcessRunnable visionRunnable;
|
2019-11-10 11:47:56 -05:00
|
|
|
private CVPipeline currentPipeline;
|
|
|
|
|
|
|
|
|
|
private final CVPipelineSettings driverVisionSettings = new CVPipelineSettings();
|
|
|
|
|
|
2019-11-16 10:17:33 -08:00
|
|
|
public VisionProcess(CameraProcess cameraProcess, String name, Long streamTimeMs, Long cameraUpdateTimeMs) {
|
2019-11-10 11:47:56 -05:00
|
|
|
this.cameraProcess = cameraProcess;
|
|
|
|
|
|
|
|
|
|
pipelines.add(new DriverVisionPipeline(() -> driverVisionSettings));
|
|
|
|
|
setPipeline(pipelines.get(0));
|
2019-11-15 16:01:50 -05:00
|
|
|
|
2019-11-16 10:17:33 -08:00
|
|
|
// Thread to grab frames from the camera
|
|
|
|
|
this.cameraRunnable = new CameraFrameRunnable(cameraUpdateTimeMs) ;
|
|
|
|
|
new Thread(cameraRunnable).start();
|
|
|
|
|
|
|
|
|
|
// Thread to put frames on the dashboard
|
|
|
|
|
this.streamRunnable = new CameraStramerRunnable(streamTimeMs, new CameraStreamer(cameraProcess, name))
|
|
|
|
|
new Thread(streamRunnable).start();
|
|
|
|
|
|
|
|
|
|
// Thread to process vision data
|
|
|
|
|
this.visionRunnable = new VisionProcessRunnable();
|
|
|
|
|
new Thread(visionRunnable).start();
|
|
|
|
|
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2019-11-16 10:17:33 -08:00
|
|
|
|
2019-11-10 11:47:56 -05:00
|
|
|
public void setPipeline(int pipelineIndex) {
|
|
|
|
|
CVPipeline newPipeline = pipelines.get(pipelineIndex);
|
|
|
|
|
if (newPipeline != null) {
|
|
|
|
|
setPipeline(newPipeline);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPipeline(CVPipeline pipeline) {
|
|
|
|
|
currentPipeline = pipeline;
|
|
|
|
|
currentPipeline.initPipeline(cameraProcess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CVPipeline getCurrentPipeline() {
|
|
|
|
|
return currentPipeline;
|
|
|
|
|
}
|
2019-11-15 16:01:50 -05:00
|
|
|
|
2019-11-16 10:17:33 -08:00
|
|
|
/**
|
|
|
|
|
* CameraFrameRunnable grabs images from the cameraProcess
|
|
|
|
|
* at a specified loopTime
|
|
|
|
|
*/
|
|
|
|
|
protected class CameraFrameRunnable extends LoopingRunnable {
|
2019-11-15 16:01:50 -05:00
|
|
|
private Mat cameraFrame = new Mat();
|
|
|
|
|
private long timestampMicros;
|
|
|
|
|
|
|
|
|
|
private final Object frameLock = new Object();
|
|
|
|
|
|
2019-11-16 10:17:33 -08:00
|
|
|
/**
|
|
|
|
|
* CameraFrameRunnable grabs images from the cameraProcess
|
|
|
|
|
* at a specified loopTime
|
|
|
|
|
* @param loopTimeMs how often to grab frames at in ms
|
|
|
|
|
*/
|
|
|
|
|
CameraFrameRunnable(Long loopTimeMs) {
|
|
|
|
|
super(loopTimeMs);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 16:01:50 -05:00
|
|
|
@Override
|
2019-11-16 10:17:33 -08:00
|
|
|
public void process() {
|
|
|
|
|
while(!Thread.interrupted()) {
|
|
|
|
|
|
|
|
|
|
// Grab camera frames
|
2019-11-15 16:01:50 -05:00
|
|
|
var camData = cameraProcess.getFrame();
|
|
|
|
|
synchronized (frameLock) {
|
|
|
|
|
cameraFrame = camData.getLeft();
|
|
|
|
|
}
|
|
|
|
|
timestampMicros = camData.getRight();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 10:17:33 -08:00
|
|
|
public Mat getFrame(Mat dst) {
|
|
|
|
|
synchronized (frameLock) {
|
|
|
|
|
cameraFrame.copyTo(dst);
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
2019-11-15 16:01:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getTimestampMicros() {
|
|
|
|
|
return timestampMicros;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 10:17:33 -08:00
|
|
|
/**
|
|
|
|
|
* VisionProcessRunnable will process images as quickly as possible
|
|
|
|
|
*/
|
|
|
|
|
private class VisionProcessRunnable implements Runnable {
|
2019-11-15 16:01:50 -05:00
|
|
|
|
|
|
|
|
private CVPipelineResult result;
|
2019-11-16 10:17:33 -08:00
|
|
|
private Mat streamBuffer = new Mat();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
while(!Thread.interrupted()) {
|
|
|
|
|
result = currentPipeline.runPipeline(cameraRunnable.getFrame(streamBuffer));
|
|
|
|
|
// TODO do something with the result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class CameraStramerRunnable extends LoopingRunnable {
|
|
|
|
|
|
|
|
|
|
private final CameraStreamer streamer;
|
|
|
|
|
private Mat streamBuffer = new Mat();
|
|
|
|
|
|
|
|
|
|
private CameraStramerRunnable(Long loopTimeMs, CameraStreamer streamer) {
|
|
|
|
|
super(loopTimeMs);
|
|
|
|
|
this.streamer = streamer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
void process() {
|
|
|
|
|
streamer.runStream(cameraRunnable.getFrame(streamBuffer));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A thread that tries to run at a specified loop time
|
|
|
|
|
*/
|
|
|
|
|
private static abstract class LoopingRunnable implements Runnable {
|
|
|
|
|
private final Long loopTimeMs;
|
|
|
|
|
|
|
|
|
|
abstract void process();
|
|
|
|
|
|
|
|
|
|
private LoopingRunnable(Long loopTimeMs) {
|
|
|
|
|
this.loopTimeMs = loopTimeMs;
|
|
|
|
|
}
|
2019-11-15 16:01:50 -05:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
while(!Thread.interrupted()) {
|
2019-11-16 10:17:33 -08:00
|
|
|
var now = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
// Do the thing
|
|
|
|
|
process();
|
|
|
|
|
|
|
|
|
|
// sleep for the remaining time
|
|
|
|
|
var timeElapsed = System.currentTimeMillis() - now;
|
|
|
|
|
var delta = loopTimeMs - timeElapsed;
|
|
|
|
|
if(delta > 0.0) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(delta, 0);
|
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
|
}
|
2019-11-15 16:01:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-16 10:17:33 -08:00
|
|
|
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|