Saved searches

Use saved searches to filter your results more quickly

Cancel Create saved search Sign up Reseting focus

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

FTC Webcam Controls

Westside Robotics edited this page Dec 26, 2022 · 8 revisions

Introduction

This basic tutorial describes 8 webcam controls available in the FTC SDK. It includes an example, using 2 of these controls to potentially improve TensorFlow recognition in Freight Frenzy.

Hats off to rgatkinson and Windwoes who developed these FTC webcam controls.

Software Overview

The FTC SDK contains a superinterface called CameraControl, which contains 5 interfaces:

Similar to Java classes, Java interfaces provide methods. A webcam can be controlled using methods of these 5 interfaces.

PtzControl allows control of 3 related features: virtual pan, tilt and zoom. ExposureControl also contains a feature called auto-exposure priority, or AE Priority. Together there are 8 webcam controls discussed in this tutorial.

The official documentation is found in the FTC Javadocs. Click the link for RobotCore, then click the CameraControl link in the left column.


FTC RobotCore Javadoc API

That page provides links to the 5 interfaces listed above.

The methods described here can be used in Android Studio or OnBot Java. They can also be provided to FTC Blocks programmers by creating myBlocks, covered in a separate myBlocks tutorial.

You will see Vuforia mentioned here, and in the sample OpModes below. Why Vuforia? The FTC implementation of Google's TensorFlow Lite receives camera images from a Vuforia video stream. The FTC SDK already includes and uses Vuforia for navigation, so it's a convenient tool for passing camera streams to TFOD.

These CameraControl interfaces allow some control of the webcam, within requirements or settings of Vuforia for its own performance. Such settings include resolution and frame rate, not covered here.

Exposure Control

Exposure is the amount of light that reaches the webcam sensor. It is an important part of how bright or dark your image appears.

Exposure varies directly with the amount of time that the shutter is open, allowing light to enter and reach the sensor. So, the FTC interface ExposureControl uses a single value of duration, in units of time that you specify, typically TimeUnit.MILLISECONDS .

For example, at a frame rate of 60 frames per second (fps), exposure duration is 1/60 of a second, or 1/60 x 1000 = 16 milliseconds. This basic tutorial does not address frame rate.

Here are the methods to manage exposure:

The webcam may support minimum and maximum allowed values of exposure. These can be retrieved with:

There are no set() methods for min and max exposure; these are hard-coded in the webcam's firmware. Note that firmware settings may vary among different versions of the same webcam model.

These and other exposure methods are called on an ExposureControl object; sample code is shown below, after Exposure Control Mode.

Exposure Control Mode

A webcam may operate in one of various exposure modes.

Many common FTC webcams offer only some of these modes. To directly control the exposure, set the webcam to Manual mode.

The SDK supports these values of ExposureControl.Mode:

Mode is managed with these ExposureControl methods:

The Logitech C920 and C270 models offer two exposure modes: AperturePriority and Manual.

Full details, and a few other methods, are described in the ExposureControl Javadoc.

Exposure Control code samples

  1. Import the interface. This line is automatically added by OnBot Java when the interface is used (coded).
  1. Declare the ExposureControl object, before runOpMode() .
  1. Assign the Vuforia/TFOD video stream control to your control object, in runOpMode() .
  1. Set the mode to Manual, for direct control.
  1. Set the exposure duration, in this case to 30 milliseconds.

See far below for these and other exposure controls illustrated in Sample OpModes.

Gain Control

Gain is a digital camera setting that controls the amplification of the signal from the webcam sensor. This amplifies the whole signal, including any associated background noise.

Gain can be managed in coordination with exposure. Raising exposure and keeping gain low, can provide a bright image and low noise. On the other hand, longer exposure can cause motion blur, which may affect target tracking performance. In some cases, reducing exposure duration and increasing gain may provide a sharper image, although with more noise.

The FTC interface GainControl uses a single value to control gain. It's used for amplification, and thus has no units -- it's just a number of type integer. Its methods are:

As with exposure, the webcam may support minimum and maximum allowed values of gain. These can be retrieved with:

There are no set() methods for min and max gain; these are hard-coded in the webcam's firmware. Note that firmware settings may vary among different versions of the same webcam model.

These and other gain methods are called on a GainControl object, as described above for exposure.

Full details are described in the GainControl Javadoc.

Example 1: Exposure's effect on TFOD

We interrupt this tutorial to demonstrate the two webcam interfaces described so far: ExposureControl and GainControl.

