All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
copy-feats-to-sphinx.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "matrix/kaldi-matrix.h"
#include "matrix/matrix-common.h"
#include "matrix/matrix-lib.h"
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
Include dependency graph for copy-feats-to-sphinx.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 37 of file copy-feats-to-sphinx.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), kaldi::kUndefined, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), MatrixBase< Real >::Range(), ParseOptions::Read(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), and kaldi::WriteSphinx().

37  {
38  try {
39  using namespace kaldi;
40 
41  const char *usage =
42  "Save features as Sphinx files:\n"
43  "Each utterance will be stored as a unique Sphinx file in a specified directory.\n"
44  "The Sphinx filename will correspond to the utterance-id (key) in the input table, with the specified extension.\n"
45  "Usage: copy-feats-to-sphinx [options] in-rspecifier\n"
46  "Example: copy-feats-to-sphinx --output-dir=/tmp/sphinx-features --output-ext=fea scp:feats.scp\n";
47 
48  ParseOptions po(usage);
49  std::string dir_out = "./";
50  std::string ext_out = "mfc";
51 
52  po.Register("output-ext", &ext_out, "Output extension of sphinx files");
53  po.Register("output-dir", &dir_out, "Output directory");
54 
55  po.Read(argc, argv);
56 
57  if (po.NumArgs() != 1) {
58  po.PrintUsage();
59  exit(1);
60  }
61 
62  std::string rspecifier = po.GetArg(1);
63 
64  // check or create output dir:
65  const char * c = dir_out.c_str();
66  if ( access( c, 0 ) != 0 ){
67 #if defined(_MSC_VER)
68  if (_mkdir(c) != 0)
69 #else
70  if (mkdir(c, S_IRWXU|S_IRGRP|S_IXGRP) != 0)
71 #endif
72  KALDI_ERR << "Could not create output directory: " << dir_out;
73  }
74 
75  // write to the sphinx files
76  int32 num_frames, dim, num_done=0;
77  SequentialBaseFloatMatrixReader feats_reader(rspecifier);
78  for (; !feats_reader.Done(); feats_reader.Next()) {
79  std::string utt = feats_reader.Key();
80  const Matrix<BaseFloat> &feats = feats_reader.Value();
81  num_frames = feats.NumRows(), dim = feats.NumCols();
82 
83  Matrix<BaseFloat> output(num_frames, dim, kUndefined);
84  std::stringstream ss;
85  ss << dir_out << "/" << utt << "." << ext_out;
86  output.Range(0, num_frames, 0, dim).CopyFromMat(feats.Range(0, num_frames, 0, dim));
87  std::ofstream os(ss.str().c_str(), std::ios::out|std::ios::binary);
88  WriteSphinx(os, output);
89  num_done++;
90  }
91  KALDI_LOG << num_done << " Sphinx feature files generated in the direcory: " << dir_out;
92  return (num_done != 0 ? 0 : 1);
93 
94  } catch(const std::exception &e) {
95  std::cerr << e.what();
96  return -1;
97  }
98 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
bool WriteSphinx(std::ostream &os, const MatrixBase< Real > &M)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:127
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:196
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_LOG
Definition: kaldi-error.h:133