7#include <initializer_list>
9#include <opencv2/opencv.hpp>
10#include <networktables/NetworkTable.h>
12#include "cpp-tools/src/types.h"
22 inline static const std::shared_ptr<nt::NetworkTable>&
ntable() {
23 static std::shared_ptr<nt::NetworkTable> base{nt::NetworkTableInstance::GetDefault().GetTable(
"Vision Server")};
31 inline static void Init() {
41 inline static const std::shared_ptr<nt::NetworkTable>&
ntable() {
42 static std::shared_ptr<nt::NetworkTable> pipes{
VisionServer::ntable()->GetSubTable(
"Pipelines")};
46 inline const std::string&
getName()
const {
return this->
name; }
47 inline const std::shared_ptr<nt::NetworkTable>&
getTable()
const {
return this->
table; }
49 virtual void process(cv::Mat& io_frame) = 0;
50 inline virtual void init() {}
81 const std::shared_ptr<nt::NetworkTable>
table;
88 static bool addCameras(std::vector<VisionCamera>&&);
89 static bool setCameras(std::vector<VisionCamera>&&);
90 static std::vector<VisionCamera>&
getCameras();
93 template<
class pipeline>
99 static bool addPipelines(std::initializer_list<BasePipe*>);
103 static bool setPipelines(std::initializer_list<BasePipe*>);
109 static bool addStream(std::string_view,
int port);
110 static bool addStream(
const cs::MjpegServer&);
111 static bool addStream(cs::MjpegServer&&);
113 static bool addStreams(std::initializer_list<std::string_view>);
114 static bool addStreams(std::initializer_list<std::pair<std::string_view, int> >);
115 static bool addStreams(
const std::vector<cs::MjpegServer>&);
116 static const std::vector<OutputStream>&
getStreams();
123 static bool run(
float fps_cap = 30.f);
124 static bool runSingle(
float fps_cap = 30.f);
125 static bool runThread(
float fps_cap = 30.f);
132 template<
class pipeline = void,
class...
pipelines>
133 static size_t pipeExpander(std::vector<std::unique_ptr<BasePipe> >&);
160 inline static const std::shared_ptr<nt::NetworkTable>&
ntable() {
184 const std::shared_ptr<nt::NetworkTable>
table;
196template<
class derived>
203 Instanced<
This_t>(),
BasePipe(
name + (
"/Instance " + std::to_string(this->instance))) {}
205 Instanced<
This_t>(),
BasePipe(
name +
"/Instance " + std::to_string(this->instance)) {}
211 inline virtual void process(cv::Mat& io_frame)
override {}
216template<
class... pipelines_t>
223 for(
size_t i = 0; i < this->
heap_ptrs.size(); i++) {
229 void addPipelines(std::vector<VisionServer::BasePipe*>&&);
230 void addPipelines(std::initializer_list<VisionServer::BasePipe*>);
232 virtual void process(cv::Mat& io_frame)
override;
235 static std::string
Construct(std::vector<std::unique_ptr<VisionServer::BasePipe> >&);
238 std::vector<std::unique_ptr<VisionServer::BasePipe> >
heap_ptrs;
258#include <type_traits>
272template<
class pipeline>
274 static_assert(std::is_base_of<VisionServer::BasePipe, pipeline>::value,
"Template argument (pipeline) must inherit from BasePipe");
275 static_assert(std::is_default_constructible<pipeline>::value,
"Template arguement (pipeline) must be default constructible");
284template<
class... pipelines_t>
291 for(
size_t i = alloc; i >= 0; --i) {
298template<
class... pipelines_t>
305 _inst.
pipelines.reserve(
sizeof...(pipelines_t));
315template<
class pipeline,
class... pipelines>
317 if constexpr(!std::is_same<pipeline, void>::value) {
318 static_assert(std::is_base_of<VisionServer::BasePipe, pipeline>::value,
"Template argument (pipeline) must inherit from BasePipe");
319 static_assert(std::is_default_constructible<pipeline>::value,
"Template arguement (pipeline) must be default constructible");
321 pipes.emplace_back(std::make_unique<pipeline>());
330template<
class... pipelines_t>
332 if(VisionServer::pipeExpander<pipelines_t...>(pipes) < 1) {
335 std::stringstream ret(pipes.at(0)->getName());
336 for(
size_t i = 1; i < pipes.size(); i++) {
337 ret <<
", " << pipes.at(i)->getName();
343template<
class... pipelines_t>
345 this->pipelines.push_back(p);
347template<
class... pipelines_t>
349 this->pipelines.insert(this->pipelines.end(), ps.begin(), ps.end());
351template<
class... pipelines_t>
353 this->pipelines.insert(this->pipelines.end(), ps.begin(), ps.end());
355template<
class... pipelines_t>
357 for(
size_t i = 0; i < this->pipelines.size(); i++) {
358 this->pipelines.at(i)->process(io_frame);
static const cv::Mat_< float > default_matrix
static const cv::Mat_< float > default_distort
virtual void process(cv::Mat &io_frame) override
void addPipeline(VisionServer::BasePipe *)
void addPipelines(std::vector< VisionServer::BasePipe * > &&)
static std::string Construct(std::vector< std::unique_ptr< VisionServer::BasePipe > > &)
std::vector< std::unique_ptr< VisionServer::BasePipe > > heap_ptrs
std::vector< VisionServer::BasePipe * > pipelines
virtual ~VPipeline()=default
VPipeline(const std::string &name)
virtual void process(cv::Mat &io_frame) override
VPipeline(const VPipeline &)=delete
VPipeline(std::string &&name)
VPipeline(const char *name)
const cv::Mat1f & getSrcDistort() const
const std::string & getName() const
BasePipe(const std::string &name)
const cv::Mat1f & getSrcMatrix() const
const cv::Mat_< float > * src_distort
void setSource(const cs::VideoSource &)
BasePipe(const char *name)
const std::shared_ptr< nt::NetworkTable > table
BasePipe(std::string &&name)
const std::shared_ptr< nt::NetworkTable > & getTable() const
void setCamera(const VisionCamera &)
virtual void process(cv::Mat &io_frame)=0
void setPipeline(const BasePipe &)
const cv::Mat_< float > * src_matrix
static const std::shared_ptr< nt::NetworkTable > & ntable()
static VisionServer & inst()
static size_t numCameras()
static size_t numPipelines()
static bool addPipeline()
void operator=(VisionServer &&)=delete
static const std::vector< BasePipe * > & getPipelines()
static bool setPipelines()
static const std::shared_ptr< nt::NetworkTable > & ntable()
std::vector< std::unique_ptr< BasePipe > > heap_allocated
static const std::vector< OutputStream > & getStreams()
static size_t numStreams()
void operator=(const VisionServer &)=delete
static bool runSingleThread(float fps_cap=30.f)
VisionServer(const VisionServer &)=delete
static bool addCameras(std::vector< VisionCamera > &&)
static bool run(float fps_cap=30.f)
static size_t pipeExpander(std::vector< std::unique_ptr< BasePipe > > &)
static bool runThread(float fps_cap=30.f)
static bool addStreams(size_t=2)
std::vector< VisionCamera > cameras
VisionServer(VisionServer &&)=delete
static void pipelineRunner(BasePipe *, float fps_cap)
static VisionServer & getInstance()
static std::vector< VisionCamera > & getCameras()
std::atomic< bool > is_running
static bool addPipelines()
std::vector< BasePipe * > pipelines
static bool addCamera(VisionCamera &&)
static bool runRawThread()
static bool runSingle(float fps_cap=30.f)
static bool setCameras(std::vector< VisionCamera > &&)
std::vector< OutputStream > streams
struct VPipeline_S BasePipe
SequentialPipeline SeqPipeline
static const std::shared_ptr< nt::NetworkTable > & ntable()
const std::shared_ptr< nt::NetworkTable > table
OutputStream(cs::MjpegServer &&s)
void setCameraIdx(uint16_t i)
OutputStream(const OutputStream &o)
OutputStream(std::string_view n, int p)
std::atomic_int local_idx
void setPipelineIdx(uint16_t i)
OutputStream(std::string_view n)
OutputStream(OutputStream &&o)