-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First working version of the library
- Loading branch information
1 parent
0618d2f
commit fe160aa
Showing
15 changed files
with
258 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <http://unlicense.org/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,47 @@ | ||
> # ⚠️ NOT WORKING YET - Work in progress | ||
# Nim Tesseract | ||
# Nim Tesseract 👑👁 | ||
Nim Tesseract is a Nim wrapper for the [Tesseract](https://github.com/tesseract-ocr/tesseract/) OCR library, via its dynamic library. | ||
|
||
### Development usage: | ||
Download trained data from https://github.com/tesseract-ocr/tessdata and put it into src folder | ||
|
||
```bash | ||
$ cd src | ||
$ TESSDATA_PREFIX=$(pwd) nim r -d:pixieUseStb nim_tesseract.nim | ||
``` | ||
|
||
`capi.h` reference: https://github.com/tesseract-ocr/tesseract/blob/main/include/tesseract/capi.h | ||
|
||
#TODO | ||
- Working library | ||
- - Recognize text | ||
- - Choose between pixie (I don't know how to get a proper imagedata and pixel depth) or nim cv2 lib (outdated) | ||
- Better description | ||
- Fix memory leaks | ||
- Fix `=destroy` maybe | ||
- Rename modules from `nim_tesseract` to `nimtesseract` | ||
- Remove bindings directory (failed attempts to create bindings via futhark of tesseract's `capi.h`) | ||
|
||
## Installation | ||
## Installation 👇 | ||
```bash | ||
$ nimble install https://github.com/DavideGalilei/nimtesseract | ||
``` | ||
|
||
## Usage | ||
Install (lib)tesseract via your packet manager or put the tesseract so/dll/dylib file in the project directory. | ||
|
||
## Usage 🌷 | ||
1. Install (lib)tesseract via your packet manager or put the tesseract so/dll/dylib file in the project directory | ||
E.g. for Arch Linux (from AUR): | ||
```bash | ||
$ yay -Sy tesseract | ||
``` | ||
2. Download trained data from https://github.com/tesseract-ocr/tessdata or https://github.com/tesseract-ocr/tessdata_fast | ||
3. Done ✅ | ||
|
||
## Example | ||
## Example 🤔 | ||
```nim | ||
discard "#TODO" | ||
import nimtesseract | ||
echo imageToText("file.png") | ||
``` | ||
More examples in the [examples folder](/examples) | ||
|
||
## Development 🔩 | ||
Download trained data and put it into src folder | ||
|
||
> ⚠️ Outdated, but still useful. Don't refer to this. | ||
> ```bash | ||
> $ cd src | ||
> $ TESSDATA_PREFIX=$(pwd) nim r -d:pixieUseStb nimtesseract.nim | ||
> ``` | ||
Run tests with nimble: | ||
```bash | ||
$ nimble test | ||
``` | ||
## Credits | ||
`capi.h` reference: https://github.com/tesseract-ocr/tesseract/blob/main/include/tesseract/capi.h | ||
|
||
## Credits 👻 | ||
Inspired from https://github.com/Altabeh/tesseract-ocr-wrapper | ||
|
||
## License | ||
#TODO ... | ||
## License 📕 | ||
This project is under the `Unlicense` license. | ||
This is free and unencumbered software released into the public domain. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pixie | ||
|
||
import std/[os, json] | ||
import ../src/nimtesseract | ||
|
||
# We get a handle to the created Tesseract instance | ||
let handle = TessBaseAPICreate() | ||
|
||
# Initialize it | ||
let status = TessBaseAPIInit3( | ||
handle = handle, | ||
language = cstring("eng"), | ||
datapath = cstring(currentSourcePath() / "../../tests"), | ||
) | ||
|
||
# Check if things went wrong | ||
if int(status) == -1: | ||
raise newException(TesseractError, "Couldn't initialize tesseract") | ||
|
||
# Open the image with Pixie | ||
let imagePath = currentSourcePath.parentDir / "test.png" | ||
var image = readImage(imagePath) | ||
|
||
# Pixie stores images as a list of RGBA tuples, hence 4 bytes each | ||
const bytesPerPixel = 4 | ||
|
||
# Set the image to recognize the text from | ||
TessBaseAPISetImage( | ||
handle = handle, | ||
imagedata = addr(image.data[0]), # Note the [0], from where the actual pixels start | ||
width = cint(image.width), | ||
height = cint(image.height), | ||
bytes_per_pixel = cint(bytesPerPixel), | ||
bytes_per_line = cint(image.width * bytesPerPixel), | ||
) | ||
|
||
# Set the PPI | ||
TessBaseAPISetSourceResolution( | ||
handle = handle, | ||
ppi = cint(70), | ||
) | ||
|
||
# Get the text. Note: since it's a cstring, | ||
# we need to convert it back to Nim's string | ||
# using cstring's `$` procedure. | ||
let text = $TessBaseAPIGetUTF8Text(handle = handle) | ||
|
||
# Don't forget to clean up to avoid memory leaks! | ||
TessBaseAPIDelete(handle = handle) | ||
|
||
echo "Recognized text: ", escapeJson(text) | ||
# Should look like "Noisy image\nto test\nTesseract OCR\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import std/[os, json, options] | ||
import ../src/nimtesseract | ||
|
||
# Make sure you downloaded eng.traineddata file | ||
let recognized = imageToText( | ||
"test.png", | ||
language = some "eng", | ||
datapath = some currentSourcePath() / "../../tests" | ||
) | ||
echo "Recognized text: ", escapeJson(recognized) | ||
# Should look like "Noisy image\nto test\nTesseract OCR\n" |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.