123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- #include <string.h>
- #include <fstream>
- #include "graft_cv_api.h"
- #include "data_def.h"
- #include "data_def_api.h"
- #include "config.h"
- #include "logger.h"
- #include "utils.h"
- #include "imstorage_manager.h"
- #include "grab_point_rs.h"
- #include "chessboard_calibration.h"
- extern CRITICAL_SECTION g_cs;
- namespace graft_cv
- {
- char *g_version_str = "0.8.4";
- //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;
- CRootStockGrabPoint g_sola_grab_rs(g_cp, &g_logger);
- CRootStockGrabPoint g_sola_grab_sc(g_cp, &g_logger);
- CChessboardCalibration g_chessboard = CChessboardCalibration(g_cp, &g_logger);
- //??多线程有问题
- map<string, cv::Mat> g_img_cache;
- //1 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;
- 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);
- g_logger.setPath(string(lpath));
- return 0;
- }
- catch(...){
- g_logger.ERRORINFO("set log path failed");
- return 1;
- }
- }
- //2 set log level: 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;
- }
- }
- //3 初始化图片存储其
- 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);
- g_sola_grab_rs.set_image_saver(&g_pImStore);
- g_sola_grab_sc.set_image_saver(&g_pImStore);
- //g_sola_reid_rs.set_image_saver(&g_pImStore);
- //g_sola_reid_sc.set_image_saver(&g_pImStore);
- g_chessboard.set_image_saver(&g_pImStore);
- return 0;
- }
- //4
- 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));
- cv::FileStorage fs(conf, cv::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
- cv::FileStorage fs(g_conf_file, cv::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;
- };
- //5
- void cv_set_param(ConfigParam&cp)
- {
- g_cp = cp;
- string pinfo = get_cparam_info(&g_cp);
- g_logger.INFO(string("set parameters:\n")+pinfo);
- };
- int cv_set_param_from_file(char*conf)
- {
- if (conf == 0) {
- return 1;
- }
- ConfigParam cp;
- CGCvConfig conf_contrainer = CGCvConfig();
- conf_contrainer.setConfParam(&g_cp);
- //read configures
- ifstream ifs(conf);
- if (!ifs.good()) { return 1; }
- ifs.close();
-
- memset(&g_cp, 0, sizeof(ConfigParam));
- cv::FileStorage fs(conf, cv::FileStorage::READ);
- conf_contrainer.read(fs["conf_parameters"]);
- fs.release();
- g_conf_file = conf;
- return 0;
- }
- //6
- int cv_release()
- {
- if (g_pImStore) {
- delete g_pImStore;
- g_pImStore = 0;
- }
- //DeleteCriticalSection(&g_cs);
- return 0;
- }
- //7
- void cv_get_conf_file(char*buff)
- {
- strcpy_s(buff, g_conf_file.size() + 1, g_conf_file.c_str());
- };
- //8
- void cv_save_param(char* conf_file/*=0*/)
- {
- //save configures
- CGCvConfig conf_contrainer = CGCvConfig();
- conf_contrainer.setConfParam(&g_cp);
- if(conf_file){
- cv::FileStorage fs(
- conf_file,
- cv::FileStorage::WRITE
- );
- fs<<"conf_parameters";
- fs<<conf_contrainer;
- fs.release();
- }
- else{
- cv::FileStorage fs(
- g_conf_file,
- cv::FileStorage::WRITE
- );
- fs<<"conf_parameters";
- fs<<conf_contrainer;
- fs.release();
- }
- }
- //9
- void cv_get_param(ConfigParam&cp)
- {
- cp = g_cp;
- };
- //10
- void get_version(char* buf)
- {
- strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
- };
- //11
- int sola_grab_point_rs(
- float* points,
- int pixel_size,
- int pt_size,
- PositionInfo& posinfo,
- const char* fn/*=0*/
- )
- {
- memset(&posinfo, 0, sizeof(PositionInfo));
- int dtype = 1;
- try {
- int rst = g_sola_grab_rs.load_data(points, pixel_size, pt_size, dtype, fn);
- if (rst <= 0) {
- g_logger.ERRORINFO("invalid points");
- return 1;
- }
- int oa = g_sola_grab_rs.grab_point_detect(posinfo);
- if (oa != 0) {
- g_logger.ERRORINFO("no points");
- return 1;
- }
- }
- 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 sola_grab_point_sc(
- float* points,
- int pixel_size,
- int pt_size,
- PositionInfo& posinfo,
- const char* fn/*=0*/
- )
- {
- memset(&posinfo, 0, sizeof(PositionInfo));
- int dtype = 0;
- try {
- int rst = g_sola_grab_sc.load_data(points, pixel_size, pt_size, dtype, fn);
- if (rst <= 0) {
- g_logger.ERRORINFO("invalid points");
- return 1;
- }
- int oa = g_sola_grab_sc.grab_point_detect(posinfo);
- if (oa != 0) {
- g_logger.ERRORINFO("no points");
- return 1;
- }
- }
- 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;
- }
- //13
- int chessboard_calibration(float* points, int pixel_size, int pt_size, PositionInfo& posinfo, const char* fn)
- {
- memset(&posinfo, 0, sizeof(PositionInfo));
- int dtype = 0;
- try {
- int rst = g_chessboard.load_data(points, pixel_size, pt_size, dtype, fn);
- if (rst <= 0) {
- g_logger.ERRORINFO("invalid points");
- return 1;
- }
- int oa = g_chessboard.cross_detect(posinfo);
- if (oa != 0) {
- g_logger.ERRORINFO("no points");
- return 1;
- }
- }
- 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;
- }
- };
|