0

I have a folder with 100+ images. I want to run an google vision analysis on each of them in R. Instead of running the analysis on one image at a time I want to create a function which will access each image one by one and run the analysis.

Using following code:

getGooglevisionResponse(file.choose(),feature = 'text_detection')

I am using file.choose() to choose one file at a time but I want to create a loop which will dynamically select each image and run the analysis on them .. Used list.files() but getting below error the following condition has length >1 and only the first element will be used

found one post but that is in python unable to replicate it in R

https://github.com/andrikosrikos/Google-Cloud-Support/blob/master/Google%20Vision/multiple_features_request_single_API_call.py

  • 1
    You might want to take a look at `list.files` – niko Jan 31 '19 at 18:23
  • actually yes .. it does select multiple image but when it comes to running the analysis its throwing an error .. it's not creating a loop where it selects one file runs the analysis then goes to second.. – Sumaiya Khatun Jan 31 '19 at 18:28

1 Answers1

0

Without seeing more code, you just need to use lapply and list.files.

fil <- list.files("<myImgDir>", full.names = T)
out <- lapply(fil, getGoogleVisionResponse, feature = 'text_detection'))
JMT2080AD
  • 1,049
  • 8
  • 15
  • thanks for the reply but the error remains, Mentioned the code already.. `getGooglevisionResponse(file.choose(),feature = 'text_detection')' .. `file.choose` only chooses one file and runs text detection .. i need a function in tht place which will choose those images one by one and run the analysis – Sumaiya Khatun Jan 31 '19 at 18:50
  • @SumaiyaKhatun I have this sense you are trying this -> getGooglevisionResponse(list.files(), feature = 'text_detection') <- Am I correct? If so, that is incorrect. – JMT2080AD Feb 01 '19 at 01:17
  • yes you are correct ... i was doing this only .... can you please suggest any other way? the code you gave is giving error.. – Sumaiya Khatun Feb 01 '19 at 10:17
  • 2
    I made and edit to my answer. Replace the `""` with the path to your images as a string, (`"C:\MyFolder"`). You will need to make sure that this path contains only images you are trying to process. If you get an error, post it here. – JMT2080AD Feb 01 '19 at 17:24
  • hey thank you for all the help.. I did use your code but the problem is R is not recognizing `getGoogleVisionResponse` within lapply...Error is `object 'getGooglevisionResponse' not found`.... and when I am using `getGoogleVisionResponse(fil, feature = 'text_detection')` ... error is `In if (stringr::str_count(imagePath, "http") > 0) { : the condition has length > 1 and only the first element will be used` – Sumaiya Khatun Feb 02 '19 at 16:21
  • I also tried below code .. do know if it is correct `Address <- setwd("path to image folder")` `fil <- list.files(Address, pattern = '*.jpeg')` `for(i in 1:length(fil)) { assign(fil[i], pic <-readJPEG(Address, fil)) Vsn <- getGoogleVisionResponse(pic,feature = 'TEXT_DETECTION') }` Error I m getting is ' unable to open 'path to image folder' 'In addition: Warning message: In readJPEG(Address, fil) : NAs introduced by coercion' – Sumaiya Khatun Feb 04 '19 at 16:06
  • I had copied and pasted you code without looking up the library. You have a typo in your original function call, R is case sensitive afterall, which I pasted into mine. The function name is `getGoogleVisionResponse`. I have updated my post to reflect this. You shouldn't get the `object ... not found error` any longer. – JMT2080AD Feb 04 '19 at 18:13
  • Hey .. the problem remains the same .. the function works `getGoogleVisionResponse(imagepath,feature=())`..in your code the path is missing .. getting the error `getGoogleVisionResponse` ..and when I added the path which is `fil` .. the same error is coming .. `the condition has length > 1 and only the first element will be used` – Sumaiya Khatun Feb 05 '19 at 07:17
  • `fil` is not a path, `fil` is a list of paths. `lapply` iterates over them, applying the `getGoogle...` function to each element. That is why you are getting the `length >1` error. I recommend reading up on working directories, and apply functions. – JMT2080AD Feb 05 '19 at 18:37