Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Add example to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jan 7, 2019
1 parent 5362f51 commit 6ca0fc2
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 65 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@ gradle-app.setting
# gradle/wrapper/gradle-wrapper.properties

*.iml
*.ipr
*.ipr

frc-vision-target-2019-output.jpg
Binary file added 2019-vision-example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
145 changes: 85 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,105 @@
# FRC Robot Vision API
The Robot Vision API allows FRC teams to specify some key parameters about the target they are looking for, and receive the location of the target in a given image. Developed through FRC Team 5112, The Gongoliers.

![2019 Vision Example](2019-vision-example.jpg)

## Installation
Download the jar file from the [releases page](https://github.com/kylecorry31/Robot-Vision-API/releases) add it to your project.
## Getting started

### FRC 2019 (alpha - official support coming after kickoff)
Move the jar to a libs folder in your project. In your `build.gradle` file, add the following line to the dependencies section:
### Install
To use the the Library of Gongolierium with Gradle projects, you can use [JitPack](https://jitpack.io/) by adding the following lines to your `build.gradle` file:

`compile fileTree(dir: 'libs', include: '*.jar')`

Here is an examples [build.gradle file](https://github.com/kylecorry31/FRC-Sample-Robot-2019/blob/master/build.gradle).


### FRC 2018 and older
Move the jar to a libs folder in your project. Add the libs folder to your `build.properties` file like so, at the end of the file:

`userLibs = libs`

Then add the jar to your build path in Eclipse.

```Gradle
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
...
compile 'com.github.kylecorry31:Robot-Vision-API:v1.1.0'
}
```


## Usage
### Usage (FRC 2019 Vision Example)
```Java
/**
* Detects the 2019 vision targets.
* @param image The image from the camera.
* @return The list of vision target groups.
*/
public List<Target> detect2019Targets(Mat image){
// Adjust these parameters for your team's needs

// Target filter parameters
double minBrightness = 200;
double maxBrightness = 255;

// Contour filter parameters
Range area = new Range(0.03, 100);
Range fullness = new Range(0, 100);
Range aspectRatio = new Range(0.2, 4);

// Camera settings
FOV fov = new FOV(50, 40); // This is the approx. Microsoft LifeCam FOV
Resolution resolution = new Resolution(640, 480);
boolean cameraInverted = false;

int imageArea = resolution.getArea();

// An HSV filter may be better for FRC target detection
TargetFilter filter = new BrightnessFilter(minBrightness, maxBrightness);
ContourFilter contourFilter = new StandardContourFilter(area, fullness, aspectRatio, imageArea);
CameraSettings cameraSettings = new CameraSettings(cameraInverted, fov, resolution);
TargetFinder targetFinder = new TargetFinder(cameraSettings, filter, contourFilter, TargetGrouping.SINGLE);

// Find the targets
List<Target> targets = targetFinder.findTargets(image);

// Sort the targets by x coordinates
targets.sort(Comparator.comparingDouble(target -> target.getBoundary().center.x));

List<Target> bays = new ArrayList<>();
// If the current target is a left and the next is a right, make it a pair
for (int i = 0; i < targets.size() - 1; i++) {
Target current = targets.get(i);
Target next = targets.get(i + 1);

// Determine if the targets are a left and right pair
if (isLeftTarget(current) && isRightTarget(next)){
// Combine the targets
Target bay = TargetUtils.combineTargets(current, next, cameraSettings);
bays.add(bay);
// Skip the next target
i++;
}
}

return bays;
}

// This image will come from the camera using WPILib
Mat image = ...;

// Fill in with your own parameters
double minBrightness = 200;
double maxBrightness = 255;

Range area = new Range(0.03, 100);
Range fullness = new Range(0, 100);
Range aspectRatio = new Range(0, 100);

FOV fov = new FOV(60, 60);
Resolution resolution = new Resolution(640, 480);
boolean cameraInverted = false;

int imageArea = resolution.getArea();



// An HSV filter may be better for FRC target detection
TargetFilter filter = new BrightnessFilter(minBrightness, maxBrightness);
ContourFilter contourFilter = new StandardContourFilter(area, fullness, aspectRatio, imageArea);
CameraSettings cameraSettings = new CameraSettings(cameraInverted, fov, resolution);
TargetFinder targetFinder = new TargetFinder(cameraSettings, filter, contourFilter, TargetGrouping.SINGLE);

// Get the image from the camera using WPILib
List<Target> targets = targetFinder.findTargets(image);

/**
* Determines if a target is a left vision target.
* @param target The target.
* @return True if it is a left target.
*/
private boolean isLeftTarget(Target target){
return target.getSkew() < 0;
}

// Draw the targets on the image
for(Target target: targets){
// Get the bounding box of the target as a rotated rectangle
RotatedRect boundary = target.getBoundary();

// Convert the rotated rect to a contour list (OpenCV stuff to draw the rectangle)
Point[] points = new Point[4];
boundary.points(points);
MatOfPoint contour = new MatOfPoint(points);
List<MatOfPoint> contours = Arrays.asList(contour);

// Draw it on the image
Imgproc.drawContours(image, contours, 0, new Scalar(0, 255, 0));
Imgproc.drawMarker(image, target.getBoundary().center, new Scalar(255, 0, 0));
/**
* Determines if a target is a right vision target.
* @param target The target.
* @return True if it is a right target.
*/
private boolean isRightTarget(Target target){
return target.getSkew() > 0;
}

```

## Contributing
Please fork this repo and submit a pull request to contribute. I will review all changes and respond if they are accepted or rejected (as well as reasons, so it will be accepted).

## License
This project is published under the [GPL-3.0 license](LICENSE).

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.kylecorry.frc.vision'
version '1.0.1'
version '1.1.0'


sourceCompatibility = 1.8
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/kylecorry/frc/vision/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public int getMajorVersion(){
}

public int getMinorVersion(){
return 0;
return 1;
}

public int getBuildVersion(){
return 1;
public int getPatchVersion(){
return 0;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/kylecorry/frc/vision/targeting/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public void setSkew(double skew){
this.skew = skew;
}

/**
* Get the skew of the target in degrees, from -90 to 90 where negative values are angled to the left.
* @return The skew in degrees.
*/
public double getSkew() {
return skew;
}
Expand Down

0 comments on commit 6ca0fc2

Please sign in to comment.