Expose object detection class id/conf in photonlib (#1266)

* Implement class id/conf in photonlib

* Maybe fix things

* run lint

* Update Packet.java comments

* Update Packet.java comments again

* Update comments

* oops

* Update packet.py

---------

Co-authored-by: Chris Gerth <gerth2@users.noreply.github.com>
This commit is contained in:
Matt
2024-05-10 14:52:16 -04:00
committed by GitHub
parent 113951100e
commit 1708376df8
15 changed files with 172 additions and 34 deletions

View File

@@ -107,6 +107,19 @@ public class Packet {
packetData[writePos++] = (byte) src;
}
/**
* Encodes the float into the packet.
*
* @param src The float to encode.
*/
public void encode(float src) {
int data = Float.floatToIntBits(src);
packetData[writePos++] = (byte) ((data >> 24) & 0xff);
packetData[writePos++] = (byte) ((data >> 16) & 0xff);
packetData[writePos++] = (byte) ((data >> 8) & 0xff);
packetData[writePos++] = (byte) (data & 0xff);
}
/**
* Encodes the double into the packet.
*
@@ -213,6 +226,23 @@ public class Packet {
return Double.longBitsToDouble(data);
}
/**
* Returns a decoded float from the packet.
*
* @return A decoded float from the packet.
*/
public float decodeFloat() {
if (packetData.length < (readPos + 3)) {
return 0;
}
int data =
(int) (0xff & packetData[readPos++]) << 24
| (int) (0xff & packetData[readPos++]) << 16
| (int) (0xff & packetData[readPos++]) << 8
| (int) (0xff & packetData[readPos++]);
return Float.intBitsToFloat(data);
}
/**
* Returns a decoded boolean from the packet.
*

View File

@@ -34,6 +34,8 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
private final double area;
private final double skew;
private final int fiducialId;
private final int classId;
private final float objDetectConf;
private final Transform3d bestCameraToTarget;
private final Transform3d altCameraToTarget;
private final double poseAmbiguity;
@@ -50,7 +52,9 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
double pitch,
double area,
double skew,
int id,
int fiducialId,
int classId,
float objDetectConf,
Transform3d pose,
Transform3d altPose,
double ambiguity,
@@ -66,7 +70,9 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
this.pitch = pitch;
this.area = area;
this.skew = skew;
this.fiducialId = id;
this.fiducialId = fiducialId;
this.classId = classId;
this.objDetectConf = objDetectConf;
this.bestCameraToTarget = pose;
this.altCameraToTarget = altPose;
this.minAreaRectCorners = minAreaRectCorners;
@@ -90,11 +96,24 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
return skew;
}
/** Get the Fiducial ID, or -1 if not set. */
/** Get the fiducial ID, or -1 if not set. */
public int getFiducialId() {
return fiducialId;
}
/** Get the object detection class ID number, or -1 if not set. */
public int getDetectedObjectClassID() {
return classId;
}
/**
* Get the object detection confidence, or -1 if not set. This will be between 0 and 1, with 1
* indicating most confidence, and 0 least.
*/
public float getDetectedObjectConfidence() {
return objDetectConf;
}
/**
* Get the ratio of best:alternate pose reprojection errors, called ambiguity. This is betweeen 0
* and 1 (0 being no ambiguity, and 1 meaning both have the same reprojection error). Numbers
@@ -219,7 +238,7 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
public static final class APacketSerde implements PacketSerde<PhotonTrackedTarget> {
@Override
public int getMaxByteSize() {
return Double.BYTES * (5 + 7 + 2 * 4 + 1 + 7 + 2 * MAX_CORNERS);
return Double.BYTES * (5 + 7 + 2 * 4 + 1 + 1 + 4 + 7 + 2 * MAX_CORNERS);
}
@Override
@@ -229,6 +248,8 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
packet.encode(value.area);
packet.encode(value.skew);
packet.encode(value.fiducialId);
packet.encode(value.classId);
packet.encode(value.objDetectConf);
PacketUtils.packTransform3d(packet, value.bestCameraToTarget);
PacketUtils.packTransform3d(packet, value.altCameraToTarget);
packet.encode(value.poseAmbiguity);
@@ -250,6 +271,8 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
var area = packet.decodeDouble();
var skew = packet.decodeDouble();
var fiducialId = packet.decodeInt();
var classId = packet.decodeInt();
var objDetectConf = packet.decodeFloat();
Transform3d best = PacketUtils.unpackTransform3d(packet);
Transform3d alt = PacketUtils.unpackTransform3d(packet);
double ambiguity = packet.decodeDouble();
@@ -271,6 +294,8 @@ public class PhotonTrackedTarget implements ProtobufSerializable {
area,
skew,
fiducialId,
classId,
objDetectConf,
best,
alt,
ambiguity,

View File

@@ -57,6 +57,8 @@ public class PhotonTrackedTargetProto
msg.getArea(),
msg.getSkew(),
msg.getFiducialId(),
msg.getObjDetectionId(),
msg.getObjDetectionConf(),
Transform3d.proto.unpack(msg.getBestCameraToTarget()),
Transform3d.proto.unpack(msg.getAltCameraToTarget()),
msg.getPoseAmbiguity(),