Skip to content

Commit

Permalink
First working version of the library
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideGalilei committed Jan 4, 2022
1 parent 0618d2f commit fe160aa
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 236 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"nim.buildOnSave": false,
"nim.buildCommand": "c",
"nim.lintOnSave": true,
"nim.project": ["./src/nim_tesseract.nim"],
"nim.project": ["./src/nimtesseract.nim"],
"nim.licenseString": ""
}
24 changes: 24 additions & 0 deletions LICENSE
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/>
63 changes: 31 additions & 32 deletions README.md
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.
52 changes: 52 additions & 0 deletions examples/advanced.nim
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"
11 changes: 11 additions & 0 deletions examples/simple.nim
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
2 changes: 2 additions & 0 deletions nim_tesseract.nimble → nimtesseract.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ author = "Davide Galilei"
description = "A wrapper to Tesseract OCR library for Nim"
license = "MIT"
srcDir = "src"

requires "pixie"
146 changes: 0 additions & 146 deletions src/nim_tesseract.nim

This file was deleted.

32 changes: 0 additions & 32 deletions src/nim_tesseract/bindings/wrapper.nim

This file was deleted.

12 changes: 0 additions & 12 deletions src/nim_tesseract/submodule.nim

This file was deleted.

Loading

0 comments on commit fe160aa

Please sign in to comment.