@edumedia/handwriting-character-recognition
Version:
Tool to recognize handwritten characters
97 lines (88 loc) • 2.79 kB
TypeScript
type CharacterPoint = [number, number];
type CharacterStroke = CharacterPoint[];
type CharacterDrawing = {
id: string;
strokes: CharacterStroke[];
strokeWidth: number;
width: number;
height: number;
};
type TransformResult = {
cropped: CharacterDrawing;
stretched: CharacterDrawing;
last: CharacterDrawing;
};
type CharacterMatch = {
character: string;
drawing: CharacterDrawing;
distance: number;
};
type GuessResult = {
transformResult: TransformResult;
character: string;
topMatches: CharacterMatch[];
confidence: number;
};
type CharacterResult = {
resultType: "character";
characterString: string;
};
type DebugResult = {
resultType: "debug";
characterString: string;
guessResults: GuessResult[];
};
type AnalyzerResult = CharacterResult | DebugResult;
/**
* Used to process and analyze a CharacterDrawing
*
* @example
* // Create your DrawingAnalyzer instance
* const drawingAnalyzer = new DrawingAnalyzer()
*
* // Initialize a drawing
* const userDrawing: CharacterDrawing = {
* id: "user",
* strokes: [],
* strokeWidth: 10,
* width: 280,
* height: 280,
* };
*
* // Let your user fill `userDrawing.strokes` with a great UI
*
* // Analyze user drawing and guess the handwritten number
* const analyzerResult = drawingAnalyzer.analyze(userDrawing)
*
* // Do something with the result
* console.log(analyzerResult.characterString)
*/
declare class DrawingAnalyzer {
private readonly guesser;
private readonly characterSplitter;
private readonly logger;
private readonly library;
private readonly showLogs;
/**
* constructor description
*
* dataPath: Optional, provide the path to your data if you don't want to use the default `/data.json`
*
* showLogs: Optional, output debug logs in the console
*/
constructor(drawingAnalyzerProps?: {
dataPath?: string;
showLogs?: boolean;
});
/**
* Main function to be used after collecting points with a drawing component
*
* Analyzes a given character drawing to detect characters and determine their accuracy.
*
* @param {CharacterDrawing} drawing - The drawing input containing character strokes to analyze.
* @param {number} [numberOfCharToDetect] - Optional parameter specifying the number of characters to detect from the drawing. ONLY IMPLEMENTED WITH VALUE `1`
* @return {AnalyzerResult} The analysis results, including the detected character string and details of individual character guesses.
*/
analyze(drawing: CharacterDrawing, numberOfCharToDetect?: number): AnalyzerResult;
}
export { type CharacterDrawing, type CharacterPoint, type CharacterStroke, DrawingAnalyzer };