graft_cv_api.cpp 7.1 KB

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