Skip to content

Commit

Permalink
RoogleVision- initial submission
Browse files Browse the repository at this point in the history
  • Loading branch information
flovv committed May 16, 2016
0 parents commit 674c606
Show file tree
Hide file tree
Showing 26 changed files with 371 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rproj.user/4779AC44/cpp-definition-cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
6 changes: 6 additions & 0 deletions .Rproj.user/4779AC44/pcs/debug-breakpoints.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"debugBreakpointsState" : {
"breakpoints" : [
]
}
}
9 changes: 9 additions & 0 deletions .Rproj.user/4779AC44/pcs/files-pane.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"path" : "C:/Users/fteschner/Desktop/googleVision/RoogleVision",
"sortOrder" : [
{
"ascending" : true,
"columnIndex" : 2
}
]
}
7 changes: 7 additions & 0 deletions .Rproj.user/4779AC44/pcs/packages-pane.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"installOptions" : {
"installDependencies" : true,
"installFromRepository" : true,
"libraryPath" : "C:/Users/fteschner/Documents/R/win-library/3.2"
}
}
3 changes: 3 additions & 0 deletions .Rproj.user/4779AC44/pcs/source-pane.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"activeTab" : 0
}
14 changes: 14 additions & 0 deletions .Rproj.user/4779AC44/pcs/windowlayoutstate.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"left" : {
"panelheight" : 647,
"splitterpos" : 141,
"topwindowstate" : "NORMAL",
"windowheight" : 685
},
"right" : {
"panelheight" : 647,
"splitterpos" : 411,
"topwindowstate" : "NORMAL",
"windowheight" : 685
}
}
6 changes: 6 additions & 0 deletions .Rproj.user/4779AC44/pcs/workbench-pane.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"TabSet1" : 2,
"TabSet2" : 0,
"TabZoom" : {
}
}
9 changes: 9 additions & 0 deletions .Rproj.user/4779AC44/persistent-state
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build-last-errors="[]"
build-last-errors-base-dir="C:/Users/fteschner/Desktop/googleVision/RoogleVision/"
build-last-outputs="[{\"output\":\"==> Rcmd.exe INSTALL --preclean --no-multiarch --with-keep.source RoogleVision\\n\\n\",\"type\":0},{\"output\":\"* installing to library 'C:/Users/fteschner/Documents/R/win-library/3.2'\\r\\n\",\"type\":1},{\"output\":\"* installing *source* package 'RoogleVision' ...\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** R\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** preparing package for lazy loading\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** help\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"*** installing help indices\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** building package indices\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** testing if installed package can be loaded\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"* DONE (RoogleVision)\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1}]"
compile_pdf_state="{\"errors\":[],\"output\":\"\",\"running\":false,\"tab_visible\":false,\"target_file\":\"\"}"
console_procs="[]"
files.monitored-path=""
find-in-files-state="{\"handle\":\"\",\"input\":\"\",\"path\":\"\",\"regex\":true,\"results\":{\"file\":[],\"line\":[],\"lineValue\":[],\"matchOff\":[],\"matchOn\":[]},\"running\":false}"
imageDirtyState="1"
saveActionState="0"
1 change: 1 addition & 0 deletions .Rproj.user/4779AC44/saved_source_markers
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"active_set":"","sets":[]}
20 changes: 20 additions & 0 deletions .Rproj.user/4779AC44/sdb/per/t/CD60C9DD
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"collab_server" : "",
"contents" : "###### Examples!\no <- getGoogleVisionResponse(\"brandlogos.png\")\no <- getGoogleVisionResponse(imagePath=\"brandlogos.png\", feature=\"LOGO_DETECTION\")\ngetGoogleVisionResponse(\"https://media-cdn.tripadvisor.com/media/photo-s/02/6b/c2/19/filename-48842881-jpg.jpg\", feature=\"LANDMARK_DETECTION\")\n",
"created" : 1463386909151.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "4179883559",
"id" : "CD60C9DD",
"lastKnownWriteTime" : 1463385008,
"last_content_update" : 1463385008,
"path" : "C:/Users/fteschner/Desktop/googleVision/examples.R",
"project_path" : null,
"properties" : {
},
"relative_order" : 2,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
20 changes: 20 additions & 0 deletions .Rproj.user/4779AC44/sdb/per/t/F6DD20D1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"collab_server" : "",
"contents" : "\n\n## please see flovv.github.com for a short blog post on running the code.\n\n#devtools::install_github(\"MarkEdmondson1234/googleAuthR\")\n\nrequire(googleAuthR)\nrequire(RCurl)\n\n### plug in your credentials\n## create project- enable billing- -- enable APIs Vision APi -- go to credentials create OAuth 2.0 client ID: copy client_id and client_secret from JSON file.\noptions(\"googleAuthR.client_id\" = \"xxx.apps.googleusercontent.com\")\noptions(\"googleAuthR.client_secret\" = \"\")\n\n### define scope!\noptions(\"googleAuthR.scopes.selected\" = c(\"https://www.googleapis.com/auth/cloud-platform\"))\n\n\n### use fantastic package!\ngoogleAuthR::gar_auth()\n\n################################# FACE DETECTION EXAMPLE\n### encode image\nimageFile <- \"arnold_wife.jpg\"\ntxt <- base64Encode(readBin(imageFile, \"raw\", file.info(imageFile)[1, \"size\"]), \"txt\")\n### create Request, following the API Docs.\nbody= paste0('{ \"requests\": [ { \"image\": { \"content\": \"',txt,'\" }, \"features\": [ { \"type\": \"FACE_DETECTION\", \"maxResults\": 2} ], } ],}')\n## generate function call\nsimpleCall <- gar_api_generator(baseURI = \"https://vision.googleapis.com/v1/images:annotate\", http_header=\"POST\" )\n## set the request!\npp <- simpleCall(the_body = body)\n## obtain results.\npp$content$responses$faceAnnotations[[1]]\n\n################################################################################\n######################################## LOGO DETECTION EXAMPLE\n\nimageFile <- \"brandlogos.png\"\ntxt <- base64Encode(readBin(imageFile, \"raw\", file.info(imageFile)[1, \"size\"]), \"txt\")\n\n### create Request, following the API Docs.\nbody= paste0('{ \"requests\": [ { \"image\": { \"content\": \"',txt,'\" }, \"features\": [ { \"type\": \"LOGO_DETECTION\", \"maxResults\": 40} ], } ],}')\n\n## generate function call\nsimpleCall <- gar_api_generator(baseURI = \"https://vision.googleapis.com/v1/images:annotate\", http_header=\"POST\" )\n\n## set the request!\npp <- simpleCall(the_body = body)\nlogos <- pp$content$responses$logoAnnotations[[1]]\n\ns <- pp$content$responses\n##################################################################################\n######################################## OCR EXAMPLE\nimageFile <- \"brandlogos.png\"\ntxt <- base64Encode(readBin(imageFile, \"raw\", file.info(imageFile)[1, \"size\"]), \"txt\")\n\n### create Request, following the API Docs.\nbody= paste0('{ \"requests\": [ { \"image\": { \"content\": \"',txt,'\" }, \"features\": [ { \"type\": \"TYPE_UNSPECIFIED\"} ], } ],}')\n\n## generate function call\nsimpleCall <- gar_api_generator(baseURI = \"https://vision.googleapis.com/v1/images:annotate\", http_header=\"POST\" )\n\n## set the request!\npp <- simpleCall(the_body = body)\nst <- pp$content$responses$\nst$boundingPoly\n####################################################################################\n",
"created" : 1463386671246.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "294705522",
"id" : "F6DD20D1",
"lastKnownWriteTime" : 1463328249,
"last_content_update" : 1463328249,
"path" : "C:/Users/fteschner/Desktop/googleVision/GoogleVisionGist.R",
"project_path" : null,
"properties" : {
},
"relative_order" : 1,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
2 changes: 2 additions & 0 deletions .Rproj.user/4779AC44/sdb/prop/7FB5FC93
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
2 changes: 2 additions & 0 deletions .Rproj.user/4779AC44/sdb/prop/AB233A51
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
2 changes: 2 additions & 0 deletions .Rproj.user/4779AC44/sdb/prop/INDEX
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
C%3A%2FUsers%2Ffteschner%2FDesktop%2FgoogleVision%2FGoogleVisionGist.R="AB233A51"
C%3A%2FUsers%2Ffteschner%2FDesktop%2FgoogleVision%2Fexamples.R="7FB5FC93"
11 changes: 11 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: RoogleVision
Title: Access to Google's Cloud Vision API for image recognition, OCR and
labeling
Version: 0.0.1.1
Authors@R: person("Florian", "Teschner", email = "teschner@gmail.com", role = c("aut", "cre"))
Description: This package is wrapper for Google's Cloud Vision API.
Depends:
R (>= 3.2.4)
License: What license is it under?
LazyData: true
RoxygenNote: 5.0.1.9000
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(getGoogleVisionResponse)
108 changes: 108 additions & 0 deletions R/googleVision-LIB.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
### types!
# TYPE_UNSPECIFIED Unspecified feature type.
# FACE_DETECTION Run face detection.
# LANDMARK_DETECTION Run landmark detection.
# LOGO_DETECTION Run logo detection.
# LABEL_DETECTION Run label detection.
# TEXT_DETECTION Run OCR.
# SAFE_SEARCH_DETECTION Run various computer vision models to compute image safe-search properties.
# IMAGE_PROPERTIES Compute a set of properties about the image (such as the images dominant colors).



