Skip to content

Commit 080a4b9

Browse files
authored
Update CameraServer code samples (#1717)
Use RLIs, fixes compilation errors. Update text to correspond to RLIs
1 parent 8ec3858 commit 080a4b9

File tree

1 file changed

+26
-94
lines changed

1 file changed

+26
-94
lines changed

source/docs/software/vision-processing/roborio/using-the-cameraserver-on-the-roborio.rst

Lines changed: 26 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -11,108 +11,40 @@ The following program starts automatic capture of a USB camera like the Microsof
1111

1212
.. tabs::
1313

14-
.. code-tab:: java
14+
.. group-tab:: Java
1515

16-
package org.usfirst.frc.team190.robot;
16+
.. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/quickvision/Robot.java
17+
:language: java
18+
:lines: 7-20
19+
:linenos:
20+
:lineno-start: 7
1721

18-
import edu.wpi.first.cameraserver.CameraServer;
19-
import edu.wpi.first.wpilibj.IterativeRobot;
22+
.. group-tab:: C++
2023

21-
public class Robot extends IterativeRobot {
22-
23-
public void robotInit() {
24-
CameraServer.startAutomaticCapture();
25-
}
26-
}
27-
28-
.. code-tab:: c++
29-
30-
#include "cameraserver/CameraServer.h"
31-
class Robot: public IterativeRobot
32-
{
33-
private:
34-
void RobotInit()
35-
{
36-
frc::CameraServer::StartAutomaticCapture();
37-
}
38-
};
39-
START_ROBOT_CLASS(Robot)
24+
.. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/examples/QuickVision/cpp/Robot.cpp
25+
:language: cpp
26+
:lines: 7-8,16-18,20,25-31
4027

4128

4229
Advanced Camera Server Program
4330
------------------------------
4431

45-
In the following example a thread created in robotInit() gets the Camera Server instance. Each frame of the video is individually processed, in this case converting a color image (BGR) to gray scale using the OpenCV cvtColor() method. The resultant images are then passed to the output stream and sent to the dashboard. You can replace the cvtColor operation with any image processing code that is necessary for your application. You can even annotate the image using OpenCV methods to write targeting information onto the image being sent to the dashboard.
32+
In the following example a thread created in robotInit() gets the Camera Server instance. Each frame of the video is individually processed, in this case drawing a rectangle on the image using the OpenCV ``rectangle()`` method. The resultant images are then passed to the output stream and sent to the dashboard. You can replace the ``rectangle`` operation with any image processing code that is necessary for your application. You can even annotate the image using OpenCV methods to write targeting information onto the image being sent to the dashboard.
4633

4734
.. tabs::
4835

49-
.. code-tab:: java
50-
51-
package org.usfirst.frc.team190.robot;
52-
53-
import org.opencv.core.Mat;
54-
import org.opencv.imgproc.Imgproc;
55-
56-
import edu.wpi.cscore.CvSink;
57-
import edu.wpi.cscore.CvSource;
58-
import edu.wpi.cscore.UsbCamera;
59-
import edu.wpi.first.cameraserver.CameraServer;
60-
import edu.wpi.first.wpilibj.IterativeRobot;
61-
62-
public class Robot extends IterativeRobot {
63-
64-
public void robotInit() {
65-
new Thread(() -> {
66-
UsbCamera camera = CameraServer.startAutomaticCapture();
67-
camera.setResolution(640, 480);
68-
69-
CvSink cvSink = CameraServer.getVideo();
70-
CvSource outputStream = CameraServer.putVideo("Blur", 640, 480);
71-
72-
Mat source = new Mat();
73-
Mat output = new Mat();
74-
75-
while(!Thread.interrupted()) {
76-
if (cvSink.grabFrame(source) == 0) {
77-
continue;
78-
}
79-
Imgproc.cvtColor(source, output, Imgproc.COLOR_BGR2GRAY);
80-
outputStream.putFrame(output);
81-
}
82-
}).start();
83-
}
84-
}
85-
86-
.. code-tab:: c++
87-
88-
#include "cameraserver/CameraServer.h"
89-
#include <opencv2/imgproc/imgproc.hpp>
90-
#include <opencv2/core/core.hpp>
91-
class Robot: public IterativeRobot
92-
{
93-
private:
94-
static void VisionThread()
95-
{
96-
cs::UsbCamera camera = frc::CameraServer::StartAutomaticCapture();
97-
camera.SetResolution(640, 480);
98-
cs::CvSink cvSink = frc::CameraServer::GetVideo();
99-
cs::CvSource outputStreamStd = frc::CameraServer::PutVideo("Gray", 640, 480);
100-
cv::Mat source;
101-
cv::Mat output;
102-
while(true) {
103-
if (cvSink.GrabFrame(source) == 0) {
104-
continue;
105-
}
106-
cvtColor(source, output, cv::COLOR_BGR2GRAY);
107-
outputStreamStd.PutFrame(output);
108-
}
109-
}
110-
void RobotInit()
111-
{
112-
std::thread visionThread(VisionThread);
113-
visionThread.detach();
114-
}
115-
};
116-
START_ROBOT_CLASS(Robot)
117-
118-
Notice that in these examples, the ``PutVideo()`` method writes the video to a named stream. To view that stream on Shuffleboard, select that named stream. In this case that is "Blur" for the Java program and "Gray" for the C++ sample.
36+
.. group-tab:: Java
37+
38+
.. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/intermediatevision/Robot.java
39+
:language: java
40+
:lines: 7-65
41+
:linenos:
42+
:lineno-start: 7
43+
44+
.. group-tab:: C++
45+
46+
.. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/examples/IntermediateVision/cpp/Robot.cpp
47+
:language: cpp
48+
:lines: 5-20,23-56,58-61,63-64,69-76
49+
50+
Notice that in these examples, the ``PutVideo()`` method writes the video to a named stream. To view that stream on SmartDashboard or Shuffleboard, select that named stream. In this case that is "Rectangle".

0 commit comments

Comments
 (0)