Refactoring Vision Code
Tags: think and controlPersonhours: 4
Task: Refactor Vision Code
Iron Reign has been working on multiple vision pipelines, including TensorFlow, OpenCV, and a home-grown Convolutional Neural Network. Until now, all our code assumed that we only used TensorFlow, and we wanted to be able to switch out vision implementations quickly. As such, we decided to abstract away the actual vision pipeline used, which allows us to be able to choose between vision implementations at runtime.
We did this by creating a java interface, VisionProvider
, seen below. We then made our TensorFlowIntegration
class (our code for detecting mineral positions using TensorFlow) implement VisionProvider
.
Next, we changed our opmode to use the new VisionProvider
interface. We added code to allow us to switch vision implementations using the left button on the dpad.
Our code for VisionProvider
is shown below.
1 2 3 4 5 6 | public interface VisionProvider { public void initializeVision(HardwareMap hardwareMap, Telemetry telemetry); public void shutdownVision(); public GoldPos detect(); } ``` |
These methods are implemented in the integration classes.
Our new code for TensorflowIntegration
is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | public class TensorflowIntegration implements VisionProvider { private static final String TFOD_MODEL_ASSET = "RoverRuckus.tflite"; private static final String LABEL_GOLD_MINERAL = "Gold Mineral"; private static final String LABEL_SILVER_MINERAL = "Silver Mineral"; private List<Recognition> cacheRecognitions = null; /** * {@link #vuforia} is the variable we will use to store our instance of the Vuforia * localization engine. */ private VuforiaLocalizer vuforia; /** * {@link #tfod} is the variable we will use to store our instance of the Tensor Flow Object * Detection engine. */ public TFObjectDetector tfod; /** * Initialize the Vuforia localization engine. */ public void initVuforia() { /* * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine. */ VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(); parameters.vuforiaLicenseKey = RC.VUFORIA_LICENSE_KEY; ; parameters.cameraDirection = CameraDirection.FRONT; // Instantiate the Vuforia engine vuforia = ClassFactory.getInstance().createVuforia(parameters); } /** * Initialize the Tensor Flow Object Detection engine. */ private void initTfod(HardwareMap hardwareMap) { int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier( "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName()); TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId); tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia); tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_GOLD_MINERAL, LABEL_SILVER_MINERAL); } @Override public void initializeVision(HardwareMap hardwareMap, Telemetry telemetry) { initVuforia(); if (ClassFactory.getInstance().canCreateTFObjectDetector()) { initTfod(hardwareMap); } else { telemetry.addData("Sorry!", "This device is not compatible with TFOD"); } if (tfod != null) { tfod.activate(); } } @Override public void shutdownVision() { if (tfod != null) { tfod.shutdown(); } } @Override public GoldPos detect() { List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions(); if (updatedRecognitions != null) { cacheRecognitions = updatedRecognitions; } if (cacheRecognitions.size() == 3) { int goldMineralX = -1; int silverMineral1X = -1; int silverMineral2X = -1; for (Recognition recognition : cacheRecognitions) { if (recognition.getLabel().equals(LABEL_GOLD_MINERAL)) { goldMineralX = (int) recognition.getLeft(); } else if (silverMineral1X == -1) { silverMineral1X = (int) recognition.getLeft(); } else { silverMineral2X = (int) recognition.getLeft(); } } if (goldMineralX != -1 && silverMineral1X != -1 && silverMineral2X != -1) if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X) { return GoldPos.LEFT; } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) { return GoldPos.RIGHT; } else { return GoldPos.MIDDLE; } } return GoldPos.NONE_FOUND; } } |
Next Steps
We need to implement detection using OpenCV, and make our class conform to VisionProvider
, so that we can easily swap it out for TensorflowIntegration
.
We also need to do the same using our Convolutional Neural Network.
Finally, it might be beneficial to have a dummy implementation that always “detects” the gold as being in the middle, so that if we know that all our vision implementations are failing, we can use this dummy one to prevent our autonomous from failing.