Created
August 18, 2017 11:04
-
-
Save simecek/52d02fedf21a0f7d8a4b9d454272d5b4 to your computer and use it in GitHub Desktop.
Classification of jpg files into several one letter subfolders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example of usage | |
setwd("c:/Users/simecekp/Downloads/") | |
fls <- dir(patter=".*jpg") | |
jpg_sort(fls,c("a", "s")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(keypress) | |
library(jpeg) | |
jpg_sort <- function(files, keys, move.files=FALSE) { | |
## checking intputs | |
# all keys should be of length 1 | |
stopifnot(nchar(keys)==1) | |
# all keys should be unique | |
stopifnot(length(keys)==length(unique(keys))) | |
# all files should exist | |
stopifnot(file.exists(files)) | |
# number of files > 0 | |
stopifnot(length(files)>0) | |
# number of keys > 1 | |
stopifnot(length(keys)>1) | |
## creating folders | |
for (k in keys) { | |
if (!dir.exists(k)) dir.create(k) | |
} | |
## copy or move? | |
if (move.files) { | |
file.proc <- file.rename | |
} else { | |
file.proc <- file.copy | |
} | |
for (i in seq_along(files)) { | |
f = files[i] | |
jpeg <- readJPEG(f, native=TRUE) | |
plot(0:1, 0:1, type="n", ann=FALSE, axes=FALSE) | |
rasterImage(jpeg,0,0,1,1) | |
Sys.sleep(0.1) | |
x <- keypress() | |
while (!(x %in% keys)) x <- keypress() | |
file.proc(f, paste0(x,"/")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
keypress
does not work in RStudio, needs to be run in terminal