2

I am an R and OpenCpu newbie. I have been able to create and deploy a simple R package with a function, and make a REST call to the function with curl get a json response.

I am using R 3.1.2 64 bit on Windows 7.

I now want to send an image file (say jpg or png) to an R function, process it and send back a response as text/json.

Is this possible with opencpu? What format and content-type should I be using?

I want to do something like:

curl -v http://server:port/ocpu/library/mylibrary/R/myimagefunc/<format?> -H "Content-Type: <content-type?>" <some_file.png>

This REST url would be embedded in a web application providing a file upload capability to the user.

------ Edit ------

aboutimage <- function(imageobj) {
    require(RProtoBuf)
    require(EBImage)
    img <- readImage(imageobj)
    nf <- numberOfFrames(img)
    print(nf)
    return(nf)
}

Thanks.

santosh
  • 23
  • 3
  • What do you want to do with the image in R? What does your `myimagefunc` look like? – Jeroen Ooms Mar 23 '15 at 17:45
  • Hi Jeroen, Thank you for replying! I want to run some image processing using Bioconductor EBImage. I want to be able to pass the image to the EBImage readImage() function. Then do some analysis on the image using e1071, and return a result to the user. Let me post the function on the main post. – santosh Mar 23 '15 at 17:53
  • Should I be uploading the image to a tmp key session in opencpu server and then have the function read the file from there? – santosh Mar 23 '15 at 18:27

1 Answers1

3

You can upload files using regular multipart post. Try this:

curl -v http://server:port/ocpu/library/mylibrary/R/myimagefunc -F "imageobj=@yourfile.png"

When POST-ing a file upload, the opencpu server will copy the file to the working directory and pass the name of the file as the argument to your function call. So in this case it executes:

myimagefunc(imageobj = "yourfile.png")
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • 1
    Hi Jeroen, It worked !! :-) As always, you are amazing !! Thank you. – santosh Mar 23 '15 at 20:14
  • Hi Jeroen, In addition to sending a file, is it possible to send additional data along with the file? ex: say "type", a string that describes the file in some way. The R code would be *aboutimage <- function(image, type) { ... }* – santosh Apr 01 '15 at 17:35
  • yes of course, just add more parameters to your http request. If it's a string make sure to add quotes, eg: -F `imageobj=@yourfile.png&type='somestring'` – Jeroen Ooms Apr 02 '15 at 01:13
  • Thank you Jeroen! did not think about that :-)! So it could be a json string, where I should make sure that I escape the " with a \ – santosh Apr 02 '15 at 05:58
  • Yes it can be anything. You only have to escape in the terminal, it's easier to test with the [/ocpu/test](http://public.opencpu.org/ocpu/test/) page. – Jeroen Ooms Apr 02 '15 at 18:05
  • Oh ok, so if it invoked from within a web app (javascript code), then we do not need to escape it? santosh – santosh Apr 03 '15 at 06:32