|
| 1 | +/* Copyright (c) 2020 FIRST. All rights reserved. |
| 2 | + * |
| 3 | + * Redistribution and use in source and binary forms, with or without modification, |
| 4 | + * are permitted (subject to the limitations in the disclaimer below) provided that |
| 5 | + * the following conditions are met: |
| 6 | + * |
| 7 | + * Redistributions of source code must retain the above copyright notice, this list |
| 8 | + * of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * Redistributions in binary form must reproduce the above copyright notice, this |
| 11 | + * list of conditions and the following disclaimer in the documentation and/or |
| 12 | + * other materials provided with the distribution. |
| 13 | + * |
| 14 | + * Neither the name of FIRST nor the names of its contributors may be used to endorse or |
| 15 | + * promote products derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS |
| 18 | + * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 22 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +package org.firstinspires.ftc.robotcontroller.external.samples; |
| 31 | + |
| 32 | +import com.qualcomm.robotcore.eventloop.opmode.Disabled; |
| 33 | +import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; |
| 34 | +import com.qualcomm.robotcore.eventloop.opmode.TeleOp; |
| 35 | +import java.util.List; |
| 36 | +import org.firstinspires.ftc.robotcore.external.ClassFactory; |
| 37 | +import org.firstinspires.ftc.robotcore.external.hardware.camera.SwitchableCamera; |
| 38 | +import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName; |
| 39 | +import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer; |
| 40 | +import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector; |
| 41 | +import org.firstinspires.ftc.robotcore.external.tfod.Recognition; |
| 42 | + |
| 43 | +/** |
| 44 | + * This 2019-2020 OpMode illustrates the basics of using the TensorFlow Object Detection API to |
| 45 | + * determine the position of the Skystone game elements. |
| 46 | + * |
| 47 | + * Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name. |
| 48 | + * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list. |
| 49 | + * |
| 50 | + * IMPORTANT: In order to use this OpMode, you need to obtain your own Vuforia license key as |
| 51 | + * is explained below. |
| 52 | + */ |
| 53 | +@TeleOp(name = "Concept: TensorFlow Object Detection Switchable Cameras", group = "Concept") |
| 54 | +@Disabled |
| 55 | +public class ConceptTensorFlowObjectDetectionSwitchableCameras extends LinearOpMode { |
| 56 | + private static final String TFOD_MODEL_ASSET = "Skystone.tflite"; |
| 57 | + private static final String LABEL_FIRST_ELEMENT = "Stone"; |
| 58 | + private static final String LABEL_SECOND_ELEMENT = "Skystone"; |
| 59 | + |
| 60 | + /* |
| 61 | + * IMPORTANT: You need to obtain your own license key to use Vuforia. The string below with which |
| 62 | + * 'parameters.vuforiaLicenseKey' is initialized is for illustration only, and will not function. |
| 63 | + * A Vuforia 'Development' license key, can be obtained free of charge from the Vuforia developer |
| 64 | + * web site at https://developer.vuforia.com/license-manager. |
| 65 | + * |
| 66 | + * Vuforia license keys are always 380 characters long, and look as if they contain mostly |
| 67 | + * random data. As an example, here is a example of a fragment of a valid key: |
| 68 | + * ... yIgIzTqZ4mWjk9wd3cZO9T1axEqzuhxoGlfOOI2dRzKS4T0hQ8kT ... |
| 69 | + * Once you've obtained a license key, copy the string from the Vuforia web site |
| 70 | + * and paste it in to your code on the next line, between the double quotes. |
| 71 | + */ |
| 72 | + private static final String VUFORIA_KEY = |
| 73 | + " -- YOUR NEW VUFORIA KEY GOES HERE --- "; |
| 74 | + |
| 75 | + /** |
| 76 | + * {@link #vuforia} is the variable we will use to store our instance of the Vuforia |
| 77 | + * localization engine. |
| 78 | + */ |
| 79 | + private VuforiaLocalizer vuforia; |
| 80 | + |
| 81 | + /** |
| 82 | + * Variables used for switching cameras. |
| 83 | + */ |
| 84 | + private WebcamName webcam1, webcam2; |
| 85 | + private SwitchableCamera switchableCamera; |
| 86 | + private boolean oldLeftBumper; |
| 87 | + private boolean oldRightBumper; |
| 88 | + |
| 89 | + /** |
| 90 | + * {@link #tfod} is the variable we will use to store our instance of the TensorFlow Object |
| 91 | + * Detection engine. |
| 92 | + */ |
| 93 | + private TFObjectDetector tfod; |
| 94 | + |
| 95 | + @Override |
| 96 | + public void runOpMode() { |
| 97 | + // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that |
| 98 | + // first. |
| 99 | + initVuforia(); |
| 100 | + initTfod(); |
| 101 | + |
| 102 | + /** |
| 103 | + * Activate TensorFlow Object Detection before we wait for the start command. |
| 104 | + * Do it here so that the Camera Stream window will have the TensorFlow annotations visible. |
| 105 | + **/ |
| 106 | + if (tfod != null) { |
| 107 | + tfod.activate(); |
| 108 | + } |
| 109 | + |
| 110 | + /** Wait for the game to begin */ |
| 111 | + telemetry.addData(">", "Press Play to start op mode"); |
| 112 | + telemetry.update(); |
| 113 | + waitForStart(); |
| 114 | + |
| 115 | + if (opModeIsActive()) { |
| 116 | + while (opModeIsActive()) { |
| 117 | + if (tfod != null) { |
| 118 | + doCameraSwitching(); |
| 119 | + List<Recognition> recognitions = tfod.getRecognitions(); |
| 120 | + telemetry.addData("# Object Detected", recognitions.size()); |
| 121 | + // step through the list of recognitions and display boundary info. |
| 122 | + int i = 0; |
| 123 | + for (Recognition recognition : recognitions) { |
| 124 | + telemetry.addData(String.format("label (%d)", i), recognition.getLabel()); |
| 125 | + telemetry.addData(String.format(" left,top (%d)", i), "%.03f , %.03f", |
| 126 | + recognition.getLeft(), recognition.getTop()); |
| 127 | + telemetry.addData(String.format(" right,bottom (%d)", i), "%.03f , %.03f", |
| 128 | + recognition.getRight(), recognition.getBottom()); |
| 129 | + } |
| 130 | + telemetry.update(); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if (tfod != null) { |
| 136 | + tfod.shutdown(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Initialize the Vuforia localization engine. |
| 142 | + */ |
| 143 | + private void initVuforia() { |
| 144 | + /* |
| 145 | + * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine. |
| 146 | + */ |
| 147 | + VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(); |
| 148 | + |
| 149 | + parameters.vuforiaLicenseKey = VUFORIA_KEY; |
| 150 | + |
| 151 | + // Indicate that we wish to be able to switch cameras. |
| 152 | + webcam1 = hardwareMap.get(WebcamName.class, "Webcam 1"); |
| 153 | + webcam2 = hardwareMap.get(WebcamName.class, "Webcam 2"); |
| 154 | + parameters.cameraName = ClassFactory.getInstance().getCameraManager().nameForSwitchableCamera(webcam1, webcam2); |
| 155 | + |
| 156 | + // Instantiate the Vuforia engine |
| 157 | + vuforia = ClassFactory.getInstance().createVuforia(parameters); |
| 158 | + |
| 159 | + // Set the active camera to Webcam 1. |
| 160 | + switchableCamera = (SwitchableCamera) vuforia.getCamera(); |
| 161 | + switchableCamera.setActiveCamera(webcam1); |
| 162 | + |
| 163 | + // Loading trackables is not necessary for the TensorFlow Object Detection engine. |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Initialize the TensorFlow Object Detection engine. |
| 168 | + */ |
| 169 | + private void initTfod() { |
| 170 | + int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier( |
| 171 | + "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName()); |
| 172 | + TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId); |
| 173 | + tfodParameters.minResultConfidence = 0.8f; |
| 174 | + tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia); |
| 175 | + tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_FIRST_ELEMENT, LABEL_SECOND_ELEMENT); |
| 176 | + } |
| 177 | + |
| 178 | + private void doCameraSwitching() { |
| 179 | + // If the left bumper is pressed, use Webcam 1. |
| 180 | + // If the right bumper is pressed, use Webcam 2. |
| 181 | + boolean newLeftBumper = gamepad1.left_bumper; |
| 182 | + boolean newRightBumper = gamepad1.right_bumper; |
| 183 | + if (newLeftBumper && !oldLeftBumper) { |
| 184 | + switchableCamera.setActiveCamera(webcam1); |
| 185 | + } else if (newRightBumper && !oldRightBumper) { |
| 186 | + switchableCamera.setActiveCamera(webcam2); |
| 187 | + } |
| 188 | + oldLeftBumper = newLeftBumper; |
| 189 | + oldRightBumper = newRightBumper; |
| 190 | + |
| 191 | + if (switchableCamera.getActiveCamera().equals(webcam1)) { |
| 192 | + telemetry.addData("activeCamera", "Webcam 1"); |
| 193 | + telemetry.addData("Press RightBumper", "to switch to Webcam 2"); |
| 194 | + } else { |
| 195 | + telemetry.addData("activeCamera", "Webcam 2"); |
| 196 | + telemetry.addData("Press LeftBumper", "to switch to Webcam 1"); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments