Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an example of using with leptonica's c-api #60

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
run: sudo apt-get install libleptonica-dev libtesseract-dev clang tesseract-ocr-eng
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
run: cargo build --verbose --no-default-features
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix build failures that depend on a newer version of tesseract

https://github.com/houqp/leptess/actions/runs/7589902627/job/20675452376

- name: Lint code
run: cargo clippy
run: cargo clippy --no-default-features
- name: Run tests
run: cargo test --verbose
run: cargo test --verbose --no-default-features
- name: Check formatting
run: cargo fmt -- --check
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ license = "MIT"
documentation = "https://houqp.github.io/leptess/leptess/index.html"

[dependencies]
tesseract-plumbing = "~0.11"
tesseract-plumbing = {version = "~0.11", default-features = false}
thiserror = "1"
leptonica-plumbing = "~1.3.0"
Copy link
Collaborator Author

@ccouzens ccouzens Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify this directly rather than as a transient dependency, as the example depends on the latest version.


[dev-dependencies]
image = "0.24.7"
regex = "1.4.3"

[features]
default = ["tesseract_5_2"]
tesseract_5_2 = ["tesseract-plumbing/tesseract_5_2"]
tesseract_5_2 = ["tesseract-plumbing/tesseract_5_2"]
19 changes: 19 additions & 0 deletions examples/with_leptonica_c_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extern crate leptess;

use leptess::{capi, tesseract};
use std::ffi::CString;

fn main() {
let tessdata_path = Some("./tests/tessdata");
let mut api = tesseract::TessApi::new(tessdata_path, "eng").unwrap();

let image_path = CString::new("./tests/di.png").unwrap();
let mut pix = leptonica_plumbing::Pix::read(&image_path).unwrap();

let pix_scaled_c = unsafe { capi::pixScaleSmooth(pix.as_mut(), 2.0, 2.0).as_mut() }
.expect("pointer should not be null");
let pix_scaled_safe = unsafe { leptonica_plumbing::Pix::new_from_pointer(pix_scaled_c) };
api.set_image(&pix_scaled_safe);

println!("Text: {}", api.get_utf8_text().unwrap());
}
6 changes: 6 additions & 0 deletions src/leptonica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ impl Pix {
}
}

impl AsRef<leptonica_plumbing::Pix> for Pix {
fn as_ref(&self) -> &leptonica_plumbing::Pix {
&self.raw
}
}

#[derive(thiserror::Error, Debug)]
pub enum PixError {
#[error("Failed to read image from {}", .0)]
Expand Down
4 changes: 2 additions & 2 deletions src/tesseract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl TessApi {
/// set_image clears all recognition results, and sets the rectangle to the full image, so it
/// may be followed immediately by a `[Self::get_utf8_text]`, and it will automatically perform
/// recognition.
pub fn set_image(&mut self, img: &leptonica::Pix) {
self.raw.set_image_2(&img.raw)
pub fn set_image(&mut self, img: impl AsRef<tesseract_plumbing::leptonica_plumbing::Pix>) {
self.raw.set_image_2(img.as_ref())
}

/// Get the dimensions of the currently loaded image, or None if no image is loaded.
Expand Down
Loading