Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploring the World Using Cloud Vision & Twilio

juliaferraioli
September 20, 2016

Exploring the World Using Cloud Vision & Twilio

We take photos for many reasons: to capture moments, because something's funny or pretty, because we're curious about something and want to know more. Luckily, there's a whole area of machine learning dedicated to understanding images.

We’ll explore how we can use machine learning ﹘ without the math ﹘ with Twilio and the pictures that we take to gain a greater understanding of the world around us. We'll dig into various ways that this fusion can be used to make people's lives richer and better.

Accompanying blog post: http://www.blog.juliaferraioli.com/2016/02/exploring-world-using-vision-twilio.html

juliaferraioli

September 20, 2016
Tweet

More Decks by juliaferraioli

Other Decks in Technology

Transcript

  1. Exploring the World Using Cloud Vision & Twilio Julia Ferraioli

    @juliaferraioli Software Engineer Google Open Source Programs Office
  2. Google Cloud Vision • Detect faces, landmarks, logos, text, and

    more • Perform sentiment analysis • Straightforward REST API • Works on a base64-encoded image • Connects to Google Cloud Storage
  3. @juliaferraioli Receiving the MMS @app.route("/", methods=['GET', 'POST']) def receive_message(): for

    i in range(int(request.values.get('NumMedia', None))): media_url = request.values.get('MediaUrl%i' % i, None) image = requests.get(media_url).content labels = get_labels(image) resp = construct_message(labels) return str(resp)
  4. @juliaferraioli Receiving the MMS @app.route("/", methods=['GET', 'POST']) def receive_message(): for

    i in range(int(request.values.get('NumMedia', None))): media_url = request.values.get('MediaUrl%i' % i, None) image = requests.get(media_url).content labels = get_labels(image) resp = construct_message(labels) return str(resp)
  5. @juliaferraioli Processing the image def get_labels(image, num_retries=3, max_results=3): # Set

    up the service that can access the API # Prepare the image for the API # Construct the request # Send it off to the API # Return the labels
  6. @juliaferraioli Authenticating with the API def get_labels(image, num_retries=3, max_results=3): #

    Set up the service that can access the API http = httplib2.Http() credentials = GoogleCredentials.get_application_default().create_scoped( ['https://www.googleapis.com/auth/cloud-platform']) credentials.authorize(http) service = discovery.build('vision', 'v1', http=http, discoveryServiceUrl=DISCOVERY_URL)
  7. @juliaferraioli Crafting the request def get_labels(image, num_retries=3, max_results=3): # Construct

    the request service_request = service.images().annotate( body={'requests': [{ 'image': { 'content': image_content, }, 'features': [{ 'type': 'LABEL_DETECTION', 'maxResults': max_results, }] }] })
  8. @juliaferraioli Executing the request def get_labels(image, num_retries=3, max_results=3): # Send

    it off to the API response = service_request.execute(num_retries=num_retries)
  9. @juliaferraioli Returning the labels def get_labels(image, num_retries=3, max_results=3): # Return

    the labels if('responses' in response and 'labelAnnotations' in response['responses'][0]): # Hey, the API found something! return response['responses'][0]['labelAnnotations'] else: # Sigh, no dice this time :-( return []
  10. @juliaferraioli Receiving the MMS @app.route("/", methods=['GET', 'POST']) def receive_message(): for

    i in range(int(request.values.get('NumMedia', None))): media_url = request.values.get('MediaUrl%i' % i, None) image = requests.get(media_url).content labels = get_labels(image) response = construct_message(labels) return str(response)
  11. @juliaferraioli Sending back the results def construct_message(labels): label_desc = ""

    # Go through labels and turn them into text of the response # Turn the string into a twiml response
  12. @juliaferraioli Sending back the results def construct_message(labels): # Go through

    labels and turn them into text of the response for i in range(len(labels)): # We've got an answer! Let's tell them about it label_desc += 'Score is %s for %s\n' % (labels[i]['score'], labels[i]['description'])
  13. @juliaferraioli Sending back the results def construct_message(labels): # Turn the

    string into a twiml response resp = twilio.twiml.Response() resp.message(label_desc)
  14. @juliaferraioli Responding to the MMS @app.route("/", methods=['GET', 'POST']) def receive_message():

    for i in range(int(request.values.get('NumMedia', None))): media_url = request.values.get('MediaUrl%i' % i, None) image = requests.get(media_url).content labels = get_labels(image) resp = construct_message(labels) return str(resp)
  15. @juliaferraioli What’s that? We found some labels for your image:

    Score is 0.90518606 for cephalopod Score is 0.79291707 for marine invertebrates Score is 0.78696847 for octopus
  16. @juliaferraioli What’s that? We found some labels for your image:

    Score is 0.59691668 for brand Score is 0.58166391 for writing
  17. @juliaferraioli Why? ¯\_(ツ)_/¯ • Aid tourists around the world with

    landmark detection • Build a bot to sort your mail and text you the senders with OCR • Run a simple Q&A service for people with low vision • Help folks analyze interactions using sentiment analysis • Boost conference scanners to recognize logos on business cards • … and lots more
  18. Learn more • Google Cloud Vision developer documentation: http://bit.ly/gcp-vision •

    Code from today: http://bit.ly/twilio-vision • Code from today running on Kubernetes (!!!): http://bit.ly/twilio-vision-k8s