Reading multiple images in R for image processing - r

I am trying to use image processing in R, which is a new concept and a new language to me. I am aiming to read a lot of images from few folders to train. As a test scenerio, I kept two images in a folder, named: n04197391_11_0, n04197391_74_0. I plan to import the images, add an extra column, and then do require processing.
The following is my code I am using:
setwd("C:/Users/Ethan/Desktop/R")
library(EBImage)
files <- list.files(path="C:\\Users\\Desktop\\data\\images\\0", pattern=".jpg",all.files=T, full.names=F, no.. = T)
#This imports the files as: files - chr[1:2] "n04197391_11_0" "n04197391_74_0"
mypic1 <- list()
for (i in 1:length(files))
{
mypic1[[i]] <- readImage(files[i])
}
print(mypic1[[1]])
The following is the error that I am getting:
Error in transpose(y) : object is NULL
In addition: Warning message:
In .loadFun(files, ...) :
cannot open file 'n04197391_11_0.jpg': No such file or directory
print(mypic1[[1]])
Error in mypic1[[1]] : subscript out of bounds
I dont understand if the images are being read in files, why is it showing an error. Is there a workaround that I can take to read multiple images from folders in such a case? Thank you in Advance.

Related

How to get passed the following error "Error in readLines(filestocopy) : 'con' is not a connection"?

I am new to coding and very new to this forum, so I hope my request makes sense.
I am trying to select images listed in a .csv file and to copy them to a new folder. The pictures and the .csv file are both in the folder GRA04. The .csv file contain only one column with the picture names.
I used the following code:
#set working directory
setwd("E:/2019/GRA04")
#create and identify a new folder in R
targetdir <- dir.create("GRA04_age")<br/>
#find the files you want to copy
filestocopy <- read.csv("age.csv", header=FALSE) #read csv as data table (only one column, each raw being a file name)
filestocopy_v <- readLines(filestocopy)#convert data table in character vector
filestocopy_v #shows the character vector
#copy the files to the new folder
file.copy(filestocopy_v, targetdir, recursive = TRUE)
When reaching the line
filestocopy_v <- readLines(filestocopy)
I get this error message:
Error in readLines(filestocopy) : 'con' is not a connection
I looked online for solutions with no luck. I ran this code before (or else something similar... didn't back it up...) and it worked fine, so I am not sure what is happening...
Thanks!
Out of interest, would the following now do what you're trying to achieve?
filestocopy_v <- filestocopy[[1]]

Function reading in a folder of files returning "Error in file(con, "r"): cannot open conection..."

I'm trying to import a folder of files into R. The following code works for one folder that contains the same type of files, but will not work for another folder. The type of data is the same (both debian files formatted in the same way, just containing different subject's data).
The following code allows me to read all the files (named subject1-subject10) in the "Data1" folder and put it into a list named Data:
files <- as.character(list.files(path="/Users/wendy/Box Sync/Data1"))
data <- list()
for (i in seq_along(files)) {
data[[i]] <- readLines(files[[i]])
}
But the following code does not work - this folder (Data2) contains subject11 - subject50:
files <- as.character(list.files(path="/Users/wendy/Box Sync/Data2"))
data <- list()
for (i in seq_along(files)) {
data[[i]] <- readLines(files[[i]])
}
This brings up the following message:
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'subject11': No such file or directory
I'm confused, because both folders, containing their respective subject data are in the same file path, except for the last folder name in the path.
The second folder (Data2) differs only in the following ways:
Number of files in the folder
contains different subjects
There is more data (more variables) recorded in "Data2" (e.g. recording age, height, race in Data 2 versus only recording age and height in Data1)
If I were to put some of Data2's files into the Data1 folder and run the top code again, it will produce the same error message as when I run the second code chunk.
You should add the full.names option.
list.files(path="/Users/wendy/Box Sync/Data2", full.names = TRUE)
Without it, it only outputs the name of the files, and thus it works only if files with that exact file name are found in the current working directory.

XMLtoDataFrame: "Duplicate subscripts for columns" When trying to load multiple files (R)

