graft_cv_api.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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.2";
  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. //
  39. map<string, cv::Mat> g_img_cache;
  40. //0 log path
  41. int cv_set_logpath(char*lpath)
  42. {
  43. try{
  44. string mp = g_logger.getPath();
  45. string np(lpath);
  46. if(mp==np){
  47. return 0;
  48. }
  49. g_logger_ofs.close();
  50. g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
  51. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  52. g_logger_ofs<<tmp;
  53. g_logger_ofs.flush();
  54. cout<<tmp<<endl;
  55. g_logger.setPath(string(lpath));
  56. return 0;
  57. }
  58. catch(...){
  59. g_logger.ERRORINFO("set log path failed");
  60. return 1;
  61. }
  62. }
  63. // 0-debug, 1-info, 2-warning, 3-error
  64. int cv_set_loglevel(int lev)
  65. {
  66. if(lev <0 || lev>3){
  67. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  68. return 1;
  69. }
  70. try{
  71. switch(lev){
  72. case 0:
  73. g_logger.setLevel(CGcvLogger::debug);
  74. break;
  75. case 1:
  76. g_logger.setLevel(CGcvLogger::info);
  77. break;
  78. case 2:
  79. g_logger.setLevel(CGcvLogger::warning);
  80. break;
  81. case 3:
  82. g_logger.setLevel(CGcvLogger::error);
  83. break;
  84. default:
  85. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. catch(...){
  91. g_logger.ERRORINFO("set log level failed");
  92. return 1;
  93. }
  94. }
  95. int cv_init_image_saver()
  96. {
  97. if( g_cp.image_save){
  98. if(g_pImStore){
  99. string folder;
  100. g_pImStore->getStoreDir(folder);
  101. if(folder!=g_cp.image_depository){
  102. delete g_pImStore;
  103. g_pImStore = new CImStoreManager();
  104. g_pImStore->setStoreDir(g_cp.image_depository);
  105. g_pImStore->setStoreDays(g_cp.image_backup_days);
  106. }
  107. }
  108. else{
  109. g_pImStore = new CImStoreManager();
  110. g_pImStore->setStoreDir(g_cp.image_depository);
  111. g_pImStore->setStoreDays(g_cp.image_backup_days);
  112. }
  113. }
  114. else{
  115. if(g_pImStore){
  116. delete g_pImStore;
  117. g_pImStore=0;
  118. }
  119. }
  120. g_oa.set_image_saver(&g_pImStore);
  121. g_rs_cp.set_image_saver(&g_pImStore);
  122. g_sc_cp.set_image_saver(&g_pImStore);
  123. return 0;
  124. }
  125. GCV_API int cv_release()
  126. {
  127. if(g_pImStore){
  128. delete g_pImStore;
  129. g_pImStore = 0;
  130. }
  131. //DeleteCriticalSection(&g_cs);
  132. return 0;
  133. }
  134. //1
  135. int cv_init(char*conf)
  136. {
  137. //InitializeCriticalSection(&g_cs);
  138. CGCvConfig conf_contrainer = CGCvConfig();
  139. conf_contrainer.setConfParam(&g_cp);
  140. if(conf){
  141. //read configures
  142. ifstream ifs(conf);
  143. if(!ifs.good()){return 1;}
  144. ifs.close();
  145. memset(&g_cp,0,sizeof(ConfigParam));
  146. cv::FileStorage fs(conf, cv::FileStorage::READ);
  147. conf_contrainer.read(fs["conf_parameters"]);
  148. fs.release();
  149. g_conf_file = conf;
  150. }
  151. else{
  152. ifstream ifs(g_conf_file);
  153. if(!ifs.good()){return 1;}
  154. ifs.close();
  155. memset(&g_cp,0,sizeof(ConfigParam));
  156. //read configures
  157. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  158. conf_contrainer.read(fs["conf_parameters"]);
  159. fs.release();
  160. }
  161. string pinfo = get_cparam_info(&g_cp);
  162. g_logger.INFO(string("lib version: ")+string(g_version_str));
  163. g_logger.INFO(string("load parameters:\n")+pinfo);
  164. return 0;
  165. };
  166. //2
  167. void cv_set_param(ConfigParam&cp)
  168. {
  169. g_cp = cp;
  170. string pinfo = get_cparam_info(&g_cp);
  171. g_logger.INFO(string("set parameters:\n")+pinfo);
  172. };
  173. //3
  174. void cv_save_param(char* conf_file/*=0*/)
  175. {
  176. //save configures
  177. CGCvConfig conf_contrainer = CGCvConfig();
  178. conf_contrainer.setConfParam(&g_cp);
  179. if(conf_file){
  180. cv::FileStorage fs(
  181. conf_file,
  182. cv::FileStorage::WRITE
  183. );
  184. fs<<"conf_parameters";
  185. fs<<conf_contrainer;
  186. fs.release();
  187. }
  188. else{
  189. cv::FileStorage fs(
  190. g_conf_file,
  191. cv::FileStorage::WRITE
  192. );
  193. fs<<"conf_parameters";
  194. fs<<conf_contrainer;
  195. fs.release();
  196. }
  197. }
  198. //4
  199. void cv_get_param(ConfigParam&cp)
  200. {
  201. cp = g_cp;
  202. };
  203. //5
  204. void get_version(char* buf)
  205. {
  206. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  207. };
  208. //6
  209. void cv_get_conf_file(char*buff)
  210. {
  211. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  212. };
  213. //7
  214. /*int rs_oa_init()
  215. {
  216. return g_oa.reset();
  217. };*/
  218. //8
  219. /*int rs_oa_append(
  220. ImgInfo* imginfo,
  221. PositionInfo& posinfo
  222. )
  223. {
  224. memset(&posinfo,0,sizeof(PositionInfo));
  225. try{
  226. g_oa.append(imginfo, posinfo);
  227. }
  228. catch(std::exception &err){
  229. g_logger.ERRORINFO(err.what());
  230. return 1;
  231. }
  232. catch(string& msg){
  233. g_logger.ERRORINFO(msg);
  234. return 1;
  235. }
  236. catch(...){
  237. g_logger.ERRORINFO("unknown error");
  238. return 1;
  239. }
  240. return 0;
  241. };*/
  242. //
  243. int rs_grab_point(
  244. float* points,
  245. int pixel_size,
  246. int pt_size,
  247. PositionInfo& posinfo,
  248. const char* fn/*=0*/
  249. )
  250. {
  251. memset(&posinfo, 0, sizeof(PositionInfo));
  252. try {
  253. int rst = g_rs_gp.load_data(points, pixel_size, pt_size, fn);
  254. if (rst <= 0) {
  255. g_logger.ERRORINFO("invalid points");
  256. return 1;
  257. }
  258. int oa = g_rs_gp.grab_point_detect(posinfo);
  259. }
  260. catch (std::exception &err) {
  261. g_logger.ERRORINFO(err.what());
  262. return 1;
  263. }
  264. catch (string& msg) {
  265. g_logger.ERRORINFO(msg);
  266. return 1;
  267. }
  268. catch (...) {
  269. g_logger.ERRORINFO("unknown error");
  270. return 1;
  271. }
  272. return 0;
  273. }
  274. //9
  275. int rs_oa_get_result(
  276. ImgInfo* imginfo,
  277. PositionInfo& posinfo
  278. )
  279. {
  280. memset(&posinfo,0,sizeof(PositionInfo));
  281. try{
  282. double oa = g_oa.angle_recognize(imginfo, posinfo);
  283. }
  284. catch(std::exception &err){
  285. g_logger.ERRORINFO(err.what());
  286. return 1;
  287. }
  288. catch(string& msg){
  289. g_logger.ERRORINFO(msg);
  290. return 1;
  291. }
  292. catch(...){
  293. g_logger.ERRORINFO("unknown error");
  294. return 1;
  295. }
  296. return 0;
  297. }
  298. //10
  299. int rs_cut_point(
  300. ImgInfo* imginfo,
  301. PositionInfo& posinfo
  302. )
  303. {
  304. memset(&posinfo,0,sizeof(PositionInfo));
  305. try{
  306. g_rs_cp.up_point_detect(
  307. imginfo,
  308. cv::Mat(),
  309. posinfo,
  310. g_img_cache
  311. );
  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. //11
  328. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  329. {
  330. memset(&posinfo,0,sizeof(PositionInfo));
  331. try{
  332. g_rs_cp_reid.cut_point_reid(
  333. imginfo,
  334. cv::Mat(),
  335. pre_img_id,
  336. posinfo,
  337. g_img_cache
  338. );
  339. }
  340. catch(std::exception &err){
  341. g_logger.ERRORINFO(err.what());
  342. return 1;
  343. }
  344. catch(string& msg){
  345. g_logger.ERRORINFO(msg);
  346. return 1;
  347. }
  348. catch(...){
  349. g_logger.ERRORINFO("unknown error");
  350. return 1;
  351. }
  352. return 0;
  353. }
  354. //12
  355. int sc_cut_point(
  356. ImgInfo* imginfo,
  357. PositionInfo& posinfo
  358. )
  359. {
  360. memset(&posinfo,0,sizeof(PositionInfo));
  361. try{
  362. g_sc_cp.up_point_detect(
  363. imginfo,
  364. cv::Mat(),
  365. 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. };