Skip to content

Instantly share code, notes, and snippets.

@mick001
Last active March 13, 2019 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mick001/6a7290d858d297eb117e923d2c0402aa to your computer and use it in GitHub Desktop.
Save mick001/6a7290d858d297eb117e923d2c0402aa to your computer and use it in GitHub Desktop.
Image recognition in R using convolutional neural networks with the MXNet package. Part 1. Full article at: https://firsttimeprogrammer.blogspot.com/2016/07/image-recognition-in-r-using.html
# Resize images and convert to grayscale
rm(list=ls())
require(EBImage)
# Set wd where images are located
setwd("C://dogs_images")
# Set d where to save images
save_in <- "C://dogs_images_resized"
# Load images names
images <- list.files()
# Set width
w <- 28
# Set height
h <- 28
# Main loop resize images and set them to greyscale
for(i in 1:length(images))
{
# Try-catch is necessary since some images
# may not work.
result <- tryCatch({
# Image name
imgname <- images[i]
# Read image
img <- readImage(imgname)
# Resize image 28x28
img_resized <- resize(img, w = w, h = h)
# Set to grayscale
grayimg <- channel(img_resized,"gray")
# Path to file
path <- paste(save_in, imgname, sep = "")
# Save image
writeImage(grayimg, path, quality = 70)
# Print status
print(paste("Done",i,sep = " "))},
# Error function
error = function(e){print(e)})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment