tea_cv_api.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include <string.h>
  2. #include <fstream>
  3. #include "tea_cv_api.h"
  4. #include "data_def.h"
  5. #include "data_def_api.h"
  6. #include "config.h"
  7. #include "logger.h"
  8. #include "utils.h"
  9. #include "imstorage_manager.h"
  10. #include "tea_sorter.h"
  11. #include "chessboard.h"
  12. extern CRITICAL_SECTION g_cs;
  13. namespace graft_cv
  14. {
  15. char *g_version_str = "0.1.26";
  16. //configure
  17. string g_conf_file = "./tcv_conf.yml";
  18. ConfigParam g_cp;
  19. //logger
  20. ofstream g_logger_ofs;
  21. CGcvLogger g_logger = CGcvLogger(
  22. g_logger_ofs,
  23. CGcvLogger::file_and_terminal,
  24. CGcvLogger::debug,
  25. "./tcv.log");
  26. //image saver
  27. CImStoreManager* g_pImStore=0;
  28. //implement
  29. CTeaSort g_tg = CTeaSort(g_cp, img_type::tea_grab, &g_logger);
  30. CTeaSort g_tc = CTeaSort(g_cp, img_type::tea_cut, &g_logger);
  31. CChessboard g_cb = CChessboard(g_cp, &g_logger);
  32. //??¶àÏß³ÌÓÐÎÊÌâ
  33. map<string, cv::Mat> g_img_cache;
  34. //1 log path
  35. int cv_set_logpath(char*lpath)
  36. {
  37. try{
  38. string mp = g_logger.getPath();
  39. string np(lpath);
  40. if(mp==np){
  41. return 0;
  42. }
  43. g_logger_ofs.close();
  44. g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
  45. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  46. g_logger_ofs<<tmp;
  47. g_logger_ofs.flush();
  48. cout<<tmp<<endl;
  49. g_logger.setPath(string(lpath));
  50. return 0;
  51. }
  52. catch(...){
  53. g_logger.ERRORINFO("set log path failed");
  54. return 1;
  55. }
  56. }
  57. //2 0-debug, 1-info, 2-warning, 3-error
  58. int cv_set_loglevel(int lev)
  59. {
  60. if(lev <0 || lev>3){
  61. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  62. return 1;
  63. }
  64. try{
  65. switch(lev){
  66. case 0:
  67. g_logger.setLevel(CGcvLogger::debug);
  68. break;
  69. case 1:
  70. g_logger.setLevel(CGcvLogger::info);
  71. break;
  72. case 2:
  73. g_logger.setLevel(CGcvLogger::warning);
  74. break;
  75. case 3:
  76. g_logger.setLevel(CGcvLogger::error);
  77. break;
  78. default:
  79. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. catch(...){
  85. g_logger.ERRORINFO("set log level failed");
  86. return 1;
  87. }
  88. }
  89. //3
  90. int cv_init_image_saver()
  91. {
  92. if( g_cp.image_save){
  93. if(g_pImStore){
  94. string folder;
  95. g_pImStore->getStoreDir(folder);
  96. if(folder!=g_cp.image_depository){
  97. delete g_pImStore;
  98. g_pImStore = new CImStoreManager();
  99. g_pImStore->setStoreDir(g_cp.image_depository);
  100. g_pImStore->setStoreDays(g_cp.image_backup_days);
  101. }
  102. }
  103. else{
  104. g_pImStore = new CImStoreManager();
  105. g_pImStore->setStoreDir(g_cp.image_depository);
  106. g_pImStore->setStoreDays(g_cp.image_backup_days);
  107. }
  108. }
  109. else{
  110. if(g_pImStore){
  111. delete g_pImStore;
  112. g_pImStore=0;
  113. }
  114. }
  115. g_tg.set_image_saver(&g_pImStore);
  116. g_tc.set_image_saver(&g_pImStore);
  117. g_cb.set_image_saver(&g_pImStore);
  118. return 0;
  119. }
  120. //4
  121. int cv_init(char*conf)
  122. {
  123. //InitializeCriticalSection(&g_cs);
  124. CGCvConfig conf_contrainer = CGCvConfig();
  125. conf_contrainer.setConfParam(&g_cp);
  126. if(conf){
  127. //read configures
  128. ifstream ifs(conf);
  129. if(!ifs.good()){return 1;}
  130. ifs.close();
  131. memset(&g_cp,0,sizeof(ConfigParam));
  132. cv::FileStorage fs(conf, cv::FileStorage::READ);
  133. conf_contrainer.read(fs["conf_parameters"]);
  134. fs.release();
  135. g_conf_file = conf;
  136. }
  137. else{
  138. ifstream ifs(g_conf_file);
  139. if(!ifs.good()){return 1;}
  140. ifs.close();
  141. memset(&g_cp,0,sizeof(ConfigParam));
  142. //read configures
  143. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  144. conf_contrainer.read(fs["conf_parameters"]);
  145. fs.release();
  146. }
  147. string pinfo = get_cparam_info(&g_cp);
  148. g_logger.INFO(string("lib version: ")+string(g_version_str));
  149. g_logger.INFO(string("load parameters:\n")+pinfo);
  150. return 0;
  151. };
  152. //5
  153. void cv_set_param(ConfigParam&cp)
  154. {
  155. g_cp = cp;
  156. string pinfo = get_cparam_info(&g_cp);
  157. g_logger.INFO(string("set parameters:\n")+pinfo);
  158. };
  159. int cv_set_param_from_file(char*conf)
  160. {
  161. if (conf == 0) {
  162. return 1;
  163. }
  164. ConfigParam cp;
  165. CGCvConfig conf_contrainer = CGCvConfig();
  166. conf_contrainer.setConfParam(&g_cp);
  167. //read configures
  168. ifstream ifs(conf);
  169. if (!ifs.good()) { return 1; }
  170. ifs.close();
  171. memset(&g_cp, 0, sizeof(ConfigParam));
  172. cv::FileStorage fs(conf, cv::FileStorage::READ);
  173. conf_contrainer.read(fs["conf_parameters"]);
  174. fs.release();
  175. g_conf_file = conf;
  176. return 0;
  177. }
  178. //6
  179. int cv_release()
  180. {
  181. if(g_pImStore){
  182. delete g_pImStore;
  183. g_pImStore = 0;
  184. }
  185. //DeleteCriticalSection(&g_cs);
  186. return 0;
  187. }
  188. //7
  189. void cv_get_conf_file(char*buff)
  190. {
  191. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  192. };
  193. //8
  194. void cv_save_param(char* conf_file/*=0*/)
  195. {
  196. //save configures
  197. CGCvConfig conf_contrainer = CGCvConfig();
  198. conf_contrainer.setConfParam(&g_cp);
  199. if(conf_file){
  200. cv::FileStorage fs(
  201. conf_file,
  202. cv::FileStorage::WRITE
  203. );
  204. fs<<"conf_parameters";
  205. fs<<conf_contrainer;
  206. fs.release();
  207. }
  208. else{
  209. cv::FileStorage fs(
  210. g_conf_file,
  211. cv::FileStorage::WRITE
  212. );
  213. fs<<"conf_parameters";
  214. fs<<conf_contrainer;
  215. fs.release();
  216. }
  217. }
  218. //9
  219. void cv_get_param(ConfigParam&cp)
  220. {
  221. cp = g_cp;
  222. };
  223. //10
  224. void get_version(char* buf)
  225. {
  226. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  227. };
  228. //11
  229. int tea_grab_point(
  230. ImgInfo* imginfo,
  231. PositionInfo& posinfo,
  232. const char* fn
  233. )
  234. {
  235. memset(&posinfo,0,sizeof(PositionInfo));
  236. try{
  237. int r = g_tg.load_model();
  238. if (r != 0) {
  239. g_logger.ERRORINFO("model load failed");
  240. return 2;
  241. }
  242. r = g_tg.detect(imginfo, posinfo, fn);
  243. return r;
  244. }
  245. catch(std::exception &err){
  246. g_logger.ERRORINFO(err.what());
  247. return 2;
  248. }
  249. catch(string& msg){
  250. g_logger.ERRORINFO(msg);
  251. return 2;
  252. }
  253. catch(...){
  254. g_logger.ERRORINFO("unknown error");
  255. return 2;
  256. }
  257. return 0;
  258. }
  259. //12
  260. int tea_cut_point(
  261. ImgInfo* imginfo,
  262. PositionInfo& posinfo,
  263. const char* fn
  264. )
  265. {
  266. memset(&posinfo, 0, sizeof(PositionInfo));
  267. try {
  268. int r = g_tc.load_model();
  269. if (r != 0) {
  270. g_logger.ERRORINFO("model load failed");
  271. return 1;
  272. }
  273. r = g_tc.detect(imginfo, posinfo, fn);
  274. return r;
  275. }
  276. catch (std::exception &err) {
  277. g_logger.ERRORINFO(err.what());
  278. return 1;
  279. }
  280. catch (string& msg) {
  281. g_logger.ERRORINFO(msg);
  282. return 1;
  283. }
  284. catch (...) {
  285. g_logger.ERRORINFO("unknown error");
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. //13
  291. int chessboard_corners(
  292. ImgInfo*imginfo,
  293. PositionInfo& posinfo,
  294. int corner_row,
  295. int corner_col,
  296. int x0,
  297. int y0,
  298. int width,
  299. int height,
  300. const char* fn)
  301. {
  302. memset(&posinfo, 0, sizeof(PositionInfo));
  303. try {
  304. int r = g_cb.detect(imginfo,
  305. posinfo,
  306. corner_row,
  307. corner_col,
  308. x0,
  309. y0,
  310. width,
  311. height,
  312. fn);
  313. return r;
  314. }
  315. catch (std::exception &err) {
  316. g_logger.ERRORINFO(err.what());
  317. return 1;
  318. }
  319. catch (string& msg) {
  320. g_logger.ERRORINFO(msg);
  321. return 1;
  322. }
  323. catch (...) {
  324. g_logger.ERRORINFO("unknown error");
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. };