<jupyter_start><jupyter_text>Beginner's Guide to Google's Vision API in Python**Learn what is Vision API and what are all the things that it offers. By the end of this tutorial, you will also learn how you can call Vision API from your Python code.**  It's been quite a while Google released a dedicated API called **[Vision API](https://cloud.google.com/vision/)** for performing _computer vision_ related tasks. Computer vision is a field that concerns how a computer processes an image. It is quite easy for us humans to derive any useful insights from a given image but how a computer does it? Say, for example, you supply an image of a dog to your computer and using some software the computer tells you that the image supplied to it is a dog's image. This is where computer vision comes in. Computer vision is a whole world of study onto itself and the _Vision API_ provides a number of utilities for performing tasks related to computer vision with absolute ease. The best part is that developers with absolutely no previous experience in computer vision can use _Vision API_ by going through its [documentation](https://cloud.google.com/vision/overview/docs/). This tutorial attempts to introduce you with the _Vision API_ and how it can be called from Python code. Specifically speaking, this tutorial is going to cover:- What is Google's Vision API (a more detailed introduction)- What are the offerings of Vision API- Vision API Client Library for Python- A case study with Vision API in Python**Note**: If you feel that you want to know more about APIs in general (from Python and Machine Learning perspectives) before getting started with this tutorial following are some good resources: - [An Introduction to APIs](https://www.analyticsvidhya.com/blog/2016/11/an-introduction-to-apis-application-programming-interfaces-5-apis-a-data-scientist-must-know/)- [Turning Machine Learning Models into APIs in Python](https://www.datacamp.com/community/tutorials/machine-learning-models-api-python?tap_a=5644-dce66f&tap_s=3575)  What is Google's Vision API (a more detailed introduction)?Google have encapsulated their Machine Learning models in an API to allow developers to use their Vision technology. The Vision API can quickly classify images into thousands of categories and assign them sensible labels. It can even detect individual objects, faces, and pieces of text within an image.On a very high level, Google's Vision API lets you do two things:- Use the API directly from your code for doing powerful image analysis that too as scale. - Build custom models using the API to accommodate more flexibility for your particular use case.This API is particularly very useful because as the modern day progresses the need for "Full-Stack" practitioners is increasing very rapidly. Now, consider a scenario where a Full-Stack web developer (this essentially means that the developer is equipped with both the front-end and back-end technologies related to web development) is asked build a website that takes images and detects its kind. Now, this would certainly require a good amount of knowledge in Computer Vision (if he does not already) because the developer will have to instruct his back-end code in such a way that it can accurately detect a given image. Also, assume that the deadline is not very long.Now, in a situation like this, if the developers starts to learn Computer Vision from scratch and then implements the required tasks he is more likely to miss the deadline. Instead if he uses some pre-trained computer vision models and learn the underlying concepts as he proceeds towards the development, it would be more practical. This is exactly one of those situations where the Vision API comes in handy. The API provides many state-of-the-art pre-trained models to serve many real-world business use-cases. The term "Full-Stack" is also getting associated with roles like _Machine Learning Engineer_, _Data Scientist_ etc. A Full-Stack Machine Learning Practitioner/Data Scientist is supposed design and develop or at least know the end-to-end business processes. This includes "Making Production-ready Models" as one of the most crucial steps wherein the concerned person/team wraps the developed model into an API or a set of APIs and deploys on the production environment. Now, the term _production_ varies accordingly to the use-cases but the general idea/framework of the processes remains the same.  The Vision API lets you easily train custom vision models with _[AutoML Vision BETA](https://cloud.google.com/automl/)_.  Great! By now, you should have got a pretty good overview of Vision API. A nice little experiment that you can do on the [Vision homepage](https://cloud.google.com/vision/) is to analyze your favorite images and derive useful insights with the help of Vision. Here are the steps to do this:- Go to [Vision homepage](https://cloud.google.com/vision/).- It has a section called "Try the API". It lets you to drag/upload an image in its interface.    - Once you suuply an image to it it provides you with a bunch of information regarding the image:      As you can see, Vision detected many facts about the supplied image within no time. Feel free to explore the other tabs as well to learn even more about the image.   Consider this task if it was to be performed on a set of billion images. Using an API like this would certainly be fruitful in that regard. Now, let's learn about the offerings of Vision API as to see some examples real-world use-cases the API has served to.   What are the offerings of Vision API - Some niche use-cases: The Vision API is known for its accurate results. Vision API [documentation](https://cloud.google.com/vision/docs/tutorials) provides an excellent collection of tutorials which gives you a very detailed insight about the API. But for a first glance, these things may appear to be overwhelming. So, to keep things simple, you will learn about few use cases which have been already served by Vision API. - **Optical Character Recognition (OCR)**: This is a classic example of Computer Vision which primarily deals with extraction of text from an image. The Vision API comprises many state-of-th-art approaches for doing this.  - **Detection of Image Properties**: This is the task that you performed in the earlier section. With Vision API you can retrive  general attributes of an image, its features such as dominant color.- **Label Detection**: This task annotates an image with a label (or "tag") based on the image content. For example, a picture of a dog may produce a label of "dog", "animal", or some other similar annotation. This is an extremely important step in the field of [Content-based Information Retrieval](http://www.intelligence.tuc.gr/~petrakis/courses/multimedia/retrieval.pdf).- **Face Detection**: Given an image or set of images the task is to detect the faces present in them. This has several large applications like Surveillance Systems. These are some of the excellent use-cases on which Vision API performs seamlessly and you can integrate any of the above into your applications within very less amount of time. If you want to learn more use-cases  like these, be sure to check out [these tutorials](https://cloud.google.com/vision/docs/tutorials). Vision API provides support for a wide range of languages like `Go`, `C`, `Java`, `PHP`, `Node.js`, `Python`, `Ruby`. In the next sections, you will see how to use Vision API in Python.   Vision API Client Library for Python:The first step for using the Python variant of Vision API, you will have to install it. The best way to install it is through `pip`.<jupyter_code>!pip install google-cloud-vision<jupyter_output><empty_output><jupyter_text>One the installation is successful, the next step is to verify if it is really successful.<jupyter_code>from google.cloud import vision<jupyter_output><empty_output><jupyter_text>If the above line of code executes successfully, you are ready to proceed. Google provides a series of amazing tutorials on [using Vision API in Python](https://developers.google.com/api-client-library/python/apis/vision/v1). Now, you will build a simple application in Python which will be able to detect some general attributes of an image, such as *dominant color*.   A case study with Vision API in Python:Your application will take a path of an image as its input and it will display the general attributes of the corresponding image. This is useful when the images are located inside the computer on which the application is going to be executed. But what if you need to read an image from the internet? The Vision API supports reading images from the internet as well. In this case study, you will learn to tackle the first scenario. But it's only a matter of one line of code to accommodate the internet variant.  As always, you will start off by importing `vision` from `google.cloud` module.<jupyter_code>from google.cloud import vision<jupyter_output><empty_output><jupyter_text>The next step is to call `ImageAnnotatorClient()` which contains the utilities for extracting image properties.<jupyter_code>client = vision.ImageAnnotatorClient()<jupyter_output><empty_output><jupyter_text>You will most likely run into an error if `GOOGLE_APPLICATION_CREDENTIALS` environment variable is not set. This is because these libraries use _Application Default Credentials (ADC)_ to locate your application's credentials. When your code uses libraries like this, the strategy checks for your credentials. Follow [this link](https://console.cloud.google.com/apis/credentials) to learn how to generate `GOOGLE_APPLICATION_CREDENTIALS`. Your aim is to generate a `client_secrets.json` file which you will use for authentication purpose.  Once `client_secrets.json` is obtained, you will execute the following code to set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.<jupyter_code>import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="client_secrets.json"<jupyter_output><empty_output><jupyter_text>Now running the below code should not give you any error.<jupyter_code>client = vision.ImageAnnotatorClient()<jupyter_output><empty_output><jupyter_text>You will now write code for reading an image through a given path. Let's take the following image for this purpose. Feel free to use an image of your choice. **[Image Courtesy](https://theconversation.com/pet-theft-is-on-the-rise-with-more-than-60-dogs-stolen-in-the-uk-every-week-91418)**<jupyter_code>import io 

path = 'Image.jpeg'
with io.open(path, 'rb') as image_file:
        content = image_file.read()<jupyter_output><empty_output><jupyter_text>You have successfully loaded an image into your workspace. Now, you will instantiate an object of type `vision.types.Image` and you will supply `content=content` as its argument.<jupyter_code>image = vision.types.Image(content=content)<jupyter_output><empty_output><jupyter_text>You are only left with the final steps of your Image Properties detection application. In these steps, you will:- Call `client.image_properties` with as `(image=image)` argument.- Store the response of `image_properties()` in a variable `response` and extract the image properties by calling the `image_properties_annotation` argument of `response`. - Display several properties of the images in a formatted manner.<jupyter_code>response = client.image_properties(image=image)
props = response.image_properties_annotation
print('Properties of the image:')

for color in props.dominant_colors.colors:
    print('Fraction: {}'.format(color.pixel_fraction))
    print('\tr: {}'.format(color.color.red))
    print('\tg: {}'.format(color.color.green))
    print('\tb: {}'.format(color.color.blue))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.060926700230395085, 'fraction_numerical': 0.002133287823193105, 'mean_word_length': 4.970453387671931, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 12, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '546752', 'license': 'MIT', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2789, 'n_words': 1597, 'path': "DataCamp Vision API Tutorial/Beginner's Guide to Google's Vision API in Python.ipynb", 'repo_name': 'sayakpaul/DataCamp-blogs', 'size': '11719'}