mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2025-01-21 17:13:09 +08:00
Replace malloc / free by new / delete for CHAR_DESC_STRUCT
Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
parent
0f90ccb9cd
commit
aa8dda89a3
@ -34,13 +34,13 @@ namespace tesseract {
|
||||
// blob_text: Ground truth text for the blob.
|
||||
void Classify::LearnBlob(const std::string &fontname, TBLOB *blob, const DENORM &cn_denorm,
|
||||
const INT_FX_RESULT_STRUCT &fx_info, const char *blob_text) {
|
||||
CHAR_DESC CharDesc = NewCharDescription(feature_defs_);
|
||||
std::unique_ptr<CHAR_DESC_STRUCT> CharDesc(new CHAR_DESC_STRUCT(feature_defs_));
|
||||
CharDesc->FeatureSets[0] = ExtractMicros(blob, cn_denorm);
|
||||
CharDesc->FeatureSets[1] = ExtractCharNormFeatures(fx_info);
|
||||
CharDesc->FeatureSets[2] = ExtractIntCNFeatures(*blob, fx_info);
|
||||
CharDesc->FeatureSets[3] = ExtractIntGeoFeatures(*blob, fx_info);
|
||||
|
||||
if (ValidCharDescription(feature_defs_, CharDesc)) {
|
||||
if (ValidCharDescription(feature_defs_, CharDesc.get())) {
|
||||
// Label the features with a class name and font name.
|
||||
tr_file_data_ += "\n";
|
||||
tr_file_data_ += fontname;
|
||||
@ -49,11 +49,10 @@ void Classify::LearnBlob(const std::string &fontname, TBLOB *blob, const DENORM
|
||||
tr_file_data_ += "\n";
|
||||
|
||||
// write micro-features to file and clean up
|
||||
WriteCharDescription(feature_defs_, CharDesc, tr_file_data_);
|
||||
WriteCharDescription(feature_defs_, CharDesc.get(), tr_file_data_);
|
||||
} else {
|
||||
tprintf("Blob learned was invalid!\n");
|
||||
}
|
||||
FreeCharDescription(CharDesc);
|
||||
} // LearnBlob
|
||||
|
||||
// Writes stored training data to a .tr file based on the given filename.
|
||||
|
@ -91,47 +91,6 @@ void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs) {
|
||||
}
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* Release the memory consumed by the specified character
|
||||
* description and all of the features in that description.
|
||||
*
|
||||
* @param CharDesc character description to be deallocated
|
||||
*
|
||||
* Globals:
|
||||
* - none
|
||||
*/
|
||||
void FreeCharDescription(CHAR_DESC CharDesc) {
|
||||
if (CharDesc) {
|
||||
for (size_t i = 0; i < CharDesc->NumFeatureSets; i++) {
|
||||
FreeFeatureSet(CharDesc->FeatureSets[i]);
|
||||
}
|
||||
free(CharDesc);
|
||||
}
|
||||
} /* FreeCharDescription */
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* Allocate a new character description, initialize its
|
||||
* feature sets to be empty, and return it.
|
||||
*
|
||||
* Globals:
|
||||
* - none
|
||||
*
|
||||
* @return New character description structure.
|
||||
*/
|
||||
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
|
||||
CHAR_DESC CharDesc;
|
||||
CharDesc = static_cast<CHAR_DESC>(malloc(sizeof(CHAR_DESC_STRUCT)));
|
||||
CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
|
||||
|
||||
for (size_t i = 0; i < CharDesc->NumFeatureSets; i++) {
|
||||
CharDesc->FeatureSets[i] = nullptr;
|
||||
}
|
||||
|
||||
return (CharDesc);
|
||||
} /* NewCharDescription */
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* Appends a textual representation of CharDesc to str.
|
||||
@ -147,7 +106,7 @@ CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
|
||||
* @param str string to append CharDesc to
|
||||
* @param CharDesc character description to write to File
|
||||
*/
|
||||
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, std::string &str) {
|
||||
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc, std::string &str) {
|
||||
int NumSetsToWrite = 0;
|
||||
|
||||
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
|
||||
@ -169,7 +128,7 @@ void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC Char
|
||||
|
||||
// Return whether all of the fields of the given feature set
|
||||
// are well defined (not inf or nan).
|
||||
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc) {
|
||||
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc) {
|
||||
bool anything_written = false;
|
||||
bool well_formed = true;
|
||||
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
|
||||
@ -210,17 +169,16 @@ bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC Char
|
||||
* @param File open text file to read character description from
|
||||
* @return Character description read from File.
|
||||
*/
|
||||
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File) {
|
||||
CHAR_DESC_STRUCT *ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File) {
|
||||
int NumSetsToRead;
|
||||
char ShortName[FEAT_NAME_SIZE];
|
||||
CHAR_DESC CharDesc;
|
||||
int Type;
|
||||
|
||||
ASSERT_HOST(tfscanf(File, "%d", &NumSetsToRead) == 1);
|
||||
ASSERT_HOST(NumSetsToRead >= 0);
|
||||
ASSERT_HOST(NumSetsToRead <= FeatureDefs.NumFeatureTypes);
|
||||
|
||||
CharDesc = NewCharDescription(FeatureDefs);
|
||||
auto CharDesc = new CHAR_DESC_STRUCT(FeatureDefs);
|
||||
for (; NumSetsToRead > 0; NumSetsToRead--) {
|
||||
tfscanf(File, "%s", ShortName);
|
||||
Type = ShortNameToFeatureType(FeatureDefs, ShortName);
|
||||
|
@ -37,35 +37,46 @@ extern TESS_API const char *const kGeoFeatureType;
|
||||
feature consists of a number of parameters. All features within a
|
||||
feature set contain the same number of parameters.*/
|
||||
|
||||
struct CHAR_DESC_STRUCT {
|
||||
uint32_t NumFeatureSets;
|
||||
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES];
|
||||
};
|
||||
using CHAR_DESC = CHAR_DESC_STRUCT *;
|
||||
|
||||
struct FEATURE_DEFS_STRUCT {
|
||||
int32_t NumFeatureTypes;
|
||||
const FEATURE_DESC_STRUCT *FeatureDesc[NUM_FEATURE_TYPES];
|
||||
};
|
||||
using FEATURE_DEFS = FEATURE_DEFS_STRUCT *;
|
||||
|
||||
struct CHAR_DESC_STRUCT {
|
||||
/// Allocate a new character description, initialize its
|
||||
/// feature sets to be empty, and return it.
|
||||
CHAR_DESC_STRUCT(const FEATURE_DEFS_STRUCT &FeatureDefs) {
|
||||
NumFeatureSets = FeatureDefs.NumFeatureTypes;
|
||||
for (size_t i = 0; i < NumFeatureSets; i++) {
|
||||
FeatureSets[i] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/// Release the memory consumed by the specified character
|
||||
/// description and all of the features in that description.
|
||||
~CHAR_DESC_STRUCT() {
|
||||
for (size_t i = 0; i < NumFeatureSets; i++) {
|
||||
FreeFeatureSet(FeatureSets[i]);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t NumFeatureSets;
|
||||
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES];
|
||||
};
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
Generic functions for manipulating character descriptions
|
||||
----------------------------------------------------------------------*/
|
||||
TESS_API
|
||||
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs);
|
||||
|
||||
TESS_API
|
||||
void FreeCharDescription(CHAR_DESC CharDesc);
|
||||
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc);
|
||||
|
||||
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs);
|
||||
|
||||
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc);
|
||||
|
||||
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, std::string &str);
|
||||
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc, std::string &str);
|
||||
|
||||
TESS_API
|
||||
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File);
|
||||
CHAR_DESC_STRUCT *ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File);
|
||||
|
||||
TESS_API
|
||||
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName);
|
||||
|
@ -377,7 +377,6 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_definitions, const c
|
||||
char unichar[UNICHAR_LEN + 1];
|
||||
LABELEDLIST char_sample;
|
||||
FEATURE_SET feature_samples;
|
||||
CHAR_DESC char_desc;
|
||||
uint32_t feature_type = ShortNameToFeatureType(feature_definitions, feature_name);
|
||||
|
||||
// Zero out the font_sample_count for all the classes.
|
||||
@ -407,7 +406,7 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_definitions, const c
|
||||
char_sample = NewLabeledList(unichar);
|
||||
*training_samples = push(*training_samples, char_sample);
|
||||
}
|
||||
char_desc = ReadCharDescription(feature_definitions, file);
|
||||
auto char_desc = ReadCharDescription(feature_definitions, file);
|
||||
feature_samples = char_desc->FeatureSets[feature_type];
|
||||
if (char_sample->font_sample_count < max_samples || max_samples <= 0) {
|
||||
char_sample->List = push(char_sample->List, feature_samples);
|
||||
|
@ -168,7 +168,7 @@ void MasterTrainer::ReadTrainingSamples(const char *page_name,
|
||||
tprintf("Bad format in tr file, reading box coords\n");
|
||||
continue;
|
||||
}
|
||||
CHAR_DESC char_desc = ReadCharDescription(feature_defs, fp);
|
||||
auto char_desc = ReadCharDescription(feature_defs, fp);
|
||||
auto *sample = new TrainingSample;
|
||||
sample->set_font_id(font_id);
|
||||
sample->set_page_num(page_number + page_images_.size());
|
||||
@ -176,7 +176,7 @@ void MasterTrainer::ReadTrainingSamples(const char *page_name,
|
||||
sample->ExtractCharDesc(int_feature_type, micro_feature_type, cn_feature_type, geo_feature_type,
|
||||
char_desc);
|
||||
AddSample(verification, unichar.c_str(), sample);
|
||||
FreeCharDescription(char_desc);
|
||||
delete char_desc;
|
||||
}
|
||||
charsetsize_ = unicharset_.size();
|
||||
fclose(fp);
|
||||
|
Loading…
Reference in New Issue
Block a user