dnn.inl.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. {
  222. type = r.type;
  223. if (r.type == Param::INT)
  224. pi = new AutoBuffer<int64, 1>(*r.pi);
  225. else if (r.type == Param::STRING)
  226. ps = new AutoBuffer<String, 1>(*r.ps);
  227. else if (r.type == Param::REAL)
  228. pd = new AutoBuffer<double, 1>(*r.pd);
  229. }
  230. inline bool DictValue::isString() const
  231. {
  232. return (type == Param::STRING);
  233. }
  234. inline bool DictValue::isInt() const
  235. {
  236. return (type == Param::INT);
  237. }
  238. inline bool DictValue::isReal() const
  239. {
  240. return (type == Param::REAL || type == Param::INT);
  241. }
  242. inline int DictValue::size() const
  243. {
  244. switch (type)
  245. {
  246. case Param::INT:
  247. return (int)pi->size();
  248. case Param::STRING:
  249. return (int)ps->size();
  250. case Param::REAL:
  251. return (int)pd->size();
  252. case Param::BOOLEAN:
  253. case Param::MAT:
  254. case Param::MAT_VECTOR:
  255. case Param::ALGORITHM:
  256. case Param::FLOAT:
  257. case Param::UNSIGNED_INT:
  258. case Param::UINT64:
  259. case Param::UCHAR:
  260. case Param::SCALAR:
  261. break; // unhandled
  262. }
  263. CV_Error_(Error::StsInternal, ("Unhandled type (%d)", static_cast<int>(type)));
  264. }
  265. inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
  266. {
  267. int i;
  268. if (dictv.isInt())
  269. {
  270. for (i = 0; i < dictv.size() - 1; i++)
  271. stream << dictv.get<int64>(i) << ", ";
  272. stream << dictv.get<int64>(i);
  273. }
  274. else if (dictv.isReal())
  275. {
  276. for (i = 0; i < dictv.size() - 1; i++)
  277. stream << dictv.get<double>(i) << ", ";
  278. stream << dictv.get<double>(i);
  279. }
  280. else if (dictv.isString())
  281. {
  282. for (i = 0; i < dictv.size() - 1; i++)
  283. stream << "\"" << dictv.get<String>(i) << "\", ";
  284. stream << dictv.get<String>(i);
  285. }
  286. return stream;
  287. }
  288. /////////////////////////////////////////////////////////////////
  289. inline bool Dict::has(const String &key) const
  290. {
  291. return dict.count(key) != 0;
  292. }
  293. inline DictValue *Dict::ptr(const String &key)
  294. {
  295. _Dict::iterator i = dict.find(key);
  296. return (i == dict.end()) ? NULL : &i->second;
  297. }
  298. inline const DictValue *Dict::ptr(const String &key) const
  299. {
  300. _Dict::const_iterator i = dict.find(key);
  301. return (i == dict.end()) ? NULL : &i->second;
  302. }
  303. inline const DictValue &Dict::get(const String &key) const
  304. {
  305. _Dict::const_iterator i = dict.find(key);
  306. if (i == dict.end())
  307. CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
  308. return i->second;
  309. }
  310. template <typename T>
  311. inline T Dict::get(const String &key) const
  312. {
  313. return this->get(key).get<T>();
  314. }
  315. template <typename T>
  316. inline T Dict::get(const String &key, const T &defaultValue) const
  317. {
  318. _Dict::const_iterator i = dict.find(key);
  319. if (i != dict.end())
  320. return i->second.get<T>();
  321. else
  322. return defaultValue;
  323. }
  324. template<typename T>
  325. inline const T &Dict::set(const String &key, const T &value)
  326. {
  327. _Dict::iterator i = dict.find(key);
  328. if (i != dict.end())
  329. i->second = DictValue(value);
  330. else
  331. dict.insert(std::make_pair(key, DictValue(value)));
  332. return value;
  333. }
  334. inline void Dict::erase(const String &key)
  335. {
  336. dict.erase(key);
  337. }
  338. inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
  339. {
  340. Dict::_Dict::const_iterator it;
  341. for (it = dict.dict.begin(); it != dict.dict.end(); it++)
  342. stream << it->first << " : " << it->second << "\n";
  343. return stream;
  344. }
  345. inline std::map<String, DictValue>::const_iterator Dict::begin() const
  346. {
  347. return dict.begin();
  348. }
  349. inline std::map<String, DictValue>::const_iterator Dict::end() const
  350. {
  351. return dict.end();
  352. }
  353. CV__DNN_INLINE_NS_END
  354. }
  355. }
  356. #endif