############################################################
#' @title helper function to load required packages
#' @description Thanks to http://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
#'
checkAndLoadPackages <- function(){
list.of.packages <- c("googleAuthR", "RCurl", "stringr", "httr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

require(stringr)
require(httr)
require(RCurl)
require(googleAuthR)
}

############################################################
#' @title helper function base_encode code the image file
#' @description
#'
#' @param provide path/url to image

#' @return get the image back as encoded file
#'
imageToText <- function(imagePath){
checkAndLoadPackages()

if(str_count(imagePath, "http")>0){### its a url!
content = getBinaryURL(u)
txt <- base64Encode(content, "txt")
}
else{
txt <- base64Encode(readBin(imagePath, "raw", file.info(imagePath)[1, "size"]), "txt")
}
return(txt)
}

############################################################
#' @title helper function code to extract the response data.frame
#' @description
#'
#' @param provide the API rest response, and the feature request

#' @return get the data frame back
#'
extractResponse <- function(pp, feature){
if(feature == "LABEL_DETECTION"){
return(pp$content$responses$labelAnnotations[[1]])
}
if(feature == "FACE_DETECTION"){
return(pp$content$responses$faceAnnotations[[1]])
}
if(feature == "LOGO_DETECTION"){
return(pp$content$responses$logoAnnotations[[1]])
}
if(feature == "TEXT_DETECTION"){
return(pp$content$responses$textAnnotations[[1]])
}
if(feature == "LANDMARK_DETECTION"){
return(pp$content$responses$landmarkAnnotations[[1]])
}
}


################## Main function: Calling the API ##################
#' @title Calling Google's Cloud Vision API
#' @description input an image, provide the feature type and maxNumber of responses
#'
#' @param path or url to the image
#' @param feature: one out of: FACE_DETECTION, LANDMARK_DETECTION, LOGO_DETECTION, LABEL_DETECTION, TEXT_DETECTION
#' @export
#' @return data frame with results
#' @examples getGoogleVisionResponse(imagePath="brandlogos.png", feature="LOGO_DETECTION")
#'
getGoogleVisionResponse <- function(imagePath, feature="LABEL_DETECTION", numResults=5){

txt <- imageToText(imagePath)
### create Request, following the API Docs.
if(is.na(numResults)){
body= paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "',feature,'", "maxResults": ',numResults,'} ], } ],}')

}
else{
body= paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "',feature,'" } ], } ],}')

}

simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header="POST" )
## set the request!
pp <- simpleCall(the_body = body)
## obtain results.
res <- extractResponse(pp, feature)

return(res)
}


52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# RoogleVision
R Package for Image Recogntion, Object Detection, and OCR using the Google's Cloud Vision API

See the the R/shiny [demo](https://flovv.shinyapps.io/gVision-shiny/)

and blog posts [1](http://flovv.github.io/Image-Recognition-Google-Vision/) and [2](http://flovv.github.io/Brand-Logos/)

## Install
```
#install.packages("devtools")
require(devtools)
install_github("flovv/RoogleVision")
```

## Get API Keys
* Visit [Google's developer console](console.cloud.google.com)
* sign in
* create a project, enable billing and enable 'Google Cloud Vision API'
* go to credentials, create OAuth 2.0 client ID: copy client_id and client_secret from JSON file.


## Usage

```
require(RoogleVision)
#devtools::install_github("MarkEdmondson1234/googleAuthR")
require(googleAuthR)
### plugin your credentials
options("googleAuthR.client_id" = "xxx.apps.googleusercontent.com")
options("googleAuthR.client_secret" = "")
## use the fantastic Google Auth R package
### define scope!
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/cloud-platform"))
googleAuthR::gar_auth()
############
#Basic: you can provide both, local as well as online images:
o <- getGoogleVisionResponse("brandlogos.png")
o <- getGoogleVisionResponse(imagePath="brandlogos.png", feature="LOGO_DETECTION", numResults=4)
getGoogleVisionResponse("https://media-cdn.tripadvisor.com/media/photo-s/02/6b/c2/19/filename-48842881-jpg.jpg", feature="LANDMARK_DETECTION")
### FEATURES
# with the parameter 'feature' you can define which type of analysis you want. Results differ by feature-type
# The default is set to 'LABEL_DETECTION' but you can choose one out of: FACE_DETECTION, LANDMARK_DETECTION, LOGO_DETECTION, LABEL_DETECTION, TEXT_DETECTION
```


21 changes: 21 additions & 0 deletions RoogleVision.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
Binary file added exampleImages/arnold_wife.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added exampleImages/brandlogos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added exampleImages/notre-dame.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions man/checkAndLoadPackages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/extractResponse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/getGoogleVisionResponse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 674c606

Please sign in to comment.