VisionServer  v2.1.1-1-g21dc5465
FRC vision library
visioncamera.cpp
Go to the documentation of this file.
1#include "visioncamera.h"
2
3#include <wpi/raw_ostream.h>
4
5#include "cpp-tools/src/resources.h"
6#include "vision.h"
7
8
9VisionCamera::VisionCamera(CS_Source handle) :
10 VideoCamera(handle), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
11{
12 this->raw.SetSource(*this);
13}
14VisionCamera::VisionCamera(const cs::VideoSource& source, const wpi::json& config) :
15 VideoCamera(source.GetHandle()), config(config), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
16{
17 if(this->properties) { this->properties = ::getJsonVideoMode(config); }
18 this->raw.SetSource(*this);
19}
20VisionCamera::VisionCamera(const cs::UsbCamera& source, const wpi::json& config) :
21 VideoCamera(source.GetHandle()), config(config), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
22{
23 if(this->properties) { this->properties = ::getJsonVideoMode(config); }
24 this->raw.SetSource(*this);
25}
26VisionCamera::VisionCamera(const cs::HttpCamera& source, const wpi::json& config) :
27 VideoCamera(source.GetHandle()), config(config), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
28{
29 if(this->properties) { this->properties = ::getJsonVideoMode(config); }
30 this->raw.SetSource(*this);
31}
32VisionCamera::VisionCamera(const wpi::json& source_config, const wpi::json& calibration) :
33 config(source_config), calibration(calibration)
34{
35 cs::UsbCamera cam;
36 try {cam = cs::UsbCamera(source_config.at("name").get<std::string>(), source_config.at("path").get<std::string>());}
37 catch (const wpi::json::exception& e) {
38 wpi::errs() << "Config error in source JSON -> could not read camera name and/or path: " << e.what() << newline;
39 }
40 cam.SetConfigJson(source_config);
41 cam.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
42 // print confirmation
43 swap(*this, cam); // this should work
46 this->raw = cs::CvSink(this->GetName() + "_cvraw");
47 this->raw.SetSource(*this);
48 this->properties = this->GetVideoMode();
49 if(this->properties) { this->properties = ::getJsonVideoMode(config); }
50}
51VisionCamera::VisionCamera(const wpi::json& source_config) :
52 config(source_config)
53{
54 cs::UsbCamera cam;
55 try {cam = cs::UsbCamera(source_config.at("name").get<std::string>(), source_config.at("path").get<std::string>());}
56 catch (const wpi::json::exception& e) {
57 wpi::errs() << "Config error in source JSON -> could not read camera name and/or path: " << e.what() << newline;
58 }
59 cam.SetConfigJson(source_config);
60 cam.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
61 // print confirmation
62 swap(*this, cam); // this should work
63 this->raw = cs::CvSink(this->GetName() + "_cvraw");
64 this->raw.SetSource(*this);
65 this->properties = this->GetVideoMode();
66 if(!this->properties) { this->properties = ::getJsonVideoMode(config); }
67}
68// VisionCamera::VisionCamera(const VisionCamera& other) :
69// VideoCamera(other.GetHandle())/*, type(other.type)*/, config(other.config), calibration(other.calibration),
70// camera(other.camera), brightness(other.brightness), exposure(other.exposure), whitebalance(other.whitebalance) {}
72 VideoCamera(other.GetHandle()),
73 config(std::move(other.config)),
74 calibration(std::move(other.calibration)),
75 raw(std::move(other.raw)),
76 camera_matrix(std::move(other.camera_matrix)),
77 distortion(std::move(other.distortion)),
78 properties(other.properties),
79 ntable(other.ntable),
80 nt_brightness(std::move(other.nt_brightness)),
81 nt_exposure(std::move(other.nt_exposure)),
82 nt_whitebalance(std::move(other.nt_whitebalance)),
83 listener_handle(other.listener_handle)
84{
85 other.m_handle = 0;
86 other.listener_handle = 0;
87}
89 this->ntable->RemoveListener(this->listener_handle);
90}
91
92// VisionCamera& VisionCamera::operator=(const VisionCamera& other) {
93// this->m_handle = other.m_handle;
94// /*this->type = other.type;*/
95// this->config = other.config;
96// this->calibration = other.calibration;
97// this->camera = other.camera;
98// this->brightness = other.brightness;
99// this->exposure = other.exposure;
100// this->whitebalance = other.whitebalance;
101// }
103 this->m_handle = other.m_handle;
104 other.m_handle = 0;
105 this->config = std::move(other.config);
106 this->calibration = std::move(other.calibration);
107 this->raw = std::move(other.raw);
108 this->camera_matrix = std::move(other.camera_matrix);
109 this->distortion = std::move(other.distortion);
110 this->properties = std::move(other.properties);
111 this->ntable = other.ntable;
112 this->nt_brightness = std::move(other.nt_brightness);
113 this->nt_whitebalance = std::move(other.nt_whitebalance);
114 this->nt_exposure = std::move(other.nt_exposure);
115 this->listener_handle = other.listener_handle;
116 other.listener_handle = 0;
117 return *this;
118}
119
120// cs::VideoSource::Kind VisionCamera::getType() const {
121// return this->type;
122// }
124 return this->config.is_object();
125}
126const wpi::json& VisionCamera::getJson() const {
127 return this->config;
128}
130 return this->config.count("stream") > 0;
131}
133 if(this->config.count("stream") > 0) {
134 return this->config.at("stream");
135 }
136 return wpi::json();
137}
138
139bool VisionCamera::getJsonCameraMatrix(cv::Mat_<double>& array) const {
140 if(this->calibration.is_object()) {
141 try{
142 wpi::json matx = this->calibration.at("camera_matrix");
143 for(size_t i = 0; i < 3; i++) {
144 for(size_t j = 0; j < 3; j++) {
145 array[i][j] = matx.at(i).at(j).get<double>();
146 }
147 }
148 } catch (const wpi::json::exception& e) {
149 wpi::errs() << "Failed to parse camera matrix: " << e.what() << newline;
150 }
151 return true;
152 }
153 return false;
154}
155bool VisionCamera::getJsonCameraMatrix(cv::Mat_<float>& array) const {
156 if(this->calibration.is_object()) {
157 try{
158 wpi::json matx = this->calibration.at("camera_matrix");
159 for(size_t i = 0; i < 3; i++) {
160 for(size_t j = 0; j < 3; j++) {
161 array[i][j] = matx.at(i).at(j).get<float>();
162 }
163 }
164 } catch (const wpi::json::exception& e) {
165 wpi::errs() << "Failed to parse camera matrix: " << e.what() << newline;
166 }
167 return true;
168 }
169 return false;
170}
171bool VisionCamera::getJsonDistortionCoefs(cv::Mat_<double>& array) const {
172 if(this->calibration.is_object()) {
173 try{
174 wpi::json matx = this->calibration.at("distortion");
175 for(size_t i = 0; i < 5; i++) {
176 array[0][i] = matx.at(0).at(i).get<double>();
177 }
178 } catch (const wpi::json::exception& e) {
179 wpi::errs() << "Failed to parse camera matrix: " << e.what() << newline;
180 }
181 return true;
182 }
183 return false;
184}
185bool VisionCamera::getJsonDistortionCoefs(cv::Mat_<float>& array) const {
186 if(this->calibration.is_object()) {
187 try{
188 wpi::json matx = this->calibration.at("distortion");
189 for(size_t i = 0; i < 5; i++) {
190 array[0][i] = matx.at(0).at(i).get<float>();
191 }
192 } catch (const wpi::json::exception& e) {
193 wpi::errs() << "Failed to parse camera matrix: " << e.what() << newline;
194 }
195 return true;
196 }
197 return false;
198}
199
200bool VisionCamera::setCalibrationJson(const wpi::json& j) {
201 if(!j.is_object()) {
202 return false;
203 }
204 this->calibration = j;
205 return
206 this->getJsonCameraMatrix(this->camera_matrix) &&
208}
209bool VisionCamera::setCameraMatrix(const cv::Mat_<float>& mat) {
210 if(mat.size().area() != default_matrix.size().area()) {
211 return false;
212 }
213 this->camera_matrix = mat.reshape(0, 3);
214 return true;
215}
216bool VisionCamera::setDistortionCoefs(const cv::Mat_<float>& mat) {
217 if(mat.size().area() != default_distort.size().area()) {
218 return false;
219 }
220 this->distortion = mat.reshape(0, 1);
221 return true;
222}
223
224uint64_t VisionCamera::getFrame(cv::Mat& o_frame, double timeout) const {
225 if(this->IsConnected()) {
226 return this->raw.GrabFrame(o_frame, timeout);
227 } else {
228 o_frame = cv::Mat::zeros(this->getResolution(), CV_8UC3);
229 return 0;
230 }
231}
232uint64_t VisionCamera::getFrameNoTmO(cv::Mat& o_frame) const {
233 if(this->IsConnected()) {
234 return this->raw.GrabFrameNoTimeout(o_frame);
235 } else {
236 o_frame = cv::Mat::zeros(this->getResolution(), CV_8UC3);
237 return 0;
238 }
239}
240
242 return this->properties.width;
243}
245 return this->properties.height;
246}
248 cs::VideoMode mode = this->properties;
249 return (mode.width*mode.height);
250}
252 return this->properties.fps;
253}
255 return cv::Size(this->properties.width, this->properties.height);
256}
257
259 return this->nt_brightness.Get();
260}
262 return this->nt_exposure.Get();
263}
265 return this->nt_whitebalance.Get();
266}
267
269 this->nt_brightness.Set(this->_setBrightness(val));
270}
272 this->nt_whitebalance.Set(this->_setWhiteBalance(val));
273}
275 this->nt_exposure.Set(this->_setExposure(val));
276}
277
278void VisionCamera::setNetworkBase(const std::shared_ptr<nt::NetworkTable>& table) {
279 this->ntable = table->GetSubTable("Cameras")->GetSubTable(this->GetName());
280}
282 this->nt_brightness = this->ntable->GetIntegerTopic("Brightness").GetEntry(50);
283 this->nt_whitebalance = this->ntable->GetIntegerTopic("WhiteBalance").GetEntry(-1);
284 this->nt_exposure = this->ntable->GetIntegerTopic("Exposure").GetEntry(-1);
285 this->setBrightness(50);
286 this->setWhiteBalance(-1);
287 this->setExposure(-1);
288 nt::NetworkTableInstance::GetDefault().RemoveListener(this->listener_handle);
289 this->listener_handle = this->ntable->AddListener(
290 nt::EventFlags::kValueRemote | nt::EventFlags::kTopic,
291 [this](nt::NetworkTable *table, std::string_view key, const nt::Event& e) {
292 if(const nt::ValueEventData* v = e.GetValueEventData()) {
293 NT_Handle h = v->topic;
294 int val = (v->value.IsInteger() ?
295 v->value.GetInteger() : (v->value.IsDouble() ?
296 v->value.GetDouble() : (v->value.IsFloat() ?
297 v->value.GetFloat() : 0xFFFFFFFF) ) );
298 if(val != 0xFFFFFFFF) {
299 if(h == this->nt_brightness.GetTopic().GetHandle()) {
300 this->_setBrightness(val);
301 } else
302 if(h == this->nt_exposure.GetTopic().GetHandle()) {
303 this->_setExposure(val);
304 } else
305 if(h == this->nt_whitebalance.GetTopic().GetHandle()) {
306 this->_setWhiteBalance(val);
307 }
308 }
309 }
310 }
311 );
312}
313
315 val = (val > 100 ? 100 : (val < 0 ? 0 : val));
316 this->SetBrightness(val);
317 return val;
318}
320 val < 0 ? this->SetWhiteBalanceAuto() : this->SetWhiteBalanceManual(val);
321 return val;
322}
324 val = (val > 100 ? 100 : val);
325 val < 0 ? this->SetExposureAuto() : this->SetExposureManual(val);
326 return val;
327}
int getWidth() const
VisionCamera(CS_Source source_handle)
Definition: visioncamera.cpp:9
bool getJsonDistortionCoefs(cv::Mat_< double > &array) const
int getPixels() const
nt::IntegerEntry nt_whitebalance
Definition: visioncamera.h:232
void setNetworkBase(const std::shared_ptr< nt::NetworkTable > &table)
wpi::json getStreamJson() const
uint64_t getFrameNoTmO(cv::Mat &o_frame) const
int _setExposure(int e)
cs::VideoMode properties
Definition: visioncamera.h:227
VisionCamera & operator=(const VisionCamera &)=delete
cv::Size getResolution() const
nt::IntegerEntry nt_brightness
Definition: visioncamera.h:232
wpi::json config
Definition: visioncamera.h:224
NT_Listener listener_handle
Definition: visioncamera.h:233
int getBrightness() const
int _setWhiteBalance(int wb)
nt::IntegerEntry nt_exposure
Definition: visioncamera.h:232
std::shared_ptr< nt::NetworkTable > ntable
Definition: visioncamera.h:229
int getExposure() const
void setBrightness(int b)
bool getJsonCameraMatrix(cv::Mat_< double > &array) const
static const cv::Mat_< float > default_matrix
Definition: visioncamera.h:18
cv::Mat_< float > camera_matrix
Definition: visioncamera.h:226
cs::CvSink raw
Definition: visioncamera.h:225
wpi::json calibration
Definition: visioncamera.h:224
bool isValidJson() const
void setWhiteBalance(int wb)
int getHeight() const
int getConfigFPS() const
int getWhiteBalance() const
const wpi::json & getJson() const
void setNetworkAdjustable()
void setExposure(int e)
cv::Mat_< float > distortion
Definition: visioncamera.h:226
bool isValidStreamJson() const
bool setCalibrationJson(const wpi::json &)
int _setBrightness(int b)
bool setDistortionCoefs(const cv::Mat_< float > &)
bool setCameraMatrix(const cv::Mat_< float > &)
static const cv::Mat_< float > default_distort
Definition: visioncamera.h:19
uint64_t getFrame(cv::Mat &o_frame, double timeout=0.225) const
cs::VideoMode getJsonVideoMode(const wpi::json &config)
Definition: vision.cpp:32