rgb_camera_control.cpp
Go to the documentation of this file.
1 
15 #include <iostream>
16 
17 #include "utility.hpp"
18 
19 // Includes common necessary includes for development using depthai library
20 #include "depthai/depthai.hpp"
21 
22 // Step size ('W','A','S','D' controls)
23 static constexpr int STEP_SIZE = 8;
24 
25 // Manual exposure/focus set step
26 static constexpr int EXP_STEP = 500; // us
27 static constexpr int ISO_STEP = 50;
28 static constexpr int LENS_STEP = 3;
29 static constexpr int WB_STEP = 200;
30 
31 static int clamp(int num, int v0, int v1) {
32  return std::max(v0, std::min(num, v1));
33 }
34 
35 int main() {
36  // Create pipeline
37  dai::Pipeline pipeline;
38 
39  // Define sources and outputs
40  auto camRgb = pipeline.create<dai::node::ColorCamera>();
41  auto videoEncoder = pipeline.create<dai::node::VideoEncoder>();
42  auto stillEncoder = pipeline.create<dai::node::VideoEncoder>();
43 
44  auto controlIn = pipeline.create<dai::node::XLinkIn>();
45  auto configIn = pipeline.create<dai::node::XLinkIn>();
46  auto videoMjpegOut = pipeline.create<dai::node::XLinkOut>();
47  auto stillMjpegOut = pipeline.create<dai::node::XLinkOut>();
48  auto previewOut = pipeline.create<dai::node::XLinkOut>();
49 
50  controlIn->setStreamName("control");
51  configIn->setStreamName("config");
52  videoMjpegOut->setStreamName("video");
53  stillMjpegOut->setStreamName("still");
54  previewOut->setStreamName("preview");
55 
56  // Properties
57  camRgb->setVideoSize(640, 360);
58  camRgb->setPreviewSize(300, 300);
59  videoEncoder->setDefaultProfilePreset(camRgb->getFps(), dai::VideoEncoderProperties::Profile::MJPEG);
61 
62  // Linking
63  camRgb->video.link(videoEncoder->input);
64  camRgb->still.link(stillEncoder->input);
65  camRgb->preview.link(previewOut->input);
66  controlIn->out.link(camRgb->inputControl);
67  configIn->out.link(camRgb->inputConfig);
68  videoEncoder->bitstream.link(videoMjpegOut->input);
69  stillEncoder->bitstream.link(stillMjpegOut->input);
70 
71  // Connect to device and start pipeline
72  dai::Device device(pipeline);
73 
74  // Get data queues
75  auto controlQueue = device.getInputQueue("control");
76  auto configQueue = device.getInputQueue("config");
77  auto previewQueue = device.getOutputQueue("preview");
78  auto videoQueue = device.getOutputQueue("video");
79  auto stillQueue = device.getOutputQueue("still");
80 
81  // Max cropX & cropY
82  float maxCropX = (camRgb->getResolutionWidth() - camRgb->getVideoWidth()) / (float)camRgb->getResolutionWidth();
83  float maxCropY = (camRgb->getResolutionHeight() - camRgb->getVideoHeight()) / (float)camRgb->getResolutionHeight();
84 
85  // Default crop
86  float cropX = 0;
87  float cropY = 0;
88  bool sendCamConfig = true;
89 
90  // Defaults and limits for manual focus/exposure controls
91  int lensPos = 150;
92  int lensMin = 0;
93  int lensMax = 255;
94 
95  int expTime = 20000;
96  int expMin = 1;
97  int expMax = 33000;
98 
99  int sensIso = 800;
100  int sensMin = 100;
101  int sensMax = 1600;
102 
103  int wbManual = 4000;
104  int wbMin = 1000;
105  int wbMax = 12000;
106 
107  while(true) {
108  auto previewFrames = previewQueue->tryGetAll<dai::ImgFrame>();
109  for(const auto& previewFrame : previewFrames) {
110  cv::Mat frame(previewFrame->getHeight(), previewFrame->getWidth(), CV_8UC3, previewFrame->getData().data());
111  cv::imshow("preview", frame);
112  }
113 
114  auto videoFrames = videoQueue->tryGetAll<dai::ImgFrame>();
115  for(const auto& videoFrame : videoFrames) {
116  // Decode JPEG
117  auto frame = cv::imdecode(videoFrame->getData(), cv::IMREAD_UNCHANGED);
118  // Display
119  cv::imshow("video", frame);
120 
121  // Send new cfg to camera
122  if(sendCamConfig) {
124  cfg.setCropRect(cropX, cropY, 0, 0);
125  configQueue->send(cfg);
126  printf("Sending new crop - x: %f, y: %f\n", cropX, cropY);
127  sendCamConfig = false;
128  }
129  }
130 
131  auto stillFrames = stillQueue->tryGetAll<dai::ImgFrame>();
132  for(const auto& stillFrame : stillFrames) {
133  // Decode JPEG
134  auto frame = cv::imdecode(stillFrame->getData(), cv::IMREAD_UNCHANGED);
135  // Display
136  cv::imshow("still", frame);
137  }
138 
139  // Update screen (1ms pooling rate)
140  int key = cv::waitKey(1);
141  if(key == 'q') {
142  break;
143  } else if(key == 'c') {
144  dai::CameraControl ctrl;
145  ctrl.setCaptureStill(true);
146  controlQueue->send(ctrl);
147  } else if(key == 't') {
148  printf("Autofocus trigger (and disable continuous)\n");
149  dai::CameraControl ctrl;
151  ctrl.setAutoFocusTrigger();
152  controlQueue->send(ctrl);
153  } else if(key == 'f') {
154  printf("Autofocus enable, continuous\n");
155  dai::CameraControl ctrl;
157  controlQueue->send(ctrl);
158  } else if(key == 'e') {
159  printf("Autoexposure enable\n");
160  dai::CameraControl ctrl;
161  ctrl.setAutoExposureEnable();
162  controlQueue->send(ctrl);
163  } else if(key == 'b') {
164  printf("Auto white-balance enable\n");
165  dai::CameraControl ctrl;
167  controlQueue->send(ctrl);
168  } else if(key == ',' || key == '.') {
169  if(key == ',') lensPos -= LENS_STEP;
170  if(key == '.') lensPos += LENS_STEP;
171  lensPos = clamp(lensPos, lensMin, lensMax);
172  printf("Setting manual focus, lens position: %d\n", lensPos);
173  dai::CameraControl ctrl;
174  ctrl.setManualFocus(lensPos);
175  controlQueue->send(ctrl);
176  } else if(key == 'i' || key == 'o' || key == 'k' || key == 'l') {
177  if(key == 'i') expTime -= EXP_STEP;
178  if(key == 'o') expTime += EXP_STEP;
179  if(key == 'k') sensIso -= ISO_STEP;
180  if(key == 'l') sensIso += ISO_STEP;
181  expTime = clamp(expTime, expMin, expMax);
182  sensIso = clamp(sensIso, sensMin, sensMax);
183  printf("Setting manual exposure, time: %d, iso: %d\n", expTime, sensIso);
184  dai::CameraControl ctrl;
185  ctrl.setManualExposure(expTime, sensIso);
186  controlQueue->send(ctrl);
187  } else if(key == '[' || key == ']') {
188  if(key == '[') wbManual -= WB_STEP;
189  if(key == ']') wbManual += WB_STEP;
190  wbManual = clamp(wbManual, wbMin, wbMax);
191  printf("Setting manual white balance, temperature: %d K\n", wbManual);
192  dai::CameraControl ctrl;
193  ctrl.setManualWhiteBalance(wbManual);
194  controlQueue->send(ctrl);
195  } else if(key == 'w' || key == 'a' || key == 's' || key == 'd') {
196  if(key == 'a') {
197  cropX -= (maxCropX / camRgb->getResolutionWidth()) * STEP_SIZE;
198  if(cropX < 0) cropX = maxCropX;
199  } else if(key == 'd') {
200  cropX += (maxCropX / camRgb->getResolutionWidth()) * STEP_SIZE;
201  if(cropX > maxCropX) cropX = 0.0f;
202  } else if(key == 'w') {
203  cropY -= (maxCropY / camRgb->getResolutionHeight()) * STEP_SIZE;
204  if(cropY < 0) cropY = maxCropY;
205  } else if(key == 's') {
206  cropY += (maxCropY / camRgb->getResolutionHeight()) * STEP_SIZE;
207  if(cropY > maxCropY) cropY = 0.0f;
208  }
209  sendCamConfig = true;
210  }
211  }
212  return 0;
213 }
STEP_SIZE
static constexpr int STEP_SIZE
Definition: rgb_camera_control.cpp:23
dai::node::XLinkIn::out
Output out
Definition: XLinkIn.hpp:25
dai::node::VideoEncoder::bitstream
Output bitstream
Definition: VideoEncoder.hpp:30
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
dai::CameraControl::setAutoWhiteBalanceMode
CameraControl & setAutoWhiteBalanceMode(AutoWhiteBalanceMode mode)
Definition: CameraControl.cpp:150
ISO_STEP
static constexpr int ISO_STEP
Definition: rgb_camera_control.cpp:27
dai::ImageManipConfig::setCropRect
ImageManipConfig & setCropRect(float xmin, float ymin, float xmax, float ymax)
Definition: ImageManipConfig.cpp:18
dai::RawCameraControl::AutoFocusMode::CONTINUOUS_VIDEO
@ CONTINUOUS_VIDEO
sendCamConfig
static std::atomic< bool > sendCamConfig
Definition: mono_camera_control.cpp:26
main
int main()
Definition: rgb_camera_control.cpp:35
dai::CameraControl::setManualExposure
CameraControl & setManualExposure(uint32_t exposureTimeUs, uint32_t sensitivityIso)
Definition: CameraControl.cpp:137
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::CameraControl
Definition: CameraControl.hpp:33
dai::CameraControl::setAutoFocusTrigger
CameraControl & setAutoFocusTrigger()
Definition: CameraControl.cpp:69
clamp
static int clamp(int num, int v0, int v1)
Definition: rgb_camera_control.cpp:31
dai::CameraControl::setManualFocus
CameraControl & setManualFocus(uint8_t lensPosition)
Definition: CameraControl.cpp:88
WB_STEP
static constexpr int WB_STEP
Definition: rgb_camera_control.cpp:29
dai::VideoEncoderProperties::Profile::MJPEG
@ MJPEG
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::node::VideoEncoder::setDefaultProfilePreset
void setDefaultProfilePreset(float fps, Properties::Profile profile)
dai::node::VideoEncoder::out
Output out
Definition: VideoEncoder.hpp:35
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::XLinkIn::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkIn.cpp:12
dai::CameraControl::setManualWhiteBalance
CameraControl & setManualWhiteBalance(int colorTemperatureK)
Definition: CameraControl.cpp:160
LENS_STEP
static constexpr int LENS_STEP
Definition: rgb_camera_control.cpp:28
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::RawCameraControl::AutoWhiteBalanceMode::AUTO
@ AUTO
dai::Device
Definition: Device.hpp:21
dai::CameraControl::setAutoExposureEnable
CameraControl & setAutoExposureEnable()
Definition: CameraControl.cpp:101
EXP_STEP
static constexpr int EXP_STEP
Definition: rgb_camera_control.cpp:26
dai::node::VideoEncoder
VideoEncoder node. Encodes frames into MJPEG, H264 or H265.
Definition: VideoEncoder.hpp:14
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::ImageManipConfig
Definition: ImageManipConfig.hpp:23
dai::RawCameraControl::AutoFocusMode::AUTO
@ AUTO
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
dai::node::VideoEncoder::input
Input input
Definition: VideoEncoder.hpp:25
dai::CameraControl::setCaptureStill
CameraControl & setCaptureStill(bool capture)
Definition: CameraControl.cpp:14
utility.hpp
dai::CameraControl::setAutoFocusMode
CameraControl & setAutoFocusMode(AutoFocusMode mode)
Definition: CameraControl.cpp:64


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19