diff --git a/NAMESPACE b/NAMESPACE index 82d55e9..e153fbc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,7 +5,6 @@ export(image_scores) export(nlp_scores) export(punctuate) export(setup_miniconda) -export(setup_modules) export(simulate_video) export(transformer_scores) export(video_scores) diff --git a/R/image_scores.R b/R/image_scores.R index 1c57975..68510c6 100644 --- a/R/image_scores.R +++ b/R/image_scores.R @@ -17,18 +17,12 @@ image_scores <- function(image, classes, face_selection = "largest"){ - if (!(reticulate::condaenv_exists("transforEmotion"))){ - print("Creating and switching to transforEmotion virtual Python environment...") - Sys.sleep(1) - setup_miniconda() - } else - { - reticulate::use_condaenv("transforEmotion", required = FALSE) + if (!conda_check()){ + stop("Python environment 'transforEmotion' is not available. Please run setup_miniconda() to install it.") + } - if (!reticulate::py_module_available("transformers") &!reticulate::py_module_available("face_recognition")){ - print("Some Python libraries are not available in the transforEmotion conda environment. We are going to set them up. We need them for facial recognition and emotion detection in text, images and video. This may take a while, please be patient.") - Sys.sleep(1) - setup_modules() + else { + reticulate::use_condaenv("transforEmotion", required = FALSE) } source_python(system.file("python", "image.py", package = "transforEmotion")) diff --git a/R/setup_miniconda.R b/R/setup_miniconda.R index 0378547..15b3db8 100644 --- a/R/setup_miniconda.R +++ b/R/setup_miniconda.R @@ -15,33 +15,6 @@ conda_check <- function(){ return (tE_env!=0) } -#' Check if required Python libraries are installed in a Conda environment -#' -#' This function checks if a list of required Python libraries are installed in a specified Conda environment. -#' -#' @return A logical value indicating whether all the required Python libraries are installed. -#' reticulate::py_module_available("transformers") -check_python_libs <- function() { - Sys.setenv(TF_CPP_MIN_LOG_LEVEL = '3') - python_libs <- c("transformers", "torch", "torchvision", "torchaudio", "tensorflow", "pytube", "pytz", "cv2") - - # Extract the names of the libraries - lib_names <- sapply(strsplit(python_libs, " "), `[`, 1) - libs_installed <- logical(length(python_libs)) - - # Check if each Python library is installed - for (i in seq_along(python_libs)) { - python_lib <- python_libs[i] - if (!reticulate::py_module_available(python_lib)) { - print(paste("Python library", python_lib, "is not installed in Conda environment")) - libs_installed[i] <- FALSE - } else{ - libs_installed[i] <- TRUE - } - } - return(all(libs_installed)) -} - #' Install Miniconda and activate the transforEmotion environment #' #' @description Installs miniconda and activates the transforEmotion environment @@ -58,58 +31,31 @@ check_python_libs <- function() { setup_miniconda <- function() { Sys.setenv(TF_CPP_MIN_LOG_LEVEL = '3') + # Install miniconda path_to_miniconda <- try( - reticulate::install_miniconda(), + install_miniconda(), silent = TRUE ) - # Check for try-error - if(any(class(path_to_miniconda) != "try-error")){ - - # Give user the deets + if(any(class(path_to_miniconda) != "try-error")){ message("\nTo uninstall miniconda, use `reticulate::miniconda_uninstall()`") - } # Create transformEmotion enviroment if it doesn't exist te_ENV <- conda_check() - if (!te_ENV){ + print("Creating 'transforEmotion' environment...") path_to_env <- try( - reticulate::conda_create("transforEmotion"), + conda_create("transforEmotion"), silent = TRUE ) - - # Check for try-error - if(any(class(path_to_env) != "try-error")){ - - # Give user the deets - message("\nNew Python virtual environment created. To remove it, use: \n `reticulate::conda_remove(\"transforEmotion\")`") - } } - # Activate the environment - Sys.unsetenv("RETICULATE_PYTHON") reticulate::use_condaenv("transforEmotion", required = TRUE) - - # Check if the enviroment is activated - - if (grepl("transforEmotion", reticulate::py_config()$python)){ - message("\ntransforEmotion Python virtual environment activated") - if (!check_python_libs()) - { - print("Installing missing Python libraries...") + + print("Installing missing Python libraries...") setup_modules() - } - - } else { - # throw an error if the environment is not activated - print("Your active Python environment is:") - print(reticulate::py_config()$python) - stop("Please activate the transforEmotion Python environment instead: `reticulate::use_condaenv(\"transforEmotion\", required = TRUE)`") - } - } diff --git a/R/setup_modules.R b/R/setup_modules.R index 681b92d..30307ea 100644 --- a/R/setup_modules.R +++ b/R/setup_modules.R @@ -19,18 +19,12 @@ #' #' @author Alexander P. Christensen #' -#' @export #' # Install modules -# Updated 13.04.2022 +# Updated 11.12.2023 + setup_modules <- function() { - Sys.setenv(TF_CPP_MIN_LOG_LEVEL = '3') - # Check if transforEmotion conda env is being used - if (!grepl("transforEmotion", reticulate::py_config()$python)) - { - reticulate::use_condaenv("transforEmotion", required = TRUE) - } # Install modules message("\nInstalling modules for 'transforEmotion'...") @@ -43,15 +37,9 @@ setup_modules <- function() "transformers", "pytube", "pytz", + "pandas", "opencv-python" ), pip = TRUE ) - if (!check_python_libs()) - { - stop("Not all Python libraries were installed correctly. Please try again.") - } - else { - message("All Python libraries were installed correctly.") - } } diff --git a/R/video_scores.R b/R/video_scores.R index f84f2f5..fd1fe02 100644 --- a/R/video_scores.R +++ b/R/video_scores.R @@ -24,25 +24,18 @@ #' video_scores <- function(video, classes, nframes=100, face_selection = "largest", cut = FALSE, start = 0, end = 60, uniform = FALSE, ffreq = 15, save_video = FALSE, save_frames = FALSE, save_dir = "temp/", video_name = "temp"){ - ################################################################ - # TODO this piece of code needs to be a function, parametrized by the use of Python libraries: text, image, video - if (!conda_check()){ - print("Creating and switching to transforEmotion virtual Python environment...") - Sys.sleep(1) - setup_miniconda() - } else - { - reticulate::use_condaenv("transforEmotion", required = FALSE) + if (!conda_check()){ + stop("Python environment 'transforEmotion' is not available. Please run setup_miniconda() to install it.") + } - if (!check_python_libs()){ - print("Some Python libraries are not available in the transforEmotion conda environment. We are going to set them up. We need them for facial recognition and emotion detection in text, images and video. This may take a while, please be patient.") - Sys.sleep(1) - setup_modules() + else { + reticulate::use_condaenv("transforEmotion", required = FALSE) } - ################################################################ reticulate::source_python(system.file("python", "image.py", package = "transforEmotion")) + reticulate::source_python(system.file("python", "video.py", package = "transforEmotion")) + if (!grepl("youtu", video)){ stop("You need to provide a YouTube video URL.") } diff --git a/R/zzz.R b/R/zzz.R index 77f010c..19df9a4 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -7,11 +7,9 @@ msg <- styletext(styletext(paste("\ntransforEmotion (version ", packageVersion("transforEmotion"), ")\n", sep = ""), defaults = "underline"), defaults = "bold") msg <- paste(msg,'\nFor help getting started, type browseVignettes("transforEmotion")\n') msg <- paste(msg,"\nFor bugs and errors, submit an issue to ") + msg <- paste(msg,"\nBefore running any analysis, please run `transforEmotion::setup_miniconda()` to install the necessary Python modules.") packageStartupMessage(msg) Sys.unsetenv("RETICULATE_PYTHON") - tryCatch({ - reticulate::use_condaenv("transforEmotion", required = TRUE) - }, error = function(e) { - message("Python transforEmotion conda environment not activate. \nPlease run `setup_miniconda()` to install and activate the the environment with all required libraries. \nIf this issue persists submit an issue to \n ", e$message) -}) + Sys.setenv(RETICULATE_PYTHON_ENV = "transforEmotion") + library(reticulate) } diff --git a/man/check_python_libs.Rd b/man/check_python_libs.Rd deleted file mode 100644 index 0a4bca5..0000000 --- a/man/check_python_libs.Rd +++ /dev/null @@ -1,15 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/setup_miniconda.R -\name{check_python_libs} -\alias{check_python_libs} -\title{Check if required Python libraries are installed in a Conda environment} -\usage{ -check_python_libs() -} -\value{ -A logical value indicating whether all the required Python libraries are installed. -reticulate::py_module_available("transformers") -} -\description{ -This function checks if a list of required Python libraries are installed in a specified Conda environment. -} diff --git a/temp.R b/temp.R index 4223dd0..b98557c 100644 --- a/temp.R +++ b/temp.R @@ -17,6 +17,7 @@ transforEmotion:::conda_check() transforEmotion:::check_python_libs() +reticulate::import("pandas") ##### IMAGE TEST url = "https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Donald_Trump_official_portrait.jpg/330px-Donald_Trump_official_portrait.jpg" @@ -27,5 +28,6 @@ transforEmotion::image_scores(url, classes) # done video = "https://www.youtube.com/watch?v=720O_yBLrTs&ab_channel=DonaldJTrump" -transforEmotion::video_scores(video, classes, nframes = 10) +Sys.setenv(RETICULATE_PYTHON_ENV = "transforEmotion") +library(reticulate)