Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Getting Target Data

Constructing a Camera

The first step to getting data from the targets in your camera’s feed is to actually create the camera in code:

# Change this to match the name of your camera as shown in the web ui
self.camera = PhotonCamera("your_camera_name_here")

Through the PhotonCamera class, you can retrieve yaw, pitch, roll, robot-relative pose, latency, and a wealth of other information.

Getting a Pipeline Result

You may be asking yourself, what is a Photon Pipeline Result? Well, A PhotonPipelineResult is a container that contains all information about currently detected targets from a PhotonCamera. You can retrieve the latest pipeline result using the PhotonCamera instance.

To get a PhotonPipelineResult from your camera there are two methods:

# Method 1: Query the latest result from PhotonVision
result = self.camera.getLatestResult()

# Method 2: Query the list of pipeline results sent by PhotonVision since the last call of this function
results = self.camera.getAllUnreadResults()

Checking for Targets

Each pipeline result has a hasTargets() method to inform you as to whether the result contains any targets.

hasTargets = result.hasTargets()

Getting Tracked Targets

A tracked target contains information about each target from a pipeline result. This information includes yaw, pitch, area, and the camera-to-target transform.

You can get a list of tracked targets using the getTargets() method from a pipeline result:

# Get a list of currently tracked targets.
targets = result.getTargets()

To get the best target you can use the getBestTarget() method from a pipeline result:

target = result.getBestTarget()

Getting Target Data

To get the data from a PhotonTrackedTarget there are a bunch of simple getter methods you can use:

yaw = target.getYaw()
pitch = target.getPitch()
area = target.getArea()
skew = target.getSkew()
pose = target.getBestCameraToTarget()
corners = target.getDetectedCorners()
boundingBoxCorners = target.getMinAreaRectCorners()

Getting AprilTag Data

AprilTag targets provide a few additional values on top of the standard target data. getFiducialId() returns the ID of the detected tag, while getPoseAmbiguity() describes how uncertain the pose solve is. A lower ambiguity value means the result is more trustworthy.

PhotonVision also provides both the best and alternate camera-to-target transforms. The best transform has the lowest reprojection error, while the alternate transform has the highest.

targetID = target.getFiducialId()
poseAmbiguity = target.getPoseAmbiguity()
bestCameraToTarget = target.getBestCameraToTarget()
alternateCameraToTarget = target.getAlternateCameraToTarget()

Saving Pictures to File

A PhotonCamera can save still images from either the raw input stream or the processed output stream. This is particularly useful when debugging what the camera saw on the field or confirming that a target was identified correctly.

# Capture a pre-process camera stream image
self.camera.takeInputSnapshot()

# Capture a post-process camera stream image
self.camera.takeOutputSnapshot()

Saved images are stored in PhotonVision’s configuration directory. To retrieve them, use the Export operation in the settings tab and open the downloaded .zip file.