dnn.inl.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef OPENCV_DNN_DNN_INL_HPP
  42. #define OPENCV_DNN_DNN_INL_HPP
  43. #include <opencv2/dnn.hpp>
  44. namespace cv {
  45. namespace dnn {
  46. CV__DNN_INLINE_NS_BEGIN
  47. template<typename TypeIter>
  48. DictValue DictValue::arrayInt(TypeIter begin, int size)
  49. {
  50. DictValue res(Param::INT, new AutoBuffer<int64, 1>(size));
  51. for (int j = 0; j < size; begin++, j++)
  52. (*res.pi)[j] = *begin;
  53. return res;
  54. }
  55. template<typename TypeIter>
  56. DictValue DictValue::arrayReal(TypeIter begin, int size)
  57. {
  58. DictValue res(Param::REAL, new AutoBuffer<double, 1>(size));
  59. for (int j = 0; j < size; begin++, j++)
  60. (*res.pd)[j] = *begin;
  61. return res;
  62. }
  63. template<typename TypeIter>
  64. DictValue DictValue::arrayString(TypeIter begin, int size)
  65. {
  66. DictValue res(Param::STRING, new AutoBuffer<String, 1>(size));
  67. for (int j = 0; j < size; begin++, j++)
  68. (*res.ps)[j] = *begin;
  69. return res;
  70. }
  71. template<>
  72. inline DictValue DictValue::get<DictValue>(int idx) const
  73. {
  74. CV_Assert(idx == -1);
  75. return *this;
  76. }
  77. template<>
  78. inline int64 DictValue::get<int64>(int idx) const
  79. {
  80. CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
  81. idx = (idx == -1) ? 0 : idx;
  82. if (type == Param::INT)
  83. {
  84. return (*pi)[idx];
  85. }
  86. else if (type == Param::REAL)
  87. {
  88. double doubleValue = (*pd)[idx];
  89. double fracpart, intpart;
  90. fracpart = std::modf(doubleValue, &intpart);
  91. CV_Assert(fracpart == 0.0);
  92. return (int64)doubleValue;
  93. }
  94. else if (type == Param::STRING)
  95. {
  96. return std::atoi((*ps)[idx].c_str());
  97. }
  98. else
  99. {
  100. CV_Assert(isInt() || isReal() || isString());
  101. return 0;
  102. }
  103. }
  104. template<>
  105. inline int DictValue::get<int>(int idx) const
  106. {
  107. return (int)get<int64>(idx);
  108. }
  109. inline int DictValue::getIntValue(int idx) const
  110. {
  111. return (int)get<int64>(idx);
  112. }
  113. template<>
  114. inline unsigned DictValue::get<unsigned>(int idx) const
  115. {
  116. return (unsigned)get<int64>(idx);
  117. }
  118. template<>
  119. inline bool DictValue::get<bool>(int idx) const
  120. {
  121. return (get<int64>(idx) != 0);
  122. }
  123. template<>
  124. inline double DictValue::get<double>(int idx) const
  125. {
  126. CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
  127. idx = (idx == -1) ? 0 : idx;
  128. if (type == Param::REAL)
  129. {
  130. return (*pd)[idx];
  131. }
  132. else if (type == Param::INT)
  133. {
  134. return (double)(*pi)[idx];
  135. }
  136. else if (type == Param::STRING)
  137. {
  138. return std::atof((*ps)[idx].c_str());
  139. }
  140. else
  141. {
  142. CV_Assert(isReal() || isInt() || isString());
  143. return 0;
  144. }
  145. }
  146. inline double DictValue::getRealValue(int idx) const
  147. {
  148. return get<double>(idx);
  149. }
  150. template<>
  151. inline float DictValue::get<float>(int idx) const
  152. {
  153. return (float)get<double>(idx);
  154. }
  155. template<>
  156. inline String DictValue::get<String>(int idx) const
  157. {
  158. CV_Assert(isString());
  159. CV_Assert((idx == -1 && ps->size() == 1) || (idx >= 0 && idx < (int)ps->size()));
  160. return (*ps)[(idx == -1) ? 0 : idx];
  161. }
  162. inline String DictValue::getStringValue(int idx) const
  163. {
  164. return get<String>(idx);
  165. }
  166. inline void DictValue::release()
  167. {
  168. switch (type)
  169. {
  170. case Param::INT:
  171. delete pi;
  172. break;
  173. case Param::STRING:
  174. delete ps;
  175. break;
  176. case Param::REAL:
  177. delete pd;
  178. break;
  179. case Param::BOOLEAN:
  180. case Param::MAT:
  181. case Param::MAT_VECTOR:
  182. case Param::ALGORITHM:
  183. case Param::FLOAT:
  184. case Param::UNSIGNED_INT:
  185. case Param::UINT64:
  186. case Param::UCHAR:
  187. case Param::SCALAR:
  188. break; // unhandled
  189. }
  190. }
  191. inline DictValue::~DictValue()
  192. {
  193. release();
  194. }
  195. inline DictValue & DictValue::operator=(const DictValue &r)
  196. {
  197. if (&r == this)
  198. return *this;
  199. if (r.type == Param::INT)
  200. {
  201. AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
  202. release();
  203. pi = tmp;
  204. }
  205. else if (r.type == Param::STRING)
  206. {
  207. AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
  208. release();
  209. ps = tmp;
  210. }
  211. else if (r.type == Param::REAL)
  212. {
  213. AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
  214. release();
  215. pd = tmp;
  216. }
  217. type = r.type;
  218. return *this;
  219. }
  220. inline DictValue::DictValue(const DictValue &r)
  221. : pv(NULL)
  222. {
  223. type = r.type;
  224. if (r.type == Param::INT)
  225. pi = new AutoBuffer<int64, 1>(*r.pi);
  226. else if (r.type == Param::STRING)
  227. ps = new AutoBuffer<String, 1>(*r.ps);
  228. else if (r.type == Param::REAL)
  229. pd = new AutoBuffer<double, 1>(*r.pd);
  230. }
  231. inline bool DictValue::isString() const
  232. {
  233. return (type == Param::STRING);
  234. }
  235. inline bool DictValue::isInt() const
  236. {
  237. return (type == Param::INT);
  238. }
  239. inline bool DictValue::isReal() const
  240. {
  241. return (type == Param::REAL || type == Param::INT);
  242. }
  243. inline int DictValue::size() const
  244. {
  245. switch (type)
  246. {
  247. case Param::INT:
  248. return (int)pi->size();
  249. case Param::STRING:
  250. return (int)ps->size();
  251. case Param::REAL:
  252. return (int)pd->size();
  253. case Param::BOOLEAN:
  254. case Param::MAT:
  255. case Param::MAT_VECTOR:
  256. case Param::ALGORITHM:
  257. case Param::FLOAT:
  258. case Param::UNSIGNED_INT:
  259. case Param::UINT64:
  260. case Param::UCHAR:
  261. case Param::SCALAR:
  262. break; // unhandled
  263. }
  264. CV_Error_(Error::StsInternal, ("Unhandled type (%d)", static_cast<int>(type)));
  265. }
  266. inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
  267. {
  268. int i;
  269. if (dictv.isInt())
  270. {
  271. for (i = 0; i < dictv.size() - 1; i++)
  272. stream << dictv.get<int64>(i) << ", ";
  273. stream << dictv.get<int64>(i);
  274. }
  275. else if (dictv.isReal())
  276. {
  277. for (i = 0; i < dictv.size() - 1; i++)
  278. stream << dictv.get<double>(i) << ", ";
  279. stream << dictv.get<double>(i);
  280. }
  281. else if (dictv.isString())
  282. {
  283. for (i = 0; i < dictv.size() - 1; i++)
  284. stream << "\"" << dictv.get<String>(i) << "\", ";
  285. stream << dictv.get<String>(i);
  286. }
  287. return stream;
  288. }
  289. /////////////////////////////////////////////////////////////////
  290. inline bool Dict::has(const String &key) const
  291. {
  292. return dict.count(key) != 0;
  293. }
  294. inline DictValue *Dict::ptr(const String &key)
  295. {
  296. _Dict::iterator i = dict.find(key);
  297. return (i == dict.end()) ? NULL : &i->second;
  298. }
  299. inline const DictValue *Dict::ptr(const String &key) const
  300. {
  301. _Dict::const_iterator i = dict.find(key);
  302. return (i == dict.end()) ? NULL : &i->second;
  303. }
  304. inline const DictValue &Dict::get(const String &key) const
  305. {
  306. _Dict::const_iterator i = dict.find(key);
  307. if (i == dict.end())
  308. CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
  309. return i->second;
  310. }
  311. template <typename T>
  312. inline T Dict::get(const String &key) const
  313. {
  314. return this->get(key).get<T>();
  315. }
  316. template <typename T>
  317. inline T Dict::get(const String &key, const T &defaultValue) const
  318. {
  319. _Dict::const_iterator i = dict.find(key);
  320. if (i != dict.end())
  321. return i->second.get<T>();
  322. else
  323. return defaultValue;
  324. }
  325. template<typename T>
  326. inline const T &Dict::set(const String &key, const T &value)
  327. {
  328. _Dict::iterator i = dict.find(key);
  329. if (i != dict.end())
  330. i->second = DictValue(value);
  331. else
  332. dict.insert(std::make_pair(key, DictValue(value)));
  333. return value;
  334. }
  335. inline void Dict::erase(const String &key)
  336. {
  337. dict.erase(key);
  338. }
  339. inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
  340. {
  341. Dict::_Dict::const_iterator it;
  342. for (it = dict.dict.begin(); it != dict.dict.end(); it++)
  343. stream << it->first << " : " << it->second << "\n";
  344. return stream;
  345. }
  346. inline std::map<String, DictValue>::const_iterator Dict::begin() const
  347. {
  348. return dict.begin();
  349. }
  350. inline std::map<String, DictValue>::const_iterator Dict::end() const
  351. {
  352. return dict.end();
  353. }
  354. CV__DNN_INLINE_NS_END
  355. }
  356. }
  357. #endif