Skip to content

Commit 7c24803

Browse files
authored
Merge pull request #1 from acharraggi/main
archive TensorFlow pages
2 parents 4b2cabd + 03affe8 commit 7c24803

File tree

99 files changed

+2676
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+2676
-1
lines changed
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
Blocks Sample OpMode for TFOD
2+
=============================
3+
4+
Introduction
5+
------------
6+
7+
This tutorial describes the FTC Blocks Sample OpMode for TensorFlow
8+
Object Detection (TFOD). This Sample, called
9+
“ConceptTensorFlowObjectDetection”, can recognize one or more official
10+
game elements and provide their visible size and position.
11+
12+
For the 2023-2024 game CENTERSTAGE, the game element is a hexagonal
13+
white **Pixel**. The FTC SDK software contains a TFOD model of this
14+
object, ready for recognition.
15+
16+
For extra points, teams may instead use their own custom TFOD models of
17+
**Team Props**. That option is described here:
18+
19+
- :doc:`Blocks Custom Model Sample OpMode for TFOD <../blocks_tfod_opmode_custom/blocks-tfod-opmode-custom>`
20+
21+
Creating the OpMode
22+
-------------------
23+
24+
At the FTC Blocks browser interface, click on the “Create New OpMode”
25+
button to display the Create New OpMode dialog box.
26+
27+
Specify a name for your new OpMode. Select
28+
“ConceptTensorFlowObjectDetection” as the Sample OpMode that will be the
29+
template for your new OpMode.
30+
31+
If no webcam is configured for your REV Control Hub, the dialog box will
32+
display a warning message (shown here). You can ignore this warning
33+
message if you will use the built-in camera of an Android RC phone.
34+
Click “OK” to create your new OpMode.
35+
36+
.. figure:: images/030-Create-New-OpMode.png
37+
:align: center
38+
:width: 75%
39+
:alt: Creating a new OpMode
40+
41+
Creating a New OpMode
42+
43+
The new OpMode should appear in edit mode in your browser.
44+
45+
.. figure:: images/040-Sample-OpMode.png
46+
:align: center
47+
:width: 75%
48+
:alt: Sample OpMode
49+
50+
Sample OpMode
51+
52+
By default, the Sample OpMode assumes you are using a webcam, configured
53+
as “Webcam 1”. If you are using the built-in camera on your Android RC
54+
phone, change the USE_WEBCAM Boolean from ``true`` to ``false`` (green
55+
arrow above).
56+
57+
Adjusting the Zoom Factor
58+
-------------------------
59+
60+
If the object to be recognized will be more than roughly 2 feet (61 cm)
61+
from the camera, you might want to set the digital zoom factor to a
62+
value greater than 1. This tells TensorFlow to use an artificially
63+
magnified portion of the image, which may offer more accurate
64+
recognitions at greater distances.
65+
66+
.. figure:: images/150-setZoom.png
67+
:align: center
68+
:width: 75%
69+
:alt: Setting Zoom
70+
71+
Setting the Zoom Factor
72+
73+
Pull out the **``setZoom``** Block, found in the toolbox or palette
74+
called “Vision”, under “TensorFlow” and “TfodProcessor” (see green oval
75+
above). Change the magnification value as desired (green arrow).
76+
77+
On REV Control Hub, the “Vision” menu appears only when the active robot
78+
configuration contains a webcam, even if not plugged in.
79+
80+
This ``setZoom`` Block can be placed in the INIT section of your OpMode,
81+
82+
- immediately after the call to the ``initTfod`` Function, or
83+
- as the very last Block inside the ``initTfod`` Function.
84+
85+
This Block is **not** part of the Processor Builder pattern, so the Zoom
86+
factor can be set to other values during the OpMode, if desired.
87+
88+
The “zoomed” region can be observed in the DS preview (Camera Stream)
89+
and the RC preview (LiveView), surrounded by a greyed-out area that is
90+
**not evaluated** by the TFOD Processor.
91+
92+
Other Adjustments
93+
-----------------
94+
95+
The Sample OpMode uses a default **minimum confidence** level of 75%.
96+
The TensorFlow Processor needs to have a confidence level of 75% or
97+
higher, to consider an object as “recognized” in its field of view.
98+
99+
You can see the object name and actual confidence (as a **decimal**,
100+
e.g. 0.75) near the Bounding Box, in the Driver Station preview (Camera
101+
Stream) and Robot Controller preview (Liveview).
102+
103+
.. figure:: images/160-min-confidence.png
104+
:align: center
105+
:width: 75%
106+
:alt: Setting Minimum Confidence
107+
108+
Setting the Minimum Confidence
109+
110+
Pull out the **``setMinResultConfidence``** Block, found in the toolbox
111+
or palette called “Vision”, under “TensorFlow” and “TfodProcessor”.
112+
Adjust this parameter to a higher value if you would like the processor
113+
to be more selective in identifying an object.
114+
115+
Another option is to define, or clip, a **custom area for TFOD
116+
evaluation**, unlike ``setZoom`` which is always centered.
117+
118+
.. figure:: images/170-clipping-margins.png
119+
:align: center
120+
:width: 75%
121+
:alt: Setting Clipping Margins
122+
123+
Setting Clipping Margins
124+
125+
From the same Blocks palette, pull out the **``setClippingMargins``**
126+
Block. Adjust the four margins as desired, in units of pixels.
127+
128+
These Blocks can be placed in the INIT section of your OpMode,
129+
130+
- immediately after the call to the ``initTfod`` Function, or
131+
- as the very last Blocks inside the ``initTfod`` Function.
132+
133+
As with ``setZoom``, these Blocks are **not** part of the Processor
134+
Builder pattern, so they can be set to other values during the OpMode,
135+
if desired.
136+
137+
Command Flow in this Sample
138+
---------------------------
139+
140+
After the ``waitForStart`` Block, this OpMode contains the main program
141+
loop:
142+
143+
.. figure:: images/180-main-loop.png
144+
:align: center
145+
:width: 75%
146+
:alt: Main Loop
147+
148+
OpMode Main Loop
149+
150+
This loop repeatedly calls a Blocks Function called
151+
**``telemetryTfod``**. That Function is the heart of the OpMode, seeking
152+
and evaluating recognized TFOD objects, and displaying DS Telemetry
153+
about those objects. It will be discussed below, in the next section.
154+
155+
The main loop also allows the user to press the ``Dpad Down`` button on
156+
the gamepad, to temporarily stop the streaming session. This
157+
``.stopStreaming`` Block pauses the flow and processing of camera
158+
frames, thus **conserving CPU resources**.
159+
160+
Pressing the ``Dpad Up`` button (``.resumeStreaming``) allows the
161+
processing to continue. The on-and-off actions can be observed in the RC
162+
preview (LiveView), described further below.
163+
164+
These two commands appear here in this Sample OpMode, to spread
165+
awareness of one tool for managing CPU and bandwidth resources. The FTC
166+
VisionPortal offers over 10 such controls, :ref:`described here
167+
<apriltag/vision_portal/visionportal_cpu_and_bandwidth/visionportal-cpu-and-bandwidth:visionportal cpu and bandwidth>`.
168+
169+
Processing TFOD Recognitions
170+
----------------------------
171+
172+
The Function called **``telemetryTfod``** is the heart of the OpMode,
173+
seeking and evaluating recognized TFOD objects, and displaying DS
174+
Telemetry about those objects.
175+
176+
.. figure:: images/190-telemetryTfod.png
177+
:align: center
178+
:width: 75%
179+
:alt: Telemetry TFOD
180+
181+
Telemetry TFOD
182+
183+
The first Block uses the TFOD Processor to gather and store all
184+
recognitions in a List, called ``myTfodRecognitions``.
185+
186+
The green “FOR Loop” iterates through that List, handling each item, one
187+
at a time. Here the “handling” is simply displaying certain TFOD fields
188+
to DS Telemetry.
189+
190+
For competition, you want to do more than display Telemetry, and you
191+
want to exit the main loop at some point. These code modifications are
192+
discussed in another section below.
193+
194+
Testing the OpMode
195+
------------------
196+
197+
Click the “Save OpMode” button, then run the OpMode from the Driver
198+
Station. The Robot Controller should use the CENTERSTAGE TFOD model to
199+
recognize and track the white Pixel.
200+
201+
For a preview during the INIT phase, touch the Driver Station’s 3-dot
202+
menu and select **Camera Stream**.
203+
204+
.. figure:: images/200-Sample-DS-Camera-Stream.png
205+
:align: center
206+
:width: 75%
207+
:alt: Sample DS Camera Stream
208+
209+
Sample DS Camera Stream
210+
211+
Camera Stream is not live video; tap to refresh the image. Use the small
212+
white arrows at lower right to expand or revert the preview size. To
213+
close the preview, choose 3-dots and Camera Stream again.
214+
215+
After touching the DS START button, the OpMode displays Telemetry for
216+
any recognized Pixel(s):
217+
218+
.. figure:: images/210-Sample-DS-Telemetry.png
219+
:align: center
220+
:width: 75%
221+
:alt: Sample DS Telemetry
222+
223+
Sample DS Telemetry
224+
225+
The above Telemetry shows the label name, and TFOD confidence level. It
226+
also gives the **center location** and **size** (in pixels) of the
227+
Bounding Box, which is the colored rectangle surrounding the recognized
228+
object.
229+
230+
The pixel origin (0, 0) is at the top left corner of the image.
231+
232+
Before and after touching DS START, the Robot Controller provides a
233+
video preview called **LiveView**.
234+
235+
.. figure:: images/240-Sample-RC-LiveView.png
236+
:align: center
237+
:width: 75%
238+
:alt: Sample RC LiveView
239+
240+
Sample RC LiveView
241+
242+
For Control Hub (with no built-in screen), plug in an HDMI monitor or
243+
learn about ``scrcpy`` (https://github.com/Genymobile/scrcpy). The
244+
above image is a LiveView screenshot via ``scrcpy``.
245+
246+
If you don’t have a physical Pixel on hand, try pointing the camera at
247+
this image:
248+
249+
.. figure:: images/300-Sample-Pixel.png
250+
:align: center
251+
:width: 75%
252+
:alt: Sample Pixel
253+
254+
Sample Pixel
255+
256+
Modifying the Sample
257+
--------------------
258+
259+
In this Sample OpMode, the main loop ends only upon touching the DS Stop
260+
button. For competition, teams should **modify this code** in at least
261+
two ways:
262+
263+
- for a significant recognition, take action or store key information –
264+
inside the FOR loop
265+
266+
- end the main loop based on your criteria, to continue the OpMode
267+
268+
As an example, you might set a Boolean variable ``isPixelDetected`` to
269+
``true``, if a significant recognition has occurred.
270+
271+
You might also evaluate and store which randomized Spike Mark (red or
272+
blue tape stripe) holds the white Pixel.
273+
274+
Regarding the main loop, it could end after the camera views all three
275+
Spike Marks, or after your code provides a high-confidence result. If
276+
the camera’s view includes more than one Spike Mark position, perhaps
277+
the white Pixel’s **Bounding Box** size and location could be useful.
278+
Teams should consider how long to seek an acceptable recognition, and
279+
what to do otherwise.
280+
281+
In any case, the OpMode should exit the main loop and continue running,
282+
using any stored information.
283+
284+
Best of luck this season!
285+
286+
============
287+
288+
Questions, comments and corrections to [email protected]
289+
11.5 KB
Loading
41.3 KB
Loading
40.7 KB
Loading
22.2 KB
Loading
34.9 KB
Loading
92.7 KB
Loading
81.3 KB
Loading
262 KB
Loading
53.8 KB
Loading

0 commit comments

Comments
 (0)