These 2 examples assume you are already using TensorFlow Object Detection (TFOD) in the Freight Frenzy game. Namely you have a TFOD model and OpMode that are working reasonably well. The model may have been supplied with the FTC SDK, or created with the FTC Machine Learning toolchain [forum] [manual] [source] [guide].

Here we will discuss only the Duck game element. Can the exposure and/or gain controls improve the chance of a fast, accurate TFOD detection?

Another way to frame this effort is: can these controls simulate the lighting conditions used for TFOD model training? Namely, if the competition field has different lighting that affects recognition, can you achieve close to your original (trained) TFOD performance?

We first try exposure alone. Setting gain to zero, we apply TFOD to webcam images at various exposure values.

Five fresh readings were taken at each exposure setting. Namely the test OpMode was opened (INIT) each time for a new TFOD initialization and webcam image processing.

This chart shows TFOD confidence levels; 'instant' is defined here as recognition within 1 second.


Five readings at each exposure level

Higher exposure does improve recognition, then performance suddenly drops. Then at higher levels, this TFOD model begins to "see" a Cube, not a Duck. Not good!

So, there does seem to be a range of exposure values that gives better results. Note the sharp drop-off at both ends of the range: below 25 and above 40. In engineering, a robust solution can withstand variation. Using a value in the middle of the improved range, can reduce the effects of unforeseen variation. But this range varies with ambient lighting conditions, which may be quite different at the tournament venue.

This data is the result of a very particular combination of: webcam model (Logitech C270), distance (12 inches), lookdown angle (30 degrees), TFOD model (FTC SDK 7.0 default), ambient lighting, background, etc. Your results will vary, perhaps significantly.

Example 2: Gain's effect on TFOD

Now we adjust only gain. We set Exposure to a fixed value of 15, selected because it was a poor performer in Example 1. Can gain help?

Five fresh readings were taken at each gain setting.


Five readings at each gain level

Higher gain does improve recognition, then performance declines. Then at higher levels, this TFOD model begins to "see" a Cube, not a Duck. The gain effect was similar to the exposure effect.

These two charts suggest that TFOD results are affected by, and can perhaps be optimized by, setting specific values for exposure and gain. A team should compare this with the default or automatic performance of their robot and webcam, in the full range of expected match conditions.

Example 3: An odd preview


Did TFOD make this recognition?

How can this be? Answer: this image was not an 'instant' result. Exposure was reduced very low, after TFOD had recognized the Duck.

The FTC implementations of TensorFlow Lite (and Vuforia) are good at tracking a currently-identified object (or image) through translation, rotation, partial blockage, and even extreme changes in exposure.

White Balance Control

Continuing with other interfaces, the FTC SDK (new for version 7.1) provides methods for white balance control.

White balance is a digital camera setting that balances the color temperature in the image. Color temperature is measured in units of degrees Kelvin (K) and is a physical property of light.

For example, sunlight at noon measures between 5200-6000 K. An incandescent light bulb (warm/orange) has a color temperature of around 3000 K, while shade (cool/blue) measures around 8000 K.

When performed automatically, white balance adds the opposite color to the image in an attempt to bring the color temperature back to neutral. This interface WhiteBalanceControl allows the color temperature to be directly programmed by an FTC user.

A single value is used here to control white balance temperature, in units of degrees Kelvin, of Java type integer. Here are the methods:

As with exposure and gain, the webcam may support minimum and maximum allowed values of white balance temperature. These can be retrieved with:

There are no set() methods for min and max temperature values; these are hard-coded in the webcam's firmware. Note that firmware settings may vary among different versions of the same webcam model.

The Logitech C920 webcam has a min value of 2000 and a max value of 6500.

White Balance Control Mode

This interface supports 3 values of WhiteBalanceControl.Mode:

To directly control the color balance temperature, set the webcam to Manual mode. Mode is managed with these WhiteBalanceControl methods:

The Logitech C920 defaults to Auto mode for white balance control, and even reverts to Auto in a fresh session, after being set to Manual in a previous session. For other CameraControl settings, some webcams revert to a default value and some preserve their last commanded value.

Full details are described in the WhiteBalanceControl Javadoc.

Focus Control

At a distance called "focus length", a subject's image (light rays) converge from the lens to form a clear image on the webcam sensor.

If supported by the webcam, focus can be managed with these FocusControl methods:

Distance units are not specified here; they may be undimensioned values within an allowed range. For example, the Logitech C920 allows values from 0 to 250, with higher values focusing on closer objects.

