VisionServer  v2.1.1-1-g21dc5465
FRC vision library
vision.cpp
Go to the documentation of this file.
1#include "vision.h"
2
3#include "cpp-tools/src/resources.h"
4
5
6// cs::CvSink switchedCameraVision(const std::vector<VisionCamera>& cameras, std::shared_ptr<nt::NetworkTable> table) {
7// if(!table->ContainsKey("Camera Index")) {
8// table->PutNumber("Camera Index", 0);
9// }
10// if(!table->ContainsKey("Cameras Available")) {
11// table->PutNumber("Cameras Available", cameras.size());
12// }
13// static cs::CvSink source;
14// if(cameras.size() > 0) {
15// source = cameras[0].getSink();
16// }
17// table->GetEntry("Camera Index").AddListener(
18// [&cameras](const nt::EntryNotification& event) {
19// if(event.value->IsDouble()) {
20// size_t idx = event.value->GetDouble();
21// if(idx >= 0 && idx < cameras.size()) {
22// source.SetSource(cameras[idx]);
23// source.SetConfigJson(cameras[idx].getStreamJson());
24// }
25// }
26// },
27// NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE
28// );
29// return source;
30// }
31
32cs::VideoMode getJsonVideoMode(const wpi::json& config) {
33 cs::VideoMode mode;
34
35// the following is copied from: https://github.com/wpilibsuite/allwpilib/blob/main/cscore/src/main/native/cpp/SourceImpl.cpp#L184
36 if (config.count("pixel format") != 0) {
37 try {
38 std::string str = config.at("pixel format").get<std::string>();
39 if (wpi::equals_lower(str, "mjpeg")) {
40 mode.pixelFormat = cs::VideoMode::kMJPEG;
41 } else if (wpi::equals_lower(str, "yuyv")) {
42 mode.pixelFormat = cs::VideoMode::kYUYV;
43 } else if (wpi::equals_lower(str, "rgb565")) {
44 mode.pixelFormat = cs::VideoMode::kRGB565;
45 } else if (wpi::equals_lower(str, "bgr")) {
46 mode.pixelFormat = cs::VideoMode::kBGR;
47 } else if (wpi::equals_lower(str, "gray")) {
48 mode.pixelFormat = cs::VideoMode::kGray;
49 } else {
50 mode.pixelFormat = cs::VideoMode::kUnknown;
51 }
52 } catch (const wpi::json::exception& e) {
53 mode.pixelFormat = cs::VideoMode::kUnknown;
54 }
55 }
56 if (config.count("width") != 0) {
57 try {
58 mode.width = config.at("width").get<unsigned int>();
59 } catch (const wpi::json::exception& e) {
60 mode.width = 0;
61 }
62 }
63 if (config.count("height") != 0) {
64 try {
65 mode.height = config.at("height").get<unsigned int>();
66 } catch (const wpi::json::exception& e) {}
67 }
68 if (config.count("fps") != 0) {
69 try {
70 mode.fps = config.at("fps").get<unsigned int>();
71 } catch (const wpi::json::exception& e) {}
72 }
73 return mode;
74}
75
76// void addNetTableVar(bool& var, const std::string& name, std::shared_ptr<nt::NetworkTable> table) {
77// if(!table->ContainsKey(name)) {
78// table->PutBoolean(name, var);
79// } else {}
80// table->GetEntry(name).AddListener(
81// [&var](const nt::EntryNotification& event){
82// if(event.value->IsBoolean()) {
83// var = event.value->GetBoolean();
84// //std::cout << " Networktable var(bool) updated to : " << var << newline;
85// }
86// },
87// NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE
88// );
89// }
90
91// const std::array<std::array<cv::Scalar, 3>, 3> markup_map = {
92// std::array<cv::Scalar, 3>{cv::Scalar(255, 0, 0), cv::Scalar(255, 127, 0), cv::Scalar(255, 255, 0)}, //blue
93// std::array<cv::Scalar, 3>{cv::Scalar(0, 255, 0), cv::Scalar(0, 255, 127), cv::Scalar(0, 255, 255)}, //green
94// std::array<cv::Scalar, 3>{cv::Scalar(0, 0, 255), cv::Scalar(127, 0, 255), cv::Scalar(255, 0, 255)}, //red
95// };
cs::VideoMode getJsonVideoMode(const wpi::json &config)
Definition: vision.cpp:32