dnn.inl.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. {
  46. namespace dnn
  47. {
  48. template<typename TypeIter>
  49. DictValue DictValue::arrayInt(TypeIter begin, int size)
  50. {
  51. DictValue res(Param::INT, new AutoBuffer<int64, 1>(size));
  52. for (int j = 0; j < size; begin++, j++)
  53. (*res.pi)[j] = *begin;
  54. return res;
  55. }
  56. template<typename TypeIter>
  57. DictValue DictValue::arrayReal(TypeIter begin, int size)
  58. {
  59. DictValue res(Param::REAL, new AutoBuffer<double, 1>(size));
  60. for (int j = 0; j < size; begin++, j++)
  61. (*res.pd)[j] = *begin;
  62. return res;
  63. }
  64. template<typename TypeIter>
  65. DictValue DictValue::arrayString(TypeIter begin, int size)
  66. {
  67. DictValue res(Param::STRING, new AutoBuffer<String, 1>(size));
  68. for (int j = 0; j < size; begin++, j++)
  69. (*res.ps)[j] = *begin;
  70. return res;
  71. }
  72. template<>
  73. inline DictValue DictValue::get<DictValue>(int idx) const
  74. {
  75. CV_Assert(idx == -1);
  76. return *this;
  77. }
  78. template<>
  79. inline int64 DictValue::get<int64>(int idx) const
  80. {
  81. CV_Assert(idx == -1 && size() == 1 || idx >= 0 && idx < size());
  82. idx = (idx == -1) ? 0 : idx;
  83. if (type == Param::INT)
  84. {
  85. return (*pi)[idx];
  86. }
  87. else if (type == Param::REAL)
  88. {
  89. double doubleValue = (*pd)[idx];
  90. double fracpart, intpart;
  91. fracpart = std::modf(doubleValue, &intpart);
  92. CV_Assert(fracpart == 0.0);
  93. return (int64)doubleValue;
  94. }
  95. else
  96. {
  97. CV_Assert(isInt() || isReal());
  98. return 0;
  99. }
  100. }
  101. template<>
  102. inline int DictValue::get<int>(int idx) const
  103. {
  104. return (int)get<int64>(idx);
  105. }
  106. template<>
  107. inline unsigned DictValue::get<unsigned>(int idx) const
  108. {
  109. return (unsigned)get<int64>(idx);
  110. }
  111. template<>
  112. inline bool DictValue::get<bool>(int idx) const
  113. {
  114. return (get<int64>(idx) != 0);
  115. }
  116. template<>
  117. inline double DictValue::get<double>(int idx) const
  118. {
  119. CV_Assert(idx == -1 && size() == 1 || idx >= 0 && idx < size());
  120. idx = (idx == -1) ? 0 : idx;
  121. if (type == Param::REAL)
  122. {
  123. return (*pd)[idx];
  124. }
  125. else if (type == Param::INT)
  126. {
  127. return (double)(*pi)[idx];
  128. }
  129. else
  130. {
  131. CV_Assert(isReal() || isInt());
  132. return 0;
  133. }
  134. }
  135. template<>
  136. inline float DictValue::get<float>(int idx) const
  137. {
  138. return (float)get<double>(idx);
  139. }
  140. template<>
  141. inline String DictValue::get<String>(int idx) const
  142. {
  143. CV_Assert(isString());
  144. CV_Assert(idx == -1 && ps->size() == 1 || idx >= 0 && idx < (int)ps->size());
  145. return (*ps)[(idx == -1) ? 0 : idx];
  146. }
  147. inline void DictValue::release()
  148. {
  149. switch (type)
  150. {
  151. case Param::INT:
  152. delete pi;
  153. break;
  154. case Param::STRING:
  155. delete ps;
  156. break;
  157. case Param::REAL:
  158. delete pd;
  159. break;
  160. }
  161. }
  162. inline DictValue::~DictValue()
  163. {
  164. release();
  165. }
  166. inline DictValue & DictValue::operator=(const DictValue &r)
  167. {
  168. if (&r == this)
  169. return *this;
  170. if (r.type == Param::INT)
  171. {
  172. AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
  173. release();
  174. pi = tmp;
  175. }
  176. else if (r.type == Param::STRING)
  177. {
  178. AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
  179. release();
  180. ps = tmp;
  181. }
  182. else if (r.type == Param::REAL)
  183. {
  184. AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
  185. release();
  186. pd = tmp;
  187. }
  188. type = r.type;
  189. return *this;
  190. }
  191. inline DictValue::DictValue(const DictValue &r)
  192. {
  193. type = r.type;
  194. if (r.type == Param::INT)
  195. pi = new AutoBuffer<int64, 1>(*r.pi);
  196. else if (r.type == Param::STRING)
  197. ps = new AutoBuffer<String, 1>(*r.ps);
  198. else if (r.type == Param::REAL)
  199. pd = new AutoBuffer<double, 1>(*r.pd);
  200. }
  201. inline bool DictValue::isString() const
  202. {
  203. return (type == Param::STRING);
  204. }
  205. inline bool DictValue::isInt() const
  206. {
  207. return (type == Param::INT);
  208. }
  209. inline bool DictValue::isReal() const
  210. {
  211. return (type == Param::REAL || type == Param::INT);
  212. }
  213. inline int DictValue::size() const
  214. {
  215. switch (type)
  216. {
  217. case Param::INT:
  218. return (int)pi->size();
  219. break;
  220. case Param::STRING:
  221. return (int)ps->size();
  222. break;
  223. case Param::REAL:
  224. return (int)pd->size();
  225. break;
  226. default:
  227. CV_Error(Error::StsInternal, "");
  228. return -1;
  229. }
  230. }
  231. inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
  232. {
  233. int i;
  234. if (dictv.isInt())
  235. {
  236. for (i = 0; i < dictv.size() - 1; i++)
  237. stream << dictv.get<int64>(i) << ", ";
  238. stream << dictv.get<int64>(i);
  239. }
  240. else if (dictv.isReal())
  241. {
  242. for (i = 0; i < dictv.size() - 1; i++)
  243. stream << dictv.get<double>(i) << ", ";
  244. stream << dictv.get<double>(i);
  245. }
  246. else if (dictv.isString())
  247. {
  248. for (i = 0; i < dictv.size() - 1; i++)
  249. stream << "\"" << dictv.get<String>(i) << "\", ";
  250. stream << dictv.get<String>(i);
  251. }
  252. return stream;
  253. }
  254. /////////////////////////////////////////////////////////////////
  255. inline bool Dict::has(const String &key)
  256. {
  257. return dict.count(key) != 0;
  258. }
  259. inline DictValue *Dict::ptr(const String &key)
  260. {
  261. _Dict::iterator i = dict.find(key);
  262. return (i == dict.end()) ? NULL : &i->second;
  263. }
  264. inline const DictValue &Dict::get(const String &key) const
  265. {
  266. _Dict::const_iterator i = dict.find(key);
  267. if (i == dict.end())
  268. CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
  269. return i->second;
  270. }
  271. template <typename T>
  272. inline T Dict::get(const String &key) const
  273. {
  274. return this->get(key).get<T>();
  275. }
  276. template <typename T>
  277. inline T Dict::get(const String &key, const T &defaultValue) const
  278. {
  279. _Dict::const_iterator i = dict.find(key);
  280. if (i != dict.end())
  281. return i->second.get<T>();
  282. else
  283. return defaultValue;
  284. }
  285. template<typename T>
  286. inline const T &Dict::set(const String &key, const T &value)
  287. {
  288. _Dict::iterator i = dict.find(key);
  289. if (i != dict.end())
  290. i->second = DictValue(value);
  291. else
  292. dict.insert(std::make_pair(key, DictValue(value)));
  293. return value;
  294. }
  295. inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
  296. {
  297. Dict::_Dict::const_iterator it;
  298. for (it = dict.dict.begin(); it != dict.dict.end(); it++)
  299. stream << it->first << " : " << it->second << "\n";
  300. return stream;
  301. }
  302. }
  303. }
  304. #endif