2007-03-08 04:03:40 +08:00
|
|
|
/******************************************************************************
|
|
|
|
** Filename: blobclass.c
|
|
|
|
** Purpose: High level blob classification and training routines.
|
|
|
|
** Author: Dan Johnson
|
|
|
|
** History: 7/21/89, DSJ, Created.
|
|
|
|
**
|
|
|
|
** (c) Copyright Hewlett-Packard Company, 1988.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**----------------------------------------------------------------------------
|
|
|
|
Include Files and Type Defines
|
|
|
|
----------------------------------------------------------------------------**/
|
|
|
|
#include "blobclass.h"
|
|
|
|
#include "fxdefs.h"
|
|
|
|
#include "extract.h"
|
|
|
|
#include "efio.h"
|
|
|
|
#include "callcpp.h"
|
|
|
|
#include "chartoname.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#define MAXFILENAME 80
|
|
|
|
#define MAXMATCHES 10
|
|
|
|
|
2010-05-20 05:08:08 +08:00
|
|
|
static const char kUnknownFontName[] = "UnknownFont";
|
|
|
|
|
|
|
|
STRING_VAR(classify_font_name, kUnknownFontName,
|
2009-07-11 10:17:36 +08:00
|
|
|
"Default font name to be used in training");
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
/**----------------------------------------------------------------------------
|
|
|
|
Global Data Definitions and Declarations
|
|
|
|
----------------------------------------------------------------------------**/
|
|
|
|
/* name of current image file being processed */
|
|
|
|
extern char imagefile[];
|
|
|
|
|
|
|
|
/**----------------------------------------------------------------------------
|
|
|
|
Public Code
|
|
|
|
----------------------------------------------------------------------------**/
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-05-20 05:08:08 +08:00
|
|
|
void LearnBlob(const STRING& filename,
|
|
|
|
TBLOB * Blob, TEXTROW * Row, const char* BlobText) {
|
2007-03-08 04:03:40 +08:00
|
|
|
/*
|
|
|
|
** Parameters:
|
|
|
|
** Blob blob whose micro-features are to be learned
|
|
|
|
** Row row of text that blob came from
|
|
|
|
** BlobText text that corresponds to blob
|
|
|
|
** TextLength number of characters in blob
|
|
|
|
** Globals:
|
|
|
|
** imagefile base filename of the page being learned
|
2009-07-11 10:17:36 +08:00
|
|
|
** classify_font_name
|
|
|
|
** name of font currently being trained on
|
2007-03-08 04:03:40 +08:00
|
|
|
** Operation:
|
|
|
|
** Extract micro-features from the specified blob and append
|
|
|
|
** them to the appropriate file.
|
|
|
|
** Return: none
|
|
|
|
** Exceptions: none
|
|
|
|
** History: 7/28/89, DSJ, Created.
|
|
|
|
*/
|
|
|
|
#define TRAIN_SUFFIX ".tr"
|
|
|
|
static FILE *FeatureFile = NULL;
|
2009-07-11 10:17:36 +08:00
|
|
|
STRING Filename(filename);
|
|
|
|
|
|
|
|
// If no fontname was set, try to extract it from the filename
|
2010-05-20 05:08:08 +08:00
|
|
|
STRING CurrFontName = classify_font_name;
|
|
|
|
if (CurrFontName != kUnknownFontName) {
|
2009-07-11 10:17:36 +08:00
|
|
|
// filename is expected to be of the form [lang].[fontname].exp[num]
|
|
|
|
// The [lang], [fontname] and [num] fields should not have '.' characters.
|
|
|
|
const char *basename = strrchr(filename.string(), '/');
|
2010-05-20 05:08:08 +08:00
|
|
|
const char *firstdot = strchr(basename ? basename : filename.string(), '.');
|
2009-07-11 10:17:36 +08:00
|
|
|
const char *lastdot = strrchr(filename.string(), '.');
|
|
|
|
if (firstdot != lastdot && firstdot != NULL && lastdot != NULL) {
|
2010-05-20 05:08:08 +08:00
|
|
|
strncpy(&CurrFontName[0], firstdot + 1, lastdot - firstdot - 1);
|
|
|
|
CurrFontName[lastdot - firstdot - 1] = '\0';
|
2009-07-11 10:17:36 +08:00
|
|
|
}
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
// if a feature file is not yet open, open it
|
|
|
|
// the name of the file is the name of the image plus TRAIN_SUFFIX
|
|
|
|
if (FeatureFile == NULL) {
|
2009-07-11 10:17:36 +08:00
|
|
|
Filename += TRAIN_SUFFIX;
|
2010-05-20 05:08:08 +08:00
|
|
|
FeatureFile = Efopen(Filename.string(), "w");
|
|
|
|
cprintf("TRAINING ... Font name = %s\n", CurrFontName.string());
|
|
|
|
}
|
|
|
|
|
|
|
|
LearnBlob(FeatureFile, Blob, Row, BlobText, CurrFontName.string());
|
|
|
|
} // LearnBlob
|
|
|
|
|
|
|
|
void LearnBlob(FILE* FeatureFile, TBLOB* Blob, TEXTROW* Row,
|
|
|
|
const char* BlobText, const char* FontName) {
|
|
|
|
CHAR_DESC CharDesc;
|
|
|
|
LINE_STATS LineStats;
|
|
|
|
|
|
|
|
EnterLearnMode;
|
|
|
|
|
|
|
|
GetLineStatsFromRow(Row, &LineStats);
|
|
|
|
|
|
|
|
CharDesc = ExtractBlobFeatures (Blob, &LineStats);
|
|
|
|
if (CharDesc == NULL) {
|
|
|
|
cprintf("LearnBLob: CharDesc was NULL. Aborting.\n");
|
|
|
|
return;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// label the features with a class name and font name
|
2010-05-20 05:08:08 +08:00
|
|
|
fprintf (FeatureFile, "\n%s %s ", FontName, BlobText);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
// write micro-features to file and clean up
|
2007-07-18 09:15:07 +08:00
|
|
|
WriteCharDescription(FeatureFile, CharDesc);
|
|
|
|
FreeCharDescription(CharDesc);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
} // LearnBlob
|