Version 1 (modified by 6 years ago) (diff) | ,
---|
The roboRIO can forward video to the driver station to allow a human driver to see while driving even when line of sight is blocked. Even better, we can process the video, either to assist the human driver (e.g. by placing cross hairs where a shooter is pointed) or by automatically finding a game piece of target during autonomous operation.
Most video processing is done using the free open-source library: OpenCV which is included with WPILib.
The example code below displays the video from a USB camera on the dashboard and creates a red rectangular overlay to assist the driver:
package frc.robot; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc; import org.opencv.core.Scalar; import org.opencv.core.Point; import edu.wpi.cscore.CvSink; import edu.wpi.cscore.CvSource; import edu.wpi.cscore.UsbCamera; import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.cameraserver.*; public class Robot extends TimedRobot { public void robotInit() { new Thread(() -> { UsbCamera camera = CameraServer.getInstance().startAutomaticCapture(); camera.setResolution(640, 480); CvSink cvSink = CameraServer.getInstance().getVideo(); CvSource outputStream = CameraServer.getInstance().putVideo("Rectangle", 640, 480); Mat mat = new Mat(); while(!Thread.interrupted()) { if (cvSink.grabFrame(mat) == 0) { outputStream.notifyError(cvSink.getError()); // skip the rest of the current iteration continue; } // Put a rectangle on the image Imgproc.rectangle(mat, new Point(160,120), new Point(480,360), new Scalar(0,0,255), 5); // Give the output stream a new image to display outputStream.putFrame(mat); } }).start(); } }