Skip to content

Commit 33aee72

Browse files
silehtmergify[bot]
authored andcommitted
feat: use oatpp::DTO to parse img-input-connector APIData
1 parent 28203ec commit 33aee72

File tree

3 files changed

+147
-42
lines changed

3 files changed

+147
-42
lines changed

src/apidata.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <vector>
3939
#include <sstream>
4040
#include <typeinfo>
41+
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
4142

4243
namespace dd
4344
{
@@ -272,6 +273,31 @@ namespace dd
272273
*/
273274
void toJVal(JDoc &jd, JVal &jv) const;
274275

276+
/**
277+
* \brief converts APIData to oat++ DTO
278+
*/
279+
template <typename T> inline std::shared_ptr<T> createSharedDTO() const
280+
{
281+
rapidjson::Document d;
282+
d.SetObject();
283+
toJDoc(reinterpret_cast<JDoc &>(d));
284+
285+
rapidjson::StringBuffer buffer;
286+
rapidjson::Writer<rapidjson::StringBuffer, rapidjson::UTF8<>,
287+
rapidjson::UTF8<>, rapidjson::CrtAllocator,
288+
rapidjson::kWriteNanAndInfFlag>
289+
writer(buffer);
290+
bool done = d.Accept(writer);
291+
if (!done)
292+
throw DataConversionException("JSON rendering failed");
293+
294+
std::shared_ptr<oatpp::data::mapping::ObjectMapper> object_mapper
295+
= oatpp::parser::json::mapping::ObjectMapper::createShared();
296+
return object_mapper
297+
->readFromString<oatpp::Object<T>>(buffer.GetString())
298+
.getPtr();
299+
}
300+
275301
public:
276302
/**
277303
* \brief render Mustache template based on this APIData object

src/dto/img_connector.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* DeepDetect
3+
* Copyright (c) 2021 Jolibrain SASU
4+
* Author: Mehdi Abaakouk <mehdi.abaakouk@jolibrain.com>
5+
*
6+
* This file is part of deepdetect.
7+
*
8+
* deepdetect is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* deepdetect is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef HTTP_DTO_IMG_CONNECTOR_HPP
23+
#define HTTP_DTO_IMG_CONNECTOR_HPP
24+
25+
#include "dd_config.h"
26+
#include "oatpp/core/Types.hpp"
27+
#include "oatpp/core/macro/codegen.hpp"
28+
29+
namespace dd
30+
{
31+
namespace DTO
32+
{
33+
#include OATPP_CODEGEN_BEGIN(DTO)
34+
35+
class ImgInputConnectorParameters : public oatpp::DTO
36+
{
37+
DTO_INIT(ImgInputConnectorParameters, DTO /* extends */)
38+
39+
DTO_FIELD(Int32, width);
40+
DTO_FIELD(Int32, height);
41+
DTO_FIELD(Int32, crop_width);
42+
DTO_FIELD(Int32, crop_height);
43+
DTO_FIELD(Boolean, bw);
44+
DTO_FIELD(Boolean, rgb);
45+
DTO_FIELD(Boolean, histogram_equalization);
46+
DTO_FIELD(Boolean, unchanged_data);
47+
DTO_FIELD(Boolean, shuffle);
48+
DTO_FIELD(Int32, seed);
49+
DTO_FIELD(Float64, test_split);
50+
DTO_FIELD(Vector<Float32>, mean);
51+
DTO_FIELD(Vector<Float32>, std);
52+
DTO_FIELD(Float64, scale);
53+
DTO_FIELD(Boolean, scaled);
54+
DTO_FIELD(Int32, scale_min);
55+
DTO_FIELD(Int32, scale_max);
56+
DTO_FIELD(Boolean, keep_orig);
57+
DTO_FIELD(String, interp);
58+
59+
// image resizing on GPU
60+
#ifdef USE_CUDA_CV
61+
DTO_FIELD(Boolean, cuda);
62+
#endif
63+
};
64+
65+
#include OATPP_CODEGEN_END(DTO)
66+
}
67+
}
68+
69+
#endif

src/imginputfileconn.h

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "utils/apitools.h"
4444
#include <random>
4545

