SHARE
TWEET

Untitled

a guest Jan 24th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. package org.firstinspires.ftc.teamcode.Auto;
  3.  
  4. import com.qualcomm.robotcore.eventloop.opmode.Disabled;
  5. import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
  6. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  7. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  8. import java.util.List;
  9. import org.firstinspires.ftc.robotcore.external.ClassFactory;
  10. import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
  11. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
  12. import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
  13. import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
  14.  
  15. /**
  16.  * This 2019-2020 OpMode illustrates the basics of using the TensorFlow Object Detection API to
  17.  * determine the position of the Skystone game elements.
  18.  *
  19.  * Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name.
  20.  * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list.
  21.  *
  22.  * IMPORTANT: In order to use this OpMode, you need to obtain your own Vuforia license key as
  23.  * is explained below.
  24.  */
  25.  
  26. @Autonomous
  27. public class ConceptTensorflow extends LinearOpMode {
  28.     private static final String TFOD_MODEL_ASSET = "Skystone.tflite";
  29.     private static final String LABEL_FIRST_ELEMENT = "Stone";
  30.     private static final String LABEL_SECOND_ELEMENT = "Skystone";
  31.  
  32.     /*
  33.      * IMPORTANT: You need to obtain your own license key to use Vuforia. The string below with which
  34.      * 'parameters.vuforiaLicenseKey' is initialized is for illustration only, and will not function.
  35.      * A Vuforia 'Development' license key, can be obtained free of charge from the Vuforia developer
  36.      * web site at https://developer.vuforia.com/license-manager.
  37.      *
  38.      * Vuforia license keys are always 380 characters long, and look as if they contain mostly
  39.      * random data. As an example, here is a example of a fragment of a valid key:
  40.      *      ... yIgIzTqZ4mWjk9wd3cZO9T1axEqzuhxoGlfOOI2dRzKS4T0hQ8kT ...
  41.      * Once you've obtained a license key, copy the string from the Vuforia web site
  42.      * and paste it in to your code on the next line, between the double quotes.
  43.      */
  44.     private static final String VUFORIA_KEY =
  45.             "AQQ87HP/////AAABmcPgv5WcsUMGp/AukiTtmnmNYEygLkYfvET3wJEYAIDYKmE+i5t7WWYVMEzOZ4iWgDHNKL4qEm2+olcZWYXYaSXbwhmTZYrL+mEemd2kQNb06BCbKzVoHhjTT7qdEsJdGYBEDUyW2vt/Re5PgJ12IgsRt2xOAcZaGiBJS56gOXTdcqfMRkIf+JKaMwduqZlnXl8zjE7w1SDIz8kFCKG4Pfkwu7d1w/x3sociz3q0k7d/wwBOxKYePxjrgm/rH2y5PPD0r5h3zDl8fFTjMa4TO5B6kAPgsAokDVRusyqYuXx59O52MF250ikxIcSzL4YuCvOJgxLpinJSquCLiAOKyLEqvfGGoAlPuebw+iHOnnim";
  46.  
  47.     /**
  48.      * {@link #vuforia} is the variable we will use to store our instance of the Vuforia
  49.      * localization engine.
  50.      */
  51.     private VuforiaLocalizer vuforia;
  52.  
  53.     /**
  54.      * {@link #tfod} is the variable we will use to store our instance of the TensorFlow Object
  55.      * Detection engine.
  56.      */
  57.     private TFObjectDetector tfod;
  58.  
  59.     @Override
  60.     public void runOpMode() {
  61.         // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
  62.         // first.
  63.         initVuforia();
  64.  
  65.         if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
  66.             initTfod();
  67.         } else {
  68.             telemetry.addData("Sorry!", "This device is not compatible with TFOD");
  69.         }
  70.  
  71.         /**
  72.          * Activate TensorFlow Object Detection before we wait for the start command.
  73.          * Do it here so that the Camera Stream window will have the TensorFlow annotations visible.
  74.          **/
  75.         if (tfod != null) {
  76.             tfod.activate();
  77.         }
  78.  
  79.         /** Wait for the game to begin */
  80.         telemetry.addData(">", "Press Play to start op mode");
  81.         telemetry.update();
  82.         waitForStart();
  83.  
  84.         if (opModeIsActive()) {
  85.             while (opModeIsActive()) {
  86.                 if (tfod != null) {
  87.                     // getUpdatedRecognitions() will return null if no new information is available since
  88.                     // the last time that call was made.
  89.                     List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
  90.                     if (updatedRecognitions != null) {
  91.                       telemetry.addData("# Object Detected", updatedRecognitions.size());
  92.                       // step through the list of recognitions and display boundary info.
  93.                       int i = 0;
  94.                       for (Recognition recognition : updatedRecognitions) {
  95.                         telemetry.addData(String.format("label (%d)", i), recognition.getLabel());
  96.                         telemetry.addData(String.format("  left,top (%d)", i), "%.03f , %.03f",
  97.                                 recognition.getLeft(), recognition.getTop());
  98.                         telemetry.addData(String.format("  right,bottom (%d)", i), "%.03f , %.03f",
  99.                                 recognition.getRight(), recognition.getBottom());
  100. if (updatedRecognitions.size() == 3) {
  101.                         int skystone1X = -1;
  102.                         int stone1X = -1;
  103.                         int stone2X = -1;
  104.                         for (Recognition recognition : updatedRecognitions) {
  105.                           if (recognition.getLabel().equals(LABEL_FIRST_ELEMENT )) {
  106.                             stone1X = (int) recognition.getLeft();
  107.                           } else if (stone1X == -1) {
  108.                             stone1X = (int) recognition.getLeft();
  109.                           } else {
  110.                             stone2X = (int) recognition.getLeft();
  111.                           }
  112.                         }
  113.                         if (skystone1X != -1 && stone1X != -1 && stone2X != -1) {
  114.                           if (skystone1X < stone1X && skystone1X < stone2X) {
  115.                             telemetry.addData("Skystone Position", "Left");
  116.                           } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) {
  117.                             telemetry.addData("Skystone Position", "Right");
  118.                           } else {
  119.                             telemetry.addData("Skystone Position", "Center");
  120.                           }
  121.                         }
  122.  
  123.                       }
  124.                       telemetry.update();
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.  
  130.         if (tfod != null) {
  131.             tfod.shutdown();
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * Initialize the Vuforia localization engine.
  137.      */
  138.     private void initVuforia() {
  139.         /*
  140.          * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
  141.          */
  142.         VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
  143.  
  144.         parameters.vuforiaLicenseKey = VUFORIA_KEY;
  145.         parameters.cameraName = hardwareMap.get(WebcamName.class, "Webcam 1");
  146.  
  147.         //  Instantiate the Vuforia engine
  148.         vuforia = ClassFactory.getInstance().createVuforia(parameters);
  149.  
  150.         // Loading trackables is not necessary for the TensorFlow Object Detection engine.
  151.     }
  152.  
  153.     /**
  154.      * Initialize the TensorFlow Object Detection engine.
  155.      */
  156.     private void initTfod() {
  157.         int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
  158.             "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  159.         TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
  160.        tfodParameters.minimumConfidence = 0.8;
  161.        tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
  162.        tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_FIRST_ELEMENT, LABEL_SECOND_ELEMENT);
  163.     }
  164. }
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top