COVAIL / cognizer

R package to call IBM Watson services.
http://columbuscollaboratory.com
Other
119 stars 41 forks source link

image recognition Shiny #8

Closed germayneng closed 7 years ago

germayneng commented 7 years ago

Hi,

Good job on this package! I need help regarding using the image recognition in shiny.

I have a fileinput where it loads in JPG file. It is in a reactive expression and it will output the answer using renderprint.

image <- reactive({ SERVICE_API_KEY_image = "8XXXXXXXXXXXXX" path <- input$file1$datapath answer <- image_classify(path, SERVICE_API_KEY_image) list(answer = answer) })

output$answer <- renderPrint({image()$answer})

However, I am not able to output the result. I think the question is, when an image is loaded into image reactive, what argument should i pass into image_classify. I believe it takes in "path" argument. Hence I tried using datapath but it does not work. What is the image mapped as when imported using fileinput

This is the result watson gave:

[[1]] [[1]]$custom_classes [1] 0 [[1]]$images error.description error.error_id image 1 Ignoring non-image or unsupported extension [0]. input_error 0 [[1]]$images_processed [1] 1

Hopefully you can provide an answer :)

Regards Germ

synikitin commented 7 years ago

Hi Germ,

Your arguments look okay: a path to the image and an api key. I am not sure, but the error suggests a format problem...so maybe a path issue? Default type is "image/jpeg", so it should be something like "~/image.jpg".

I would start testing this outside of shiny. Make sure the test for image classify works:

image_face_path <- system.file("extdata/images_faces", package = "cognizer")
images <- list.files(image_face_path, full.names = TRUE)
image_classes <- image_classify(images, IMAGE_API_KEY)
str(image_classes)
germayneng commented 7 years ago

Hi Synikitin,

I managed to solved the problem. When we use file input for an image, it is temporary stored as a dataframe in R and the image file will be renamed to 0. Therefore, a code is needed to simply rename the file extension and name of the file and added as an argument to image_classify.

I found this function online:

fixUploadedFilesNames <- function(x) { if (is.null(x)) { return() }

oldNames = x$datapath newNames = file.path(dirname(x$datapath), x$name) file.rename(from = oldNames, to = newNames) x$datapath <- newNames x

}

synikitin commented 7 years ago

So there is no issue with image_classify?

germayneng commented 7 years ago

Hi,

image_classify is good to go. No issues with shiny either.

To sidetrack, will there be other developments to expand on other watson API?

Regards Germ

synikitin commented 7 years ago

Glad it worked out.

The APIs in the package got the most mileage in our's and other's work, so I have not felt the pressure. Maybe a classifier would be a good one to add. Submit an issue with APIs that you would find useful.