Vision Processing ERROR: too many simultaneous streams

There’s a problem with my vision code. I’m only using 1 output stream, but I’m getting the error “too many output streams”. Here is my code


	// Vision Processing
	private VisionThread visionThread;
	private final Object imgLock = new Object();
	double centerX = 0.0;
	double width = 0.0;
	public String shotReady = "Shot Ready";
	int gripTolerance = 3;
	int tooFar = 90;
	int tooClose = 110;
	int camWidth = 320;
	int camHeight = 240;
	int imageCenter = (camWidth / 2);
	boolean isShotReady = false;
	VisionClass pipeline = new VisionClass();
	VisionRunner<?> visionRunner;
	UsbCamera cam0, cam1, selectedCam;
	Mat image;
	CvSink selectedVideo, gearVid, highGoalVid;
	CvSource outputStream;
	boolean isTargetFound = false;

	@Override
	public void robotInit() {

		// Switch Vision Targets
		cam0 = new UsbCamera("cam0", 0); // High Goal Camera
		cam1 = new UsbCamera("cam1", 1); // Gear Camera
		cam0.setResolution(camWidth, camHeight);
		cam1.setResolution(camWidth, camHeight);
		cam0.setFPS(16);
		cam1.setFPS(16);
		selectedCam = cam0;
		
		image = new Mat();
		// MjpegServer server = new MjpegServer("server", 1181);

		highGoalVid = CameraServer.getInstance().getVideo(cam0);
		highGoalVid.setEnabled(true);

		gearVid = CameraServer.getInstance().getVideo(cam1);
		gearVid.setEnabled(false);

		highGoalVid.grabFrame(image);
		selectedVideo = highGoalVid;

		outputStream = CameraServer.getInstance().putVideo("Selected Video", camWidth, camHeight);
		// server.setSource(outputStream);
		outputStream.putFrame(image);

		visionThread = new VisionThread(selectedCam, pipeline, pipeline -> {
			if (!pipeline.filterContoursOutput().isEmpty()) {
				isTargetFound = true;
				Rect r = Imgproc.boundingRect(pipeline.filterContoursOutput().get(0));
				Rect r1 = Imgproc.boundingRect(pipeline.filterContoursOutput().get(1));
				Mat processedImage = new Mat();
				synchronized (imgLock) {
					selectedVideo.grabFrame(processedImage);
					Imgproc.rectangle(processedImage, new Point(r.x, r.y), new Point(r.x + r.width, r.y + r.height),
							new Scalar(0, 0, 255), 2);
					Imgproc.rectangle(processedImage, new Point(r1.x, r1.y), new Point(r1.x + r1.width, r1.y + r1.height),
							new Scalar(0, 0, 255), 2);
					outputStream.putFrame(processedImage);
				}
			} else {
				isTargetFound = false;
			}
		});

		visionThread.start();

I had this problem even though the code was 100% correct. Rebooting the roboRIO while both webcams were plugged in fixed the issue.

Running into similar issue with two webcams. Rebooted Roborio with cams connected. Still get the same issue. How was this issue resolved?

Thanks :slight_smile:

We are having this issue as well. What exactly trips this error? The piece of code that keeps breaking it for us simply creates an ArrayList of integers.

After hours and hours of frustration, I believe the error occurs when either A) a frame is outputted at the same time, a cvSink grabs the same frame at the same time, or the CvSource does not output a new image.

If you’re using a boolean to switch between camera streams in a thread, using a volatile boolean will prevent the thread from caching the original state of the boolean in order to save memory.

Could I see your source code?

(P.S, these are just my observations)