I'm trying to load many files from a folder in R that are of the form '.xml'. The files are contained in one folder with any subfolders. I tried to get them in one shot using the following code:
allfiles <- list.files("MyDirectory", pattern = '*.xml', recursive = TRUE, full.names = TRUE)
hope <- do.call(rbind.fill,lapply(allfiles,xmlToDataFrame))
Unfortunately, this is the result:
"Error in `[<-.data.frame`(`*tmp*`, i, names(nodes[[i]]), value = c("12786998421436773", : duplicate subscripts for columns'"
And I'm not even quite sure what the problem is? It works when I do it by individual files but there are upwards of 30,000 files so it's not really feasible. I've tried it a different way (using a for loop) but will get an error like the "xml file is not of type XML' even though the file name it outputs ends in the '.xml' tag.
Any clarity would be greatly appreciated.

import multiple txt files into R

I am working with MODIS 8-day data and am trying to import all the txt files of one MODIS product into the R, but not as one single data.frame, as individual txt files. So I can later apply same functions on them. The main objective is to export specific elements within each txt file. I was successful in excluding the desired elements from one txt file with the following command:
# selecting the element within the table
idxs <- gsub("\\]",")", gsub("\\[", "c(", "[24,175], [47,977], [159,520], [163,530]
,[165,721], [168,56], [217,820],[243,397],[252,991],[284,277],[292,673]
,[322,775], [369,832], [396,872], [434,986],[521,563],[522,717],[604,554]
,[608,50],[614,69],[752,213],[780,535],[786,898],[788,1008],[853,1159],[1014,785],[1078,1070]") )
lst <- rbind( c(24,175), c(47,977), c(159,520), c(163,530) ,c(165,721), c(168,56), c(217,820),c(243,397),c(252,991),c(284,277),c(292,673),c(322,775), c(369,832), c(396,872), c(434,986),c(521,563),c(522,717),c(604,554),c(608,50),c(614,69),c(752,213),c(780,535),c(786,898),c(788,1008),c(853,1159),c(1014,785),c(1078,1070))
mat <- matrix(scan("lst.txt",skip = 6),nrow=1200)
Clist <- as.data.frame(mat[lst])
But I need these element from all of the txt files and honestly I do not want to run it manually for 871 times. So I try to read all the txt files and then apply this function to them. but unfortunately it does not work. here is my approach:
folder <- "C:/Users/Documents/R/MODIS/txt/"
txt_files <- list.files(path=folder, pattern=".txt")
df= c(rep(data.frame(), length(txt_files)))
for(i in 1:length(txt_files)) {df[[i]]<- as.list(read.table(txt_files[i]))}
and this is the error I encounter:
**Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'rastert_a2001361.txt': No such file or directory**
additional information: each txt file includes 1200rows and 1200columns and 20-30 elements need to be extracted from the table.
I am very much looking forward for your answers and appreciate any helps or recommendations with this matter.
The issue is that list.files returns only the file name within the folder, not the full path to the file. If you working direction is not "C:/Users/Documents/R/MODIS/txt/" your code could not work. Change your code to
for(i in 1:length(txt_files)) {df[[i]]<- as.list(read.table(file.path(folder, txt_files[i])))}
Now it should be working.
file.path combines your path and your file with correct, OS specific, path seperator.

read csv file from zipped temp file with multiple folders in R

I am trying to read a csv file that is contained in a file I extracted from the web. The problem is the zipped file has multiple cascading folders. I have to do that for several different units, so I am performing a loop. There is no problem with the loop, the file name is correct and I get to download the file. However I get an error message (and I think is because R cannot find the exact file I am asking it to find). The error is:
Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
cannot locate file 'XXXX.csv' in zip file 'c:\yyy\temp\bla\'
download.file(paste("http://web.com_",units[i],"_",places[j],".zip",
sep=""),
temp,
cacheOK = F )
data <- read.csv2(unz(temp,
paste("name_",units[i],"_",places[j],".csv",
sep="")),
header=F,
skip=1)
unlink(temp)
fili<-rbind(X,
data)
}
How do I make R find the file I want?
You have the right approach but (as the warning tells you) the wrong filename.
It's worth double checking that the zip file does exist before you start trying to read its contents.
if(file.exists(temp))
{
read.csv2(unz(...))
} else
{
stop("ZIP file has not been downloaded to the place you expected.")
}
It's also a good idea to a browse around inside the downloaded file (you may wish to unzip it first) to make sure that you are looking in the right place for the CSV contents.
It looks like the file, you're going to read, is located in directory. In this case your reading should be changed as follows:
data <- read.csv2(unz(temp,
paste("**dirname**/name_",units[i],"_",places[j],".csv",
sep="")),
header=F,
skip=1)

Resources