forked from GreenBlitz/2025-Offseason
-
Notifications
You must be signed in to change notification settings - Fork 0
Object detection -> master #271
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
Open
rys4590
wants to merge
23
commits into
master
Choose a base branch
from
object-detection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f94dc1f
i hate my life
rys4590 a5cd974
huh-?
rys4590 5320f05
huh-?
rys4590 d3022e9
huh-?
rys4590 04af104
okay fixed a few things, should be smooth sailing from here.
rys4590 02b29e9
okay fixed a few things, should be smooth sailing from here.
rys4590 9926495
checking smth
rys4590 90d2461
sA
rys4590 469b2b4
i coked
rys4590 7826605
i sadly did not cook
rys4590 8c0711a
when your code works, do spotless!
rys4590 75097f5
kid it does not work
rys4590 1e397c2
big overhaul: 22.3.26 19:22
rys4590 efeb30d
ahh merge
rys4590 883c428
ahh merge
rys4590 0ca9cfa
ahh merge
rys4590 3b043ce
bruh
rys4590 4da4eb0
Merge branch 'master' into object-detection
rys4590 10b8bcd
1.5
rys4590 a240f12
uh its 25.6.26 if thats what ur wondering. i spotlessed!!
rys4590 8785631
Merge branch 'master' into object-detection
rys4590 fa457f1
weeeeeeeeeeeeeeeeee
rys4590 79e3901
master merge
rys4590 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import edu.wpi.first.math.geometry.*; | ||
| import frc.robot.vision.DetectedObjectObservation; | ||
| import frc.robot.vision.DetectedObjectType; | ||
| import frc.robot.vision.RobotPoseObservation; | ||
| import frc.robot.vision.cameras.limelight.inputs.LimelightInputsSet; | ||
| import frc.robot.vision.interfaces.ObjectDetector; | ||
|
|
@@ -21,17 +22,24 @@ | |
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static frc.robot.vision.cameras.limelight.ObjectDetectionMath.*; | ||
|
|
||
| public class Limelight implements ObjectDetector, IndependentRobotPoseSupplier, OrientationRequiringRobotPoseSupplier { | ||
|
|
||
| private static final int THROTTLE_ENABLE_VALUE = 200; | ||
| private static final int THROTTLE_DISABLE_VALUE = 0; | ||
| private static final double THREE_FOURTHS_OBJECT_FACTOR = 1.5; | ||
|
|
||
| private final String name; | ||
| private final String logPath; | ||
| private final Pose3d robotRelativeCameraPose; | ||
|
|
||
| private final ArrayList<DetectedObjectObservation> neuralDetections; | ||
| private final ArrayList<DetectedObjectObservation> colorDetections; | ||
| private final ArrayList<DetectedObjectObservation> weirdObjectThing; | ||
|
|
||
| private final Rotation2d horizontalFOV; | ||
| private final Rotation2d verticalFOV; | ||
|
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused |
||
|
|
||
| private final LimelightInputsSet inputs; | ||
|
|
||
|
|
@@ -50,7 +58,14 @@ public class Limelight implements ObjectDetector, IndependentRobotPoseSupplier, | |
|
|
||
| private LimelightPipeline pipeline; | ||
|
|
||
| public Limelight(String name, String logPathPrefix, Pose3d robotRelativeCameraPose, LimelightPipeline pipeline) { | ||
| public Limelight( | ||
| String name, | ||
| String logPathPrefix, | ||
| Pose3d robotRelativeCameraPose, | ||
| LimelightPipeline pipeline, | ||
| Rotation2d horizontalFOV, | ||
| Rotation2d verticalFOV | ||
| ) { | ||
| this.name = name; | ||
| this.logPath = logPathPrefix + "/" + name; | ||
|
|
||
|
|
@@ -59,6 +74,10 @@ public Limelight(String name, String logPathPrefix, Pose3d robotRelativeCameraPo | |
|
|
||
| this.neuralDetections = new ArrayList<>(); | ||
| this.colorDetections = new ArrayList<>(); | ||
| this.weirdObjectThing = new ArrayList<>(); | ||
|
|
||
| this.horizontalFOV = horizontalFOV; | ||
| this.verticalFOV = verticalFOV; | ||
|
|
||
| this.mt1PoseObservation = new RobotPoseObservation(); | ||
| this.mt2PoseObservation = new RobotPoseObservation(); | ||
|
|
@@ -109,6 +128,34 @@ public void updateNeuralDetection() { | |
| } | ||
| } | ||
|
|
||
| public void updateHeatMapObjectDetection() { | ||
| double tx = LimelightHelpers.getTX(name); | ||
| double ty = LimelightHelpers.getTY(name); | ||
|
|
||
| Rotation2d yawOffset = Rotation2d.fromDegrees(tx); | ||
| Rotation2d pitchOffset = Rotation2d.fromDegrees(ty); | ||
| double objectRelativeToCameraX = getCameraRelativeObjectX( | ||
| robotRelativeCameraPose, | ||
| DetectedObjectType.FUEL.getCenterHeightFromFloorMeters() * THREE_FOURTHS_OBJECT_FACTOR, | ||
| pitchOffset | ||
| ); | ||
|
|
||
| double objectRelativeToCameraY = getCameraRelativeObjectY( | ||
| robotRelativeCameraPose, | ||
| DetectedObjectType.FUEL.getCenterHeightFromFloorMeters() * THREE_FOURTHS_OBJECT_FACTOR, | ||
| yawOffset, | ||
| objectRelativeToCameraX | ||
| ); | ||
|
|
||
| Translation2d objectRelativeToCamera = new Translation2d(objectRelativeToCameraX, objectRelativeToCameraY); | ||
| Translation2d objectRelativeToField = objectRelativeToCamera.rotateBy(robotRelativeCameraPose.getRotation().toRotation2d()); | ||
|
|
||
|
Comment on lines
+137
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't you use ObjectDetectionMath.getDetectedObjectObservation()? |
||
| Logger.recordOutput(logPath + "/objectRelativeToCamera", objectRelativeToCamera); | ||
| Logger.recordOutput(logPath + "/objectDetection", objectRelativeToField); | ||
| Logger.recordOutput(logPath + "/tx", tx); | ||
| Logger.recordOutput(logPath + "/ty", ty); | ||
| } | ||
|
|
||
| public void updateColorDetection() { | ||
| if (pipeline.isColorDetecting()) { | ||
| colorDetections.clear(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/python/objectdetection/SnapscriptDetectionCode.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import cv2 | ||
| import numpy as np | ||
|
|
||
| lower_yellow = np.array([20, 100, 100]) | ||
| upper_yellow = np.array([30, 255, 255]) #wrong values impact the accuracy a lot. PLS NEXT PROJECT MAKE GOOD CALIBARATION CODE IN PYTHON | ||
|
|
||
| COLUMNS = 25 | ||
|
|
||
| def count_yellow_in_cell(mask, x_start, x_end, y_start, y_end): | ||
| cell_mask = mask[y_start:y_end, x_start:x_end] | ||
| return np.sum(cell_mask) | ||
|
|
||
| def runPipeline(image, llrobot): | ||
| height, width = image.shape[:2] | ||
|
|
||
| start = np.float32([[0, 0], [width, 0], [0, height], [width, height]]) | ||
| top_y = (start[0][1] + start[1][1]) / 2 | ||
| bottom_y = (start[2][1] + start[3][1]) / 2 | ||
| xfactor = bottom_y - top_y | ||
| yfactor = -xfactor | ||
|
|
||
| dsize = (int(width), int(height + xfactor)) | ||
| end = np.float32([ | ||
| [0, 0], | ||
| [width, 0], | ||
| [-yfactor/2, height + xfactor], | ||
| [width + yfactor/2, height + xfactor] | ||
| ]) | ||
|
|
||
| matrix = cv2.getPerspectiveTransform(start, end) | ||
| inv_matrix = np.linalg.inv(matrix) | ||
| warped = cv2.warpPerspective(image, matrix, dsize, flags=cv2.INTER_LINEAR) | ||
|
|
||
| heightAfter, widthAfter = warped.shape[:2] | ||
|
|
||
| hsv_img = cv2.cvtColor(warped, cv2.COLOR_BGR2HSV) | ||
| mask = cv2.inRange(hsv_img, lower_yellow, upper_yellow) | ||
|
|
||
| rows = round(heightAfter / (widthAfter / COLUMNS)) | ||
| cell_w = int(widthAfter / COLUMNS) | ||
| cell_h = int(heightAfter / rows) | ||
|
|
||
| most_concentrated = -1 | ||
| most_x, most_y = -1, -1 | ||
|
|
||
| for i in range(rows): | ||
| for j in range(COLUMNS): | ||
| x_start = j * cell_w | ||
| x_end = (j + 1) * cell_w | ||
| y_start = i * cell_h | ||
| y_end = (i + 1) * cell_h | ||
|
|
||
| avg = count_yellow_in_cell(mask, x_start, x_end, y_start, y_end) / (cell_h * cell_w) | ||
| percent = avg * 100 / 255 | ||
|
|
||
| if percent > most_concentrated: | ||
| most_concentrated = percent | ||
| most_x = j | ||
| most_y = i | ||
|
|
||
| middle_x = most_x * cell_w + cell_w // 2 | ||
| middle_y = most_y * cell_h + cell_h // 2 | ||
|
|
||
| pt = np.array([[middle_x], [middle_y], [1]]) | ||
| orig_pt = inv_matrix @ pt | ||
| orig_pt = orig_pt.flatten() | ||
|
|
||
| orig_x = int(orig_pt[0] / orig_pt[2]) | ||
| orig_y = int(orig_pt[1] / orig_pt[2]) | ||
|
|
||
| nx = (orig_x - (width / 2)) / (width / 2) | ||
| ny = (orig_y - (height / 2)) / (height / 2) | ||
|
|
||
| contour = np.array([ | ||
| [[orig_x - cell_w, orig_y - cell_h]], | ||
| [[orig_x + cell_w, orig_y - cell_h]], | ||
| [[orig_x + cell_w, orig_y + cell_h]], | ||
| [[orig_x - cell_w, orig_y + cell_h]] | ||
| ]) | ||
|
|
||
| x, y, w, h = cv2.boundingRect(contour) | ||
|
|
||
| cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 255), 2) | ||
|
|
||
| llpython = [1, nx, ny, orig_x, orig_y, most_concentrated, 0, 0] | ||
|
|
||
| print(f"Grid cell: ({most_x}, {most_y}) | Pixel: ({orig_x}, {orig_y}) | nx={nx:.3f}, ny={ny:.3f}") | ||
|
|
||
| return contour, image, llpython |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.