Package 'imgrec'

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

Help Index


get image annotations

Description

Calls the 'Google Vision' API to return annotations. The function automatically creates batches

Usage

get_annotations(images, features, max_res, mode)

Arguments

images

A character vector for images to be annotated. Can either be url strings or local images, as specified with mode.

features

A character vector for the features to be returned. Accepts 'all' or any combination of the following inputs: 'label', 'web', 'text', 'face', 'landmark', 'logo', 'safe_search', 'object', 'properties'

max_res

An integer specifying the maximum number of results to be returned for each feature.

mode

Accepts 'url' for image urls and 'local' for file paths to local images.

Value

An response object of class 'gvision_annotations'.

See Also

Google Vision features and quotas.

Examples

## 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)

authorization for Google Vision

Description

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.

Usage

gvision_init()

Value

nothing.

Examples

## Not run: 
Sys.setenv(gvision_key = "Your Google Vision API key")

gvision_init()

## End(Not run)

parse image annotations

Description

Parses the annotations and converts most of the features to data frames. Also stores the corresponding image identifiers for each feature as 'img_id'

Usage

parse_annotations(annotations)

Arguments

annotations

An annotation object created with get_annotations.

Value

A list containing data frames for each feature:

labels

label annotations

web_labels

web label annotations

web_similar

similar web images

web_match_partial

partial matching web images

web_match_full

full matching web images

web_match_pages

matching web pages

faces

face annotations

objects

object annotations

logos

logo annotations

landmarks

landmark annotations

full_text

full text annotation

safe_serarch

safe search annotation

colors

dominant color annotations

crop_hints

crop hints for ratios 0.8/1.0/1.2

Examples

## 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)

save annotation data as JSON

Description

Writes raw JSON data as returned by the Google Vision API to a UTF-8 encoded local file.

Usage

save_json(annotations, file)

Arguments

annotations

An annotation object created with get_annotations.

file

Local path where the JSON data should be stored.

Value

nothing.

Examples

## 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)