Using the Robot Vision API

Why use the Robot Vision API?

The Robot Vision API provides a higher level of abstraction to the problem of target identification. This API will allow your team to focus more on solving higher level problems such as alignment with the target instead of lower level computer vision code. The Robot Vision API functions either synchronously, meaning you can immediately access the location of the most probably targets, or asynchronously, which means a provided method will be called (the processor) to allow you to work with the found targets at a set interval, in the background.

This was built using several of the new features provided to teams in the 2017 WPILib release, which includes the CameraServer, OpenCV framework, and cscore library.

Simple Target Detection

The following program creates a CameraSource for a USB camera and allows for the detection of a target which was created using the TargetSpecs interface.

package com.kylecorry.frc.robot;

import com.kylecorry.frc.vision.CameraSource;
import com.kylecorry.frc.vision.TargetDetector;
import com.kylecorry.frc.vision.Target;
import edu.wpi.first.wpilibj.IterativeRobot;
import java.util.List;

public class Robot extends IterativeRobot {

 TargetDetector highGoalDetector;
 CameraSource camera;

 public void robotInit() {
  /* Note, the following code is for a single target, if a group of targets is needed, use the TargetGroupDetector, which also requires the setTargetGroupSpecs method to be called */

  highGoalDetector = new TargetDetector.Builder()
   .setTargetSpecs(new HighGoalSpecs())
   .build();

  camera = new CameraSource.Builder(highGoalDetector)
   .setType(CameraSource.Type.USB)
   .build();
 }

 public void autonomousPeriodic() {
  List targets = highGoalDetector.detect(camera.getPicture());
  if(!targets.isEmpty()){
   Target highGoal = targets.get(0);
   // do something with target
  }
 }
}

This is a very simple tutorial, but for a more detailed description of the API, and examples of asynchronous detection of targets, see the README on GitHub at Robot Vision API.

Below is an example of the Robot Vision API being used to detect the 2017 peg target using with a non exact distance as the view angle was not calibrated yet.

Vision Example