The webcam may support minimum and maximum allowed values of focus length. These can be retrieved with:

There are no set() methods for min and max focus length; these are hard-coded in the webcam's firmware. Note that firmware settings may vary among different versions of the same webcam model.

These and other focus methods are called on a FocusControl object, as described above for exposure.

Focus Control Mode

A webcam may operate in one of various focus modes. To directly control the focus length, set the webcam to Fixed mode.

The SDK supports these values of FocusControl.Mode:

Mode is managed with these FocusControl methods:

The Logitech C920 webcam offers two modes: ContinuousAuto and Fixed, which does respond to FTC FocusControl methods. The Logitech C270 (older model) offers only Fixed mode, but does not allow programmed control.

Full details are described in the FocusControl Javadoc.

Pan-Tilt-Zoom Control

The FTC SDK provides methods for virtual pan (horizontal motion), tilt (vertical motion), and zoom (enlargement and reduction of image size). This is virtual PTZ since the actions are digitally simulated, within the full original image captured by the webcam. Pan and tilt are possible only to the extent that zoom has provided extra image space to move in that direction.

Pan and Tilt

A webcam does not typically express pan and tilt values in pixels, the smallest unit of image capture by the webcam sensor. For example, the Logitech C920 and the Microsoft LifeCam VX-5000 have a range of +/-36,000 units, far greater than the pixel count in each axis.

The webcam accepts pan and tilt as a pair of (x, y) values. Thus the FTC SDK pan and tilt methods handle these values only as a pair, in a special class named PanTiltHolder. This class has two fields, named pan and tilt, of type integer.

Here's an example to illustrate using the basic methods:

myHolder.pan = 5; // assign the pan field myHolder.tilt = 10; // assign the tilt field myPtzControl.setPanTilt(myHolder); // command the webcam with (x, y) pair

To retrieve values from the webcam:

newHolder = myPtzControl.getPanTilt(); // retrieve (x, y) pair from webcam int currentPanValue = newHolder.pan; // access the pan value int currentTiltValue = newHolder.tilt; // access the tilt value

The above examples assume these objects already exist:

PtzControl myPtzControl = vuforia.getCamera().getControl(PtzControl.class); // create PTZ webcam control object PtzControl.PanTiltHolder myHolder = new PtzControl.PanTiltHolder(); // instantiate input holder object PtzControl.PanTiltHolder newHolder; // declare output holder object

The webcam may support minimum and maximum allowed pan/tilt paired values. Subject to the control object guidelines shown above, these can be retrieved as follows:

There are no set() methods for min and max pan/tilt values; these are hard-coded in the webcam's firmware. Note that firmware settings may vary among different versions of the same webcam model.

These pan and tilt methods are called on a PtzControl object, as described above for exposure.

Full details are described in the PtzControl Javadoc.

Zoom

Virtual zoom is described with a single dimensionless value of type integer. Similar to the interfaces described above, virtual zoom can be managed with these methods:

The Logitech C920 allows zoom values ranging from 100 to 500, although values higher than 250-280 have no further effect on the preview image (influenced by Vuforia).

These zoom methods are called on a PtzControl object, as described above for exposure.

Full details are described in the PtzControl Javadoc.

AE Priority

Auto-Exposure Priority is a setting within the ExposureControl interface. It's listed here at the end, not likely to be needed in FTC since it it operates in very low lighting.

What does it do? Imagine that the webcam is operating at its default frame rate, for example 30 frames per second (fps). Note that frame rate is not covered in this basic tutorial.

If the webcam's built-in auto-exposure detects that the image is very dark, AE Priority allows the frame rate to decrease. This slowdown, or 'undershoot', allows more light per frame, which can 'brighten' the image.

Its methods are:

These AE Priority methods are called on an ExposureControl object, as described above.


Two examples of AE Priority

Here are two pairs of previews, each with AE Priority off and on. In both pairs, the ambient light level is very low. These results are from a Logitech C270 webcam.

The Exposure=0 recognition here was made before reducing exposure and gain. When testing 'instant' results, AE Priority could improve the chance of recognition.

Again, this effect is triggered only in very low lighting, not expected in FTC. If the building loses all power, Duck recognition becomes. less essential.

Evaluating Your Webcam

The firmware of a specific webcam may or may not support certain features described here. The FTC SDK provides some methods to query the webcam and/or return values that indicate whether a valid response was available.

Exposure Support

Here are two methods to query exposure and a specific exposure mode: