1

I want to pass image from C# to R. I'm uploading my image using FileUpload and storing it into a folder "images".When i'm passing images location to R it gives me error. So,can you guys suggest me any alternate way to solve this error.Following are my code.

// Get Filename from fileupload control

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);             

engine.Evaluate("imgPath<-'~/images/filename'"); //error in this line

// Read the image into a raster array

engine.Evaluate("img<-readJPEG(imgPath, native = FALSE)");

// convert the array to a data.frame 

engine.Evaluate("mystring<-as.data.frame(img)");

engine.Evaluate("myfreqs <- mystring / sum(mystring)");

// vectorize

engine.Evaluate("abc <- as.data.frame(myfreqs)[,2]");

// create input matrices

engine.Evaluate(@"a <- matrix(c(abc), nrow=4)"); 
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jay
  • 23
  • 3
  • Are you sure your '~/Image...' syntax applies here? Know this only from ASP.NET. Even there you have to map the 'logical' path to the 'physical' to use a file from code. – nabuchodonossor Nov 20 '15 at 12:39
  • i'm not sure about the syntax. When i'm passing the exact location then code works perfectly. – Jay Nov 20 '15 at 12:41
  • 1
    Then it´s clear: '~/...' is wrong. Then it should be: engine.Evaluate("img<-'" + yourpath + '"); – nabuchodonossor Nov 20 '15 at 13:17

2 Answers2

1

And here as answer:

 engine.Evaluate("imgPath<-'" + filename + "'");

filename should be the complete path

nabuchodonossor
  • 2,095
  • 20
  • 18
  • ya it's working but i'm getting this error:The filename, directory name, or volume label syntax is incorrect. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) – Jay Nov 20 '15 at 19:13
  • what is the Content of maybeFullPath? Does this file (including complete path) exists? – nabuchodonossor Nov 23 '15 at 09:26
0

PREFACE: I know nothing of C#. However, your situation is familiar. Why not call a shell command or a R process from C#? I have done this numerous times between Python and R. Consider abstracting the two languages by using R's automated executable RScript and pass image name as argument from C# using Process.Start or a new Process object?

C# Script

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string args = @"C\Path\To\RScript.R" + " " + filename;

// SHELL COMMAND
// (RScript path can be shortened if using environment variable)
System.Diagnostics.Process.Start(@"C:\Path\To\RScript.exe", args);

// PROCESS OBJECT
var proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = @"C:\Path\To\RScript.exe",
        Arguments = @"C\Path\To\RScript.R" + " " + filename,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

R Script

options(echo=TRUE)
args <- commandArgs(trailingOnly = TRUE)

# pass argument into string 
imgPath <- args[1]

img <- readJPEG(imgPath, native = FALSE)

# convert the array to a data.frame 
mystring <- as.data.frame(img)    
myfreqs <- mystring / sum(mystring)

# vectorize
abc <- as.data.frame(myfreqs)[,2]

# create input matrices
a <- matrix(c(abc), nrow=4)
Community
  • 1
  • 1
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • every thing worked fine but i'm getting "No overload for method 'Start' takes 3 arguments" error at System.Diagnostics.Process.Start("C:/Program Files/R/R-3.2.1/bin/i386/RScript.exe", "C:/Program Files/R/R-3.2.1/bin/i386/RScript.R", filename); – Jay Nov 22 '15 at 13:36
  • See edit. I had to concatenate the R file string with your image filename for two arguments. Be sure to only use one type: shell command or process object. Also, is your script really called RScript.R and located in same folder as RScript.exe? You can name your script anything and place it anywhere. My post used placeholders. – Parfait Nov 22 '15 at 15:11
  • Ya, I placed my RScript.R file and RScript.exe in same folder – Jay Nov 22 '15 at 15:27
  • No bro i'm still getting "No overload for method 'Start' takes 3 arguments" error . – Jay Nov 30 '15 at 08:57
  • See update. Backslashes might need [escaping](http://stackoverflow.com/questions/1987950/using-or-for-folder-paths-in-c-sharp). – Parfait Dec 01 '15 at 22:28