46 #include <visp3/core/vpConfig.h>
48 #if defined(VISP_HAVE_V4L2) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK)) && defined(VISP_HAVE_THREADS)
50 #include <condition_variable>
57 #include <visp3/core/vpDisplay.h>
58 #include <visp3/core/vpImageFilter.h>
59 #include <visp3/core/vpIoTools.h>
60 #include <visp3/core/vpTime.h>
61 #include <visp3/gui/vpDisplayGTK.h>
62 #include <visp3/gui/vpDisplayX.h>
63 #include <visp3/io/vpParseArgv.h>
64 #include <visp3/io/vpVideoWriter.h>
65 #include <visp3/sensor/vpV4l2Grabber.h>
67 #define GETOPTARGS "d:oh"
69 #ifdef ENABLE_VISP_NAMESPACE
76 void usage(
const char *name,
const char *badparam)
80 %s [-d <device count>] [-o] [-h]\n\
83 Capture multiple camera streams and save the stream without slowing down the acquisition.\n\
87 Open the specified number of camera streams.\n\
90 Save each stream in a dedicated folder.\n\
97 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
100 bool getOptions(
int argc,
char **argv,
unsigned int &deviceCount,
bool &saveVideo)
103 const char **argv1 = (
const char **)argv;
109 deviceCount = (
unsigned int)atoi(optarg);
115 usage(argv[0],
nullptr);
120 usage(argv[0], optarg);
126 if ((c == 1) || (c == -1)) {
128 usage(argv[0],
nullptr);
129 std::cerr <<
"ERROR: " << std::endl;
130 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
137 #ifndef DOXYGEN_SHOULD_SKIP_THIS
148 : m_cancelled(false), m_cond(), m_queueColor(), m_maxQueueSize(std::numeric_limits<size_t>::max()), m_mutex()
153 std::lock_guard<std::mutex> lock(m_mutex);
161 std::lock_guard<std::mutex> lock(m_mutex);
163 m_queueColor.push(image);
166 while (m_queueColor.size() > m_maxQueueSize) {
176 std::unique_lock<std::mutex> lock(m_mutex);
178 while (m_queueColor.empty()) {
180 throw vpCancelled_t();
186 throw vpCancelled_t();
196 void setMaxQueueSize(
const size_t max_queue_size) { m_maxQueueSize = max_queue_size; }
200 std::condition_variable m_cond;
201 std::queue<vpImage<vpRGBa> > m_queueColor;
202 size_t m_maxQueueSize;
206 class vpStorageWorker
209 vpStorageWorker(vpFrameQueue &queue,
const std::string &filename,
unsigned int width,
unsigned int height)
210 : m_queue(queue), m_filename(filename), m_width(width), m_height(height)
219 if (!m_filename.empty()) {
221 writer.
open(O_color);
228 if (!m_filename.empty()) {
233 catch (vpFrameQueue::vpCancelled_t &) {
238 vpFrameQueue &m_queue;
239 std::string m_filename;
240 unsigned int m_width;
241 unsigned int m_height;
249 std::condition_variable m_cond;
251 unsigned char *m_pImgData;
252 unsigned int m_totalSize;
258 vpShareImage() : m_cancelled(false), m_cond(), m_mutex(), m_pImgData(nullptr), m_totalSize(0) { }
260 virtual ~vpShareImage()
262 if (m_pImgData !=
nullptr) {
269 std::lock_guard<std::mutex> lock(m_mutex);
275 void getImage(
unsigned char *
const imageData,
const unsigned int totalSize)
277 std::unique_lock<std::mutex> lock(m_mutex);
280 throw vpCancelled_t();
286 throw vpCancelled_t();
290 if (totalSize <= m_totalSize) {
291 memcpy(imageData, m_pImgData, totalSize *
sizeof(
unsigned char));
294 std::cerr <<
"totalSize <= m_totalSize !" << std::endl;
300 std::lock_guard<std::mutex> lock(m_mutex);
305 void setImage(
const unsigned char *
const imageData,
const unsigned int totalSize)
307 std::lock_guard<std::mutex> lock(m_mutex);
309 if (m_pImgData ==
nullptr || m_totalSize != totalSize) {
310 m_totalSize = totalSize;
312 if (m_pImgData !=
nullptr) {
316 m_pImgData =
new unsigned char[m_totalSize];
320 memcpy(m_pImgData, imageData, m_totalSize *
sizeof(
unsigned char));
326 void capture(
vpV4l2Grabber *
const pGrabber, vpShareImage &share_image)
331 pGrabber->
open(local_img);
334 if (share_image.isCancelled()) {
341 share_image.setImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
345 void display(
unsigned int width,
unsigned int height,
int win_x,
int win_y,
unsigned int deviceId,
346 vpShareImage &share_image, vpFrameQueue &queue,
bool save)
350 #if defined(VISP_HAVE_X11)
352 #elif defined(VISP_HAVE_GTK)
358 std::stringstream ss;
359 ss <<
"Camera stream " << deviceId;
360 display.init(local_img, win_x, win_y, ss.str());
366 vpImage<unsigned char> I_red(height, width), I_green(height, width), I_blue(height, width), I_alpha(height, width);
368 I_blue_gaussian(height, width);
369 vpImage<double> I_red_gaussian_double, I_green_gaussian_double, I_blue_gaussian_double;
371 bool exit =
false, gaussian_blur =
false;
376 share_image.getImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
398 std::stringstream ss;
399 ss <<
"Time: " << t <<
" ms";
409 queue.push(local_img);
415 gaussian_blur = !gaussian_blur;
425 catch (vpShareImage::vpCancelled_t &) {
426 std::cout <<
"Cancelled!" << std::endl;
429 share_image.cancel();
435 int main(
int argc,
char *argv[])
437 unsigned int deviceCount = 1;
438 unsigned int cameraScale = 1;
439 bool saveVideo =
false;
442 if (!getOptions(argc, argv, deviceCount, saveVideo)) {
446 std::vector<vpV4l2Grabber *> grabbers;
448 const unsigned int offsetX = 100, offsetY = 100;
449 for (
unsigned int devicedId = 0; devicedId < deviceCount; devicedId++) {
452 std::stringstream ss;
453 ss <<
"/dev/video" << devicedId;
457 grabbers.push_back(pGrabber);
460 std::cerr <<
"Exception: " << e.
what() << std::endl;
464 std::cout <<
"Grabbers: " << grabbers.size() << std::endl;
466 std::vector<vpShareImage> share_images(grabbers.size());
467 std::vector<std::thread> capture_threads;
468 std::vector<std::thread> display_threads;
471 std::vector<vpFrameQueue> save_queues(grabbers.size());
472 std::vector<vpStorageWorker> storages;
473 std::vector<std::thread> storage_threads;
476 for (
size_t deviceId = 0; deviceId < grabbers.size(); deviceId++) {
478 capture_threads.emplace_back(capture, grabbers[deviceId], std::ref(share_images[deviceId]));
479 int win_x = deviceId * offsetX, win_y = deviceId * offsetY;
482 display_threads.emplace_back(display, grabbers[deviceId]->getWidth(), grabbers[deviceId]->getHeight(), win_x, win_y,
483 deviceId, std::ref(share_images[deviceId]), std::ref(save_queues[deviceId]),
487 std::stringstream ss;
488 ss << parent_directory <<
"/Camera_Stream" << deviceId;
489 std::cout <<
"Create directory: " << ss.str() << std::endl;
492 std::string filename = ss.str();
494 storages.emplace_back(std::ref(save_queues[deviceId]), std::cref(filename), grabbers[deviceId]->getWidth(),
495 grabbers[deviceId]->getHeight());
500 for (
auto &s : storages) {
502 storage_threads.emplace_back(&vpStorageWorker::run, &s);
507 for (
auto &ct : capture_threads) {
511 for (
auto &dt : display_threads) {
517 for (
auto &g : grabbers) {
522 std::cout <<
"\nWaiting for finishing thread to write images..." << std::endl;
526 for (
auto &qu : save_queues) {
531 for (
auto &st : storage_threads) {
538 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
541 std::cout <<
"You do not have X11, or GTK functionalities to display images..." << std::endl;
542 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
543 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
544 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
545 std::cout <<
"- Install GTK, configure again ViSP using cmake and build again this example" << std::endl;
548 #elif !defined(VISP_HAVE_V4L2)
551 std::cout <<
"You do not have Video 4 Linux 2 functionality enabled" << std::endl;
552 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
553 std::cout <<
"- Install libv4l2, configure again ViSP using cmake and build again this example" << std::endl;
559 std::cout <<
"You do not build ViSP with c++11 or higher compiler flag" << std::endl;
560 std::cout <<
"Tip:" << std::endl;
561 std::cout <<
"- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
const char * what() const
static void merge(const vpImage< unsigned char > *R, const vpImage< unsigned char > *G, const vpImage< unsigned char > *B, const vpImage< unsigned char > *a, vpImage< vpRGBa > &RGBa)
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=nullptr)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void gaussianBlur(const vpImage< ImageType > &I, vpImage< FilterType > &GI, unsigned int size=7, FilterType sigma=0., bool normalize=true, const vpImage< bool > *p_mask=nullptr)
unsigned int getSize() const
Type * bitmap
points toward the bitmap
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
VISP_EXPORT double measureTimeMs()