40{
return itsImageSize; }
45 if (num >= itsAttrs.size())
LFATAL(
"Invalid blob number " << num <<
", only have " << itsAttrs.size() <<
" blobs");
52 if (blobnum >= itsCrops.size())
53 LFATAL(
"Invalid blob number " << blobnum <<
", only have " << itsCrops.size() <<
" crops");
55 cv::Rect
const & r = itsCrops[blobnum];
56 b2i(x, y, blobsize(blobnum), (r.x != 0 || r.y != 0));
62 if (bsiz.width == 0 || bsiz.height == 0)
LFATAL(
"Cannot handle zero blob width or height");
67 float const fac = std::min(itsImageSize.width /
float(bsiz.width), itsImageSize.height /
float(bsiz.height));
68 float const cropw = fac * bsiz.width + 0.4999F;
69 float const croph = fac * bsiz.height + 0.4999F;
70 x = (itsImageSize.width - cropw) * 0.5F + x * fac;
71 y = (itsImageSize.height - croph) * 0.5F + y * fac;
75 x *= itsImageSize.width / float(bsiz.width);
76 y *= itsImageSize.height / float(bsiz.height);
83 if (blobnum >= itsCrops.size())
84 LFATAL(
"Invalid blob number " << blobnum <<
", only have " << itsCrops.size() <<
" crops");
86 cv::Rect
const & r = itsCrops[blobnum];
87 b2is(sx, sy, blobsize(blobnum), (r.x != 0 || r.y != 0));
93 if (bsiz.width == 0 || bsiz.height == 0)
LFATAL(
"Cannot handle zero blob width or height");
98 float const fac = std::min(itsImageSize.width /
float(bsiz.width), itsImageSize.height /
float(bsiz.height));
104 sx *= itsImageSize.width / float(bsiz.width);
105 sy *= itsImageSize.height / float(bsiz.height);
112 if (num >= itsCrops.size())
LFATAL(
"Invalid blob number " << num <<
", only have " << itsCrops.size() <<
" blobs");
113 return itsCrops[num];
119 cv::Rect
const & r = getUnscaledCropRect(num);
120 tlx = r.x; tly = r.y; brx = r.x + r.width; bry = r.y + r.height;
126 if (blobnum >= itsCrops.size())
127 LFATAL(
"Invalid blob number " << blobnum <<
", only have " << itsCrops.size() <<
" crops");
129 cv::Rect
const & r = itsCrops[blobnum];
130 i2b(x, y, blobsize(blobnum), (r.x != 0 || r.y != 0));
136 if (itsImageSize.width == 0 || itsImageSize.height == 0)
LFATAL(
"Cannot handle zero image width or height");
137 if (bsiz.width == 0 || bsiz.height == 0)
LFATAL(
"Cannot handle zero blob width or height");
142 float const fac = std::min(itsImageSize.width /
float(bsiz.width), itsImageSize.height /
float(bsiz.height));
143 float const cropw = fac * bsiz.width + 0.4999F;
144 float const croph = fac * bsiz.height + 0.4999F;
145 x = (x - (itsImageSize.width - cropw) * 0.5F) / fac;
146 y = (y - (itsImageSize.height - croph) * 0.5F) / fac;
150 x *= float(bsiz.width) / itsImageSize.width;
151 y *= float(bsiz.height) / itsImageSize.height;
161 std::vector<vsi_nn_tensor_attr_t>
const & attrs)
164 itsImageSize.width = img.
width; itsImageSize.height = img.
height; itsImageFmt = img.
fmt;
165 itsCrops.clear(); itsBlobs.clear();
167 if (itsAttrs.empty()) itsAttrs = attrs;
168 if (itsAttrs.empty())
LFATAL(
"Cannot work with no input tensors");
171 if (img.
fmt == V4L2_PIX_FMT_RGB24)
173 else if (img.
fmt == V4L2_PIX_FMT_BGR24)
189 if (helper && idle ==
false && ImGui::CollapsingHeader(
"Pre-Processing", ImGuiTreeNodeFlags_DefaultOpen))
191 ImGui::BulletText(
"Input image: %dx%d %s", itsImageSize.width, itsImageSize.height,
194 if (itsImageFmt != V4L2_PIX_FMT_RGB24 && itsImageFmt != V4L2_PIX_FMT_BGR24)
196 if (rgb::get()) ImGui::BulletText(
"Convert to RGB");
197 else ImGui::BulletText(
"Convert to BGR");
201 report(mod, outimg, helper, overlay, idle);
205 for (cv::Rect
const & r : itsCrops)
206 ImGui::GetBackgroundDrawList()->AddRect(helper->
i2d(r.x, r.y),
207 helper->
i2d(r.x + r.width, r.y + r.height), 0x80808080, 0, 0, 5);
210 if (details::get() ==
false)
213 for (cv::Mat
const & blob : itsBlobs)
215 cv::Rect
const & r = itsCrops[idx];
216 bool const stretch = (r.x == 0 && r.y == 0);
218 ImGui::BulletText(
"Crop %d: %dx%d @ %d,%d %s", idx, r.width, r.height, r.x, r.y,
219 stretch ?
"" :
"(letterbox)");
221 stretch ?
"(stretch)" :
"(uniform)");
228 (void)mod; (void)helper; (void)overlay; (void)idle;
235 for (cv::Rect
const & r : itsCrops)
A component of a model hierarchy.
Helper class to assist modules in creating graphical and GUI elements.
ImVec2 i2d(ImVec2 p, char const *name=nullptr)
Convert coordinates of a point from within a rendered image to on-screen.
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
unsigned int fmt
Pixel format as a V4L2_PIX_FMT_XXX.
unsigned int width
Image width in pixels.
unsigned int height
Image height in pixels.
Base class for a module that supports standardized serial messages.
std::shared_ptr< PreProcessorForPython > getPreProcForPy() const
Get a pointer to our python-friendly interface.
cv::Rect getUnscaledCropRect(size_t blobnum=0)
Get unscaled crop rectangle in image coordinates.
PreProcessor(std::string const &instance)
Constructor.
void i2b(float &x, float &y, size_t blobnum=0)
Convert coordinates from image to blob.
virtual void sendreport(jevois::StdModule *mod, jevois::RawImage *outimg=nullptr, jevois::OptGUIhelper *helper=nullptr, bool overlay=true, bool idle=false)
Report what happened in last process() to console/output video/GUI.
std::vector< cv::Mat > process(jevois::RawImage const &img, std::vector< vsi_nn_tensor_attr_t > const &attrs)
Extract blobs from input image.
std::vector< cv::Mat > const & blobs() const
Access the last computed blobs (or empty if process() has not yet been called)
cv::Size const & imagesize() const
Access the last processed image size.
void b2is(float &sx, float &sy, size_t blobnum=0)
Convert box size from blob back to original image.
virtual ~PreProcessor()
Destructor.
void b2i(float &x, float &y, size_t blobnum=0)
Convert coordinates from blob back to original image.
cv::Size blobsize(size_t num) const
Access the width and height of a given blob, accounting for NCHW or NHWC.
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
cv::Size attrsize(vsi_nn_tensor_attr_t const &attr)
Get a tensor's (width, height) size in cv::Size format, skipping over other dimensions.
std::string shapestr(cv::Mat const &m)
Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional cv::Mat with data type TYPE.
cv::Mat cvImage(RawImage const &src)
Create an OpenCV image from the existing RawImage data, sharing the pixel memory rather than copying ...
cv::Mat convertToCvRGB(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB byte.
cv::Mat convertToCvBGR(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV BGR byte.
void drawRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col)
Draw a rectangle in a YUYV image.
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
unsigned short constexpr MedGrey
YUYV color value.
Main namespace for all JeVois classes and functions.