Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
kumeS committed Jul 25, 2023
1 parent 37de0e0 commit 0a347c4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
13 changes: 7 additions & 6 deletions R/Display.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @description This function displays images that are stored in a list. It also has options for saving the images to a .Rds or .png file.
#'
#' @param img A list. The list of images to display. Each image in the list should be of class "Image".
#' @param write A logical. Whether to write the images to .png files. Defaults to FALSE.
#' @param write_file A logical. Whether to write the images to .png files. Defaults to FALSE.
#' @param Rds A logical. Whether to save the images as .Rds files. Defaults to FALSE.
#' @param dim An integer. The dimension of the image data. Defaults to 3.
#' @param mar A numeric. The margin size for the plot. Defaults to 0.05.
Expand All @@ -31,13 +31,13 @@
#' Display(img)
#'
#' # Display the images and write them to .png files
#' Display(img, write = TRUE)
#' Display(img, write_file = TRUE)
#'
#' # Display the images and save them as .Rds files
#' Display(img, Rds = TRUE)
#' }

Display <- function(img, write = FALSE, Rds = FALSE, dim = 3, mar = 0.05){
Display <- function(img, write_file = FALSE, Rds = FALSE, dim = 3, mar = 0.05){
# Store the current graphics parameters
oldpar <- graphics::par(no.readonly = TRUE)

Expand Down Expand Up @@ -67,7 +67,7 @@ Display <- function(img, write = FALSE, Rds = FALSE, dim = 3, mar = 0.05){
f <- paste0("Img_", formatC(length(dir(pattern = "[.]png"))+1, flag = "0", width = 3), ".png")

# If the image is RGBA, rotate it and write it out
if(write){
if(write_file){
if(dim(img[[n]])[3] == 4){
img0[[n]] <- EBImage::rotate(img[[n]], angle=-90)
alpha_channel <- matrix(0, nrow = dim(img0[[n]])[1], ncol = dim(img0[[n]])[2])
Expand Down Expand Up @@ -98,7 +98,8 @@ Display <- function(img, write = FALSE, Rds = FALSE, dim = 3, mar = 0.05){
graphics::par(mfrow=ans, mar=rep(mar, 4))

# Display the images
if(length(img) != 1){
for(n in seq_len(length(img))){
plot(t(grDevices::as.raster(img0[[n]]@.Data, max=max(img[[n]]@.Data))))
}
plot(t(grDevices::as.raster(img[[n]]@.Data, max=max(img[[n]]@.Data))))
}}
}
26 changes: 11 additions & 15 deletions R/img2img_upscale_StableDiffusion4R.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' init_image_path <- system.file("./inst/", "JP_castle.png", package = "chatAI4R")
#' image <- img2img_upscale_StableDiffusion4R(init_image_path)
#' Display(image)
#' init_image_path <- system.file("images", "JP_castle.png", package = "chatAI4R")
#' result <- img2img_upscale_StableDiffusion4R(init_image_path)
#' Display(result, write_file = TRUE)
#' }

# system.file(package = "chatAI4R")
# ?chatAI4R::chat4R

img2img_upscale_StableDiffusion4R <- function(
init_image_path,
width_height = 1024,
width = 1024,
engine_id = "esrgan-v1-x2plus",
api_host = "https://api.stability.ai",
api_key = Sys.getenv("DreamStudio_API_KEY")
Expand Down Expand Up @@ -61,29 +58,28 @@ img2img_upscale_StableDiffusion4R <- function(

payload <- list(
"image" = httr::upload_file(init_image_path),
"width" = width_height,
"height" = width_height)
"width" = width)

# Creating empty variable
result <- list()
cat("Generating an up-scaled image\n")

response <- httr::POST(uri,
body = payload,
encode = "multipart",
config = headers)
body = payload,
encode = "multipart",
config = headers)

if (httr::http_status(response)$category != "Success") {
if (httr::http_status(response)$category != "Success") {
stop("Non-200 response: ", httr::content(response, "text", encoding = "UTF-8"))
}
}

image_data <- jsonlite::fromJSON(httr::content(response, "text", encoding = "UTF-8"))

decode_image <- png::readPNG(base64enc::base64decode(image_data$artifacts$base64))

Img <- EBImage::rotate(EBImage::Image(decode_image, colormode = 'Color' ), angle=90)

result[[i]] <- Img
result[[1]] <- Img

return(result)
}
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ Flowcharts of the R functions were created by GPT-4 + Skrive plugin.

|Function|Description|Script|Flowchart|
|:---|:---|:---:|:---:|
|conversation4R|Manage a conversation with OpenAI's GPT-3.5-turbo model|Script|Flowchart|
|createFunction4R|Generate and Improve R Functions|Script|Flowchart|
|TextSummary|Function description is missing.|Script|Flowchart|
|conversation4R|Manage a conversation using a chatting history|Script|Flowchart|
|createFunction4R|Generate and improve R functions (experimental)|Script|Flowchart|
|TextSummary| Summarize a long text (experimental)|Script|Flowchart|

### Image generation functions using DALL·E 2

Expand All @@ -95,15 +95,16 @@ Flowcharts of the R functions were created by GPT-4 + Skrive plugin.

### Image generation functions using Stable Diffusion

- generageTxt2img_StableDiffusion4R
- generageImg2img_StableDiffusion4R
- txt2img_StableDiffusion4R
- img2img_StableDiffusion4R
- img2img_upscale_StableDiffusion4R

### Visualization functions

|Function|Description|Script|Flowchart|
|:---|:---|:---:|:---:|
|Display|Display Images and Optionally Write Them to File|Script|Flowchart|
|createHEX|Create Hex Stickers for R Packages|Script|Flowchart|
|createHEX|Create Hex Stickers for R Packages (experimental)|Script|Flowchart|

### Functions for RIKEN press release (future developments)

Expand Down
Binary file removed inst/exdata/JP_castle.png
Binary file not shown.

0 comments on commit 0a347c4

Please sign in to comment.