Funko Price Checker with Google Cloud Vision API & R

How I built a Funko Price Checker in 20 Minutes with Google Cloud Vision

Holly Emblem
Jul 24, 2017 · 3 min read

Confession time: I’m a big Funko fan. I have too many of the things and in recent months, I’ve gotten into the habit of checking the price of my collection and potential new additions on sites like www.PopPriceGuide.com.

Recently, I’ve been thinking about how I could use machine learning to automate this process, with the end goal of holding up a Funko in the store and checking if the price is greater than/less than online. In this blog post, I’ll cover how to get started with a basic prototype of this idea, using Google Cloud Vision, R and a couple of neat R packages. This isn’t so much a coding tutorial, more of an introduction to Cloud Vision and how quickly it is to get started.

First off, if you haven’t already, I recommend checking out the Cloud Vision API: You can detect everything from locations to brands. It’s free for the first 1,000 API calls and Google also give you $300 in credit.

Cloud Vision is essentially Google’s machine learning API, which allows you detect and classify the likes of the following:

  • Labels
  • Explicit Content
  • Logos
  • Landmarks
  • Faces

Cloud Vision API & R

For my prototype, I used RoogleVision, a Cloud Vision package for R developed by Florian Teschner. Getting started is easy: Once you’ve signed up, simply fill in your OAuth Credentials:

#install.packages("devtools")
require(devtools)
install_github("flovv/RoogleVision")
#install.packages("rvest")require(RoogleVision)
#devtools::install_github("MarkEdmondson1234/googleAuthR")
require(googleAuthR)
require(rvest)
### 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()

Code adapted from http://flovv.github.io/RoogleVision-released/

In my example, I take a photo of the Funko in question with my Mac camera, then want to pull out the text from this using text detection:

x <- ‘log-lady.jpg’input <- getGoogleVisionResponse(x, feature=”TEXT_DETECTION”)
value <- input$description[1]
# This pulls all of the text into one variable for R
remover <- gsub('\n|\\+|\\s+|/','+',value)
remover <- tolower(remover)
# We clean up the text a little before passing it to the scraper.

Pre tidy up, you then receive an output like so (obviously, it will differ per photo)…

"WIN PEAKS\n451\nTELEVISION\nTHE LOG LADY\nVINYL FIGURE/FIGURINE EN VINYLE/\nFIGURA DE VINIL\nAGE\n17+\n"

Once we’ve cleaned up the unnecessary \n and other extraneous details, we can pass it to rvest, a scraper package from R. An example call with a URL to return cost data would be:

url <- paste('URL to query',remover,sep="")
webpage <- read_html(url)
costData <- html_nodes(webpage,'div:nth-child(2) > div:nth-child(2)')
costData <- html_text(costData)

Rvest is covered extensively online, and I don’t want to encourage mass scraping of Funko price guides, so if you’d like to learn more, you can find a good tutorial for Rvest here.

Within costData, we can then find the market value for the Log Lady Funko Pop, a whole $12 in this instance. Below, I’ve also wrapped these various little bits of code into a function.

funkoChecker<- function(x)
{
googleChecker() # This houses my call to Cloud Vision
input <- getGoogleVisionResponse(x, feature="TEXT_DETECTION")
value <- input$description[1]
# A very basic couple of regexs just to get going - Can be optimised.remover <- gsub('VINYL|VIYL|FIGURE|FIGURINE|FIGURA DE VINIL|\nAGE\n17+|A WARNING! CHOKING HAZARD|\\+','',value)
remover <- gsub('\n|\\+|\\s+|/','+',remover)
remover <- gsub('\\s+','+',remover)
remover <- gsub('\\s+','',remover)
remover <- tolower(remover)

url <- paste('URL to query',remover,sep="")
webpage <- read_html(url)
costData <- html_nodes(webpage,'#i9617 > div:nth-child(2) > div:nth-child(2)')costData <- html_text(costData)
return(costData)
}
loglady <- funkoChecker('log-lady.jpg')
loglady
#Output
[1] “$12.00”

Some Limitations

I’ve covered how to quickly get started in R with Cloud Vision and querying external web sources with a scraper. However, this is by no means a fully fledged service! As is, the code isn’t scalable (that regex is not ideal and the function could definitely be optimised), nor is the solution, as there is no price checker API for Funkos as far as I know.

Hopefully it gives you a good understanding of just how easy it is to get started with Cloud Vision and using machine learning to solve everyday (well, my everyday!) problems.

Really in 20 minutes?

Yes: To be honest, a basic prototype was up and working in 10 minutes, as you can tell by the not-so-elegant-code. I had a little bit of lag with Oauth credentials within the API console, which caused a delay. This blog post actually took much longer to write and thanks to Rupi Dosanjh for the gif.

Holly Emblem

Written by

Data Scientist @ Microsoft. Views my Own. Writer for https://www.r-bloggers.com, Towards Data Science & Coinmonks. BSc Computer Science

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade