3#include <wpi/raw_ostream.h>
5#include "cpp-tools/src/resources.h"
10 VideoCamera(handle), raw(this->GetName() +
"_cvraw"), properties(this->GetVideoMode())
12 this->
raw.SetSource(*
this);
15 VideoCamera(source.GetHandle()), config(config), raw(this->GetName() +
"_cvraw"), properties(this->GetVideoMode())
18 this->
raw.SetSource(*
this);
21 VideoCamera(source.GetHandle()), config(config), raw(this->GetName() +
"_cvraw"), properties(this->GetVideoMode())
24 this->
raw.SetSource(*
this);
27 VideoCamera(source.GetHandle()), config(config), raw(this->GetName() +
"_cvraw"), properties(this->GetVideoMode())
30 this->
raw.SetSource(*
this);
33 config(source_config), calibration(calibration)
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;
40 cam.SetConfigJson(source_config);
41 cam.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
46 this->
raw = cs::CvSink(this->GetName() +
"_cvraw");
47 this->
raw.SetSource(*
this);
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;
59 cam.SetConfigJson(source_config);
60 cam.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
63 this->
raw = cs::CvSink(this->GetName() +
"_cvraw");
64 this->
raw.SetSource(*
this);
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),
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)
86 other.listener_handle = 0;
103 this->m_handle = other.m_handle;
105 this->
config = std::move(other.config);
107 this->
raw = std::move(other.raw);
109 this->
distortion = std::move(other.distortion);
110 this->
properties = std::move(other.properties);
111 this->
ntable = other.ntable;
116 other.listener_handle = 0;
124 return this->
config.is_object();
130 return this->
config.count(
"stream") > 0;
133 if(this->
config.count(
"stream") > 0) {
134 return this->
config.at(
"stream");
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>();
148 }
catch (
const wpi::json::exception& e) {
149 wpi::errs() <<
"Failed to parse camera matrix: " << e.what() << newline;
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>();
164 }
catch (
const wpi::json::exception& e) {
165 wpi::errs() <<
"Failed to parse camera matrix: " << e.what() << newline;
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>();
178 }
catch (
const wpi::json::exception& e) {
179 wpi::errs() <<
"Failed to parse camera matrix: " << e.what() << newline;
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>();
192 }
catch (
const wpi::json::exception& e) {
193 wpi::errs() <<
"Failed to parse camera matrix: " << e.what() << newline;
225 if(this->IsConnected()) {
226 return this->
raw.GrabFrame(o_frame, timeout);
233 if(this->IsConnected()) {
234 return this->
raw.GrabFrameNoTimeout(o_frame);
249 return (mode.width*mode.height);
255 return cv::Size(this->
properties.width, this->properties.height);
279 this->
ntable = table->GetSubTable(
"Cameras")->GetSubTable(this->GetName());
288 nt::NetworkTableInstance::GetDefault().RemoveListener(this->
listener_handle);
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);
302 if(h == this->nt_exposure.GetTopic().GetHandle()) {
303 this->_setExposure(val);
305 if(h == this->nt_whitebalance.GetTopic().GetHandle()) {
306 this->_setWhiteBalance(val);
315 val = (val > 100 ? 100 : (val < 0 ? 0 : val));
316 this->SetBrightness(val);
320 val < 0 ? this->SetWhiteBalanceAuto() : this->SetWhiteBalanceManual(val);
324 val = (val > 100 ? 100 : val);
325 val < 0 ? this->SetExposureAuto() : this->SetExposureManual(val);
VisionCamera(CS_Source source_handle)
bool getJsonDistortionCoefs(cv::Mat_< double > &array) const
nt::IntegerEntry nt_whitebalance
void setNetworkBase(const std::shared_ptr< nt::NetworkTable > &table)
wpi::json getStreamJson() const
uint64_t getFrameNoTmO(cv::Mat &o_frame) const
VisionCamera & operator=(const VisionCamera &)=delete
cv::Size getResolution() const
nt::IntegerEntry nt_brightness
NT_Listener listener_handle
int getBrightness() const
int _setWhiteBalance(int wb)
nt::IntegerEntry nt_exposure
std::shared_ptr< nt::NetworkTable > ntable
void setBrightness(int b)
bool getJsonCameraMatrix(cv::Mat_< double > &array) const
static const cv::Mat_< float > default_matrix
cv::Mat_< float > camera_matrix
void setWhiteBalance(int wb)
int getWhiteBalance() const
const wpi::json & getJson() const
void setNetworkAdjustable()
cv::Mat_< float > distortion
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
uint64_t getFrame(cv::Mat &o_frame, double timeout=0.225) const
cs::VideoMode getJsonVideoMode(const wpi::json &config)