Skip to content

Instantly share code, notes, and snippets.

@oto313
Last active April 29, 2021 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oto313/0f7b3017796d1dba1cee5d7846b33b74 to your computer and use it in GitHub Desktop.
Save oto313/0f7b3017796d1dba1cee5d7846b33b74 to your computer and use it in GitHub Desktop.
class DepthAiCamera : public Camera{
private:
std::shared_ptr<dai::node::ColorCamera> color_camera;
std::shared_ptr<dai::node::VideoEncoder> encoder;
std::shared_ptr<dai::node::XLinkOut> video_out;
std::shared_ptr<dai::node::XLinkIn> control;
std::shared_ptr<dai::DataInputQueue> control_queue_in;
std::shared_ptr<dai::DataOutputQueue> queue_out;
std::unique_ptr<dai::Device> device;
std::unique_ptr<dai::Pipeline> pipeline;
std::string device_id;
bool opened = false;
public:
virtual bool start(){
if(opened){
gst_print("already opened\n");
return true;
}
gst_print("starting\n");
if(width > 3840 || height > 2160){
return false;
}
opened = true;
device_id = std::string();
get_deviceid(); // force to load device id
pipeline = std::make_unique<dai::Pipeline>();
color_camera = pipeline->create<dai::node::ColorCamera>();
encoder = pipeline->create<dai::node::VideoEncoder>();
video_out = pipeline->create<dai::node::XLinkOut>();
control = pipeline->create<dai::node::XLinkIn>();
control->setStreamName("ctrl");
video_out->setStreamName("h264");
color_camera->setInterleaved(true);
color_camera->setImageOrientation(dai::CameraImageOrientation::ROTATE_180_DEG);
color_camera->setBoardSocket(dai::CameraBoardSocket::RGB);
if(width > 1920 || height > 1080){
color_camera->setResolution(dai::ColorCameraProperties::SensorResolution::THE_4_K);
}else{
color_camera->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P);
}
color_camera->setVideoSize(width, height);
color_camera->setFps(fps);
// color_camera->initialControl.setAutoWhiteBalanceMode(dai::CameraControl::AutoWhiteBalanceMode::SHADE);
color_camera->initialControl.setAutoFocusMode(dai::CameraControl::AutoFocusMode::OFF);
encoder->setDefaultProfilePreset(width, height, fps, dai::VideoEncoderProperties::Profile::H264_MAIN);
color_camera->video.link(encoder->input);
control->out.link(color_camera->inputControl);
encoder->bitstream.link(video_out->input);
device = std::make_unique<dai::Device>(*pipeline);
device->setLogOutputLevel(dai::LogLevel::TRACE);
device->setLogLevel(dai::LogLevel::TRACE);
queue_out = device->getOutputQueue("h264");
control_queue_in = device->getInputQueue("ctrl");
try{
device->startPipeline();
}catch (std::exception& e) {
cout << "An exception occurred. Exception Nr. " << e.what() << '\n';
return false;
}
return true;
}
virtual void stop(){
opened = false;
gst_print("stopping\n");
if(device){
device->close();
device = nullptr;
}
}
...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment