Skip to content

Commit

Permalink
Merge branch 'master' into cpp11
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev authored Aug 6, 2024
2 parents 11223e4 + 7688528 commit 559495a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Imports:
digest
LinkingTo:
cpp11
RoxygenNote: 7.3.1
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
2 changes: 1 addition & 1 deletion R/tesseract.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
tesseract <- local({
store <- new.env()
function(language = "eng", datapath = NULL, configs = NULL, options = NULL, cache = TRUE){
datapath <- as.character(datapath)
datapath <- normalizePath(as.character(datapath), mustWork = TRUE)
language <- as.character(language)
configs <- as.character(configs)
options <- as.list(options)
Expand Down
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 559495a

Please sign in to comment.