graft_cv_api.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "logger.h"
  8. #include "utils.h"
  9. #include "imstorage_manager.h"
  10. #include "grab_point_rs.h"
  11. #include "chessboard_calibration.h"
  12. extern CRITICAL_SECTION g_cs;
  13. namespace graft_cv
  14. {
  15. char *g_version_str = "0.8.2";
  16. //configure
  17. string g_conf_file = "./gcv_conf.yml";
  18. ConfigParam g_cp;
  19. //logger
  20. ofstream g_logger_ofs;
  21. CGcvLogger g_logger = CGcvLogger(
  22. g_logger_ofs,
  23. CGcvLogger::file_and_terminal,
  24. CGcvLogger::debug,
  25. "./gcv.log");
  26. //image saver
  27. CImStoreManager* g_pImStore=0;
  28. CRootStockGrabPoint g_sola_grab_rs(g_cp, &g_logger);
  29. CRootStockGrabPoint g_sola_grab_sc(g_cp, &g_logger);
  30. CChessboardCalibration g_chessboard = CChessboardCalibration(g_cp, &g_logger);
  31. //??多线程有问题
  32. map<string, cv::Mat> g_img_cache;
  33. //1 log path
  34. int cv_set_logpath(char*lpath)
  35. {
  36. try{
  37. string mp = g_logger.getPath();
  38. string np(lpath);
  39. if(mp==np){
  40. return 0;
  41. }
  42. g_logger_ofs.close();
  43. g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
  44. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  45. g_logger_ofs<<tmp;
  46. g_logger_ofs.flush();
  47. cout<<tmp<<endl;
  48. string pinfo = get_cparam_info(&g_cp);
  49. g_logger.INFO(string("lib version: ") + string(g_version_str));
  50. g_logger.INFO(string("load parameters:\n") + pinfo);
  51. g_logger.setPath(string(lpath));
  52. return 0;
  53. }
  54. catch(...){
  55. g_logger.ERRORINFO("set log path failed");
  56. return 1;
  57. }
  58. }
  59. //2 set log level: 0-debug, 1-info, 2-warning, 3-error
  60. int cv_set_loglevel(int lev)
  61. {
  62. if(lev <0 || lev>3){
  63. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  64. return 1;
  65. }
  66. try{
  67. switch(lev){
  68. case 0:
  69. g_logger.setLevel(CGcvLogger::debug);
  70. break;
  71. case 1:
  72. g_logger.setLevel(CGcvLogger::info);
  73. break;
  74. case 2:
  75. g_logger.setLevel(CGcvLogger::warning);
  76. break;
  77. case 3:
  78. g_logger.setLevel(CGcvLogger::error);
  79. break;
  80. default:
  81. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. catch(...){
  87. g_logger.ERRORINFO("set log level failed");
  88. return 1;
  89. }
  90. }
  91. //3 初始化图片存储其
  92. int cv_init_image_saver()
  93. {
  94. if( g_cp.image_save){
  95. if(g_pImStore){
  96. string folder;
  97. g_pImStore->getStoreDir(folder);
  98. if(folder!=g_cp.image_depository){
  99. delete g_pImStore;
  100. g_pImStore = new CImStoreManager();
  101. g_pImStore->setStoreDir(g_cp.image_depository);
  102. g_pImStore->setStoreDays(g_cp.image_backup_days);
  103. }
  104. }
  105. else{
  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. if(g_pImStore){
  113. delete g_pImStore;
  114. g_pImStore=0;
  115. }
  116. }
  117. //g_oa.set_image_saver(&g_pImStore);
  118. //g_rs_cp.set_image_saver(&g_pImStore);
  119. //g_sc_cp.set_image_saver(&g_pImStore);
  120. g_sola_grab_rs.set_image_saver(&g_pImStore);
  121. g_sola_grab_sc.set_image_saver(&g_pImStore);
  122. //g_sola_reid_rs.set_image_saver(&g_pImStore);
  123. //g_sola_reid_sc.set_image_saver(&g_pImStore);
  124. g_chessboard.set_image_saver(&g_pImStore);
  125. return 0;
  126. }
  127. //4
  128. int cv_init(char*conf)
  129. {
  130. //InitializeCriticalSection(&g_cs);
  131. CGCvConfig conf_contrainer = CGCvConfig();
  132. conf_contrainer.setConfParam(&g_cp);
  133. if(conf){
  134. //read configures
  135. ifstream ifs(conf);
  136. if(!ifs.good()){return 1;}
  137. ifs.close();
  138. memset(&g_cp,0,sizeof(ConfigParam));
  139. cv::FileStorage fs(conf, cv::FileStorage::READ);
  140. conf_contrainer.read(fs["conf_parameters"]);
  141. fs.release();
  142. g_conf_file = conf;
  143. }
  144. else{
  145. ifstream ifs(g_conf_file);
  146. if(!ifs.good()){return 1;}
  147. ifs.close();
  148. memset(&g_cp,0,sizeof(ConfigParam));
  149. //read configures
  150. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  151. conf_contrainer.read(fs["conf_parameters"]);
  152. fs.release();
  153. }
  154. string pinfo = get_cparam_info(&g_cp);
  155. g_logger.INFO(string("lib version: ")+string(g_version_str));
  156. g_logger.INFO(string("load parameters:\n")+pinfo);
  157. return 0;
  158. };
  159. //5
  160. void cv_set_param(ConfigParam&cp)
  161. {
  162. g_cp = cp;
  163. string pinfo = get_cparam_info(&g_cp);
  164. g_logger.INFO(string("set parameters:\n")+pinfo);
  165. };
  166. int cv_set_param_from_file(char*conf)
  167. {
  168. if (conf == 0) {
  169. return 1;
  170. }
  171. ConfigParam cp;
  172. CGCvConfig conf_contrainer = CGCvConfig();
  173. conf_contrainer.setConfParam(&g_cp);
  174. //read configures
  175. ifstream ifs(conf);
  176. if (!ifs.good()) { return 1; }
  177. ifs.close();
  178. memset(&g_cp, 0, sizeof(ConfigParam));
  179. cv::FileStorage fs(conf, cv::FileStorage::READ);
  180. conf_contrainer.read(fs["conf_parameters"]);
  181. fs.release();
  182. g_conf_file = conf;
  183. return 0;
  184. }
  185. //6
  186. int cv_release()
  187. {
  188. if (g_pImStore) {
  189. delete g_pImStore;
  190. g_pImStore = 0;
  191. }
  192. //DeleteCriticalSection(&g_cs);
  193. return 0;
  194. }
  195. //7
  196. void cv_get_conf_file(char*buff)
  197. {
  198. strcpy_s(buff, g_conf_file.size() + 1, g_conf_file.c_str());
  199. };
  200. //8
  201. void cv_save_param(char* conf_file/*=0*/)
  202. {
  203. //save configures
  204. CGCvConfig conf_contrainer = CGCvConfig();
  205. conf_contrainer.setConfParam(&g_cp);
  206. if(conf_file){
  207. cv::FileStorage fs(
  208. conf_file,
  209. cv::FileStorage::WRITE
  210. );
  211. fs<<"conf_parameters";
  212. fs<<conf_contrainer;
  213. fs.release();
  214. }
  215. else{
  216. cv::FileStorage fs(
  217. g_conf_file,
  218. cv::FileStorage::WRITE
  219. );
  220. fs<<"conf_parameters";
  221. fs<<conf_contrainer;
  222. fs.release();
  223. }
  224. }
  225. //9
  226. void cv_get_param(ConfigParam&cp)
  227. {
  228. cp = g_cp;
  229. };
  230. //10
  231. void get_version(char* buf)
  232. {
  233. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  234. };
  235. //11
  236. int sola_grab_point_rs(
  237. float* points,
  238. int pixel_size,
  239. int pt_size,
  240. PositionInfo& posinfo,
  241. const char* fn/*=0*/
  242. )
  243. {
  244. memset(&posinfo, 0, sizeof(PositionInfo));
  245. int dtype = 1;
  246. try {
  247. int rst = g_sola_grab_rs.load_data(points, pixel_size, pt_size, dtype, fn);
  248. if (rst <= 0) {
  249. g_logger.ERRORINFO("invalid points");
  250. return 1;
  251. }
  252. int oa = g_sola_grab_rs.grab_point_detect(posinfo);
  253. if (oa != 0) {
  254. g_logger.ERRORINFO("no points");
  255. return 1;
  256. }
  257. }
  258. catch (std::exception &err) {
  259. g_logger.ERRORINFO(err.what());
  260. return 1;
  261. }
  262. catch (string& msg) {
  263. g_logger.ERRORINFO(msg);
  264. return 1;
  265. }
  266. catch (...) {
  267. g_logger.ERRORINFO("unknown error");
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. //12
  273. int sola_grab_point_sc(
  274. float* points,
  275. int pixel_size,
  276. int pt_size,
  277. PositionInfo& posinfo,
  278. const char* fn/*=0*/
  279. )
  280. {
  281. memset(&posinfo, 0, sizeof(PositionInfo));
  282. int dtype = 0;
  283. try {
  284. int rst = g_sola_grab_sc.load_data(points, pixel_size, pt_size, dtype, fn);
  285. if (rst <= 0) {
  286. g_logger.ERRORINFO("invalid points");
  287. return 1;
  288. }
  289. int oa = g_sola_grab_sc.grab_point_detect(posinfo);
  290. if (oa != 0) {
  291. g_logger.ERRORINFO("no points");
  292. return 1;
  293. }
  294. }
  295. catch (std::exception &err) {
  296. g_logger.ERRORINFO(err.what());
  297. return 1;
  298. }
  299. catch (string& msg) {
  300. g_logger.ERRORINFO(msg);
  301. return 1;
  302. }
  303. catch (...) {
  304. g_logger.ERRORINFO("unknown error");
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. //13
  310. int chessboard_calibration(float* points, int pixel_size, int pt_size, PositionInfo& posinfo, const char* fn)
  311. {
  312. memset(&posinfo, 0, sizeof(PositionInfo));
  313. int dtype = 0;
  314. try {
  315. int rst = g_chessboard.load_data(points, pixel_size, pt_size, dtype, fn);
  316. if (rst <= 0) {
  317. g_logger.ERRORINFO("invalid points");
  318. return 1;
  319. }
  320. int oa = g_chessboard.cross_detect(posinfo);
  321. if (oa != 0) {
  322. g_logger.ERRORINFO("no points");
  323. return 1;
  324. }
  325. }
  326. catch (std::exception &err) {
  327. g_logger.ERRORINFO(err.what());
  328. return 1;
  329. }
  330. catch (string& msg) {
  331. g_logger.ERRORINFO(msg);
  332. return 1;
  333. }
  334. catch (...) {
  335. g_logger.ERRORINFO("unknown error");
  336. return 1;
  337. }
  338. return 0;
  339. }
  340. };