Git Product home page Git Product logo

googlecloudvisionr's Introduction

googleCloudVisionR

Travis build status Codecov test coverage

R Package for Image Recognition, Object Detection, and OCR using the Google Cloud Vision API.

Google Cloud Vision API

Cloud Vision API enables developers to understand the content of an image by encapsulating powerful machine learning models in an easy-to-use REST API. It quickly classifies images into thousands of categories (such as, “sailboat”), detects individual objects and faces within images, and reads printed words contained within images. You can build metadata on your image catalog, moderate offensive content, or enable new marketing scenarios through image sentiment analysis.

Read more on the capabilities of the Google Cloud Vision API.

About this R package

This package allows you to call the following functionalities: label, text, landmark, face and logo detection.

The project is a continuation of the RoogleVision package by Florian Teschner and is relying on the authentication procedures offered by googleAuthR.

WARNING: the project is in active development and is not guaranteed to be stable in terms of function names, signatures, etc. Contributions through issues, pull requests are more than welcome.

Installation

v0.1.0 is already available on CRAN, you can install it with:

install.packages("googleCloudVisionR")

If you need the latest version, just install the package directly from Github:

devtools::install_github("cloudyr/googleCloudVisionR")

Authentication

You should use a .json file pertaining to a service account for authentication whose path you should specify in an environment variable GCV_AUTH_FILE for auto-authentication:

Sys.setenv("GCV_AUTH_FILE" = "/fullpath/to/auth.json")

This can alternatively be set on the command line or via an Renviron.site or .Renviron file (see here for instructions).

If this is set, upon loading the package authentication happens automatically.

# GCV_AUTH_FILE set so auto-authentication happens
library(googleCloudVisionR)
#> Successfully authenticated via /fullpath/to/auth.json

Example

Let us retrieve annotations for this image:

Label detection (default), with maximum 7 results returned per image:

imagePath <- system.file(
    "extdata", "golden_retriever_puppies.jpg", package = "googleCloudVisionR"
  )
gcv_get_image_annotations(
    imagePaths = imagePath,
    feature = "LABEL_DETECTION",
    maxNumResults = 7
)

#>                      image_path       mid      description     score topicality
#> 1: golden_retriever_puppies.jpg /m/0bt9lr              Dog 0.9953705  0.9953705
#> 2: golden_retriever_puppies.jpg  /m/04rky           Mammal 0.9890478  0.9890478
#> 3: golden_retriever_puppies.jpg  /m/09686       Vertebrate 0.9851104  0.9851104
#> 4: golden_retriever_puppies.jpg  /m/01z5f          Canidae 0.9813780  0.9813780
#> 5: golden_retriever_puppies.jpg  /m/0kpmf        Dog breed 0.9683250  0.9683250
#> 6: golden_retriever_puppies.jpg /m/0d7s3w            Puppy 0.9400384  0.9400384
#> 7: golden_retriever_puppies.jpg /m/01t032 Golden retriever 0.8966703  0.8966703

Annotation can also happen in batches, allowing for continuing previously stopped (or interrupted) annotations as well.

gcv_get_image_annotations(
    imagePaths = c(image_path_1, image_path_2,..., image_path_128),
    batchSize = 64L,
    savePath = "path/to/annotation_file.csv"
)

Here we take 128 images and annotate them in batces of size 64. If, for example, we have previously annotated the first batch of images whose results have been stored in savePath, then only the leftovers are annotated. A dataframe with all annotations is returned and the new set of annotations gets appended to savePath.

Images can not only be read from local disk, but you can use URLs or Google Cloud Storage URIs as well:

gcv_get_image_annotations(c(
    "https://bit.ly/2IhUzdE",
    "gs://vision-api-handwriting-ocr-bucket/handwriting_image.png"
), maxNumResults = 2)

#>                                                      image_path        mid description     score topicality
#> 1:                                       https://bit.ly/2IhUzdE  /m/0bt9lr         Dog 0.9953705  0.9953705
#> 2:                                       https://bit.ly/2IhUzdE   /m/04rky      Mammal 0.9890478  0.9890478
#> 3: gs://vision-api-handwriting-ocr-bucket/handwriting_image.png /m/07s6nbt        Text 0.9540842  0.9540842
#> 4: gs://vision-api-handwriting-ocr-bucket/handwriting_image.png  /m/03scnj        Line 0.9027576  0.9027576

