Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cropping mechanism #1602

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

GGreenix
Copy link

Added cropping mechanism, the client side was done by someone else and I just continued its work and finished the core mechanism, the dynamic cropping is working as well and can be transmitted from a NT table via setDynamicCropping method of a PhotonCamera object.

Some stuff still needs to be polished but overall I want to understand if there are certain stuff that needs to be changed before I continue.

thanks in advance looking forward to see it in production :D

@GGreenix GGreenix requested a review from a team as a code owner November 23, 2024 00:12
@@ -1,50 +0,0 @@
{
"name": "photonclient",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was all of photon client deleted?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to branch out from the new master so I copied the code from the other guys branch and the one I worked on some to the new one, its overall the same just with the cropping tab

@mcm001
Copy link
Contributor

mcm001 commented Nov 23, 2024

Supersedes #1446

@@ -59,7 +59,6 @@ protected List<AprilTagDetection> process(CVMat in) {
public void setParams(AprilTagDetectionPipeParams newParams) {
if (this.params == null || !this.params.equals(newParams)) {
m_detector.setConfig(newParams.detectorParams);
m_detector.setQuadThresholdParameters(newParams.quadParams);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sus

Comment on lines 26 to 27
public final AprilTagDetector.QuadThresholdParameters quadParams;

public AprilTagDetectionPipeParams(
AprilTagFamily tagFamily,
AprilTagDetector.Config config,
AprilTagDetector.QuadThresholdParameters quadParams) {
public AprilTagDetectionPipeParams(AprilTagFamily tagFamily, AprilTagDetector.Config config) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert these changes?

Comment on lines -61 to -64
if (c.x < 0 || c.y < 0) {
// Skip if the corner is less than zero
continue;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this diff?

this.divisor = divisor;
this.drawAllSnapshots = drawSnapshots;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accidental changes here? restore?

Comment on lines 105 to 108
public int dynamic_x = 0;
public int dynamic_y = 0;
public int dynamic_width = Integer.MAX_VALUE;
public int dynamic_height = Integer.MAX_VALUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make sense to serialize?

More in general I'm not sure how I feel about dynamic crop via NT -- it's pretty perpendicular to all our other stuff to date.

Rect staticCrop = settings.getStaticCrop();

staticCropPipe.setParams(staticCrop);
staticCropPipe.setDynamicRect(settings.getDynamicCrop());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static crop
setdynamic

hmmm

Comment on lines -91 to -103
var quadParams = new AprilTagDetector.QuadThresholdParameters();
// 5 was the default minClusterPixels in WPILib prior to 2025
// increasing it causes detection problems when decimate > 1
quadParams.minClusterPixels = 5;
// these are the same as the values in WPILib 2025
// setting them here to prevent upstream changes from changing behavior of the detector
quadParams.maxNumMaxima = 10;
quadParams.criticalAngle = 45 * Math.PI / 180.0;
quadParams.maxLineFitMSE = 10.0f;
quadParams.minWhiteBlackDiff = 5;
quadParams.deglitch = false;

aprilTagDetectionPipe.setParams(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accidental changes?

Comment on lines 38 to 49
Mat mat = in.getMat();
// if (fullyCovers(params, mat)) {
// return in;
// }
Rect proccesor = false ? this.params : dynamiRect;

int x = MathUtil.clamp(proccesor.x, 0, mat.width());
int y = MathUtil.clamp(proccesor.y, 0, mat.height());
int width = MathUtil.clamp(proccesor.width, 0, mat.width() - x);
int height = MathUtil.clamp(proccesor.height, 0, mat.height() - y);

return new CVMat(mat.submat(y, y + height, x, x + width));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The submat here is just a submat of the overall data array, and does not actually own the native memory. but it still needs to be released. So we need to make sure that we release both after we are done with them. I'd argue we should release the cropped Mat immediately after Apriltag detection, since we don't use it anywhere else. See this example:

   @Test
    public void testRelease() {
        var pipe = new CropPipe();
        pipe.setParams(new Rect(10, 10, 50, 50));

        var frameProvider =
                new FileFrameProvider(
                        TestUtils.getApriltagImagePath(TestUtils.ApriltagTestImages.kTag1_640_480, false),
                        TestUtils.WPI2020Image.FOV,
                        TestUtils.get2020LifeCamCoeffs(false));
        frameProvider.requestFrameThresholdType(FrameThresholdType.GREYSCALE);

        var frame = frameProvider.get();
        var out = pipe.run(frame.processedImage);

        System.out.println(frame.processedImage.getMat());
        System.out.println(out.output.getMat());

        Imgcodecs.imwrite("pre_release_full.png", frame.processedImage.getMat());
        Imgcodecs.imwrite("pre_release_crop.png", out.output.getMat());
        
        frame.release();

        System.out.println(frame.processedImage.getMat());
        System.out.println(out.output.getMat());

        Imgcodecs.imwrite("pose_releaseframe_crop.png", out.output.getMat());

        out.output.getMat().release();

        System.out.println(frame.processedImage.getMat());
        System.out.println(out.output.getMat());
    }

Output is:

// Starting state
    Mat [ 480*640*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7ff994f80a30, dataAddr=0x7ff994fb26c0 ]
    Mat [ 50*50*CV_8UC1, isCont=false, isSubmat=true, nativeObj=0x7ff994f87910, dataAddr=0x7ff994fb3fca ]
// After releasing the full image Mat
    Mat [ 0*0*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7ff994f80a30, dataAddr=0x0 ]
    Mat [ 50*50*CV_8UC1, isCont=false, isSubmat=true, nativeObj=0x7ff994f87910, dataAddr=0x7ff994fb3fca ]
// after releasing both
    Mat [ 0*0*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7ff994f80a30, dataAddr=0x0 ]
    Mat [ 0*0*CV_8UC1, isCont=false, isSubmat=true, nativeObj=0x7ff994f87910, dataAddr=0x0 ]

Copy link
Contributor

@Juniormunk Juniormunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the robot examples contain changes as that shouldn't happen either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not have changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not have changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is autogenerated and is not supposed to be committed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After resetting it back to the version in master, you can run git update-index --skip-worktree photon-server/src/main/resources/web/index.html to prevent git from ever seeing it as changed and adding it to a commit.

Comment on lines +1 to +14
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Photon Client</title>
<script type="module" crossorigin src="./assets/index-CIrfZRaJ.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-Bt-HXv2B.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepting this suggestion will revert it to the version in master:

Suggested change
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Photon Client</title>
<script type="module" crossorigin src="./assets/index-CIrfZRaJ.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-Bt-HXv2B.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
<p>UI has not been copied!</p>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the changes to files under photonlib-java-examples/aimandrange/ are replacing the existing "Aim and Range" example with an example for using cropping. The cropping example should be moved to it's own folder and these files should be reverted to be identical to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants