· 6 years ago · Nov 09, 2019, 11:30 AM
1/* Copyright (c) 2019 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
30package org.firstinspires.ftc.teamcode.examples
31
32import com.qualcomm.robotcore.eventloop.opmode.Autonomous
33import com.qualcomm.robotcore.eventloop.opmode.Disabled
34import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode
35import com.qualcomm.robotcore.eventloop.opmode.TeleOp
36import com.qualcomm.robotcore.util.ElapsedTime
37
38import org.firstinspires.ftc.robotcore.external.ClassFactory
39import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer
40import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection
41import org.firstinspires.ftc.robotcore.external.tfod.Recognition
42import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector
43
44/**
45 * This 2019-2020 OpMode illustrates the basics of using the TensorFlow Object Detection API to
46 * determine the position of the Skystone game elements.
47 *
48 * Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name.
49 * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list.
50 *
51 * IMPORTANT: In order to use this OpMode, you need to obtai if(updatedRecognitions[])
52n your own Vuforia license key as
53 * is explained below.
54 */
55@Disabled
56@Autonomous(name = "TensorFlow", group = "Concept")
57abstract class Detection : LinearOpMode() {
58
59
60 val timpfa = ElapsedTime()
61 var poza = 0
62 /**
63 * [.vuforia] is the variable we will use to store our instance of the Vuforia
64 * localization engine.
65 */
66 private var vuforia: VuforiaLocalizer? = null
67
68 /**
69 * [.tfod] is the variable we will use to store our instance of the TensorFlow Object* Detection engine.
70 */
71 private var tfod: TFObjectDetector? = null
72
73 fun runOpMode(timeout : Double ) {
74 // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
75 // first.
76 initVuforia()
77
78 if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
79 initTfod()
80 } else {
81 telemetry.addData("Sorry!", "This device is not compatible with TFOD")
82 }
83
84 /**
85 * Activate TensorFlow Object Detection before we wait for the start command.
86 * Do it here so that the Camera Stream window will have the TensorFlow annotations visible.
87 */
88 if (tfod != null) {
89 tfod!!.activate()
90 }
91
92 /** Wait for the game to begin */
93 telemetry.addData(">", "Press Play to start op mode")
94 telemetry.update()
95 waitForStart()
96
97 if (opModeIsActive()) {
98 while (timpfa.seconds() < timeout) if (tfod != null) {
99 // getUpdatedRecognitions() will return null if no new information is available since
100 // the last time that call was made.
101
102 val updatedRecognitions = tfod!!.updatedRecognitions
103 val firstRec = updatedRecognitions[0]
104 val secondRec = updatedRecognitions[1]
105
106 if(firstRec.label == "Skystone"){poza = 1}
107 else if(secondRec.label == "Skystone"){poza = 2}
108 else{poza = 3}
109 if (updatedRecognitions != null) {
110 telemetry.addData("# Object Detected", updatedRecognitions.size)
111
112 // step through the list of recognitions and display boundary info.
113 val i = 0
114 for (recognition in updatedRecognitions) {
115 telemetry.addData(String.format("label (%d)", i), recognition.label)
116 telemetry.addData(String.format(" left,top (%d)", i), "%.03f , %.03f",
117 recognition.left, recognition.top)
118 telemetry.addData(String.format(" right,bottom (%d)", i), "%.03f , %.03f",
119 recognition.right, recognition.bottom)
120 }
121 telemetry.update()
122 }
123 }
124 }
125
126 if (tfod != null) {
127 tfod!!.shutdown()
128 }
129 }
130
131 /**
132 * Initialize the Vuforia localization engine.
133 */
134 private fun initVuforia() {
135 /*
136 * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
137 */
138 val parameters = VuforiaLocalizer.Parameters()
139
140 parameters.vuforiaLicenseKey = VUFORIA_KEY
141 parameters.cameraDirection = CameraDirection.BACK
142
143 // Instantiate the Vuforia engine
144 vuforia = ClassFactory.getInstance().createVuforia(parameters)
145
146 // Loading trackables is not necessary for the TensorFlow Object Detection engine.
147 }
148
149 /**
150 * Initialize the TensorFlow Object Detection engine.
151 */
152 private fun initTfod() {
153 val tfodMonitorViewId = hardwareMap.appContext.resources.getIdentifier(
154 "tfodMonitorViewId", "id", hardwareMap.appContext.packageName)
155 val tfodParameters = TFObjectDetector.Parameters(tfodMonitorViewId)
156 tfodParameters.minimumConfidence = 0.8
157 tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia)
158 tfod!!.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_FIRST_ELEMENT, LABEL_SECOND_ELEMENT)
159 }
160
161 companion object {
162 private val TFOD_MODEL_ASSET = "Skystone.tflite"
163 private val LABEL_FIRST_ELEMENT = "Stone"
164 private val LABEL_SECOND_ELEMENT = "Skystone"
165
166 /*
167 * IMPORTANT: You need to obtain your own license key to use Vuforia. The string below with which
168 * 'parameters.vuforiaLicenseKey' is initialized is for illustration only, and will not function.
169 * A Vuforia 'Development' license key, can be obtained free of charge from the Vuforia developer
170 * web site at https://developer.vuforia.com/license-manager.
171 *
172 * Vuforia license keys are always 380 characters long, and look as if they contain mostly
173 * random data. As an example, here is a example of a fragment of a valid key:
174 * ... yIgIzTqZ4mWjk9wd3cZO9T1axEqzuhxoGlfOOI2dRzKS4T0hQ8kT ...
175 * Once you've obtained a license key, copy the string from the Vuforia web site
176 * and paste it in to your code on the next line, between the double quotes.
177 */
178 private val VUFORIA_KEY = "AWo7bzb/////AAABmcbdWZ79Y049lfMcsRS8waNYev8AbC1EwUWqhJnr1poItrv7+etQ1bwW4BiQpg151evO66Pzt3L2LvfbBgzn4aQ3QzVBXYQBjqMScjg/gQEj0g3ldi/0ENHSKwnT48YDxtQQb5/twpwjew9wlaSkZuZ8KtZGwOZHh7vhV0xQmjh1akuPF0zmKvCn5HPnd/O9YxXR5Ef7eyQ+r15XMT7Vd7kG/PUbpCvkexwsRZ4BKGv+oV1ZWOqrYrP5WKbpzHmEOl8RggfJKD707G2Q61vTUW+MEksQwrydbwTCqzTxDUTWdOlgzG9JfGjS+jUdQ3CAN+EETNZDOQs8fIxn3Q+Bdmi823AJLEU3GDhptc7KHcjo"
179 }
180}