googlecloudvisionr's People

Contributors

tkoncz avatar flovv avatar bvarkoly avatar peteratemarsys avatar paljenczy avatar leeper avatar j450h1 avatar

Stargazers

Julian Hohner avatar Justin Marciszewski avatar tikeda-caravan avatar  avatar Aditya L. Ramadona avatar SennaGuo avatar

Watchers

boB Rudis avatar James Cloos avatar  avatar  avatar Mark Edmondson avatar  avatar

googlecloudvisionr's Issues

`gcv_get_response()` returns incorrect columns when multiple images are sent in for "IMAGE_PROPERTIES"

library(googleCloudVisionR)

gcv_get_response(
    imagePaths = c("inst/extdata/bali_small.jpeg", "inst/extdata/essex.jpg"),
    feature = "IMAGE_PROPERTIES",
    maxNumResults = 1
)

#>                      image_path red green blue     score pixel_fraction color.red.1 color.green.1 color.blue.1   score.1 pixelFraction.1
#> 1: inst/extdata/bali_small.jpeg 241   178  132 0.1905586     0.01451833         198           195          198 0.8104908       0.4618453
#> 2:       inst/extdata/essex.jpg 241   178  132 0.1905586     0.01451833         198           195          198 0.8104908       0.4618453

`gcv_get_image_annotations()` fails when an error occurs in a later batch and earlier batches were error-free

library(googleCloudVisionR)

testPath <- tempfile()
testImages <- c(
    "https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg",
    "https://incorrect.image.path"
)

# this works
gcv_get_image_annotations(imagePaths = testImages, batchSize = 2)
#>                                                                                       image_path        mid    description     score topicality error_code                                                                                               error_message         feature
#>  1: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg   /m/04rky         Mammal 0.9890478  0.9890478         NA                                                                                                        <NA> LABEL_DETECTION
#>  2: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg  /m/0bt9lr            Dog 0.9876207  0.9876207         NA                                                                                                        <NA> LABEL_DETECTION
#>  3: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg   /m/09686     Vertebrate 0.9851104  0.9851104         NA                                                                                                        <NA> LABEL_DETECTION
#>  4: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg   /m/0kpmf      Dog breed 0.9838449  0.9838449         NA                                                                                                        <NA> LABEL_DETECTION
#>  5: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg   /m/01z5f        Canidae 0.9765394  0.9765394         NA                                                                                                        <NA> LABEL_DETECTION
#>  6: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg  /m/0d7s3w          Puppy 0.9450111  0.9450111         NA                                                                                                        <NA> LABEL_DETECTION
#>  7: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg   /m/01lrl      Carnivore 0.8226868  0.8226868         NA                                                                                                        <NA> LABEL_DETECTION
#>  8: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg  /m/03yl64  Companion dog 0.7883269  0.7883269         NA                                                                                                        <NA> LABEL_DETECTION
#>  9: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg  /m/05mqq3          Snout 0.7342774  0.7342774         NA                                                                                                        <NA> LABEL_DETECTION
#> 10: https://image.shutterstock.com/image-photo/keep-calm-labrador-cute-puppy-260nw-633162677.jpg /m/0265rtm Sporting Group 0.6977496  0.6977496         NA                                                                                                        <NA> LABEL_DETECTION
#> 11:                                                                 https://incorrect.image.path       <NA>           <NA>        NA         NA          3 The URL does not appear to be accessible by us. Please double check or download the content and pass it in. LABEL_DETECTION

# this fails
gcv_get_image_annotations(imagePaths = testImages, batchSize = 1)
#> Error in rbindlist(.) : 
#> Item 2 has 4 columns, inconsistent with item 1 which has 6 columns. To fill missing columns use fill=TRUE.

unlink(testPath)

Support for DOCUMENT_TEXT_DETECTION

Can this package support a call for DOCUMENT_TEXT_DETECTION as documented here?

Supposedly this would be better than TEXT_DETECTION for dense documents like the average PDF with lots of text.

Web Entity Detection

Hello,

I am working on a research project that utilizes web entity detection. I was wondering if this is still something that is being worked on, and if not, is there another R library that can do this?

Error in GCP credentials.json authorization and .httr-oauth for CRAN package?

