Title: | An Interface for Image Recognition |
---|---|
Description: | Provides an interface for image recognition using the 'Google Vision API' <https://cloud.google.com/vision/> . Converts API data for features such as object detection and optical character recognition to data frames. The package also includes functions for analyzing image annotations. |
Authors: | Carsten Schwemmer [aut, cre] |
Maintainer: | Carsten Schwemmer <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.4 |
Built: | 2024-10-31 18:37:31 UTC |
Source: | https://github.com/cschwem2er/imgrec |
Calls the 'Google Vision' API to return annotations. The function automatically creates batches
get_annotations(images, features, max_res, mode)
get_annotations(images, features, max_res, mode)
images |
A character vector for images to be annotated. Can either be url strings or local images, as specified with |
features |
A character vector for the features to be returned. Accepts |
max_res |
An integer specifying the maximum number of results to be returned for each feature. |
mode |
Accepts |
An response object of class 'gvision_annotations'
.
Google Vision features and quotas.
## Not run: gvision_init() # one image url sw_image <- 'https://upload.wikimedia.org/wikipedia/en/4/40/Star_Wars_Phantom_Menace_poster.jpg' results <- get_annotations(images = sw_image, # image character vector features = 'all', # request all available features max_res = 10, # maximum number of results per feature mode = 'url') # maximum number of results per feature # multiple image urls finn_image <- 'https://upload.wikimedia.org/wikipedia/en/2/2a/Finn-Force_Awakens_%282015%29.png' padme_image <- 'https://upload.wikimedia.org/wikipedia/en/e/ee/Amidala.png' input_imgs <- c(sw_image, finn_image, padme_image) results <- get_annotations(images = input_imgs, features = c('label', 'face'), max_res = 5, mode = 'url') # one local image temp_img_path <- tempfile(fileext = '.png') download.file(finn_image, temp_img_path, mode = 'wb', quiet = TRUE) results <- get_annotations(images = temp_img_path, features = c('label', 'face'), max_res = 5, mode = 'local') ## End(Not run)
## Not run: gvision_init() # one image url sw_image <- 'https://upload.wikimedia.org/wikipedia/en/4/40/Star_Wars_Phantom_Menace_poster.jpg' results <- get_annotations(images = sw_image, # image character vector features = 'all', # request all available features max_res = 10, # maximum number of results per feature mode = 'url') # maximum number of results per feature # multiple image urls finn_image <- 'https://upload.wikimedia.org/wikipedia/en/2/2a/Finn-Force_Awakens_%282015%29.png' padme_image <- 'https://upload.wikimedia.org/wikipedia/en/e/ee/Amidala.png' input_imgs <- c(sw_image, finn_image, padme_image) results <- get_annotations(images = input_imgs, features = c('label', 'face'), max_res = 5, mode = 'url') # one local image temp_img_path <- tempfile(fileext = '.png') download.file(finn_image, temp_img_path, mode = 'wb', quiet = TRUE) results <- get_annotations(images = temp_img_path, features = c('label', 'face'), max_res = 5, mode = 'local') ## End(Not run)
Initializes the authorization credentials for the 'Google Vision' API.
Needs to be called before using any other functions of imgrec
and requires gvision_key
as environment variable.
gvision_init()
gvision_init()
nothing.
## Not run: Sys.setenv(gvision_key = "Your Google Vision API key") gvision_init() ## End(Not run)
## Not run: Sys.setenv(gvision_key = "Your Google Vision API key") gvision_init() ## End(Not run)
Parses the annotations and converts most of the features to data frames. Also stores the corresponding image identifiers for each feature as 'img_id'
parse_annotations(annotations)
parse_annotations(annotations)
annotations |
An annotation object created with |
A list containing data frames for each feature:
label annotations
web label annotations
similar web images
partial matching web images
full matching web images
matching web pages
face annotations
object annotations
logo annotations
landmark annotations
full text annotation
safe search annotation
dominant color annotations
crop hints for ratios 0.8/1.0/1.2
## Not run: # initialize api credentials gvision_init() # annotate images finn_image <- 'https://upload.wikimedia.org/wikipedia/en/2/2a/Finn-Force_Awakens_%282015%29.png' sw_image <- 'https://upload.wikimedia.org/wikipedia/en/8/82/Leiadeathstar.jpg' padme_image <- 'https://upload.wikimedia.org/wikipedia/en/e/ee/Amidala.png' results <- get_annotations(images = c(finn_image, sw_image, padme_image), features = 'all', max_res = 10, mode = 'url') # parse annotations img_data <- parse_annotations(results) # available feature data frames names(img_data) ## End(Not run)
## Not run: # initialize api credentials gvision_init() # annotate images finn_image <- 'https://upload.wikimedia.org/wikipedia/en/2/2a/Finn-Force_Awakens_%282015%29.png' sw_image <- 'https://upload.wikimedia.org/wikipedia/en/8/82/Leiadeathstar.jpg' padme_image <- 'https://upload.wikimedia.org/wikipedia/en/e/ee/Amidala.png' results <- get_annotations(images = c(finn_image, sw_image, padme_image), features = 'all', max_res = 10, mode = 'url') # parse annotations img_data <- parse_annotations(results) # available feature data frames names(img_data) ## End(Not run)
Writes raw JSON data as returned by the Google Vision API to a UTF-8 encoded local file.
save_json(annotations, file)
save_json(annotations, file)
annotations |
An annotation object created with |
file |
Local path where the JSON data should be stored. |
nothing.
## Not run: gvision_init() finn_image <- 'https://upload.wikimedia.org/wikipedia/en/2/2a/Finn-Force_Awakens_%282015%29.png' results <- get_annotations(images = finn_image, features = 'all', max_res = 10, mode = 'url') temp_file_path <- tempfile(fileext = '.json') save_json(results, temp_file_path) ## End(Not run)
## Not run: gvision_init() finn_image <- 'https://upload.wikimedia.org/wikipedia/en/2/2a/Finn-Force_Awakens_%282015%29.png' results <- get_annotations(images = finn_image, features = 'all', max_res = 10, mode = 'url') temp_file_path <- tempfile(fileext = '.json') save_json(results, temp_file_path) ## End(Not run)