123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- #include <string.h>
- #include <fstream>
- #include "graft_cv_api.h"
- #include "data_def.h"
- #include "data_def_api.h"
- #include "config.h"
- #include "optimal_angle.h"
- #include "cut_point_rs.h"
- #include "cut_point_sc.h"
- #include "logger.h"
- #include "utils.h"
- #include "imstorage_manager.h"
- extern CRITICAL_SECTION g_cs;
- namespace graft_cv
- {
- char *g_version_str = "0.5.9.20";
- //configure
- string g_conf_file = "./gcv_conf.yml";
- ConfigParam g_cp;
- //logger
- ofstream g_logger_ofs;
- CGcvLogger g_logger = CGcvLogger(
- g_logger_ofs,
- CGcvLogger::file_and_terminal,
- CGcvLogger::debug,
- "./gcv.log");
- //image saver
-
- CImStoreManager* g_pImStore=0;
- //implement
- COptimalAnglePart g_oa = COptimalAnglePart(g_cp,&g_logger);
- CRootStockCutPoint g_rs_cp = CRootStockCutPoint(g_cp,&g_logger);
- CScionCutPoint g_sc_cp = CScionCutPoint(g_cp,&g_logger);
-
- //0 log path
- int cv_set_logpath(char*lpath)
- {
- try{
- string mp = g_logger.getPath();
- string np(lpath);
- if(mp==np){
- return 0;
- }
- g_logger_ofs.close();
- g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
- string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
-
- g_logger_ofs<<tmp;
- g_logger_ofs.flush();
- cout<<tmp<<endl;
- g_logger.setPath(string(lpath));
- return 0;
- }
- catch(...){
- g_logger.ERRORINFO("set log path failed");
- return 1;
- }
- }
- // 0-debug, 1-info, 2-warning, 3-error
- int cv_set_loglevel(int lev)
- {
- if(lev <0 || lev>3){
- g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
- return 1;
- }
- try{
- switch(lev){
- case 0:
- g_logger.setLevel(CGcvLogger::debug);
- break;
- case 1:
- g_logger.setLevel(CGcvLogger::info);
- break;
- case 2:
- g_logger.setLevel(CGcvLogger::warning);
- break;
- case 3:
- g_logger.setLevel(CGcvLogger::error);
- break;
- default:
- g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
- return 1;
- }
- return 0;
- }
- catch(...){
- g_logger.ERRORINFO("set log level failed");
- return 1;
- }
- }
- int cv_init_image_saver()
- {
- if( g_cp.image_save){
- if(g_pImStore){
- string folder;
- g_pImStore->getStoreDir(folder);
- if(folder!=g_cp.image_depository){
- delete g_pImStore;
- g_pImStore = new CImStoreManager();
- g_pImStore->setStoreDir(g_cp.image_depository);
- g_pImStore->setStoreDays(g_cp.image_backup_days);
- }
- }
- else{
- g_pImStore = new CImStoreManager();
- g_pImStore->setStoreDir(g_cp.image_depository);
- g_pImStore->setStoreDays(g_cp.image_backup_days);
- }
- }
- else{
- if(g_pImStore){
- delete g_pImStore;
- g_pImStore=0;
- }
- }
- g_oa.set_image_saver(&g_pImStore);
- g_rs_cp.set_image_saver(&g_pImStore);
- g_sc_cp.set_image_saver(&g_pImStore);
- return 0;
- }
- GCV_API int cv_release()
- {
- if(g_pImStore){
- delete g_pImStore;
- g_pImStore = 0;
- }
- //DeleteCriticalSection(&g_cs);
- return 0;
- }
- //1
- int cv_init(char*conf)
- {
- //InitializeCriticalSection(&g_cs);
- CGCvConfig conf_contrainer = CGCvConfig();
- conf_contrainer.setConfParam(&g_cp);
- if(conf){
- //read configures
- ifstream ifs(conf);
- if(!ifs.good()){return 1;}
- ifs.close();
- memset(&g_cp,0,sizeof(ConfigParam));
- FileStorage fs(conf, FileStorage::READ);
- conf_contrainer.read(fs["conf_parameters"]);
- fs.release();
- g_conf_file = conf;
-
-
- }
- else{
- ifstream ifs(g_conf_file);
- if(!ifs.good()){return 1;}
- ifs.close();
- memset(&g_cp,0,sizeof(ConfigParam));
-
- //read configures
- FileStorage fs(g_conf_file, FileStorage::READ);
- conf_contrainer.read(fs["conf_parameters"]);
- fs.release();
-
- }
- string pinfo = get_cparam_info(&g_cp);
- g_logger.INFO(string("lib version: ")+string(g_version_str));
- g_logger.INFO(string("load parameters:\n")+pinfo);
-
- return 0;
- };
- //2
- void cv_set_param(ConfigParam&cp)
- {
- g_cp = cp;
- string pinfo = get_cparam_info(&g_cp);
- g_logger.INFO(string("set parameters:\n")+pinfo);
- };
- //3
- void cv_save_param(char* conf_file/*=0*/)
- {
- //save configures
- CGCvConfig conf_contrainer = CGCvConfig();
- conf_contrainer.setConfParam(&g_cp);
- if(conf_file){
- FileStorage fs(
- conf_file,
- FileStorage::WRITE
- );
- fs<<"conf_parameters";
- fs<<conf_contrainer;
- fs.release();
- }
- else{
- FileStorage fs(
- g_conf_file,
- FileStorage::WRITE
- );
- fs<<"conf_parameters";
- fs<<conf_contrainer;
- fs.release();
- }
- }
- //4
- void cv_get_param(ConfigParam&cp)
- {
- cp = g_cp;
- };
- //5
- void get_version(char* buf)
- {
- strcpy(buf, g_version_str);
- };
-
- //6
- void cv_get_conf_file(char*buff)
- {
- strcpy(buff, g_conf_file.c_str());
- };
- //7
- int rs_oa_init()
- {
- return g_oa.reset();
- };
- //8
- int rs_oa_append(
- ImgInfo* imginfo,
- PositionInfo& posinfo
- )
- {
- memset(&posinfo,0,sizeof(PositionInfo));
- try{
- g_oa.append(imginfo, posinfo);
-
- }
- catch(std::exception &err){
- g_logger.ERRORINFO(err.what());
- return 1;
- }
- catch(string& msg){
- g_logger.ERRORINFO(msg);
- return 1;
- }
- catch(...){
- g_logger.ERRORINFO("unknown error");
- return 1;
- }
- return 0;
- };
- //9
- int rs_oa_get_result(PositionInfo& posinfo)
- {
- memset(&posinfo,0,sizeof(PositionInfo));
- try{
- double oa = g_oa.infer(posinfo);
-
- }
- catch(std::exception &err){
- g_logger.ERRORINFO(err.what());
- return 1;
- }
- catch(string& msg){
- g_logger.ERRORINFO(msg);
- return 1;
- }
- catch(...){
- g_logger.ERRORINFO("unknown error");
- return 1;
- }
- return 0;
-
- }
- //10
- int rs_cut_point(
- ImgInfo* imginfo,
- PositionInfo& posinfo
- )
- {
- memset(&posinfo,0,sizeof(PositionInfo));
- try{
- g_rs_cp.up_point_detect(
- imginfo,
- Mat(),
- posinfo
- );
- }
- catch(std::exception &err){
- g_logger.ERRORINFO(err.what());
- return 1;
- }
- catch(string& msg){
- g_logger.ERRORINFO(msg);
- return 1;
- }
- catch(...){
- g_logger.ERRORINFO("unknown error");
- return 1;
- }
- return 0;
-
- }
- //11
- /*int rs_cut_point_reid(ImgInfo*imginfo , double edge_length, PositionInfo& posinfo)
- {
- memset(&posinfo,0,sizeof(PositionInfo));
- try{
- g_rs_cp.up_point_reid(
- imginfo,
- Mat(),
- edge_length,
- posinfo
- );
- }
- catch(std::exception &err){
- g_logger.ERRORINFO(err.what());
- return 1;
- }
- catch(string& msg){
- g_logger.ERRORINFO(msg);
- return 1;
- }
- catch(...){
- g_logger.ERRORINFO("unknown error");
- return 1;
- }
- return 0;
- }*/
- //12
- int sc_cut_point(
- ImgInfo* imginfo,
- PositionInfo& posinfo
- )
- {
- memset(&posinfo,0,sizeof(PositionInfo));
- try{
- g_sc_cp.up_point_detect(
- imginfo,
- Mat(),
- posinfo
- );
- }
- catch(std::exception &err){
- g_logger.ERRORINFO(err.what());
- return 1;
- }
- catch(string& msg){
- g_logger.ERRORINFO(msg);
- return 1;
- }
- catch(...){
- g_logger.ERRORINFO("unknown error");
- return 1;
- }
- return 0;
- }
- };
|