graft_cv_api.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.15";
  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. g_rs_gp.set_image_saver(&g_pImStore);
  126. g_sola_rs.set_image_saver(&g_pImStore);
  127. g_sola_rs.set_image_saver(&g_pImStore);
  128. return 0;
  129. }
  130. GCV_API int cv_release()
  131. {
  132. if(g_pImStore){
  133. delete g_pImStore;
  134. g_pImStore = 0;
  135. }
  136. //DeleteCriticalSection(&g_cs);
  137. return 0;
  138. }
  139. //1
  140. int cv_init(char*conf)
  141. {
  142. //InitializeCriticalSection(&g_cs);
  143. CGCvConfig conf_contrainer = CGCvConfig();
  144. conf_contrainer.setConfParam(&g_cp);
  145. if(conf){
  146. //read configures
  147. ifstream ifs(conf);
  148. if(!ifs.good()){return 1;}
  149. ifs.close();
  150. memset(&g_cp,0,sizeof(ConfigParam));
  151. cv::FileStorage fs(conf, cv::FileStorage::READ);
  152. conf_contrainer.read(fs["conf_parameters"]);
  153. fs.release();
  154. g_conf_file = conf;
  155. }
  156. else{
  157. ifstream ifs(g_conf_file);
  158. if(!ifs.good()){return 1;}
  159. ifs.close();
  160. memset(&g_cp,0,sizeof(ConfigParam));
  161. //read configures
  162. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  163. conf_contrainer.read(fs["conf_parameters"]);
  164. fs.release();
  165. }
  166. string pinfo = get_cparam_info(&g_cp);
  167. g_logger.INFO(string("lib version: ")+string(g_version_str));
  168. g_logger.INFO(string("load parameters:\n")+pinfo);
  169. return 0;
  170. };
  171. //2
  172. void cv_set_param(ConfigParam&cp)
  173. {
  174. g_cp = cp;
  175. string pinfo = get_cparam_info(&g_cp);
  176. g_logger.INFO(string("set parameters:\n")+pinfo);
  177. };
  178. int cv_set_param_from_file(char*conf)
  179. {
  180. if (conf == 0) {
  181. return 1;
  182. }
  183. ConfigParam cp;
  184. CGCvConfig conf_contrainer = CGCvConfig();
  185. conf_contrainer.setConfParam(&g_cp);
  186. //read configures
  187. ifstream ifs(conf);
  188. if (!ifs.good()) { return 1; }
  189. ifs.close();
  190. memset(&g_cp, 0, sizeof(ConfigParam));
  191. cv::FileStorage fs(conf, cv::FileStorage::READ);
  192. conf_contrainer.read(fs["conf_parameters"]);
  193. fs.release();
  194. g_conf_file = conf;
  195. return 0;
  196. }
  197. //3
  198. void cv_save_param(char* conf_file/*=0*/)
  199. {
  200. //save configures
  201. CGCvConfig conf_contrainer = CGCvConfig();
  202. conf_contrainer.setConfParam(&g_cp);
  203. if(conf_file){
  204. cv::FileStorage fs(
  205. conf_file,
  206. cv::FileStorage::WRITE
  207. );
  208. fs<<"conf_parameters";
  209. fs<<conf_contrainer;
  210. fs.release();
  211. }
  212. else{
  213. cv::FileStorage fs(
  214. g_conf_file,
  215. cv::FileStorage::WRITE
  216. );
  217. fs<<"conf_parameters";
  218. fs<<conf_contrainer;
  219. fs.release();
  220. }
  221. }
  222. //4
  223. void cv_get_param(ConfigParam&cp)
  224. {
  225. cp = g_cp;
  226. };
  227. //5
  228. void get_version(char* buf)
  229. {
  230. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  231. };
  232. //6
  233. void cv_get_conf_file(char*buff)
  234. {
  235. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  236. };
  237. //7
  238. /*int rs_oa_init()
  239. {
  240. return g_oa.reset();
  241. };*/
  242. //8
  243. /*int rs_oa_append(
  244. ImgInfo* imginfo,
  245. PositionInfo& posinfo
  246. )
  247. {
  248. memset(&posinfo,0,sizeof(PositionInfo));
  249. try{
  250. g_oa.append(imginfo, posinfo);
  251. }
  252. catch(std::exception &err){
  253. g_logger.ERRORINFO(err.what());
  254. return 1;
  255. }
  256. catch(string& msg){
  257. g_logger.ERRORINFO(msg);
  258. return 1;
  259. }
  260. catch(...){
  261. g_logger.ERRORINFO("unknown error");
  262. return 1;
  263. }
  264. return 0;
  265. };*/
  266. //
  267. int sola_grab_point(
  268. float* points,
  269. int pixel_size,
  270. int pt_size,
  271. int dtype,
  272. PositionInfo& posinfo,
  273. const char* fn/*=0*/
  274. )
  275. {
  276. memset(&posinfo, 0, sizeof(PositionInfo));
  277. try {
  278. int rst = g_rs_gp.load_data(points, pixel_size, pt_size, fn);
  279. if (rst <= 0) {
  280. g_logger.ERRORINFO("invalid points");
  281. return 1;
  282. }
  283. int oa = g_rs_gp.grab_point_detect(dtype, posinfo);
  284. if (oa != 0) {
  285. g_logger.ERRORINFO("no points");
  286. return 1;
  287. }
  288. }
  289. catch (std::exception &err) {
  290. g_logger.ERRORINFO(err.what());
  291. return 1;
  292. }
  293. catch (string& msg) {
  294. g_logger.ERRORINFO(msg);
  295. return 1;
  296. }
  297. catch (...) {
  298. g_logger.ERRORINFO("unknown error");
  299. return 1;
  300. }
  301. return 0;
  302. }
  303. //9
  304. int rs_oa_get_result(
  305. ImgInfo* imginfo,
  306. PositionInfo& posinfo
  307. )
  308. {
  309. memset(&posinfo,0,sizeof(PositionInfo));
  310. try{
  311. double oa = g_oa.angle_recognize(imginfo, posinfo);
  312. }
  313. catch(std::exception &err){
  314. g_logger.ERRORINFO(err.what());
  315. return 1;
  316. }
  317. catch(string& msg){
  318. g_logger.ERRORINFO(msg);
  319. return 1;
  320. }
  321. catch(...){
  322. g_logger.ERRORINFO("unknown error");
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. //10
  328. int rs_cut_point(
  329. ImgInfo* imginfo,
  330. PositionInfo& posinfo
  331. )
  332. {
  333. memset(&posinfo,0,sizeof(PositionInfo));
  334. try{
  335. g_rs_cp.up_point_detect(
  336. imginfo,
  337. cv::Mat(),
  338. posinfo,
  339. g_img_cache
  340. );
  341. }
  342. catch(std::exception &err){
  343. g_logger.ERRORINFO(err.what());
  344. return 1;
  345. }
  346. catch(string& msg){
  347. g_logger.ERRORINFO(msg);
  348. return 1;
  349. }
  350. catch(...){
  351. g_logger.ERRORINFO("unknown error");
  352. return 1;
  353. }
  354. return 0;
  355. }
  356. //11
  357. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  358. {
  359. memset(&posinfo,0,sizeof(PositionInfo));
  360. try{
  361. g_rs_cp_reid.cut_point_reid(
  362. imginfo,
  363. cv::Mat(),
  364. pre_img_id,
  365. posinfo,
  366. g_img_cache
  367. );
  368. }
  369. catch(std::exception &err){
  370. g_logger.ERRORINFO(err.what());
  371. return 1;
  372. }
  373. catch(string& msg){
  374. g_logger.ERRORINFO(msg);
  375. return 1;
  376. }
  377. catch(...){
  378. g_logger.ERRORINFO("unknown error");
  379. return 1;
  380. }
  381. return 0;
  382. }
  383. //
  384. int sola_cut_point_reid(ImgInfo*imginfo, int sola_type, PositionInfo& posinfo)
  385. {
  386. memset(&posinfo, 0, sizeof(PositionInfo));
  387. try {
  388. if (sola_type == 0) {
  389. g_sola_sc.cut_point_reid(imginfo, cv::Mat(), posinfo);
  390. }
  391. else {
  392. g_sola_rs.cut_point_reid(imginfo, cv::Mat(), posinfo);
  393. }
  394. }
  395. catch (std::exception &err) {
  396. g_logger.ERRORINFO(err.what());
  397. return 1;
  398. }
  399. catch (string& msg) {
  400. g_logger.ERRORINFO(msg);
  401. return 1;
  402. }
  403. catch (...) {
  404. g_logger.ERRORINFO("unknown error");
  405. return 1;
  406. }
  407. return 0;
  408. }
  409. //12
  410. int sc_cut_point(
  411. ImgInfo* imginfo,
  412. PositionInfo& posinfo
  413. )
  414. {
  415. memset(&posinfo,0,sizeof(PositionInfo));
  416. try{
  417. g_sc_cp.up_point_detect(
  418. imginfo,
  419. cv::Mat(),
  420. posinfo
  421. );
  422. }
  423. catch(std::exception &err){
  424. g_logger.ERRORINFO(err.what());
  425. return 1;
  426. }
  427. catch(string& msg){
  428. g_logger.ERRORINFO(msg);
  429. return 1;
  430. }
  431. catch(...){
  432. g_logger.ERRORINFO("unknown error");
  433. return 1;
  434. }
  435. return 0;
  436. }
  437. };