graft_cv_api.cpp 6.8 KB

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