示例#1
0
/** parsing function; do not call directly but 
* call "Load" instead
*/
void VisionConductor::ParseFromFile(const string& filename)
{
  ifstream file(filename.c_str());
  if (!file) {
    throw HVEFileNotFound(filename);
  }

  // $IT_DATA environmet variable, NULL if not set
  const char *it_data = getenv("IT_DATA");
  string std_it_data;
  if (it_data!=NULL) {
    std_it_data = ConvertPathToStandard(it_data);
  } else {
    std_it_data = "";
  }

  string line;
  do {
    getline(file, line);
  } while (line=="" || line[0]=='#');

  if (line==HV_CONDUCTOR_VERSION_1_5_STRING) {
	  // version 1.5

    do {
      getline(file, line);
    } while (line=="" || line[0]=='#');

    {
      char* buf = new char[line.length()];
      int scanned = sscanf(line.c_str(), "camera calibration: %s", buf);
      if (scanned!=1) {
        throw HVEFile(filename, string("expected camera calibration file, found: ")+line);
      }  
      m_camera_calib = string(buf);
      delete[] buf;

      // substitute $IT_DATA environment variable, if requested
      string::size_type pos = m_camera_calib.find("$IT_DATA");
      if (pos!=string::npos) {
        if (it_data==NULL) {
          throw HVEFile(filename, "The file requests the environment variable "
            "$IT_DATA to be set.");
        }
        // substitute "$IT_DATA" for the value of that environment variable
        ReplaceAll(m_camera_calib, "$IT_DATA", std_it_data);
      }

      if (m_camera_calib!="-") {
        FILE* fp = fopen(m_camera_calib.c_str(), "rb");
        if (!fp) {
          throw HVEFile(filename, string("can not find file: ")+m_camera_calib);
        }
      } else {
        m_camera_calib = "";
      }
    }


    // what sort of camera exposure control is desired
    do {
      getline(file, line);
    } while (line=="" || line[0]=='#');

    {
      char* buf = new char[line.length()];
      int scanned = sscanf(line.c_str(), "camera exposure: %s", buf);
      if (scanned!=1) {
        throw HVEFile(filename, string("expected camera exposure control setting, found: ")+line);
      }
      if (strcmp(buf, "camera")==0) {
        m_adjust_exposure = false;
      } else if (strcmp(buf, "software")==0) {
        m_adjust_exposure = true;
      } else {
        throw HVEFile(filename, string("expected camera exposure control setting (\"camera\" or \"software\"), found: ")+line);
      }
      delete[] buf;
    }

    // detection parameters
    do {
      getline(file, line);
    } while (line=="" || line[0]=='#');
    float coverage;
    float radius;
    int scanned = sscanf(line.c_str(), "detection params: coverage %f, duration %d, radius %f",
      &coverage, &m_dt_min_match_duration, &radius);
    if (scanned!=3) {
      throw HVEFile(filename, string("expected detection params, found: ")+line);
    }  
    m_dt_min_color_coverage = coverage;
    if (radius>1) {
      HVEFile(filename, "radius must be width-relative");
    }
    m_dt_radius = radius;

    // tracking parameters
    do {
      getline(file, line);
    } while (line=="" || line[0]=='#');
    float min_dist;
    float max_err;
    scanned = sscanf(line.c_str(), "tracking params: num_f %d, min_f %d, win_w %d, win_h %d, min_dist %f, max_err %f",
      &m_tr_num_KLT_features, &m_tr_min_KLT_features, &m_tr_winsize_width, &m_tr_winsize_height, &min_dist, &max_err);
    if (scanned!=6) {
      throw HVEFile(filename, string("expected tracking params, found: ")+line);
    }  
    m_tr_min_feature_distance = min_dist;
    if (max_err<=0) {
      m_tr_max_feature_error = DBL_MAX;  // DBL_MAX switches this off
    } else {
      m_tr_max_feature_error = max_err;
    }


    // tracking style
    do {
      getline(file, line);
    } while (line=="" || line[0]=='#');
    string::size_type pos = line.find("tracking style: ");
    if (pos==string::npos) {
      throw HVEFile(filename, string("expected tracking style, found: ")+line);
    }
    string style = line.substr(strlen("tracking style: "));
    if (style=="OPTICAL_FLOW_ONLY") {
      m_tr_type = VC_TT_OPTICAL_FLOW_ONLY;
    } else if (style=="OPTICAL_FLOW_COLOR") {
      m_tr_type = VC_TT_OPTICAL_FLOW_COLOR;
    } else if (style=="OPTICAL_FLOW_FLOCK") {
      m_tr_type = VC_TT_OPTICAL_FLOW_FLOCK;
    } else if (style=="OPTICAL_FLOW_COLORFLOCK") {
      m_tr_type = VC_TT_OPTICAL_FLOW_COLORFLOCK;
    } else if (style=="CAMSHIFT_HSV") {
      m_tr_type = VC_TT_CAMSHIFT_HSV;
    } else if (style=="CAMSHIFT_LEARNED") {
      m_tr_type = VC_TT_CAMSHIFT_LEARNED;
    } else {
      throw HVEFile(filename, string("wrong tracking style: ")+style);
    }

    // recognition params
    do {
      getline(file, line);
    } while (line=="" || line[0]=='#');
    float max_scan_width;
    float max_scan_height;
    scanned = sscanf(line.c_str(), "recognition params: max_scan_width %f, max_scan_height %f",
      &max_scan_width, &max_scan_height);
    if (scanned!=2) {
      throw HVEFile(filename, string("expected recognition params, found: ")+line);
    }  
    m_rc_max_scan_width = max_scan_width;
    m_rc_max_scan_height = max_scan_height;

    m_rc_scale_tolerance = 1.75;  // magic!


    int num;
    // detection cascades
    m_dt_cascades_start = 0;
    num = ReadScannerData(file, filename, "detection");
    m_dt_cascades_end = m_dt_cascades_start+num;

    // tracking cascades
    m_tr_cascades_start = m_dt_cascades_end;
    num = ReadScannerData(file, filename, "tracking");
    m_tr_cascades_end = m_tr_cascades_start+num;
    
    // recognition cascades
    m_rc_cascades_start = m_tr_cascades_end;
    num = ReadScannerData(file, filename, "recognition");
    m_rc_cascades_end = m_rc_cascades_start+num;

    //
    // load masks
    //
    do {
      getline(file, line);
    } while (line=="" || line[0]=='#');
    scanned = sscanf(line.c_str(), "%d masks", &num);
    if (scanned!=1) {
      throw HVEFile(filename, string("expected number of masks, found: ")+line);
    }
    for (int mcnt=0; mcnt<num; mcnt++) {
      Mask mask;
      string mask_filename;
      do {
        getline(file, mask_filename);
      } while (mask_filename=="" || mask_filename[0]=='#');
      // substitute $IT_DATA environment variable, if requested
      string::size_type pos = mask_filename.find("$IT_DATA");
      if (pos!=string::npos) {
        if (it_data==NULL) {
          throw HVEFile(filename, "The file requests the environment variable "
            "$IT_DATA to be set.");
        }
        // substitute "$IT_DATA" for the value of that environment variable
        ReplaceAll(mask_filename, "$IT_DATA", std_it_data);
      }

      mask.ParseFrom(ConvertPathToWindows(mask_filename).c_str());
      if (m_masks.find(mask.GetName())!=m_masks.end()) {
        throw HVException(string("mask '")+mask.GetName()+string("' exists already!"));
      }
      m_masks[mask.GetName()] = mask;
    }

  } else {
    throw HVEFile(filename, string("Unknown or no conductor file version: ")+line);
  }
}