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