std::vector<double> zeropoint;
};
+enum class Resizer
+{
+ SQUARE,
+ LETTERBOX,
+};
+
struct Options
{
Normalization normalization;
Quantization quantization;
+ Resizer resizer = Resizer::SQUARE;
};
struct InputMetadata
const Options &options, void *buffer);
private:
- int Resize(cv::Mat &source, cv::Mat &dest, cv::Size size);
+ int Resize(cv::Mat &source, cv::Mat &dest, cv::Size size, Resizer resizer = Resizer::SQUARE);
int ColorConvert(cv::Mat &source, cv::Mat &dest, int sType, int dType);
int Normalize(cv::Mat &source, cv::Mat &dest, const std::vector<double> &mean, const std::vector<double> &std);
int Quantize(cv::Mat &source, cv::Mat &dest, const std::vector<double> &scale,
}
}
+ if (json_object_has_member(preprocess_object, "resize")) {
+ const char *resizer = static_cast<const char *>(json_object_get_string_member(preprocess_object, "resize"));
+ if (strcmp(resizer, "LETTERBOX") == 0) {
+ opt.resizer = Resizer::LETTERBOX;
+ LOGI("resizer changed to letterbox");
+ }
+ }
+
option.insert(std::make_pair(iterLayer->first, opt));
LOGI("LEAVE");
for (auto idx = 0; idx < json_array_get_length(inputList); ++idx) {
JsonNode *node = json_array_get_element(inputList, idx);
- std::string token(json_to_string(node, 1));
+ std::string token(json_to_string(node, 1));
int pos = token.find(":");
std::string tensor_name = token.substr(0, pos);
- const std::vector<char> delimiters = {'{', ' ', ':', '\n', '\"'};
+ const std::vector<char> delimiters = { '{', ' ', ':', '\n', '\"' };
- for (auto& delimiter : delimiters)
+ for (auto &delimiter : delimiters)
tensor_name.erase(std::remove(tensor_name.begin(), tensor_name.end(), delimiter), tensor_name.end());
if (tensor_name.compare((std::string("tensor") + std::to_string(idx + 1))) != 0) {
{
namespace inference
{
-int PreProcess::Resize(cv::Mat &source, cv::Mat &dest, cv::Size size)
+int PreProcess::Resize(cv::Mat &source, cv::Mat &dest, cv::Size size, Resizer resizer)
{
LOGI("ENTER");
try {
- cv::resize(source, dest, size);
+ if (source.size() == size) {
+ dest = source;
+ } else {
+ if (resizer == Resizer::LETTERBOX) {
+ double srcW = static_cast<double>(source.size().width);
+ double srcH = static_cast<double>(source.size().height);
+ double scale = std::min(1.0, std::min(size.width / srcW, size.height / srcH));
+ int dstW = static_cast<int>(srcW * scale);
+ int dstH = static_cast<int>(srcH * scale);
+ cv::Mat _dest;
+ cv::resize(source, _dest, cv::Size(dstW, dstH));
+ dest = cv::Mat(size, source.type(), cv::Scalar(114, 114, 114));
+ _dest.copyTo(dest(cv::Rect((size.width - dstW) / 2, (size.height - dstH) / 2, dstW, dstH)));
+ } else {
+ cv::resize(source, dest, size);
+ }
+ }
} catch (cv::Exception &e) {
LOGE("Fail to resize with msg: %s", e.what());
return MEDIA_VISION_ERROR_INVALID_OPERATION;
cv::Mat cvSource, cvDest;
// cvSource has new allocation with dest.size()
- Resize(source, cvSource, dest.size());
+ Resize(source, cvSource, dest.size(), options.resizer);
// cvDest has new allocation if it's colorSpace is not RGB888
// cvDest share the data with cvSource it's colorSpace is RGB888