diff --git a/.gitignore b/.gitignore index 9324ffb..182b07a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,8 @@ vesseract *.so *.dylib *.dll + +# Tesseract *.txt -*.xml \ No newline at end of file +*.xml +*.box \ No newline at end of file diff --git a/README.md b/README.md index 34892bd..1b3d54a 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ langs := vesseract.get_languages() or { panic(err) } println("$langs") // Get alto xml - Require Tesseract >4.1.0 -alto := vesseract.image_to_alto_xml('sample/demo.png') or { panic(err) } +alto := vesseract.image_to_alto_xml_path('sample/demo.png') or { panic(err) } // "XML: ... " println("XML: $alto") ``` diff --git a/alternatives.v b/alternatives.v new file mode 100644 index 0000000..41e9d46 --- /dev/null +++ b/alternatives.v @@ -0,0 +1,13 @@ +module vesseract + +// Variant of image_to_alto_xml() but don't need extra parameters +[inline] +pub fn image_to_alto_xml_path(image_path string) ?string { + return image_to_alto_xml(image: image_path, lang: 'eng', args: '') +} + +// Variant of image_to_string, only a file path is required +[inline] +pub fn image_to_string_path(filepath string) ?string { + return image_to_string(image: filepath, lang: 'eng', args: '') +} \ No newline at end of file diff --git a/vesseract.v b/vesseract.v index 94e7cf8..103297b 100644 --- a/vesseract.v +++ b/vesseract.v @@ -2,6 +2,17 @@ module vesseract import os +// Used for bounding box detection +pub struct Tesseract_box { +pub: + letter string + x1 int + y1 int + x2 int + y2 int +} + +// Used as a parameter pub struct Tesseract { pub: // Image path @@ -12,6 +23,7 @@ pub: lang string = 'eng' } +// Used to make it easier to get tesseract version pub struct Tesseract_version { pub: major int @@ -43,11 +55,6 @@ pub fn image_to_string(t Tesseract) ?string { return str[..str.len - 2] } -// Variant of image_to_string, only a file path is required -pub fn image_to_string_path(filepath string) ?string { - return image_to_string(image: filepath, lang: 'eng', args: '') -} - // Get installed languages from Tesseract-OCR // return a list of languages code pub fn get_languages() ?[]string { @@ -104,7 +111,7 @@ pub fn get_tesseract_version() ?Tesseract_version { } // Get alto representation from Tesseract-OCR as XML format -pub fn image_to_alto_xml(image string) ?string { +pub fn image_to_alto_xml(t Tesseract) ?string { // Tesseract option: -c tessedit_create_alto=1 // Check version for alto support @@ -119,7 +126,7 @@ pub fn image_to_alto_xml(image string) ?string { xml_filename := id + '.xml' // Run tesseract - run_tesseract([image, id, '-c tessedit_create_alto=1']) or { return err } + run_tesseract([t.image, id, '-c tessedit_create_alto=1', t.args]) or { return err } // Read output xml := os.read_file(xml_filename) or { return err } diff --git a/vesseract_test.v b/vesseract_test.v index 09e67df..23f4ba6 100644 --- a/vesseract_test.v +++ b/vesseract_test.v @@ -50,6 +50,11 @@ fn test_image_to_string_empty() { } fn test_image_to_alto_xml() { - xml := image_to_alto_xml('sample/demo.png') or { panic(err) } + xml := image_to_alto_xml(image: 'sample/demo.png', lang: 'eng', args: '') or { panic(err) } + assert xml.contains('http://www.loc.gov/standards/alto/ns-v3#') +} + +fn test_image_to_alto_xml_path() { + xml := image_to_alto_xml_path('sample/demo.png') or { panic(err) } assert xml.contains('http://www.loc.gov/standards/alto/ns-v3#') }