tea_cv_api.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include <string.h>
  2. #include <fstream>
  3. #include "tea_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 "tea_sorter.h"
  14. extern CRITICAL_SECTION g_cs;
  15. namespace graft_cv
  16. {
  17. char *g_version_str = "0.1.1";
  18. //configure
  19. string g_conf_file = "./tcv_conf.yml";
  20. ConfigParam g_cp;
  21. //logger
  22. ofstream g_logger_ofs;
  23. CGcvLogger g_logger = CGcvLogger(
  24. g_logger_ofs,
  25. CGcvLogger::file_and_terminal,
  26. CGcvLogger::debug,
  27. "./tcv.log");
  28. //image saver
  29. CImStoreManager* g_pImStore=0;
  30. //implement
  31. CTeaSort g_tg = CTeaSort(g_cp, img_type::tea_grab, &g_logger);
  32. CTeaSort g_tc = CTeaSort(g_cp, img_type::tea_cut, &g_logger);
  33. //??¶àÏß³ÌÓÐÎÊÌâ
  34. map<string, cv::Mat> g_img_cache;
  35. //1 log path
  36. int cv_set_logpath(char*lpath)
  37. {
  38. try{
  39. string mp = g_logger.getPath();
  40. string np(lpath);
  41. if(mp==np){
  42. return 0;
  43. }
  44. g_logger_ofs.close();
  45. g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
  46. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  47. g_logger_ofs<<tmp;
  48. g_logger_ofs.flush();
  49. cout<<tmp<<endl;
  50. g_logger.setPath(string(lpath));
  51. return 0;
  52. }
  53. catch(...){
  54. g_logger.ERRORINFO("set log path failed");
  55. return 1;
  56. }
  57. }
  58. //2 0-debug, 1-info, 2-warning, 3-error
  59. int cv_set_loglevel(int lev)
  60. {
  61. if(lev <0 || lev>3){
  62. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  63. return 1;
  64. }
  65. try{
  66. switch(lev){
  67. case 0:
  68. g_logger.setLevel(CGcvLogger::debug);
  69. break;
  70. case 1:
  71. g_logger.setLevel(CGcvLogger::info);
  72. break;
  73. case 2:
  74. g_logger.setLevel(CGcvLogger::warning);
  75. break;
  76. case 3:
  77. g_logger.setLevel(CGcvLogger::error);
  78. break;
  79. default:
  80. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. catch(...){
  86. g_logger.ERRORINFO("set log level failed");
  87. return 1;
  88. }
  89. }
  90. //3
  91. int cv_init_image_saver()
  92. {
  93. if( g_cp.image_save){
  94. if(g_pImStore){
  95. string folder;
  96. g_pImStore->getStoreDir(folder);
  97. if(folder!=g_cp.image_depository){
  98. delete g_pImStore;
  99. g_pImStore = new CImStoreManager();
  100. g_pImStore->setStoreDir(g_cp.image_depository);
  101. g_pImStore->setStoreDays(g_cp.image_backup_days);
  102. }
  103. }
  104. else{
  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. if(g_pImStore){
  112. delete g_pImStore;
  113. g_pImStore=0;
  114. }
  115. }
  116. g_tg.set_image_saver(&g_pImStore);
  117. g_tc.set_image_saver(&g_pImStore);
  118. return 0;
  119. }
  120. //4
  121. int cv_init(char*conf)
  122. {
  123. //InitializeCriticalSection(&g_cs);
  124. CGCvConfig conf_contrainer = CGCvConfig();
  125. conf_contrainer.setConfParam(&g_cp);
  126. if(conf){
  127. //read configures
  128. ifstream ifs(conf);
  129. if(!ifs.good()){return 1;}
  130. ifs.close();
  131. memset(&g_cp,0,sizeof(ConfigParam));
  132. cv::FileStorage fs(conf, cv::FileStorage::READ);
  133. conf_contrainer.read(fs["conf_parameters"]);
  134. fs.release();
  135. g_conf_file = conf;
  136. }
  137. else{
  138. ifstream ifs(g_conf_file);
  139. if(!ifs.good()){return 1;}
  140. ifs.close();
  141. memset(&g_cp,0,sizeof(ConfigParam));
  142. //read configures
  143. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  144. conf_contrainer.read(fs["conf_parameters"]);
  145. fs.release();
  146. }
  147. string pinfo = get_cparam_info(&g_cp);
  148. g_logger.INFO(string("lib version: ")+string(g_version_str));
  149. g_logger.INFO(string("load parameters:\n")+pinfo);
  150. return 0;
  151. };
  152. //5
  153. void cv_set_param(ConfigParam&cp)
  154. {
  155. g_cp = cp;
  156. string pinfo = get_cparam_info(&g_cp);
  157. g_logger.INFO(string("set parameters:\n")+pinfo);
  158. };
  159. int cv_set_param_from_file(char*conf)
  160. {
  161. if (conf == 0) {
  162. return 1;
  163. }
  164. ConfigParam cp;
  165. CGCvConfig conf_contrainer = CGCvConfig();
  166. conf_contrainer.setConfParam(&g_cp);
  167. //read configures
  168. ifstream ifs(conf);
  169. if (!ifs.good()) { return 1; }
  170. ifs.close();
  171. memset(&g_cp, 0, sizeof(ConfigParam));
  172. cv::FileStorage fs(conf, cv::FileStorage::READ);
  173. conf_contrainer.read(fs["conf_parameters"]);
  174. fs.release();
  175. g_conf_file = conf;
  176. return 0;
  177. }
  178. //6
  179. int cv_release()
  180. {
  181. if(g_pImStore){
  182. delete g_pImStore;
  183. g_pImStore = 0;
  184. }
  185. //DeleteCriticalSection(&g_cs);
  186. return 0;
  187. }
  188. //7
  189. void cv_get_conf_file(char*buff)
  190. {
  191. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  192. };
  193. //8
  194. void cv_save_param(char* conf_file/*=0*/)
  195. {
  196. //save configures
  197. CGCvConfig conf_contrainer = CGCvConfig();
  198. conf_contrainer.setConfParam(&g_cp);
  199. if(conf_file){
  200. cv::FileStorage fs(
  201. conf_file,
  202. cv::FileStorage::WRITE
  203. );
  204. fs<<"conf_parameters";
  205. fs<<conf_contrainer;
  206. fs.release();
  207. }
  208. else{
  209. cv::FileStorage fs(
  210. g_conf_file,
  211. cv::FileStorage::WRITE
  212. );
  213. fs<<"conf_parameters";
  214. fs<<conf_contrainer;
  215. fs.release();
  216. }
  217. }
  218. //9
  219. void cv_get_param(ConfigParam&cp)
  220. {
  221. cp = g_cp;
  222. };
  223. //10
  224. void get_version(char* buf)
  225. {
  226. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  227. };
  228. //11
  229. int tea_grab_point(
  230. ImgInfo* imginfo,
  231. PositionInfo& posinfo,
  232. const char* fn
  233. )
  234. {
  235. memset(&posinfo,0,sizeof(PositionInfo));
  236. try{
  237. int r = g_tg.load_model();
  238. if (r != 0) {
  239. g_logger.ERRORINFO("model load failed");
  240. return 1;
  241. }
  242. r = g_tg.detect(imginfo, posinfo, fn);
  243. return r;
  244. }
  245. catch(std::exception &err){
  246. g_logger.ERRORINFO(err.what());
  247. return 1;
  248. }
  249. catch(string& msg){
  250. g_logger.ERRORINFO(msg);
  251. return 1;
  252. }
  253. catch(...){
  254. g_logger.ERRORINFO("unknown error");
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. //12
  260. int tea_cut_point(
  261. ImgInfo* imginfo,
  262. PositionInfo& posinfo,
  263. const char* fn
  264. )
  265. {
  266. memset(&posinfo, 0, sizeof(PositionInfo));
  267. try {
  268. int r = g_tc.load_model();
  269. if (r != 0) {
  270. g_logger.ERRORINFO("model load failed");
  271. return 1;
  272. }
  273. r = g_tc.detect(imginfo, posinfo, fn);
  274. return r;
  275. }
  276. catch (std::exception &err) {
  277. g_logger.ERRORINFO(err.what());
  278. return 1;
  279. }
  280. catch (string& msg) {
  281. g_logger.ERRORINFO(msg);
  282. return 1;
  283. }
  284. catch (...) {
  285. g_logger.ERRORINFO("unknown error");
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. };