summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 67649a2)
raw | patch | inline | side by side (parent: 67649a2)
author | Ajay Jayaraj <ajayj@ti.com> | |
Mon, 1 Oct 2018 18:49:16 +0000 (13:49 -0500) | ||
committer | Ajay Jayaraj <ajayj@ti.com> | |
Mon, 1 Oct 2018 21:31:33 +0000 (16:31 -0500) |
- Renamed to imgutils::PreprocessImage
- Remove alloc/memcpy of data buffer, work off split OpenCV::Mat(s)
- Use Configuration to extract width, height, #channels etc.
- Number of ROIs is always 1 for TIDL API, remove from parameter list
(MCT-1063)
- Remove alloc/memcpy of data buffer, work off split OpenCV::Mat(s)
- Use Configuration to extract width, height, #channels etc.
- Number of ROIs is always 1 for TIDL API, remove from parameter list
(MCT-1063)
examples/imagenet/main.cpp | patch | blob | history | |
tidl_api/inc/imgutil.h | patch | blob | history | |
tidl_api/src/imgutil.cpp | patch | blob | history |
index 9039271de28b4a85f5a552cb94b90da9bc0c48ca..4cce811f52017279c0a7bed0898091acf2aa0e45 100644 (file)
using namespace std;
using namespace tidl;
-using namespace tidl::imgutil;
using namespace cv;
#define NUM_VIDEO_FRAMES 300
}
// TI DL image preprocessing, into frame_buffer
- return PreProcImage(image, frame_buffer, 1, 3, c.inWidth, c.inHeight,
- c.inWidth, c.inWidth * c.inHeight, 1, c.preProcType);
+ return imgutil::PreprocessImage(image, frame_buffer, c);
}
// Display top 5 classified imagenet classes with probabilities
diff --git a/tidl_api/inc/imgutil.h b/tidl_api/inc/imgutil.h
index e052548a420572e6f48605697216c5ff07ade76a..b0bf081f9ba687ec5c33de03b9fb7a3e9d788086 100644 (file)
--- a/tidl_api/inc/imgutil.h
+++ b/tidl_api/inc/imgutil.h
#pragma once
#include "opencv2/core.hpp"
-#include "opencv2/imgproc.hpp"
-using namespace cv;
+#include "configuration.h"
namespace tidl {
namespace imgutil {
//! the DL network expects
//! @param image Input image data (OpenCV data structure)
//! @param ptr Output buffer that TI DL takes as input
-//! @param roi Number of Region-Of-Interests
-//! @param n Number of channels
-//! @param width Input image width
-//! @param height Input image height
-//! @param pitch Output buffer (ptr) pitch for each line
-//! @param chOffset Output buffer (ptr) offset for each channel
-//! @param frameCount Number of frames in input image data
-//! @param preProcType Preprocessing type, specified in network config
-bool PreProcImage(Mat& image, char *ptr, int16_t roi, int16_t n,
- int16_t width, int16_t height, int16_t pitch,
- int32_t chOffset, int32_t frameCount, int32_t preProcType);
+bool PreprocessImage(cv::Mat& image, char *ptr, const tidl::Configuration& c);
} // namesapce tidl::imgutil
} // namespace tidl
index bcd7f2333b2ee8eaecb81075cd6a04d90e66fbb7..18896f28820237be595dc35803c2c7758a78fe4f 100644 (file)
--- a/tidl_api/src/imgutil.cpp
+++ b/tidl_api/src/imgutil.cpp
#include <iostream>
#include "imgutil.h"
+#include "opencv2/imgproc.hpp"
using namespace tidl;
+using namespace cv;
-bool tidl::imgutil::PreProcImage(
- Mat& image, char *ptr, int16_t roi, int16_t n,
- int16_t width, int16_t height, int16_t pitch,
- int32_t chOffset, int32_t frameCount, int32_t preProcType)
+bool tidl::imgutil::PreprocessImage(Mat& image, char *ptr,
+ const Configuration& c)
{
- int32_t i0, i1, i2, i3;
- Mat spl[3];
- int32_t enableMeanSub = 0;
- Mat tempImage;
- int32_t meanVlaues[n];
-
- if(preProcType == 0) // Caffe-Jacinto Models
- {
- int32_t crop_width = width;
- int32_t crop_height = height;
- int32_t half_the_width = 256/ 2;
- int32_t half_the_height = 256/ 2;
-
- int32_t startX = half_the_width -crop_width/2;
- int32_t startY = half_the_height -crop_height/2;
-
- cv::Rect myROI(startX,
- startY,
- crop_width,
- crop_height);
- cv::resize(image, tempImage, Size(256,256), 0,0,cv::INTER_AREA);
- image = tempImage(myROI);
- enableMeanSub = 0;
- }
- else if (preProcType == 1) // Caffe Models , eg : SqueezeNet
- {
-
- int32_t crop_width = width;
- int32_t crop_height = height;
- int32_t half_the_width = 256/ 2;
- int32_t half_the_height = 256/ 2;
-
- int32_t startX = half_the_width -crop_width/2;
- int32_t startY = half_the_height -crop_height/2;
-
- cv::Rect myROI(startX,
- startY,
- crop_width,
- crop_height);
- cv::resize(image, tempImage, Size(256,256), 0,0,cv::INTER_AREA);
- image = tempImage(myROI);
- enableMeanSub = 1;
- meanVlaues[0] = 104;
- meanVlaues[1] = 117;
- meanVlaues[2] = 123;
-
- }
- else if (preProcType == 2)
- {
- cv::cvtColor(image, image, CV_BGR2RGB);
- float factor = 0.875;
- int32_t orgWidth = image.size[1];
- int32_t orgHeight = image.size[0];
-
- int32_t crop_width = orgWidth*factor;
- int32_t crop_height = orgHeight*factor;
- int32_t half_the_width = orgWidth/ 2;
- int32_t half_the_height = orgHeight / 2;
-
- int32_t startX = half_the_width -crop_width/2;
- int32_t startY = half_the_height -crop_height/2;
-
- cv::Rect myROI(startX,
- startY,
- crop_width,
- crop_height);
- tempImage = image(myROI);
- cv::resize(tempImage, image, Size(width,height), 0,0,CV_INTER_AREA);
- enableMeanSub = 1;
- meanVlaues[0] = 128;
- meanVlaues[1] = 128;
- meanVlaues[2] = 128;
- }
- else if (preProcType == 3)
- {
- cv::cvtColor(image, image, CV_BGR2RGB);
- int32_t crop_width = width;
- int32_t crop_height = height;
- int32_t half_the_width = 32/ 2;
- int32_t half_the_height = 32/ 2;
-
- int32_t startX = half_the_width -crop_width/2;
- int32_t startY = half_the_height -crop_height/2;
-
- cv::Rect myROI(startX,
- startY,
- crop_width,
- crop_height);
- cv::resize(image, tempImage, Size(32,32), 0,0,cv::INTER_AREA);
- image = tempImage(myROI);
- enableMeanSub = 0;
- }
- else if (preProcType == 4)
- {
- //cv::cvtColor(image, image, CV_BGR2RGB);
- int32_t crop_width = width;
- int32_t crop_height = height;
- int32_t half_the_width = width/ 2;
- int32_t half_the_height = height/ 2;
-
- int32_t startX = half_the_width -crop_width/2;
- int32_t startY = half_the_height -crop_height/2;
-
- cv::Rect myROI(startX,
- startY,
- crop_width,
- crop_height);
- cv::resize(image, tempImage, Size(width,height), 0,0,cv::INTER_AREA);
- image = tempImage(myROI);
- enableMeanSub = 0;
- }
- else
- {
- std::cerr << "Unsupported preProcType : " << preProcType << std::endl;
- return false;
- }
-
- if(image.channels() > 3)
- {
- return false;
- }
- if(image.total() != (unsigned int) (height * width))
- {
- return false;
- }
- int size = (int)image.total()*image.channels();
- uint8_t * data = (uint8_t*)malloc(size);
-
- if(data == NULL)
- {
- return false;
- }
-
- split(image,spl);
- for(i0 = 0; i0 < image.channels(); i0++)
- {
- std::memcpy(&data[i0*((int)image.total())],spl[i0].data,((int)image.total()) * sizeof(uint8_t));
- }
- for(i2 = 0; i2 < roi; i2++)
- {
- for(i0 = 0; i0 < n; i0++)
- {
- for(i1 = 0; i1 < height; i1++)
+ int num_channels = c.inNumChannels;
+ int output_width = c.inWidth;
+ int output_height = c.inHeight;
+ int pitch = c.inWidth;
+ int chOffset = c.inWidth * c.inHeight;
+ int preProcType = c.preProcType;
+
+ bool enableMeanSub = false;
+ Mat tempImage;
+ int32_t meanValues[num_channels];
+
+ if(preProcType == 0) // Caffe-Jacinto Models
{
- for(i3 = 0; i3 < width; i3++)
- {
- int32_t in;
-
- if(enableMeanSub)
- {
- if(n != 1)
- {
- in = data[i2*n*width*height + i0*width*height+ i1*width + i3] - meanVlaues[i0];
- }
- else
- {
- in = data[i2*1*width*height + i1*width + i3] - meanVlaues[i0];
- }
-
- if(in > 127) in = 127;
- if(in < -128) in = -128;
- }
- else
- {
-
- if(n != 1)
- {
- in = data[i2*n*width*height + i0*width*height+ i1*width + i3];
- }
- else
- {
- in = data[i2*1*width*height + i1*width + i3];
- }
- }
-
- ptr[i2*n*chOffset + i0*chOffset + i1*pitch + i3] = in;
- }
+ int32_t half_the_width = 256/ 2;
+ int32_t half_the_height = 256/ 2;
+
+ int32_t startX = half_the_width - output_width/2;
+ int32_t startY = half_the_height - output_height/2;
+
+ cv::resize(image, tempImage, Size(256,256), 0,0,cv::INTER_AREA);
+
+ cv::Rect myROI(startX, startY, output_width, output_height);
+ image = tempImage(myROI);
+
+ enableMeanSub = false;
+ }
+ else if (preProcType == 1) // Caffe Models , eg : SqueezeNet
+ {
+ int32_t half_the_width = 256/ 2;
+ int32_t half_the_height = 256/ 2;
+
+ int32_t startX = half_the_width - output_width/2;
+ int32_t startY = half_the_height - output_height/2;
+
+ cv::resize(image, tempImage, Size(256,256), 0,0,cv::INTER_AREA);
+
+ cv::Rect myROI(startX, startY, output_width, output_height);
+ image = tempImage(myROI);
+
+ enableMeanSub = true;
+ meanValues[0] = 104;
+ meanValues[1] = 117;
+ meanValues[2] = 123;
+
+ }
+ else if (preProcType == 2) // mobileNet, inceptionNet
+ {
+ cv::cvtColor(image, image, CV_BGR2RGB);
+ float factor = 0.875;
+ int32_t orgWidth = image.size[1];
+ int32_t orgHeight = image.size[0];
+
+ int32_t crop_width = orgWidth*factor;
+ int32_t crop_height = orgHeight*factor;
+ int32_t half_the_width = orgWidth/ 2;
+ int32_t half_the_height = orgHeight / 2;
+
+ int32_t startX = half_the_width - crop_width/2;
+ int32_t startY = half_the_height - crop_height/2;
+
+ cv::Rect myROI(startX, startY, crop_width, crop_height);
+ tempImage = image(myROI);
+
+ cv::resize(tempImage, image, Size(output_width,output_height),
+ 0, 0, CV_INTER_AREA);
+
+ enableMeanSub = true;
+ meanValues[0] = 128;
+ meanValues[1] = 128;
+ meanValues[2] = 128;
+ }
+ else if (preProcType == 3)
+ {
+ cv::cvtColor(image, image, CV_BGR2RGB);
+ int32_t half_the_width = 32/ 2;
+ int32_t half_the_height = 32/ 2;
+
+ int32_t startX = half_the_width - output_width/2;
+ int32_t startY = half_the_height - output_height/2;
+
+
+ cv::resize(image, tempImage, Size(32,32), 0,0,cv::INTER_AREA);
+ cv::Rect myROI(startX, startY, output_width, output_height);
+ image = tempImage(myROI);
+ enableMeanSub = false;
+ }
+ else if (preProcType == 4)
+ {
+ cv::resize(image, tempImage, Size(output_width,output_height),
+ 0, 0, cv::INTER_AREA);
+ enableMeanSub = false;
+ }
+ else
+ {
+ std::cerr << "Unsupported preProcType : " << preProcType << std::endl;
+ return false;
+ }
+
+ if (image.channels() > 3)
+ return false;
+
+ if (image.total() != (unsigned int) (output_height * output_width))
+ return false;
+
+ Mat spl[num_channels];
+ split(image,spl);
+
+ for(int c = 0; c < num_channels; c++)
+ {
+ const unsigned char* data = spl[c].ptr();
+
+ for(int rows = 0; rows < output_height; rows++)
+ for(int cols = 0; cols < output_width; cols++)
+ {
+ int32_t in = data[rows*output_width + cols];
+
+ if(enableMeanSub)
+ {
+ in -= meanValues[c];
+
+ if(in > 127) in = 127;
+ if(in < -128) in = -128;
+ }
+
+ ptr[c*chOffset + rows*pitch + cols] = in;
+ }
}
- }
- }
- std::free(data);
- return true;
+ return true;
}