Skip to content

Commit 579331f

Browse files
committed
simulate confidence for object detection and populate classId and computed confidence level in Java (while building the PhotonTrackedTarget)
1 parent 77bf5bd commit 579331f

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

photon-lib/py/photonlibpy/simulation/photonCameraSim.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ def distance(target: VisionTargetSim):
368368
noisyTargetCorners,
369369
)
370370

371+
# Compute object detection confidence if this is an obj det target
372+
classId = tgt.objDetClassId
373+
conf = tgt.objDetConf
374+
if classId >= 0 and conf < 0:
375+
# Simulate confidence using sqrt-scaled area for a more realistic
376+
# curve. Raw areaPercent/100 is tiny for most targets; sqrt scaling
377+
# gives reasonable values even for small-but-visible objects.
378+
conf = max(0.0, min(1.0, math.sqrt(areaPercent / 100.0) * 2.0))
379+
371380
smallVec: list[TargetCorner] = []
372381
for corner in minAreaRectPts:
373382
smallVec.append(TargetCorner(corner[0], corner[1]))
@@ -381,6 +390,8 @@ def distance(target: VisionTargetSim):
381390
area=areaPercent,
382391
skew=math.degrees(centerRot.X()),
383392
fiducialId=tgt.fiducialId,
393+
objDetectId=classId,
394+
objDetectConf=conf,
384395
detectedCorners=cornersFloat,
385396
minAreaRectCorners=smallVec,
386397
bestCameraToTarget=pnpSim.best if pnpSim else Transform3d(),

photon-lib/src/main/java/org/photonvision/simulation/PhotonCameraSim.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,25 @@ public PhotonPipelineResult process(
525525
.get();
526526
}
527527

528+
// Compute object detection confidence if this is an obj det target
529+
int classId = tgt.objDetClassId;
530+
float conf = tgt.objDetConf;
531+
if (classId >= 0 && conf < 0) {
532+
// Simulate confidence using sqrt-scaled area for a more realistic
533+
// curve. Raw areaPercent/100 is tiny for most targets; sqrt scaling
534+
// gives reasonable values even for small-but-visible objects.
535+
conf = (float) Math.max(0.0, Math.min(1.0, Math.sqrt(areaPercent / 100.0) * 2.0));
536+
}
537+
528538
detectableTgts.add(
529539
new PhotonTrackedTarget(
530540
-Math.toDegrees(centerRot.getZ()),
531541
-Math.toDegrees(centerRot.getY()),
532542
areaPercent,
533543
Math.toDegrees(centerRot.getX()),
534544
tgt.fiducialID,
535-
-1,
536-
-1,
545+
classId,
546+
conf,
537547
pnpSim.best,
538548
pnpSim.alt,
539549
pnpSim.ambiguity,

photon-lib/src/main/java/org/photonvision/simulation/VisionTargetSim.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public class VisionTargetSim {
3636

3737
public final int fiducialID;
3838

39+
/** The object detection class ID, or -1 if not applicable. */
40+
public int objDetClassId = -1;
41+
42+
/** The object detection confidence, or -1 if not applicable. */
43+
public float objDetConf = -1;
44+
3945
/**
4046
* Describes a vision target located somewhere on the field that your vision system can detect.
4147
*

photon-lib/src/main/native/cpp/photon/simulation/PhotonCameraSim.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ PhotonPipelineResult PhotonCameraSim::Process(
220220
tgt.GetModel().GetVertices(), noisyTargetCorners);
221221
}
222222

223+
// Compute object detection confidence if this is an obj det target
224+
int classId = tgt.objDetClassId;
225+
float conf = tgt.objDetConf;
226+
if (classId >= 0 && conf < 0) {
227+
// Simulate confidence using sqrt-scaled area for a more realistic
228+
// curve. Raw areaPercent/100 is tiny for most targets; sqrt scaling
229+
// gives reasonable values even for small-but-visible objects.
230+
conf = static_cast<float>(
231+
std::clamp(std::sqrt(areaPercent / 100.0) * 2.0, 0.0, 1.0));
232+
}
233+
223234
std::vector<std::pair<float, float>> tempCorners =
224235
OpenCVHelp::PointsToCorners(minAreaRectPts);
225236
std::vector<TargetCorner> smallVec;
@@ -237,7 +248,7 @@ PhotonPipelineResult PhotonCameraSim::Process(
237248
-centerRot.Z().convert<units::degrees>().to<double>(),
238249
-centerRot.Y().convert<units::degrees>().to<double>(), areaPercent,
239250
centerRot.X().convert<units::degrees>().to<double>(), tgt.fiducialId,
240-
tgt.objDetClassId, tgt.objDetConf,
251+
classId, conf,
241252
pnpSim ? pnpSim->best : frc::Transform3d{},
242253
pnpSim ? pnpSim->alt : frc::Transform3d{},
243254
pnpSim ? pnpSim->ambiguity : -1, smallVec, cornersDouble);

0 commit comments

Comments
 (0)