I am in trouble finding proper credential authorization for my ''RoogleVision" from Google. Here I'm providing the whole detail.
On my GCP account, I've:

 - just added no scope, just the support email - in OAuth consent screen
 - On my Credentials - Just created the - OAuth 2.0 Client IDs. With javascript origin URI and Redirect URI with my rstudio local domain and shiny.io.

I saved the credentials.json on my current working directory. On my R console I used this code:

library(googleAuthR) gar_set_client(web_json = "credentials.json")

The browser opens the app just fine, but for connecting google API it shows this error in console:

No authorization yet in this session!
No .httr-OAuth file exists in current working directory. Do library authentication steps to provide credentials.
Warning: Error in : Invalid token
78: stop
77: simpleCall
76: getGoogleVisionResponse
72: observeEventHandler [/cloud/project/server.R#28]

Here is my session info:

Failed to create bus connection: No such file or directory
─ Session info ─────────────────────────────────────────────────────────────────────────────
setting value
version R version 3.6.3 (2020-02-29)
os Ubuntu 16.04.6 LTS
system x86_64, linux-gnu
ui RStudio
language (EN)
collate C.UTF-8
ctype C.UTF-8
tz Etc/UTC
date 2020-07-10

─ Packages ─────────────────────────────────────────────────────────────────────────────────
package * version date lib source
abind 1.4-5 2016-07-21 [1] RSPM (R 3.6.0)
assertthat 0.2.1 2019-03-21 [1] RSPM (R 3.6.0)
backports 1.1.8 2020-06-17 [1] RSPM (R 3.6.0)
base64enc 0.1-3 2015-07-28 [1] RSPM (R 3.6.0)
bitops 1.0-6 2013-08-17 [1] RSPM (R 3.6.0)
broom 0.5.3 2019-12-14 [1] RSPM (R 3.6.0)
callr 3.4.3 2020-03-28 [1] RSPM (R 3.6.0)
cellranger 1.1.0 2016-07-27 [1] RSPM (R 3.6.0)
cli 2.0.2 2020-02-28 [1] RSPM (R 3.6.0)
colorspace 1.4-2 2019-12-29 [1] R-Forge (R 3.6.0)
crayon 1.3.4 2017-09-16 [1] RSPM (R 3.6.0)
crosstalk 1.1.0.1 2020-03-13 [1] RSPM (R 3.6.0)
DBI 1.1.0 2019-12-15 [1] RSPM (R 3.6.0)
dbplyr 1.4.2 2019-06-17 [1] RSPM (R 3.6.0)
desc 1.2.0 2018-05-01 [1] RSPM (R 3.6.0)
devtools * 2.3.0 2020-04-10 [1] RSPM (R 3.6.0)
digest 0.6.25 2020-02-23 [1] RSPM (R 3.6.0)
dplyr * 0.8.3 2019-07-04 [1] RSPM (R 3.6.0)
DT * 0.13 2020-03-23 [1] RSPM (R 3.6.0)
EBImage * 4.28.1 2019-12-06 [1] Bioconductor
ellipsis 0.3.1 2020-05-15 [1] RSPM (R 3.6.0)
evaluate 0.14 2019-05-28 [1] RSPM (R 3.6.0)
fansi 0.4.1 2020-01-08 [1] RSPM (R 3.6.0)
fastmap 1.0.1 2019-10-08 [1] RSPM (R 3.6.0)
fftwtools 0.9-8 2017-03-25 [1] RSPM (R 3.6.0)
flexdashboard * 0.5.1.1 2018-06-29 [1] RSPM (R 3.6.0)
glue 1.4.1 2020-05-13 [1] RSPM (R 3.6.0)
googleAuthR * 1.3.0 2020-06-16 [1] RSPM (R 3.6.3)
gtable 0.3.0 2019-03-25 [1] RSPM (R 3.6.0)
haven 2.2.0 2019-11-08 [1] RSPM (R 3.6.0)
hms 0.5.3 2020-01-08 [1] RSPM (R 3.6.0)
htmltools 0.5.0 2020-06-16 [1] RSPM (R 3.6.0)
htmlwidgets 1.5.1 2019-10-08 [1] RSPM (R 3.6.0)
httpuv 1.5.2 2019-09-11 [1] RSPM (R 3.6.0)
httr 1.4.1 2019-08-05 [1] RSPM (R 3.6.0)
janitor * 2.0.1 2020-04-12 [1] RSPM (R 3.6.0)
jpeg 0.1-8.1 2019-10-24 [1] RSPM (R 3.6.0)
jsonlite * 1.7.0 2020-06-25 [1] CRAN (R 3.6.3)
leaflet * 2.0.3 2019-11-16 [1] RSPM (R 3.6.3)
lifecycle 0.2.0 2020-03-06 [1] RSPM (R 3.6.0)
locfit 1.5-9.1 2013-04-20 [1] RSPM (R 3.6.0)
lubridate 1.7.4 2018-04-11 [1] RSPM (R 3.6.0)
magick * 2.4.0 2020-06-23 [1] RSPM (R 3.6.3)
magrittr 1.5 2014-11-22 [1] RSPM (R 3.6.0)
memoise 1.1.0 2017-04-21 [1] RSPM (R 3.6.0)
mime 0.9 2020-02-04 [1] RSPM (R 3.6.0)
modelr 0.1.5 2019-08-08 [1] RSPM (R 3.6.0)
munsell 0.5.0 2018-06-12 [1] RSPM (R 3.6.0)
nlme 3.1-144 2020-02-06 [2] CRAN (R 3.6.3)
packrat 0.5.0 2018-11-14 [1] RSPM (R 3.6.0)
pkgbuild 1.0.8 2020-05-07 [1] RSPM (R 3.6.0)
pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 3.6.0)
pkgload 1.1.0 2020-05-29 [1] RSPM (R 3.6.0)
png 0.1-8 2020-01-17 [1] local
prettyunits 1.1.1 2020-01-24 [1] RSPM (R 3.6.0)
processx 3.4.2 2020-02-09 [1] RSPM (R 3.6.0)
promises 1.1.0 2019-10-04 [1] RSPM (R 3.6.0)
ps 1.3.3 2020-05-08 [1] RSPM (R 3.6.0)
purrr * 0.3.3 2019-10-18 [1] RSPM (R 3.6.0)
R6 2.4.1 2019-11-12 [1] RSPM (R 3.6.0)
Rcpp 1.0.3 2019-11-08 [1] RSPM (R 3.6.0)
RCurl 1.98-1.2 2020-04-18 [1] RSPM (R 3.6.3)
readr * 1.3.1 2018-12-21 [1] CRAN (R 3.5.0)
readxl 1.3.1 2019-03-13 [1] RSPM (R 3.6.0)
remotes 2.1.1 2020-02-15 [1] RSPM (R 3.6.0)
reprex 0.3.0 2019-05-16 [1] RSPM (R 3.6.0)
rlang 0.4.6 2020-05-02 [1] RSPM (R 3.6.0)
rmarkdown 2.0 2019-12-12 [1] RSPM (R 3.6.0)
RoogleVision * 0.0.1.1 2020-07-03 [1] Github (shaun-jacks/RoogleVision@6c2909d)
rprojroot 1.3-2 2018-01-03 [1] RSPM (R 3.6.0)
rsconnect 0.8.16 2019-12-13 [1] RSPM (R 3.6.0)
rstudioapi 0.11 2020-02-07 [1] RSPM (R 3.6.0)
sessioninfo 1.1.1 2018-11-05 [1] RSPM (R 3.6.0)
shiny * 1.5.0 2020-06-23 [1] CRAN (R 3.6.3)
shinycssloaders * 0.3 2020-01-16 [1] RSPM (R 3.6.3)
shinydashboard 0.7.1 2018-10-17 [1] RSPM (R 3.6.3)

[1] /home/R/x86_64-pc-linux-gnu-library/3.6
[2] /opt/R/3.6.3/lib/R/library
Warning message:
In system("timedatectl", intern = TRUE) :
running command 'timedatectl' had status 1

I badly need experts' suggestions on this. Kindly help me with this issue.

return empty result instead of hard-to-understand internal error if imagePaths is of length 0

If there is no images to annotate and also no saved annotations, function returns error

googleCloudVisionR::gcv_get_image_annotations(character(0))
#> Error in googleCloudVisionR::gcv_get_image_annotations(character(0)): object 'imageAnnotations' not found

Created on 2019-08-23 by the reprex package (v0.3.0.9000)

It might make sense to return a data table with 0 rows (but the same columns you would get otherwise) so that it is easier in a loop. If that is not preferred for some reason I'd welcome a more specific error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.