Skip to content

Commit

Permalink
Merge pull request #3 from npernat/master
Browse files Browse the repository at this point in the history
update with no-reject parameter
  • Loading branch information
AugustT authored Mar 9, 2022
2 parents 8bb16f5 + 4475d15 commit 8d0ff10
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 34 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Package: plantnet
Type: Package
Title: Automated Plant Identification with PlantNet
Version: 0.1.0
Version: 1.0.0
Date: 2019-07-25
Author: Tom August
Author: Tom August, Nadja Pernat
Maintainer: Tom August <tomaug@ceh.ac.uk>
Description: Using the PlantNet API (https://my.plantnet.org/) this package will assign a plant species name to images.
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
RoxygenNote: 7.1.2
Imports: httr
Suggests:
testthat,
Expand Down
38 changes: 24 additions & 14 deletions R/buildURL.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
#' @param key character, your API key (get from my-api.plantnet.org)
#' @param imageURL character, the URL path top the image you want to identify
#' @param organs character, the organ in the image. Must be one of,
#' leaf, flower, fruit, or bark. Currently this does not effect the result.
#' auto, leaf, flower, fruit, or bark. Currently this does not effect the result.
#' You can also use the tags habit (the overall form of the plant), or other,
#' but you can only have an image labelled as one of these if you also have
#' an image labelled as one of the primay organs (i.e. leaf, flower, fruit, bark).
#' an image labelled as one of the primary organs (i.e. auto, leaf, flower, fruit, bark).
#' @param lang can be one of 'en' (English), 'fr' (French), 'de' (German)
#' @param no_reject character, can be one of 'true' or 'false' (default),
#' if 'true' 'no results' are disabled in case of reject class match
#' @return The URL required, as a character of length 1
#' @importFrom utils URLencode
#' @export

buildURL <- function(key, imageURL, organs = 'leaf', lang = 'en'){
buildURL <- function(key, imageURL, organs = 'auto',
lang = 'en', no_reject = 'false'){

if(length(imageURL) != length(organs)){
stop('imageURL and organs must be the same length')
}

if(!all(organs %in% c('flower','leaf','fruit','bark','habit','other'))){
stop("The only allowed values of organs are 'flower','leaf','fruit','bark','habit','other'")
if(!all(organs %in% c('auto', 'flower','leaf','fruit','bark','habit','other'))){
stop("The only allowed values of organs are 'auto', 'flower','leaf','fruit','bark','habit','other'")
}

if(!any(organs %in% c('flower','leaf','fruit','bark'))){
stop("At leaset one image must be tagged as 'flower','leaf','fruit','bark'")
if(!any(organs %in% c('auto', 'flower','leaf','fruit','bark'))){
stop("At leaset one image must be tagged as 'auto', 'flower','leaf','fruit','bark'")
}

if(length(lang) > 1){
Expand All @@ -36,13 +39,20 @@ buildURL <- function(key, imageURL, organs = 'leaf', lang = 'en'){
if(!lang %in% c('en', 'fr', 'de')){
stop("lang must be one of 'en', 'fr', 'de'")
}


if(!no_reject %in% c('true','false'))
stop("no_reject must be one of 'true' or 'false'(default)")

URLencoded <- sapply(imageURL, FUN = URLencode, reserved = TRUE, repeated = TRUE)

paste0("https://my-api.plantnet.org/v1/identify/all?",
"images=", paste(URLencoded, collapse = "&images="),
"&organs=", paste(organs, collapse = "&organs="),
"&lang=", lang,
"&api-key=", key)
url_string <- paste0("https://my-api.plantnet.org/v2/identify/all?",
"images=", paste(URLencoded, collapse = "&images="),
"&organs=", paste(organs, collapse = "&organs="),
"&no-reject=", no_reject,
"&lang=", lang,
"&api-key=", key)

print(url_string)
return(url_string)

}
}
16 changes: 11 additions & 5 deletions R/identify.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
#' @param simplify logical, if `TRUE` the output will be simplified into a
#' data.frame
#' @param organs character, the organ in the image. Must be one of,
#' leaf, flower, fruit, or bark. Must be the same length as `imageURL`.
#' auto, leaf, flower, fruit, or bark. Must be the same length as `imageURL`.
#' You can also use the tags habit (the overall form of the plant), or other,
#' but you can only have an image labelled as one of these if you also have
#' an image labelled as one of the primay organs (i.e. leaf, flower, fruit, bark).
#' an image labelled as one of the primary organs (i.e. auto, leaf, flower, fruit, bark).
#' @param lang can be one of 'en' (English), 'fr' (French), 'de' (German)
#' @param no_reject character, can be one of 'true' or 'false' (default),
#' if 'true' 'no results' are disabled in case of reject class match
#' @details The function uses the PlantNet API. To use this service you need
#' to have registered an account and generated an API key. All this can be
#' done here: https://my.plantnet.org/.
#' @return If `simplify` is set to `FALSE` then a list is returned. The
#' structure of this list can be explored using hte tool at
#' structure of this list can be explored using the tool at
#' https://my-api.plantnet.org. If `simplify` is set to `TRUE` (default)
#' then a data.frame is with three columns for the score, latin name,
#' and common name. There are as many rows as there are species predictions
Expand Down Expand Up @@ -60,12 +62,16 @@
#'}

identify <- function(key, imageURL, simplify = TRUE,
organs = rep('leaf', length(imageURL)), lang = 'en'){
organs = rep('auto', length(imageURL)),
lang = 'en', no_reject = 'false'){

if(!is.character(key)) stop('key should be a character')
if(!is.character(imageURL)) stop('image should be a character')
if(!is.character(no_reject)) stop('no_reject should be a character')


URL <- buildURL(key = key, imageURL = imageURL, organs = organs, lang = lang)
URL <- buildURL(key = key, imageURL = imageURL, organs = organs,
lang = lang, no_reject = no_reject)

# Hit the API
response <- httr::GET(URL)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# PlantNet

### Developed by Tom August
last built 2019-07-23
last built 09/03/2022

The R-package interfaces with the PlantNet image classification API. This API is designed to receive images and return species classifications. You can find out more about the PlantNet project here: https://plantnet.org. To use the API you need to have registered an account and generated an API key. All this can be done here: https://my.plantnet.org/.

Expand Down
9 changes: 6 additions & 3 deletions man/buildURL.Rd

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

19 changes: 14 additions & 5 deletions man/identify.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/testbuildURL.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("test warnings and errors", {
imageURL = imageURL,
organs = 'head',
lang = lang),
"The only allowed values of organs are 'flower','leaf','fruit','bark','habit','other'")
"The only allowed values of organs are 'auto', 'flower','leaf','fruit','bark','habit','other'")
expect_error(buildURL(key = key,
imageURL = imageURL,
organs = c('flower', 'flower'),
Expand All @@ -31,7 +31,7 @@ test_that("test warnings and errors", {
imageURL = imageURL,
organs = c('habit'),
lang = lang),
"At leaset one image must be tagged as 'flower','leaf','fruit','bark'")
"At leaset one image must be tagged as 'auto', 'flower','leaf','fruit','bark'")
expect_error(buildURL(key = key,
imageURL = c(imageURL, imageURL),
organs = organs,
Expand All @@ -46,7 +46,7 @@ test_that("test built URL is as expected", {
imageURL = c(imageURL, imageURL),
organs = c(organs, organs),
lang = lang)
expected <- "https://my-api.plantnet.org/v1/identify/all?images=IMAGEURL&images=IMAGEURL&organs=flower&organs=flower&lang=en&api-key=MYKEY"
expected <- "https://my-api.plantnet.org/v2/identify/all?images=IMAGEURL&images=IMAGEURL&organs=flower&organs=flower&no-reject=false&lang=en&api-key=MYKEY"
expect_equal(tested, expected)

})

0 comments on commit 8d0ff10

Please sign in to comment.