Changes between Initial Version and Version 1 of ControlSystems/SoftwareTeam/Training/GettingStarted/Overlays


Ignore:
Timestamp:
Nov 28, 2019, 10:58:10 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/Overlays

    v1 v1  
     1The 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.
     2
     3Most video processing is done using the free open-source library: OpenCV which is included with WPILib.
     4
     5The example code below displays the video from a USB camera on the dashboard and creates a red rectangular overlay to assist the driver:
     6{{{
     7package frc.robot;
     8
     9import org.opencv.core.Mat;
     10import org.opencv.imgproc.Imgproc;
     11import org.opencv.core.Scalar;
     12import org.opencv.core.Point;
     13
     14import edu.wpi.cscore.CvSink;
     15import edu.wpi.cscore.CvSource;
     16import edu.wpi.cscore.UsbCamera;
     17import edu.wpi.first.wpilibj.TimedRobot;
     18import edu.wpi.first.cameraserver.*;
     19
     20public class Robot extends TimedRobot {
     21   
     22    public void robotInit() {
     23            new Thread(() -> {
     24                UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();
     25                camera.setResolution(640, 480);
     26               
     27                CvSink cvSink = CameraServer.getInstance().getVideo();
     28                CvSource outputStream = CameraServer.getInstance().putVideo("Rectangle", 640, 480);
     29               
     30                Mat mat = new Mat();
     31               
     32                while(!Thread.interrupted()) {
     33                  if (cvSink.grabFrame(mat) == 0) {
     34                    outputStream.notifyError(cvSink.getError());
     35                    // skip the rest of the current iteration
     36                    continue;
     37                  }
     38                  // Put a rectangle on the image
     39                  Imgproc.rectangle(mat, new Point(160,120), new Point(480,360), new Scalar(0,0,255), 5);
     40                  // Give the output stream a new image to display
     41                  outputStream.putFrame(mat);
     42              }
     43            }).start();
     44    }
     45}
     46}}}