graft_cv_api.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. #include "grab_point_rs.h"
  15. extern CRITICAL_SECTION g_cs;
  16. namespace graft_cv
  17. {
  18. char *g_version_str = "0.6.3";
  19. //configure
  20. string g_conf_file = "./gcv_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. "./gcv.log");
  29. //image saver
  30. CImStoreManager* g_pImStore=0;
  31. //implement
  32. //COptimalAnglePart g_oa = COptimalAnglePart(g_cp,&g_logger);
  33. CCotyledonAngle g_oa = CCotyledonAngle(g_cp,&g_logger);
  34. CRootStockCutPoint g_rs_cp = CRootStockCutPoint(g_cp,&g_logger);
  35. CRootStockCutPointReid g_rs_cp_reid = CRootStockCutPointReid(g_cp,&g_logger);
  36. CScionCutPoint g_sc_cp = CScionCutPoint(g_cp,&g_logger);
  37. CRootStockGrabPoint g_rs_gp(g_cp, &g_logger);
  38. CSolaCutPointReid g_sola_rs = CSolaCutPointReid(g_cp, 0, &g_logger);
  39. CSolaCutPointReid g_sola_sc = CSolaCutPointReid(g_cp, 1, &g_logger);
  40. //
  41. map<string, cv::Mat> g_img_cache;
  42. //0 log path
  43. int cv_set_logpath(char*lpath)
  44. {
  45. try{
  46. string mp = g_logger.getPath();
  47. string np(lpath);
  48. if(mp==np){
  49. return 0;
  50. }
  51. g_logger_ofs.close();
  52. g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
  53. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  54. g_logger_ofs<<tmp;
  55. g_logger_ofs.flush();
  56. cout<<tmp<<endl;
  57. g_logger.setPath(string(lpath));
  58. return 0;
  59. }
  60. catch(...){
  61. g_logger.ERRORINFO("set log path failed");
  62. return 1;
  63. }
  64. }
  65. // 0-debug, 1-info, 2-warning, 3-error
  66. int cv_set_loglevel(int lev)
  67. {
  68. if(lev <0 || lev>3){
  69. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  70. return 1;
  71. }
  72. try{
  73. switch(lev){
  74. case 0:
  75. g_logger.setLevel(CGcvLogger::debug);
  76. break;
  77. case 1:
  78. g_logger.setLevel(CGcvLogger::info);
  79. break;
  80. case 2:
  81. g_logger.setLevel(CGcvLogger::warning);
  82. break;
  83. case 3:
  84. g_logger.setLevel(CGcvLogger::error);
  85. break;
  86. default:
  87. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. catch(...){
  93. g_logger.ERRORINFO("set log level failed");
  94. return 1;
  95. }
  96. }
  97. int cv_init_image_saver()
  98. {
  99. if( g_cp.image_save){
  100. if(g_pImStore){
  101. string folder;
  102. g_pImStore->getStoreDir(folder);
  103. if(folder!=g_cp.image_depository){
  104. delete g_pImStore;
  105. g_pImStore = new CImStoreManager();
  106. g_pImStore->setStoreDir(g_cp.image_depository);
  107. g_pImStore->setStoreDays(g_cp.image_backup_days);
  108. }
  109. }
  110. else{
  111. g_pImStore = new CImStoreManager();
  112. g_pImStore->setStoreDir(g_cp.image_depository);
  113. g_pImStore->setStoreDays(g_cp.image_backup_days);
  114. }
  115. }
  116. else{
  117. if(g_pImStore){
  118. delete g_pImStore;
  119. g_pImStore=0;
  120. }
  121. }
  122. g_oa.set_image_saver(&g_pImStore);
  123. g_rs_cp.set_image_saver(&g_pImStore);
  124. g_sc_cp.set_image_saver(&g_pImStore);
  125. return 0;
  126. }
  127. GCV_API int cv_release()
  128. {
  129. if(g_pImStore){
  130. delete g_pImStore;
  131. g_pImStore = 0;
  132. }
  133. //DeleteCriticalSection(&g_cs);
  134. return 0;
  135. }
  136. //1
  137. int cv_init(char*conf)
  138. {
  139. //InitializeCriticalSection(&g_cs);
  140. CGCvConfig conf_contrainer = CGCvConfig();
  141. conf_contrainer.setConfParam(&g_cp);
  142. if(conf){
  143. //read configures
  144. ifstream ifs(conf);
  145. if(!ifs.good()){return 1;}
  146. ifs.close();
  147. memset(&g_cp,0,sizeof(ConfigParam));
  148. cv::FileStorage fs(conf, cv::FileStorage::READ);
  149. conf_contrainer.read(fs["conf_parameters"]);
  150. fs.release();
  151. g_conf_file = conf;
  152. }
  153. else{
  154. ifstream ifs(g_conf_file);
  155. if(!ifs.good()){return 1;}
  156. ifs.close();
  157. memset(&g_cp,0,sizeof(ConfigParam));
  158. //read configures
  159. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  160. conf_contrainer.read(fs["conf_parameters"]);
  161. fs.release();
  162. }
  163. string pinfo = get_cparam_info(&g_cp);
  164. g_logger.INFO(string("lib version: ")+string(g_version_str));
  165. g_logger.INFO(string("load parameters:\n")+pinfo);
  166. return 0;
  167. };
  168. //2
  169. void cv_set_param(ConfigParam&cp)
  170. {
  171. g_cp = cp;
  172. string pinfo = get_cparam_info(&g_cp);
  173. g_logger.INFO(string("set parameters:\n")+pinfo);
  174. };
  175. int cv_set_param_from_file(char*conf)
  176. {
  177. if (conf == 0) {
  178. return 1;
  179. }
  180. ConfigParam cp;
  181. CGCvConfig conf_contrainer = CGCvConfig();
  182. conf_contrainer.setConfParam(&g_cp);
  183. //read configures
  184. ifstream ifs(conf);
  185. if (!ifs.good()) { return 1; }
  186. ifs.close();
  187. memset(&g_cp, 0, sizeof(ConfigParam));
  188. cv::FileStorage fs(conf, cv::FileStorage::READ);
  189. conf_contrainer.read(fs["conf_parameters"]);
  190. fs.release();
  191. g_conf_file = conf;
  192. return 0;
  193. }
  194. //3
  195. void cv_save_param(char* conf_file/*=0*/)
  196. {
  197. //save configures
  198. CGCvConfig conf_contrainer = CGCvConfig();
  199. conf_contrainer.setConfParam(&g_cp);
  200. if(conf_file){
  201. cv::FileStorage fs(
  202. conf_file,
  203. cv::FileStorage::WRITE
  204. );
  205. fs<<"conf_parameters";
  206. fs<<conf_contrainer;
  207. fs.release();
  208. }
  209. else{
  210. cv::FileStorage fs(
  211. g_conf_file,
  212. cv::FileStorage::WRITE
  213. );
  214. fs<<"conf_parameters";
  215. fs<<conf_contrainer;
  216. fs.release();
  217. }
  218. }
  219. //4
  220. void cv_get_param(ConfigParam&cp)
  221. {
  222. cp = g_cp;
  223. };
  224. //5
  225. void get_version(char* buf)
  226. {
  227. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  228. };
  229. //6
  230. void cv_get_conf_file(char*buff)
  231. {
  232. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  233. };
  234. //7
  235. /*int rs_oa_init()
  236. {
  237. return g_oa.reset();
  238. };*/
  239. //8
  240. /*int rs_oa_append(
  241. ImgInfo* imginfo,
  242. PositionInfo& posinfo
  243. )
  244. {
  245. memset(&posinfo,0,sizeof(PositionInfo));
  246. try{
  247. g_oa.append(imginfo, posinfo);
  248. }
  249. catch(std::exception &err){
  250. g_logger.ERRORINFO(err.what());
  251. return 1;
  252. }
  253. catch(string& msg){
  254. g_logger.ERRORINFO(msg);
  255. return 1;
  256. }
  257. catch(...){
  258. g_logger.ERRORINFO("unknown error");
  259. return 1;
  260. }
  261. return 0;
  262. };*/
  263. //
  264. int rs_grab_point(
  265. float* points,
  266. int pixel_size,
  267. int pt_size,
  268. PositionInfo& posinfo,
  269. const char* fn/*=0*/
  270. )
  271. {
  272. memset(&posinfo, 0, sizeof(PositionInfo));
  273. try {
  274. int rst = g_rs_gp.load_data(points, pixel_size, pt_size, fn);
  275. if (rst <= 0) {
  276. g_logger.ERRORINFO("invalid points");
  277. return 1;
  278. }
  279. int oa = g_rs_gp.grab_point_detect(posinfo);
  280. }
  281. catch (std::exception &err) {
  282. g_logger.ERRORINFO(err.what());
  283. return 1;
  284. }
  285. catch (string& msg) {
  286. g_logger.ERRORINFO(msg);
  287. return 1;
  288. }
  289. catch (...) {
  290. g_logger.ERRORINFO("unknown error");
  291. return 1;
  292. }
  293. return 0;
  294. }
  295. //9
  296. int rs_oa_get_result(
  297. ImgInfo* imginfo,
  298. PositionInfo& posinfo
  299. )
  300. {
  301. memset(&posinfo,0,sizeof(PositionInfo));
  302. try{
  303. double oa = g_oa.angle_recognize(imginfo, posinfo);
  304. }
  305. catch(std::exception &err){
  306. g_logger.ERRORINFO(err.what());
  307. return 1;
  308. }
  309. catch(string& msg){
  310. g_logger.ERRORINFO(msg);
  311. return 1;
  312. }
  313. catch(...){
  314. g_logger.ERRORINFO("unknown error");
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. //10
  320. int rs_cut_point(
  321. ImgInfo* imginfo,
  322. PositionInfo& posinfo
  323. )
  324. {
  325. memset(&posinfo,0,sizeof(PositionInfo));
  326. try{
  327. g_rs_cp.up_point_detect(
  328. imginfo,
  329. cv::Mat(),
  330. posinfo,
  331. g_img_cache
  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. //11
  349. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  350. {
  351. memset(&posinfo,0,sizeof(PositionInfo));
  352. try{
  353. g_rs_cp_reid.cut_point_reid(
  354. imginfo,
  355. cv::Mat(),
  356. pre_img_id,
  357. posinfo,
  358. g_img_cache
  359. );
  360. }
  361. catch(std::exception &err){
  362. g_logger.ERRORINFO(err.what());
  363. return 1;
  364. }
  365. catch(string& msg){
  366. g_logger.ERRORINFO(msg);
  367. return 1;
  368. }
  369. catch(...){
  370. g_logger.ERRORINFO("unknown error");
  371. return 1;
  372. }
  373. return 0;
  374. }
  375. //
  376. int sola_cut_point_reid(ImgInfo*imginfo, int sola_type, PositionInfo& posinfo)
  377. {
  378. memset(&posinfo, 0, sizeof(PositionInfo));
  379. try {
  380. if (sola_type == 0) {
  381. g_sola_rs.cut_point_reid(imginfo, cv::Mat(), posinfo);
  382. }
  383. else {
  384. g_sola_sc.cut_point_reid(imginfo, cv::Mat(), posinfo);
  385. }
  386. }
  387. catch (std::exception &err) {
  388. g_logger.ERRORINFO(err.what());
  389. return 1;
  390. }
  391. catch (string& msg) {
  392. g_logger.ERRORINFO(msg);
  393. return 1;
  394. }
  395. catch (...) {
  396. g_logger.ERRORINFO("unknown error");
  397. return 1;
  398. }
  399. return 0;
  400. }
  401. //12
  402. int sc_cut_point(
  403. ImgInfo* imginfo,
  404. PositionInfo& posinfo
  405. )
  406. {
  407. memset(&posinfo,0,sizeof(PositionInfo));
  408. try{
  409. g_sc_cp.up_point_detect(
  410. imginfo,
  411. cv::Mat(),
  412. posinfo
  413. );
  414. }
  415. catch(std::exception &err){
  416. g_logger.ERRORINFO(err.what());
  417. return 1;
  418. }
  419. catch(string& msg){
  420. g_logger.ERRORINFO(msg);
  421. return 1;
  422. }
  423. catch(...){
  424. g_logger.ERRORINFO("unknown error");
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. };