graft_cv_api.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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.10";
  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, 1, &g_logger);
  39. CSolaCutPointReid g_sola_sc = CSolaCutPointReid(g_cp, 0, &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 sola_grab_point(
  265. float* points,
  266. int pixel_size,
  267. int pt_size,
  268. int dtype,
  269. PositionInfo& posinfo,
  270. const char* fn/*=0*/
  271. )
  272. {
  273. memset(&posinfo, 0, sizeof(PositionInfo));
  274. try {
  275. int rst = g_rs_gp.load_data(points, pixel_size, pt_size, fn);
  276. if (rst <= 0) {
  277. g_logger.ERRORINFO("invalid points");
  278. return 1;
  279. }
  280. int oa = g_rs_gp.grab_point_detect(dtype, posinfo);
  281. if (oa != 0) {
  282. g_logger.ERRORINFO("no points");
  283. return 1;
  284. }
  285. }
  286. catch (std::exception &err) {
  287. g_logger.ERRORINFO(err.what());
  288. return 1;
  289. }
  290. catch (string& msg) {
  291. g_logger.ERRORINFO(msg);
  292. return 1;
  293. }
  294. catch (...) {
  295. g_logger.ERRORINFO("unknown error");
  296. return 1;
  297. }
  298. return 0;
  299. }
  300. //9
  301. int rs_oa_get_result(
  302. ImgInfo* imginfo,
  303. PositionInfo& posinfo
  304. )
  305. {
  306. memset(&posinfo,0,sizeof(PositionInfo));
  307. try{
  308. double oa = g_oa.angle_recognize(imginfo, posinfo);
  309. }
  310. catch(std::exception &err){
  311. g_logger.ERRORINFO(err.what());
  312. return 1;
  313. }
  314. catch(string& msg){
  315. g_logger.ERRORINFO(msg);
  316. return 1;
  317. }
  318. catch(...){
  319. g_logger.ERRORINFO("unknown error");
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. //10
  325. int rs_cut_point(
  326. ImgInfo* imginfo,
  327. PositionInfo& posinfo
  328. )
  329. {
  330. memset(&posinfo,0,sizeof(PositionInfo));
  331. try{
  332. g_rs_cp.up_point_detect(
  333. imginfo,
  334. cv::Mat(),
  335. posinfo,
  336. g_img_cache
  337. );
  338. }
  339. catch(std::exception &err){
  340. g_logger.ERRORINFO(err.what());
  341. return 1;
  342. }
  343. catch(string& msg){
  344. g_logger.ERRORINFO(msg);
  345. return 1;
  346. }
  347. catch(...){
  348. g_logger.ERRORINFO("unknown error");
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. //11
  354. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  355. {
  356. memset(&posinfo,0,sizeof(PositionInfo));
  357. try{
  358. g_rs_cp_reid.cut_point_reid(
  359. imginfo,
  360. cv::Mat(),
  361. pre_img_id,
  362. posinfo,
  363. g_img_cache
  364. );
  365. }
  366. catch(std::exception &err){
  367. g_logger.ERRORINFO(err.what());
  368. return 1;
  369. }
  370. catch(string& msg){
  371. g_logger.ERRORINFO(msg);
  372. return 1;
  373. }
  374. catch(...){
  375. g_logger.ERRORINFO("unknown error");
  376. return 1;
  377. }
  378. return 0;
  379. }
  380. //
  381. int sola_cut_point_reid(ImgInfo*imginfo, int sola_type, PositionInfo& posinfo)
  382. {
  383. memset(&posinfo, 0, sizeof(PositionInfo));
  384. try {
  385. if (sola_type == 0) {
  386. g_sola_sc.cut_point_reid(imginfo, cv::Mat(), posinfo);
  387. }
  388. else {
  389. g_sola_rs.cut_point_reid(imginfo, cv::Mat(), posinfo);
  390. }
  391. }
  392. catch (std::exception &err) {
  393. g_logger.ERRORINFO(err.what());
  394. return 1;
  395. }
  396. catch (string& msg) {
  397. g_logger.ERRORINFO(msg);
  398. return 1;
  399. }
  400. catch (...) {
  401. g_logger.ERRORINFO("unknown error");
  402. return 1;
  403. }
  404. return 0;
  405. }
  406. //12
  407. int sc_cut_point(
  408. ImgInfo* imginfo,
  409. PositionInfo& posinfo
  410. )
  411. {
  412. memset(&posinfo,0,sizeof(PositionInfo));
  413. try{
  414. g_sc_cp.up_point_detect(
  415. imginfo,
  416. cv::Mat(),
  417. posinfo
  418. );
  419. }
  420. catch(std::exception &err){
  421. g_logger.ERRORINFO(err.what());
  422. return 1;
  423. }
  424. catch(string& msg){
  425. g_logger.ERRORINFO(msg);
  426. return 1;
  427. }
  428. catch(...){
  429. g_logger.ERRORINFO("unknown error");
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. };