graft_cv_api.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. //3
  176. void cv_save_param(char* conf_file/*=0*/)
  177. {
  178. //save configures
  179. CGCvConfig conf_contrainer = CGCvConfig();
  180. conf_contrainer.setConfParam(&g_cp);
  181. if(conf_file){
  182. cv::FileStorage fs(
  183. conf_file,
  184. cv::FileStorage::WRITE
  185. );
  186. fs<<"conf_parameters";
  187. fs<<conf_contrainer;
  188. fs.release();
  189. }
  190. else{
  191. cv::FileStorage fs(
  192. g_conf_file,
  193. cv::FileStorage::WRITE
  194. );
  195. fs<<"conf_parameters";
  196. fs<<conf_contrainer;
  197. fs.release();
  198. }
  199. }
  200. //4
  201. void cv_get_param(ConfigParam&cp)
  202. {
  203. cp = g_cp;
  204. };
  205. //5
  206. void get_version(char* buf)
  207. {
  208. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  209. };
  210. //6
  211. void cv_get_conf_file(char*buff)
  212. {
  213. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  214. };
  215. //7
  216. /*int rs_oa_init()
  217. {
  218. return g_oa.reset();
  219. };*/
  220. //8
  221. /*int rs_oa_append(
  222. ImgInfo* imginfo,
  223. PositionInfo& posinfo
  224. )
  225. {
  226. memset(&posinfo,0,sizeof(PositionInfo));
  227. try{
  228. g_oa.append(imginfo, posinfo);
  229. }
  230. catch(std::exception &err){
  231. g_logger.ERRORINFO(err.what());
  232. return 1;
  233. }
  234. catch(string& msg){
  235. g_logger.ERRORINFO(msg);
  236. return 1;
  237. }
  238. catch(...){
  239. g_logger.ERRORINFO("unknown error");
  240. return 1;
  241. }
  242. return 0;
  243. };*/
  244. //
  245. int rs_grab_point(
  246. float* points,
  247. int pixel_size,
  248. int pt_size,
  249. PositionInfo& posinfo,
  250. const char* fn/*=0*/
  251. )
  252. {
  253. memset(&posinfo, 0, sizeof(PositionInfo));
  254. try {
  255. int rst = g_rs_gp.load_data(points, pixel_size, pt_size, fn);
  256. if (rst <= 0) {
  257. g_logger.ERRORINFO("invalid points");
  258. return 1;
  259. }
  260. int oa = g_rs_gp.grab_point_detect(posinfo);
  261. }
  262. catch (std::exception &err) {
  263. g_logger.ERRORINFO(err.what());
  264. return 1;
  265. }
  266. catch (string& msg) {
  267. g_logger.ERRORINFO(msg);
  268. return 1;
  269. }
  270. catch (...) {
  271. g_logger.ERRORINFO("unknown error");
  272. return 1;
  273. }
  274. return 0;
  275. }
  276. //9
  277. int rs_oa_get_result(
  278. ImgInfo* imginfo,
  279. PositionInfo& posinfo
  280. )
  281. {
  282. memset(&posinfo,0,sizeof(PositionInfo));
  283. try{
  284. double oa = g_oa.angle_recognize(imginfo, posinfo);
  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. //10
  301. int rs_cut_point(
  302. ImgInfo* imginfo,
  303. PositionInfo& posinfo
  304. )
  305. {
  306. memset(&posinfo,0,sizeof(PositionInfo));
  307. try{
  308. g_rs_cp.up_point_detect(
  309. imginfo,
  310. cv::Mat(),
  311. posinfo,
  312. g_img_cache
  313. );
  314. }
  315. catch(std::exception &err){
  316. g_logger.ERRORINFO(err.what());
  317. return 1;
  318. }
  319. catch(string& msg){
  320. g_logger.ERRORINFO(msg);
  321. return 1;
  322. }
  323. catch(...){
  324. g_logger.ERRORINFO("unknown error");
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. //11
  330. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  331. {
  332. memset(&posinfo,0,sizeof(PositionInfo));
  333. try{
  334. g_rs_cp_reid.cut_point_reid(
  335. imginfo,
  336. cv::Mat(),
  337. pre_img_id,
  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. //
  357. int sola_cut_point_reid(ImgInfo*imginfo, int sola_type, PositionInfo& posinfo)
  358. {
  359. memset(&posinfo, 0, sizeof(PositionInfo));
  360. try {
  361. if (sola_type == 0) {
  362. g_sola_rs.cut_point_reid(imginfo, cv::Mat(), posinfo);
  363. }
  364. else {
  365. g_sola_sc.cut_point_reid(imginfo, cv::Mat(), posinfo);
  366. }
  367. }
  368. catch (std::exception &err) {
  369. g_logger.ERRORINFO(err.what());
  370. return 1;
  371. }
  372. catch (string& msg) {
  373. g_logger.ERRORINFO(msg);
  374. return 1;
  375. }
  376. catch (...) {
  377. g_logger.ERRORINFO("unknown error");
  378. return 1;
  379. }
  380. return 0;
  381. }
  382. //12
  383. int sc_cut_point(
  384. ImgInfo* imginfo,
  385. PositionInfo& posinfo
  386. )
  387. {
  388. memset(&posinfo,0,sizeof(PositionInfo));
  389. try{
  390. g_sc_cp.up_point_detect(
  391. imginfo,
  392. cv::Mat(),
  393. posinfo
  394. );
  395. }
  396. catch(std::exception &err){
  397. g_logger.ERRORINFO(err.what());
  398. return 1;
  399. }
  400. catch(string& msg){
  401. g_logger.ERRORINFO(msg);
  402. return 1;
  403. }
  404. catch(...){
  405. g_logger.ERRORINFO("unknown error");
  406. return 1;
  407. }
  408. return 0;
  409. }
  410. };