Skip to content

Commit

Permalink
Add option to download best models with Tesseract 4 or higher (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Aug 3, 2024
1 parent 9131570 commit 59d4e34
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Imports:
rappdirs,
digest
LinkingTo: Rcpp
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Suggests:
magick (>= 1.7),
Expand Down
25 changes: 18 additions & 7 deletions R/tessdata.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#' Tesseract Training Data
#'
#' Helper function to download training data from the official
#' [tessdata](https://tesseract-ocr.github.io/tessdoc/Data-Files) repository. Only use this function on
#' Windows and OS-X. On Linux, training data can be installed directly with
#' [tessdata](https://tesseract-ocr.github.io/tessdoc/Data-Files) repository. On Linux, the fast training data can be installed directly with
#' [yum](https://src.fedoraproject.org/rpms/tesseract) or
#' [apt-get](https://packages.debian.org/search?suite=stable&section=all&arch=any&searchon=names&keywords=tesseract-ocr-).
#'
Expand All @@ -23,31 +22,43 @@
#' @family tesseract
#' @param lang three letter code for language, see [tessdata](https://github.com/tesseract-ocr/tessdata) repository.
#' @param datapath destination directory where to download store the file
#' @param model either `fast` or `best` is currently supported. The latter downloads
#' more accurate (but slower) trained models for Tesseract 4.0 or higher
#' @param progress print progress while downloading
#' @references [tesseract wiki: training data](https://tesseract-ocr.github.io/tessdoc/Data-Files)
#' @examples \dontrun{
#' if(is.na(match("fra", tesseract_info()$available)))
#' tesseract_download("fra")
#' tesseract_download("fra", model = 'best')
#' french <- tesseract("fra")
#' text <- ocr("https://jeroen.github.io/images/french_text.png", engine = french)
#' cat(text)
#' }
tesseract_download <- function(lang, datapath = NULL, progress = interactive()){
tesseract_download <- function(lang, datapath = NULL, model = c("fast", "best"), progress = interactive()) {
stopifnot(is.character(lang))
model <- match.arg(model)
if(!length(datapath)){
warn_on_linux()
datapath <- tesseract_info()$datapath
}
datapath <- normalizePath(datapath, mustWork = TRUE)
version <- tesseract_version_major()

if(version < 4){
repo <- "tessdata"
release <- "3.04.00"
} else {
repo <- "tessdata_fast"
repo <- paste0("tessdata_", model)
release <- "4.1.0"
}
url <- sprintf('https://github.com/tesseract-ocr/%s/raw/%s/%s.traineddata', repo, release, lang)

url <- sprintf("https://github.com/tesseract-ocr/%s/raw/%s/%s.traineddata", repo, release, lang)

destfile <- file.path(datapath, basename(url))

if (file.exists(destfile)) {
message(paste("Training data already exists. Overwriting", destfile))
}

req <- curl::curl_fetch_memory(url, curl::new_handle(
progressfunction = progress_fun,
noprogress = !isTRUE(progress)
Expand All @@ -56,7 +67,7 @@ tesseract_download <- function(lang, datapath = NULL, progress = interactive()){
cat("\n")
if(req$status_code != 200)
stop("Download failed: HTTP ", req$status_code, call. = FALSE)
destfile <- file.path(datapath, basename(url))

writeBin(req$content, destfile)
return(destfile)
}
Expand Down
4 changes: 2 additions & 2 deletions man/ocr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions man/tessdata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 59d4e34

Please sign in to comment.