46+
#include "dto/img_connector.hpp"
47+
4648
namespace dd
4749
{
4850

@@ -455,81 +457,89 @@ namespace dd
455457

456458
void fillup_parameters(const APIData &ad)
457459
{
460+
auto params = ad.createSharedDTO<dd::DTO::ImgInputConnectorParameters>();
461+
458462
// optional parameters.
459-
if (ad.has("width"))
460-
_width = ad.get("width").get<int>();
461-
if (ad.has("height"))
462-
_height = ad.get("height").get<int>();
463-
if (ad.has("crop_width"))
463+
if (params->width)
464+
_width = params->width;
465+
if (params->height)
466+
_height = params->height;
467+
if (params->crop_width)
464468
{
465-
_crop_width = ad.get("crop_width").get<int>();
466-
if (_crop_width > _width)
469+
if (params->crop_width > _width)
467470
{
468471
_logger->error("Crop width must be less than or equal to width");
469472
throw InputConnectorBadParamException(
470473
"Crop width must be less than or equal to width");
471474
}
475+
_width = params->crop_width;
472476
}
473-
if (ad.has("crop_height"))
477+
if (params->crop_height)
474478
{
475-
_crop_height = ad.get("crop_height").get<int>();
476-
if (_crop_height > _height)
479+
if (params->crop_height > _height)
477480
{
478481
_logger->error(
479482
"Crop height must be less than or equal to height");
480483
throw InputConnectorBadParamException(
481484
"Crop height must be less than or equal to height");
482485
}
486+
_height = params->crop_height;
483487
}
484-
if (ad.has("bw"))
485-
_bw = ad.get("bw").get<bool>();
486-
if (ad.has("rgb"))
487-
_rgb = ad.get("rgb").get<bool>();
488-
if (ad.has("histogram_equalization"))
489-
_histogram_equalization = ad.get("histogram_equalization").get<bool>();
490-
if (ad.has("unchanged_data"))
491-
_unchanged_data = ad.get("unchanged_data").get<bool>();
492-
if (ad.has("shuffle"))
493-
_shuffle = ad.get("shuffle").get<bool>();
494-
if (ad.has("seed"))
495-
_seed = ad.get("seed").get<int>();
496-
if (ad.has("test_split"))
497-
_test_split = ad.get("test_split").get<double>();
498-
if (ad.has("mean"))
488+
489+
_bw |= params->bw;
490+
_rgb |= params->rgb;
491+
_histogram_equalization |= params->histogram_equalization;
492+
_unchanged_data |= params->unchanged_data;
493+
_shuffle |= params->shuffle;
494+
if (params->seed)
495+
_seed = params->seed;
496+
if (params->test_split)
497+
_test_split = params->test_split;
498+
if (params->mean)
499499
{
500-
apitools::get_floats(ad, "mean", _mean);
500+
// NOTE(sileht): if we have two much of this we can create
501+
// an oat++ type that directly handle std::vector<float> instead
502+
// of using the oatpp::Vector<oatpp::Float32>
503+
_mean = std::vector<float>();
504+
for (auto &v : *params->mean)
505+
_mean.push_back(v);
501506
_has_mean_scalar = true;
502507
}
503-
if (ad.has("std"))
508+
if (params->std)
504509
{
505-
apitools::get_floats(ad, "std", _std);
510+
_std = std::vector<float>();
511+
for (auto &v : *params->std)
512+
_std.push_back(v);
506513
}
507514

508515
// Variable size
509-
if (ad.has("scale"))
510-
_scale = ad.get("scale").get<double>();
511-
if (ad.has("scaled") || ad.has("scale_min") || ad.has("scale_max"))
512-
_scaled = true;
513-
if (ad.has("scale_min"))
514-
_scale_min = ad.get("scale_min").get<int>();
515-
if (ad.has("scale_max"))
516-
_scale_max = ad.get("scale_max").get<int>();
516+
_scaled |= params->scaled;
517+
if (params->scale)
518+
_scale = params->scale;
519+
if (params->scale_min)
520+
{
521+
_scaled = true;
522+
_scale_min = params->scale_min;
523+
}
524+
if (params->scale_max)
525+
{
526+
_scaled = true;
527+
_scale_max = params->scale_max;
528+
}
517529

518530
// whether to keep original image (for chained ops, e.g. cropping)
519-
if (ad.has("keep_orig"))
520-
_keep_orig = ad.get("keep_orig").get<bool>();
531+
_keep_orig |= params->keep_orig;
521532

522533
// image interpolation method
523-
if (ad.has("interp"))
524-
_interp = ad.get("interp").get<std::string>();
534+
if (params->interp)
535+
_interp = params->interp->std_str();
525536

526537
// timeout
527538
this->set_timeout(ad);
528539

529540
#ifdef USE_CUDA_CV
530541
// image resizing on GPU
531-
if (ad.has("cuda"))
532-
_cuda = ad.get("cuda").get<bool>();
542+
_cuda |= params->cuda;
533543
#endif
534544
}
535545

0 commit comments

Comments
 (0)