Notes
![]() ![]() Notes - notes.io |
#include <stdlib.h>
#include <iostream>
#include <signal.h>
#include <string>
#include <chrono>
#include <thread>
#include "Lepton3.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio/videoio.hpp>
#include "stopwatch.hpp"
using namespace std;
// ----> Global variables
Lepton3* lepton3 = nullptr;
static bool close = false;
static bool rgb_mode = true;
// <---- Global variables
// ----> Global functions
void close_handler(int s);
void keyboard_handler(int key);
void set_rgb_mode(bool enable);
// <---- Global functions
int main(int argc, char *argv[])
{
cout << "Lepton3 to HDMI via GStreamer pipeline" << std::endl;
// ----> Set Ctrl+C handler
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = close_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
// <---- Set Ctrl+C handler
Lepton3::DebugLvl deb_lvl = Lepton3::DBG_NONE;
lepton3 = new Lepton3("/dev/spidev0.0", "/dev/i2c-1", deb_lvl); // use SPI1 and I2C-1 ports
lepton3->start();
// Set initial data mode
set_rgb_mode(rgb_mode);
// Define GStreamer pipeline for HDMI output
string gstreamer_pipeline = "appsrc ! videoconvert ! nvvidconv ! nvdrmvideosink sync=false set_mode=0";
// Initialize GStreamer VideoWriter
cv::VideoWriter video_writer(gstreamer_pipeline, cv::CAP_GSTREAMER, 0, 30, cv::Size(240, 180), true);
if (!video_writer.isOpened())
{
cerr << "Failed to open GStreamer pipeline for HDMI output." << endl;
delete lepton3;
return -1;
}
uint64_t frameIdx = 0;
uint16_t min;
uint16_t max;
uint8_t w, h;
StopWatch stpWtc;
stpWtc.tic();
while (!close)
{
const uint16_t* data16 = lepton3->getLastFrame16(w, h, &min, &max);
const uint8_t* dataRGB = lepton3->getLastFrameRGB(w, h);
cv::Mat dispFrame;
if (data16 || dataRGB)
{
double period_usec = stpWtc.toc();
stpWtc.tic();
double freq = (1000. * 1000.) / period_usec;
cv::Mat frame16(h, w, CV_16UC1);
cv::Mat frameRGB(h, w, CV_8UC3);
if (rgb_mode && dataRGB)
{
memcpy(frameRGB.data, dataRGB, 3 * w * h * sizeof(uint8_t));
cv::cvtColor(frameRGB, dispFrame, cv::COLOR_RGB2BGR);
}
else if (!rgb_mode && data16)
{
memcpy(frame16.data, data16, w * h * sizeof(uint16_t));
// Rescaling/Normalization to 8bit
double diff = static_cast<double>(max - min); // Image range
double scale = 255. / diff; // Scale factor
frame16 -= min; // Bias
frame16 *= scale; // Rescale data
frame16.convertTo(dispFrame, CV_8UC3);
}
cv::Mat rescaledImg;
cv::resize(dispFrame, rescaledImg, cv::Size(), 3.0, 3.0);
// Write to GStreamer pipeline
video_writer.write(rescaledImg);
frameIdx++;
if (deb_lvl >= Lepton3::DBG_INFO)
{
cout << "> Frame period: " << period_usec << " usec - FPS: " << freq << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(0));
}
video_writer.release();
delete lepton3;
return EXIT_SUCCESS;
}
void close_handler(int s)
{
if (s == 2)
{
cout << std::endl
<< "Ctrl+C pressed..." << std::endl;
close = true;
}
}
void keyboard_handler(int key)
{
switch (key)
{
case 'c': // Set RGB mode
set_rgb_mode(true);
break;
case 'r': // Set radiometry mode
set_rgb_mode(false);
break;
default:
break;
}
}
void set_rgb_mode(bool enable)
{
rgb_mode = enable;
if (lepton3->enableRadiometry(!rgb_mode) < 0)
{
cerr << "Failed to set radiometry status" << std::endl;
}
if (lepton3->enableAgc(rgb_mode) < 0)
{
cerr << "Failed to set AGC status" << std::endl;
}
if (lepton3->enableRgbOutput(rgb_mode) < 0)
{
cerr << "Failed to enable RGB output" << std::endl;
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team