graft_cv_api.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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.7.9";
  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_sola_grab_rs(g_cp, &g_logger);
  38. CRootStockGrabPoint g_sola_grab_sc(g_cp, &g_logger);
  39. CSolaCutPointReid g_sola_reid_rs = CSolaCutPointReid(g_cp, 1, &g_logger);
  40. CSolaCutPointReid g_sola_reid_sc = CSolaCutPointReid(g_cp, 0, &g_logger);
  41. //??¶àÏß³ÌÓÐÎÊÌâ
  42. map<string, cv::Mat> g_img_cache;
  43. //0 log path
  44. int cv_set_logpath(char*lpath)
  45. {
  46. try{
  47. string mp = g_logger.getPath();
  48. string np(lpath);
  49. if(mp==np){
  50. return 0;
  51. }
  52. g_logger_ofs.close();
  53. g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
  54. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  55. g_logger_ofs<<tmp;
  56. g_logger_ofs.flush();
  57. cout<<tmp<<endl;
  58. g_logger.setPath(string(lpath));
  59. return 0;
  60. }
  61. catch(...){
  62. g_logger.ERRORINFO("set log path failed");
  63. return 1;
  64. }
  65. }
  66. // 0-debug, 1-info, 2-warning, 3-error
  67. int cv_set_loglevel(int lev)
  68. {
  69. if(lev <0 || lev>3){
  70. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  71. return 1;
  72. }
  73. try{
  74. switch(lev){
  75. case 0:
  76. g_logger.setLevel(CGcvLogger::debug);
  77. break;
  78. case 1:
  79. g_logger.setLevel(CGcvLogger::info);
  80. break;
  81. case 2:
  82. g_logger.setLevel(CGcvLogger::warning);
  83. break;
  84. case 3:
  85. g_logger.setLevel(CGcvLogger::error);
  86. break;
  87. default:
  88. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. catch(...){
  94. g_logger.ERRORINFO("set log level failed");
  95. return 1;
  96. }
  97. }
  98. int cv_init_image_saver()
  99. {
  100. if( g_cp.image_save){
  101. if(g_pImStore){
  102. string folder;
  103. g_pImStore->getStoreDir(folder);
  104. if(folder!=g_cp.image_depository){
  105. delete g_pImStore;
  106. g_pImStore = new CImStoreManager();
  107. g_pImStore->setStoreDir(g_cp.image_depository);
  108. g_pImStore->setStoreDays(g_cp.image_backup_days);
  109. }
  110. }
  111. else{
  112. g_pImStore = new CImStoreManager();
  113. g_pImStore->setStoreDir(g_cp.image_depository);
  114. g_pImStore->setStoreDays(g_cp.image_backup_days);
  115. }
  116. }
  117. else{
  118. if(g_pImStore){
  119. delete g_pImStore;
  120. g_pImStore=0;
  121. }
  122. }
  123. g_oa.set_image_saver(&g_pImStore);
  124. g_rs_cp.set_image_saver(&g_pImStore);
  125. g_sc_cp.set_image_saver(&g_pImStore);
  126. g_sola_grab_rs.set_image_saver(&g_pImStore);
  127. g_sola_grab_sc.set_image_saver(&g_pImStore);
  128. g_sola_reid_rs.set_image_saver(&g_pImStore);
  129. g_sola_reid_sc.set_image_saver(&g_pImStore);
  130. return 0;
  131. }
  132. GCV_API int cv_release()
  133. {
  134. if(g_pImStore){
  135. delete g_pImStore;
  136. g_pImStore = 0;
  137. }
  138. //DeleteCriticalSection(&g_cs);
  139. return 0;
  140. }
  141. //1
  142. int cv_init(char*conf)
  143. {
  144. //InitializeCriticalSection(&g_cs);
  145. CGCvConfig conf_contrainer = CGCvConfig();
  146. conf_contrainer.setConfParam(&g_cp);
  147. if(conf){
  148. //read configures
  149. ifstream ifs(conf);
  150. if(!ifs.good()){return 1;}
  151. ifs.close();
  152. memset(&g_cp,0,sizeof(ConfigParam));
  153. cv::FileStorage fs(conf, cv::FileStorage::READ);
  154. conf_contrainer.read(fs["conf_parameters"]);
  155. fs.release();
  156. g_conf_file = conf;
  157. }
  158. else{
  159. ifstream ifs(g_conf_file);
  160. if(!ifs.good()){return 1;}
  161. ifs.close();
  162. memset(&g_cp,0,sizeof(ConfigParam));
  163. //read configures
  164. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  165. conf_contrainer.read(fs["conf_parameters"]);
  166. fs.release();
  167. }
  168. string pinfo = get_cparam_info(&g_cp);
  169. g_logger.INFO(string("lib version: ")+string(g_version_str));
  170. g_logger.INFO(string("load parameters:\n")+pinfo);
  171. return 0;
  172. };
  173. //2
  174. void cv_set_param(ConfigParam&cp)
  175. {
  176. g_cp = cp;
  177. string pinfo = get_cparam_info(&g_cp);
  178. g_logger.INFO(string("set parameters:\n")+pinfo);
  179. };
  180. int cv_set_param_from_file(char*conf)
  181. {
  182. if (conf == 0) {
  183. return 1;
  184. }
  185. ConfigParam cp;
  186. CGCvConfig conf_contrainer = CGCvConfig();
  187. conf_contrainer.setConfParam(&g_cp);
  188. //read configures
  189. ifstream ifs(conf);
  190. if (!ifs.good()) { return 1; }
  191. ifs.close();
  192. memset(&g_cp, 0, sizeof(ConfigParam));
  193. cv::FileStorage fs(conf, cv::FileStorage::READ);
  194. conf_contrainer.read(fs["conf_parameters"]);
  195. fs.release();
  196. g_conf_file = conf;
  197. return 0;
  198. }
  199. //3
  200. void cv_save_param(char* conf_file/*=0*/)
  201. {
  202. //save configures
  203. CGCvConfig conf_contrainer = CGCvConfig();
  204. conf_contrainer.setConfParam(&g_cp);
  205. if(conf_file){
  206. cv::FileStorage fs(
  207. conf_file,
  208. cv::FileStorage::WRITE
  209. );
  210. fs<<"conf_parameters";
  211. fs<<conf_contrainer;
  212. fs.release();
  213. }
  214. else{
  215. cv::FileStorage fs(
  216. g_conf_file,
  217. cv::FileStorage::WRITE
  218. );
  219. fs<<"conf_parameters";
  220. fs<<conf_contrainer;
  221. fs.release();
  222. }
  223. }
  224. //4
  225. void cv_get_param(ConfigParam&cp)
  226. {
  227. cp = g_cp;
  228. };
  229. //5
  230. void get_version(char* buf)
  231. {
  232. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  233. };
  234. //6
  235. void cv_get_conf_file(char*buff)
  236. {
  237. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  238. };
  239. //7
  240. /*int rs_oa_init()
  241. {
  242. return g_oa.reset();
  243. };*/
  244. //8
  245. /*int rs_oa_append(
  246. ImgInfo* imginfo,
  247. PositionInfo& posinfo
  248. )
  249. {
  250. memset(&posinfo,0,sizeof(PositionInfo));
  251. try{
  252. g_oa.append(imginfo, posinfo);
  253. }
  254. catch(std::exception &err){
  255. g_logger.ERRORINFO(err.what());
  256. return 1;
  257. }
  258. catch(string& msg){
  259. g_logger.ERRORINFO(msg);
  260. return 1;
  261. }
  262. catch(...){
  263. g_logger.ERRORINFO("unknown error");
  264. return 1;
  265. }
  266. return 0;
  267. };*/
  268. //
  269. int sola_grab_point_rs(
  270. float* points,
  271. int pixel_size,
  272. int pt_size,
  273. PositionInfo& posinfo,
  274. const char* fn/*=0*/
  275. )
  276. {
  277. memset(&posinfo, 0, sizeof(PositionInfo));
  278. int dtype = 1;
  279. try {
  280. int rst = g_sola_grab_rs.load_data(points, pixel_size, pt_size, dtype, fn);
  281. if (rst <= 0) {
  282. g_logger.ERRORINFO("invalid points");
  283. return 1;
  284. }
  285. int oa = g_sola_grab_rs.grab_point_detect(posinfo);
  286. if (oa != 0) {
  287. g_logger.ERRORINFO("no points");
  288. return 1;
  289. }
  290. }
  291. catch (std::exception &err) {
  292. g_logger.ERRORINFO(err.what());
  293. return 1;
  294. }
  295. catch (string& msg) {
  296. g_logger.ERRORINFO(msg);
  297. return 1;
  298. }
  299. catch (...) {
  300. g_logger.ERRORINFO("unknown error");
  301. return 1;
  302. }
  303. return 0;
  304. }
  305. int sola_grab_point_sc(
  306. float* points,
  307. int pixel_size,
  308. int pt_size,
  309. PositionInfo& posinfo,
  310. const char* fn/*=0*/
  311. )
  312. {
  313. memset(&posinfo, 0, sizeof(PositionInfo));
  314. int dtype = 0;
  315. try {
  316. int rst = g_sola_grab_sc.load_data(points, pixel_size, pt_size, dtype, fn);
  317. if (rst <= 0) {
  318. g_logger.ERRORINFO("invalid points");
  319. return 1;
  320. }
  321. int oa = g_sola_grab_sc.grab_point_detect(posinfo);
  322. if (oa != 0) {
  323. g_logger.ERRORINFO("no points");
  324. return 1;
  325. }
  326. }
  327. catch (std::exception &err) {
  328. g_logger.ERRORINFO(err.what());
  329. return 1;
  330. }
  331. catch (string& msg) {
  332. g_logger.ERRORINFO(msg);
  333. return 1;
  334. }
  335. catch (...) {
  336. g_logger.ERRORINFO("unknown error");
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. //9
  342. int rs_oa_get_result(
  343. ImgInfo* imginfo,
  344. PositionInfo& posinfo
  345. )
  346. {
  347. memset(&posinfo,0,sizeof(PositionInfo));
  348. try{
  349. double oa = g_oa.angle_recognize(imginfo, posinfo);
  350. }
  351. catch(std::exception &err){
  352. g_logger.ERRORINFO(err.what());
  353. return 1;
  354. }
  355. catch(string& msg){
  356. g_logger.ERRORINFO(msg);
  357. return 1;
  358. }
  359. catch(...){
  360. g_logger.ERRORINFO("unknown error");
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. //10
  366. int rs_cut_point(
  367. ImgInfo* imginfo,
  368. PositionInfo& posinfo
  369. )
  370. {
  371. memset(&posinfo,0,sizeof(PositionInfo));
  372. try{
  373. g_rs_cp.up_point_detect(
  374. imginfo,
  375. cv::Mat(),
  376. posinfo,
  377. g_img_cache
  378. );
  379. }
  380. catch(std::exception &err){
  381. g_logger.ERRORINFO(err.what());
  382. return 1;
  383. }
  384. catch(string& msg){
  385. g_logger.ERRORINFO(msg);
  386. return 1;
  387. }
  388. catch(...){
  389. g_logger.ERRORINFO("unknown error");
  390. return 1;
  391. }
  392. return 0;
  393. }
  394. //11
  395. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  396. {
  397. memset(&posinfo,0,sizeof(PositionInfo));
  398. try{
  399. g_rs_cp_reid.cut_point_reid(
  400. imginfo,
  401. cv::Mat(),
  402. pre_img_id,
  403. posinfo,
  404. g_img_cache
  405. );
  406. }
  407. catch(std::exception &err){
  408. g_logger.ERRORINFO(err.what());
  409. return 1;
  410. }
  411. catch(string& msg){
  412. g_logger.ERRORINFO(msg);
  413. return 1;
  414. }
  415. catch(...){
  416. g_logger.ERRORINFO("unknown error");
  417. return 1;
  418. }
  419. return 0;
  420. }
  421. //
  422. int sola_cut_point_reid_rs(ImgInfo*imginfo, PositionInfo& posinfo)
  423. {
  424. memset(&posinfo, 0, sizeof(PositionInfo));
  425. int sola_type = 1;
  426. try {
  427. g_sola_reid_rs.cut_point_reid(imginfo, cv::Mat(), posinfo);
  428. }
  429. catch (std::exception &err) {
  430. g_logger.ERRORINFO(err.what());
  431. return 1;
  432. }
  433. catch (string& msg) {
  434. g_logger.ERRORINFO(msg);
  435. return 1;
  436. }
  437. catch (...) {
  438. g_logger.ERRORINFO("unknown error");
  439. return 1;
  440. }
  441. return 0;
  442. }
  443. int sola_cut_point_reid_sc(ImgInfo*imginfo, PositionInfo& posinfo)
  444. {
  445. memset(&posinfo, 0, sizeof(PositionInfo));
  446. int sola_type = 0;
  447. try {
  448. g_sola_reid_sc.cut_point_reid(imginfo, cv::Mat(), posinfo);
  449. }
  450. catch (std::exception &err) {
  451. g_logger.ERRORINFO(err.what());
  452. return 1;
  453. }
  454. catch (string& msg) {
  455. g_logger.ERRORINFO(msg);
  456. return 1;
  457. }
  458. catch (...) {
  459. g_logger.ERRORINFO("unknown error");
  460. return 1;
  461. }
  462. return 0;
  463. }
  464. //12
  465. int sc_cut_point(
  466. ImgInfo* imginfo,
  467. PositionInfo& posinfo
  468. )
  469. {
  470. memset(&posinfo,0,sizeof(PositionInfo));
  471. try{
  472. g_sc_cp.up_point_detect(
  473. imginfo,
  474. cv::Mat(),
  475. posinfo
  476. );
  477. }
  478. catch(std::exception &err){
  479. g_logger.ERRORINFO(err.what());
  480. return 1;
  481. }
  482. catch(string& msg){
  483. g_logger.ERRORINFO(msg);
  484. return 1;
  485. }
  486. catch(...){
  487. g_logger.ERRORINFO("unknown error");
  488. return 1;
  489. }
  490. return 0;
  491. }
  492. };