tea_cv_api.cpp 6.9 KB

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