JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
Utils.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#include <jevois/Util/Utils.H>
19#include <jevois/Debug/Log.H>
20
21#include <linux/videodev2.h>
22#include <string>
23#include <vector>
24#include <regex>
25
26#include <string.h> // for strncmp
27#include <fstream>
28#include <cstdarg> // for va_start, etc
29#include <cstdio>
30#include <iostream>
31#include <memory>
32#include <stdexcept>
33#include <array>
34#include <cctype> // for std::isspace()
35#include <cmath> // for std::sqrt()
36
37#include <opencv2/core/hal/interface.h> // for CV_MAT_DEPTH_MASK
38
39// For ""s std::string literals:
40using namespace std::literals;
41using namespace std::string_literals;
42using namespace std::literals::string_literals;
43
44// ####################################################################################################
45std::string jevois::fccstr(unsigned int fcc)
46{
47 if (fcc == 0) return "NONE"; // for no video over USB output
48
49 std::string ret(" ");
50 ret[0] = static_cast<char>(fcc & 0xff);
51 ret[1] = static_cast<char>((fcc >> 8) & 0xff);
52 ret[2] = static_cast<char>((fcc >> 16) & 0xff);
53 ret[3] = static_cast<char>((fcc >> 24) & 0xff);
54 return ret;
55}
56
57// ####################################################################################################
58std::string jevois::cvtypestr(unsigned int cvtype)
59{
60 // From: https://gist.github.com/rummanwaqar/cdaddd5a175f617c0b107d353fd33695
61 std::string r;
62
63 uchar depth = cvtype & CV_MAT_DEPTH_MASK;
64 uchar chans = 1 + (cvtype >> CV_CN_SHIFT);
65
66 switch (depth)
67 {
68 case CV_8U: r = "8U"; break;
69 case CV_8S: r = "8S"; break;
70 case CV_16U: r = "16U"; break;
71 case CV_16S: r = "16S"; break;
72 case CV_16F: r = "16F"; break;
73 case CV_32S: r = "32S"; break;
74 case CV_32F: r = "32F"; break;
75 case CV_64F: r = "64F"; break;
76 default: r = "User"; break;
77 }
78
79 if (chans > 1)
80 {
81 r += 'C';
82 if (chans < 10) r += (chans + '0'); else r += std::to_string(chans);
83 }
84
85 return r;
86}
87
88// ####################################################################################################
89unsigned int jevois::cvBytesPerPix(unsigned int cvtype)
90{
91 uchar depth = cvtype & CV_MAT_DEPTH_MASK;
92 uchar chans = 1 + (cvtype >> CV_CN_SHIFT);
93 int r = 0;
94 switch (depth)
95 {
96 case CV_8U: r = 1; break;
97 case CV_8S: r = 1; break;
98 case CV_16U: r = 2; break;
99 case CV_16S: r = 2; break;
100 case CV_16F: r = 2; break;
101 case CV_32S: r = 4; break;
102 case CV_32F: r = 4; break;
103 case CV_64F: r = 4; break;
104 default: LFATAL("Unsupported OpenCV type " << cvtype);
105 }
106
107 return r * chans;
108}
109
110// ####################################################################################################
111unsigned int jevois::strfcc(std::string const & str)
112{
113 if (str == "BAYER") return V4L2_PIX_FMT_SRGGB8;
114 else if (str == "YUYV") return V4L2_PIX_FMT_YUYV;
115 else if (str == "GREY" || str == "GRAY") return V4L2_PIX_FMT_GREY;
116 else if (str == "MJPG") return V4L2_PIX_FMT_MJPEG;
117 else if (str == "RGB565") return V4L2_PIX_FMT_RGB565;
118 else if (str == "BGR24") return V4L2_PIX_FMT_BGR24;
119 else if (str == "RGB3") return V4L2_PIX_FMT_RGB24;
120 else if (str == "BGR3") return V4L2_PIX_FMT_BGR24;
121
122 // Additional modes supported by JeVois-Pro ISP. We do not necessarily support all but we can name them:
123 else if (str == "RGB24") return V4L2_PIX_FMT_RGB24;
124 else if (str == "RGB32") return V4L2_PIX_FMT_RGB32;
125 else if (str == "UYVY") return V4L2_PIX_FMT_UYVY;
126#ifdef JEVOIS_PRO
127 else if (str == "BGGR16") return V4L2_PIX_FMT_SBGGR16;
128 else if (str == "GRBG16") return V4L2_PIX_FMT_SGRBG16;
129#endif
130 else if (str == "NV12") return V4L2_PIX_FMT_NV12;
131 else if (str == "YUV444") return V4L2_PIX_FMT_YUV444;
132 else if (str == "META") return ISP_V4L2_PIX_FMT_META;
133
134 else if (str == "JVUI") return JEVOISPRO_FMT_GUI;
135
136 else if (str == "NONE") return 0;
137 else throw std::runtime_error("Invalid pixel format " + str);
138}
139
140// ####################################################################################################
141unsigned int jevois::v4l2BytesPerPix(unsigned int fcc)
142{
143 switch (fcc)
144 {
145 case V4L2_PIX_FMT_YUYV: return 2U;
146 case V4L2_PIX_FMT_GREY: return 1U;
147 case V4L2_PIX_FMT_SRGGB8: return 1U;
148 case V4L2_PIX_FMT_RGB565: return 2U;
149 case V4L2_PIX_FMT_MJPEG: return 2U; // at most??
150 case V4L2_PIX_FMT_BGR24: return 3U;
151 case V4L2_PIX_FMT_RGB24: return 3U;
152 case V4L2_PIX_FMT_RGB32: return 4U;
153 case V4L2_PIX_FMT_UYVY: return 2U;
154#ifdef JEVOIS_PRO
155 case V4L2_PIX_FMT_SBGGR16: return 2U;
156 case V4L2_PIX_FMT_SGRBG16: return 2U;
157#endif
158 case V4L2_PIX_FMT_NV12: return 2U; // actually, it has 4:2:0 sampling so 1.5 bytes/pix
159 case V4L2_PIX_FMT_YUV444: return 3U;
160 case ISP_V4L2_PIX_FMT_META: return 1U;
161 case JEVOISPRO_FMT_GUI: return 4U; // RGBA
162 case 0: return 0U; // for NONE output to USB mode
163 default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
164 }
165}
166
167// ####################################################################################################
168unsigned int jevois::v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
169{ return width * height * jevois::v4l2BytesPerPix(fcc); }
170
171// ####################################################################################################
172unsigned int jevois::blackColor(unsigned int fcc)
173{
174 switch (fcc)
175 {
176 case V4L2_PIX_FMT_YUYV: return 0x8000;
177 case V4L2_PIX_FMT_GREY: return 0;
178 case V4L2_PIX_FMT_SRGGB8: return 0;
179 case V4L2_PIX_FMT_RGB565: return 0;
180 case V4L2_PIX_FMT_MJPEG: return 0;
181 case V4L2_PIX_FMT_BGR24: return 0;
182 case V4L2_PIX_FMT_RGB24: return 0;
183 case V4L2_PIX_FMT_RGB32: return 0;
184 case V4L2_PIX_FMT_UYVY: return 0x8000;
185#ifdef JEVOIS_PRO
186 case V4L2_PIX_FMT_SBGGR16: return 0;
187 case V4L2_PIX_FMT_SGRBG16: return 0;
188#endif
189 case V4L2_PIX_FMT_NV12: return 0;
190 case V4L2_PIX_FMT_YUV444: return 0x008080;
191 case JEVOISPRO_FMT_GUI: return 0;
192 default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
193 }
194}
195
196// ####################################################################################################
197unsigned int jevois::whiteColor(unsigned int fcc)
198{
199 switch (fcc)
200 {
201 case V4L2_PIX_FMT_YUYV: return 0x80ff;
202 case V4L2_PIX_FMT_GREY: return 0xff;
203 case V4L2_PIX_FMT_SRGGB8: return 0xff;
204 case V4L2_PIX_FMT_RGB565: return 0xffff;
205 case V4L2_PIX_FMT_MJPEG: return 0xff;
206 case V4L2_PIX_FMT_BGR24: return 0xffffff;
207 case V4L2_PIX_FMT_RGB24: return 0xffffff;
208 case V4L2_PIX_FMT_RGB32: return 0xffffffff;
209 case V4L2_PIX_FMT_UYVY: return 0xff80;
210#ifdef JEVOIS_PRO
211 case V4L2_PIX_FMT_SBGGR16: return 0xffff;
212 case V4L2_PIX_FMT_SGRBG16: return 0xffff;
213#endif
214 case V4L2_PIX_FMT_NV12: return 0xffff; // probably wrong
215 case V4L2_PIX_FMT_YUV444: return 0xff8080;
216 case JEVOISPRO_FMT_GUI: return 0xffffffff;
217 default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
218 }
219}
220
221// ####################################################################################################
222void jevois::applyLetterBox(unsigned int & imw, unsigned int & imh,
223 unsigned int const winw, unsigned int const winh, bool noalias)
224{
225 if (imw == 0 || imh == 0 || winw == 0 || winh == 0) LFATAL("Cannot handle zero width or height");
226
227 if (noalias)
228 {
229 // We want an integer scaling factor to avoid aliasing:
230 double const facw = double(winw) / imw, fach = double(winh) / imh;
231 double const minfac = std::min(facw, fach);
232 if (minfac >= 1.0)
233 {
234 // Image is smaller than window: scale image up by integer part of fac:
235 unsigned int const ifac = minfac;
236 imw *= ifac; imh *= ifac;
237 }
238 else
239 {
240 // Image is larger than window: scale image down by integer part of 1/fac:
241 double const maxfac = std::max(facw, fach);
242 unsigned int const ifac = 1.0 / maxfac;
243 imw /= ifac; imh /= ifac;
244 }
245 }
246 else
247 {
248 // Slightly different algo here to make sure we exactly hit the window width or height with no rounding errors:
249 double const ia = double(imw) / double (imh);
250 double const wa = double(winw) / double (winh);
251
252 if (ia >= wa)
253 {
254 // Image aspect ratio is wider than window aspect ratio. Thus, resize image width to window width, then resize
255 // image height accordingly using the image aspect ratio:
256 imw = winw;
257 imh = (unsigned int)(imw / ia + 0.4999999);
258 }
259 else
260 {
261 // Image aspect ratio is taller than window aspect ratio. Thus, resize image height to window height, then resize
262 // image width accordingly using the image aspect ratio:
263 imh = winh;
264 imw = (unsigned int)(imh * ia + 0.4999999);
265 }
266 }
267}
268
269// ####################################################################################################
270std::vector<std::string> jevois::split(std::string const & input, std::string const & regex)
271{
272 // This code is from: http://stackoverflow.com/questions/9435385/split-a-string-using-c11
273 // passing -1 as the submatch index parameter performs splitting
274 std::regex re(regex);
275 std::sregex_token_iterator first{input.begin(), input.end(), re, -1}, last;
276 return { first, last };
277}
278
279// ####################################################################################################
280template <>
281std::string jevois::join<std::string>(std::vector<std::string> const & tokens, std::string const & delimiter)
282{
283 if (tokens.empty()) return "";
284 if (tokens.size() == 1) return tokens[0];
285
286 std::string ret; size_t const szm1 = tokens.size() - 1;
287
288 for (size_t i = 0; i < szm1; ++i) ret += tokens[i] + delimiter;
289 ret += tokens[szm1];
290
291 return ret;
292}
293
294// ####################################################################################################
295bool jevois::stringStartsWith(std::string const & str, std::string const & prefix)
296{
297 return (strncmp(str.c_str(), prefix.c_str(), prefix.length()) == 0);
298}
299
300// ####################################################################################################
301std::string jevois::replaceWhitespace(std::string const & str, char rep)
302{
303 std::string ret = str;
304 for (char & c : ret) if (std::isspace(c)) c = rep;
305 return ret;
306}
307
308// ####################################################################################################
309std::string jevois::strip(std::string const & str)
310{
311 int idx = str.length() - 1;
312 while (idx >= 0 && std::isspace(str[idx])) --idx;
313 return str.substr(0, idx + 1);
314}
315
316// ####################################################################################################
317std::string jevois::extractString(std::string const & str, std::string const & startsep, std::string const & endsep)
318{
319 size_t idx = str.find(startsep);
320 if (idx == std::string::npos) return std::string();
321 idx += startsep.size();
322
323 if (endsep.empty()) return str.substr(idx);
324
325 size_t idx2 = str.find(endsep, idx);
326 if (idx2 == std::string::npos) return std::string();
327 return str.substr(idx, idx2 - idx);
328}
329
330// ####################################################################################################
331// Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
332size_t jevois::replaceStringFirst(std::string & str, std::string const & from, std::string const & to)
333{
334 if (from.empty()) return 0;
335
336 size_t start_pos = str.find(from);
337 if (start_pos == std::string::npos) return 0;
338
339 str.replace(start_pos, from.length(), to);
340 return 1;
341}
342
343// ####################################################################################################
344// Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
345size_t jevois::replaceStringAll(std::string & str, std::string const & from, std::string const & to)
346{
347 if (from.empty()) return 0;
348
349 size_t start_pos = 0, n = 0;
350 while((start_pos = str.find(from, start_pos)) != std::string::npos)
351 {
352 str.replace(start_pos, from.length(), to);
353 start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
354 ++n;
355 }
356
357 return n;
358}
359
360// ####################################################################################################
361// Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
362std::string jevois::replaceAll(std::string const & str, std::string const & from, std::string const & to)
363{
364 if (from.empty()) return str;
365 std::string ret = str;
366
367 size_t start_pos = 0;
368 while((start_pos = ret.find(from, start_pos)) != std::string::npos)
369 {
370 ret.replace(start_pos, from.length(), to);
371 start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
372 }
373
374 return ret;
375}
376
377// ####################################################################################################
378std::string jevois::tolower(std::string const & str)
379{
380 std::string ret = str;
381 std::transform(ret.begin(), ret.end(), ret.begin(), [](unsigned char c) { return std::tolower(c); });
382 return ret;
383}
384
385// ####################################################################################################
386std::filesystem::path jevois::absolutePath(std::filesystem::path const & root, std::filesystem::path const & path)
387{
388 // If path is empty, return root (be it empty of not):
389 if (path.empty()) return root;
390
391 // no-op if the given path is already absolute:
392 if (path.is_absolute()) return path;
393
394 // no-op if root is empty:
395 if (root.empty()) return path;
396
397 // We know root is not empty and path does not start with a / and is not empty; concatenate both:
398 return root / path;
399}
400
401// ####################################################################################################
402namespace
403{
404 // This code is from NRT, and before that from the iLab C++ neuromorphic vision toolkit
405 std::string vsformat(char const * fmt, va_list ap)
406 {
407 // if we have a null pointer or an empty string, then just return an empty std::string
408 if (fmt == nullptr || fmt[0] == '\0') return std::string();
409
410 int bufsize = 1024;
411 while (true)
412 {
413 char buf[bufsize];
414
415 int const nchars = vsnprintf(buf, bufsize, fmt, ap);
416
417 if (nchars < 0)
418 {
419 // Better leave this as LFATAL() rather than LERROR(), otherwise we have to return a bogus std::string (e.g. an
420 // empty string, or "none", or...), which might be dangerous if it is later used as a filename, for example.
421 LFATAL("vsnprintf failed for format '" << fmt << "' with bufsize = " << bufsize);
422 }
423 else if (nchars >= bufsize)
424 {
425 // buffer was too small, so let's double the bufsize and try again:
426 bufsize *= 2;
427 continue;
428 }
429 else
430 {
431 // OK, the vsnprintf() succeeded:
432 return std::string(&buf[0], nchars);
433 }
434 }
435 return std::string(); // can't happen, but placate the compiler
436 }
437}
438
439// ####################################################################################################
440std::string jevois::sformat(char const * fmt, ...)
441{
442 va_list a;
443 va_start(a, fmt);
444 std::string result = vsformat(fmt, a);
445 va_end(a);
446 return result;
447}
448
449// ####################################################################################################
451{
452#ifdef JEVOIS_PLATFORM_A33
453 std::ofstream ofs("/proc/sys/vm/drop_caches");
454 if (ofs.is_open()) ofs << "3" << std::endl;
455 else LERROR("Failed to flush cache -- ignored");
456#endif
457}
458
459// ####################################################################################################
460// This code modified from here: https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-
461// output-of-command-within-c-using-posix
462std::string jevois::system(std::string const & cmd, bool errtoo)
463{
464 std::array<char, 128> buffer; std::string result;
465
466 FILE * pip;
467 if (errtoo) pip = popen((cmd + " 2>&1").c_str(), "r"); else pip = popen(cmd.c_str(), "r");
468 if (pip == nullptr) LFATAL("popen() failed for command [" << cmd << ']');
469 while (!feof(pip)) if (fgets(buffer.data(), 128, pip) != NULL) result += buffer.data();
470
471 int status = pclose(pip);
472 if (status == -1 && errno == ECHILD) LFATAL("Could not start command: " << cmd);
473 else if (status) LFATAL("Command [" << cmd << "] exited with status " << status << ":\n\n" << result);
474
475 return result;
476}
477
478// ####################################################################################################
479std::string jevois::secs2str(double secs)
480{
481 if (secs < 1.0e-6) return jevois::sformat("%.2fns", secs * 1.0e9);
482 else if (secs < 1.0e-3) return jevois::sformat("%.2fus", secs * 1.0e6);
483 else if (secs < 1.0) return jevois::sformat("%.2fms", secs * 1.0e3);
484 else return jevois::sformat("%.2fs", secs);
485}
486
487// ####################################################################################################
488std::string jevois::secs2str(std::vector<double> secs)
489{
490 if (secs.empty()) return "0.0 +/- 0.0s";
491 double sum = 0.0, sumsq = 0.0;
492 for (double s : secs) { sum += s; sumsq += s*s; }
493 double const avg = sum / secs.size();
494 double const std = std::sqrt(sumsq/secs.size() - avg*avg); // E[(X-E[X])^2] = E[X^2]-E[X]^2
495
496 if (avg < 1.0e-6) return jevois::sformat("%.1f +/- %.1f ns", avg * 1.0e9, std * 1.0e9);
497 else if (avg < 1.0e-3) return jevois::sformat("%.1f +/- %.1f us", avg * 1.0e6, std * 1.0e6);
498 else if (avg < 1.0) return jevois::sformat("%.1f +/- %.1f ms", avg * 1.0e3, std * 1.0e3);
499 else return jevois::sformat("%.1f +/- %.1f s", avg, std);
500}
501
502// ####################################################################################################
503void jevois::secs2str(std::ostringstream & ss, double secs)
504{
505 if (secs < 1.0e-6) ss << secs * 1.0e9 << "ns";
506 else if (secs < 1.0e-3) ss << secs * 1.0e6 << "us";
507 else if (secs < 1.0) ss << secs * 1.0e3 << "ms";
508 else ss << secs << 's';
509}
510
511// ####################################################################################################
512std::string jevois::num2str(double n)
513{
514 if (n < 1.0e3) return jevois::sformat("%.2f", n);
515 else if (n < 1.0e6) return jevois::sformat("%.2fK", n / 1.0e3);
516 else if (n < 1.0e9) return jevois::sformat("%.2fM", n / 1.0e6);
517 else if (n < 1.0e12) return jevois::sformat("%.2fG", n / 1.0e9);
518 else if (n < 1.0e15) return jevois::sformat("%.2fT", n / 1.0e12);
519 else if (n < 1.0e18) return jevois::sformat("%.2fP", n / 1.0e15);
520 else if (n < 1.0e21) return jevois::sformat("%.2fE", n / 1.0e18);
521 else if (n < 1.0e24) return jevois::sformat("%.2fZ", n / 1.0e21);
522 else if (n < 1.0e27) return jevois::sformat("%.2fY", n / 1.0e24);
523 else return jevois::sformat("%.2f", n);
524}
525
526// ####################################################################################################
527void jevois::num2str(std::ostringstream & ss, double n)
528{
529 if (n < 1.0e3) ss << n;
530 else if (n < 1.0e6) ss << n / 1.0e3 << 'K';
531 else if (n < 1.0e9) ss << n / 1.0e6 << 'M';
532 else if (n < 1.0e12) ss << n / 1.0e9 << 'G';
533 else if (n < 1.0e15) ss << n / 1.0e12 << 'T';
534 else if (n < 1.0e18) ss << n / 1.0e15 << 'P';
535 else if (n < 1.0e21) ss << n / 1.0e18 << 'E';
536 else if (n < 1.0e24) ss << n / 1.0e21 << 'Z';
537 else if (n < 1.0e27) ss << n / 1.0e24 << 'Y';
538 else ss << n;
539}
540
541// ####################################################################################################
542std::string jevois::getFileString(char const * fname, int skip)
543{
544 std::ifstream ifs(fname);
545 if (ifs.is_open() == false) throw std::runtime_error("Cannot read file: "s + fname);
546 std::string str;
547 while (skip-- >= 0) std::getline(ifs, str);
548 ifs.close();
549
550 return str;
551}
#define JEVOISPRO_FMT_GUI
JeVois-Pro zero-copy display of camera input frame (to be used as output mode in VideoMapping)
Definition Utils.H:31
#define ISP_V4L2_PIX_FMT_META
Metadata V4L2 format used by Amlogic A311D camera ISP.
Definition Utils.H:27
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition Log.H:211
std::string join< std::string >(std::vector< std::string > const &tokens, std::string const &delimiter)
Concatenate a vector of string tokens into a string.
unsigned int cvBytesPerPix(unsigned int cvtype)
Return the number of bytes per pixel for a given OpenCV pixel type.
Definition Utils.C:89
std::string strip(std::string const &str)
Strip white space (including CR, LF, tabs, etc) from the end of a string.
Definition Utils.C:309
size_t replaceStringFirst(std::string &str, std::string const &from, std::string const &to)
Replace first instance of 'from' with 'to'.
Definition Utils.C:332
unsigned int strfcc(std::string const &str)
Convert a JeVois video format string to V4L2 four-cc code (V4L2_PIX_FMT_...)
Definition Utils.C:111
void flushcache()
Flush the caches, may sometimes be useful when running the camera in turbo mode.
Definition Utils.C:450
std::string num2str(double n)
Report a number with variable multipliers (K, M, G, T, P, E, Z, Y), with precision of 2 decimal point...
Definition Utils.C:512
std::string extractString(std::string const &str, std::string const &startsep, std::string const &endsep)
Extract a portion of a string between two delimiters.
Definition Utils.C:317
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition Utils.C:141
std::string tolower(std::string const &str)
Convert string to lowercase.
Definition Utils.C:378
std::string secs2str(double secs)
Report a duration given in seconds with variable units (ns, us, ms, or s), with precision of 2 decima...
Definition Utils.C:479
std::string getFileString(char const *fname, int skip=0)
Read one line from a file and return it as a string.
Definition Utils.C:542
std::string cvtypestr(unsigned int cvtype)
Convert cv::Mat::type() code to to a string (e.g., CV_8UC1, CV_32SC3, etc)
Definition Utils.C:58
std::string system(std::string const &cmd, bool errtoo=true)
Execute a command and grab stdout output to a string.
Definition Utils.C:462
unsigned int whiteColor(unsigned int fcc)
Return a value that corresponds to white for the given video format.
Definition Utils.C:197
std::string sformat(char const *fmt,...) __attribute__((format(__printf__
Create a string using printf style arguments.
Definition Utils.C:440
std::string replaceWhitespace(std::string const &str, char rep='_')
Replace white space characters in a string with underscore (default) or another character.
Definition Utils.C:301
size_t replaceStringAll(std::string &str, std::string const &from, std::string const &to)
Replace all instances of 'from' with 'to'.
Definition Utils.C:345
std::filesystem::path absolutePath(std::filesystem::path const &root, std::filesystem::path const &path)
Compute an absolute path from two paths.
Definition Utils.C:386
bool stringStartsWith(std::string const &str, std::string const &prefix)
Return true if str starts with prefix (including if both strings are equal)
Definition Utils.C:295
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition Utils.C:45
std::vector< std::string > split(std::string const &input, std::string const &regex="\\s+")
Split string into vector of tokens using a regex to specify what to split on; default regex splits by...
Definition Utils.C:270
void applyLetterBox(unsigned int &imw, unsigned int &imh, unsigned int const winw, unsigned int const winh, bool noalias)
Apply a letterbox resizing to fit an image into a window.
Definition Utils.C:222
unsigned int blackColor(unsigned int fcc)
Return a value that corresponds to black for the given video format.
Definition Utils.C:172
unsigned int v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
Return the image size in bytes for a given V4L2_PIX_FMT_..., width, height.
Definition Utils.C:168
std::string replaceAll(std::string const &str, std::string const &from, std::string const &to)
Replace all instances of 'from' with 'to'.
Definition Utils.C:362