|
| 1 | +Using the Vision API |
| 2 | +==================== |
| 3 | + |
| 4 | +Authentication and Configuration |
| 5 | +-------------------------------- |
| 6 | + |
| 7 | +- For an overview of authentication in ``gcloud-python``, |
| 8 | + see :doc:`gcloud-auth`. |
| 9 | + |
| 10 | +- In addition to any authentication configuration, you should also set the |
| 11 | + :envvar:`GCLOUD_PROJECT` environment variable for the project you'd like |
| 12 | + to interact with. If the GCLOUD_PROJECT environment variable is not present, |
| 13 | + the project ID from JSON file credentials is used. |
| 14 | + |
| 15 | + If you are using Google App Engine or Google Compute Engine |
| 16 | + this will be detected automatically. |
| 17 | + |
| 18 | +- After configuring your environment, create a |
| 19 | + :class:`Client <gcloud.vision.client.Client>` |
| 20 | + |
| 21 | +.. code-block:: python |
| 22 | +
|
| 23 | + >>> from gcloud import vision |
| 24 | + >>> client = vision.Client() |
| 25 | +
|
| 26 | +or pass in ``credentials`` and ``project`` explicitly |
| 27 | + |
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + >>> from gcloud import vision |
| 31 | + >>> client = vision.Client(project='my-project', credentials=creds) |
| 32 | +
|
| 33 | +Annotating an Image |
| 34 | +------------------- |
| 35 | + |
| 36 | +Annotate a single image |
| 37 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 | + |
| 39 | +.. code-block:: python |
| 40 | +
|
| 41 | + >>> from gcloud import vision |
| 42 | + >>> client = vision.Client() |
| 43 | + >>> image = client.image('./image.png') |
| 44 | + >>> faces = image.detect_faces(limit=10) |
| 45 | +
|
| 46 | +Annotate multiple images |
| 47 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + >>> first_image = client.image('./image.jpg') |
| 52 | + >>> second_image = client.image('gs://my-storage-bucket/image2.jpg') |
| 53 | + >>> with client.batch(): |
| 54 | + ... labels = first_image.detect_labels() |
| 55 | + ... faces = second_image.detect_faces(limit=10) |
| 56 | +
|
| 57 | +or |
| 58 | + |
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + >>> images = [] |
| 62 | + >>> images.append(client.image('./image.jpg')) |
| 63 | + >>> images.append(client.image('gs://my-storage-bucket/image2.jpg')) |
| 64 | + >>> faces = client.detect_faces_multi(images, limit=10) |
| 65 | +
|
| 66 | +No results returned |
| 67 | +~~~~~~~~~~~~~~~~~~~ |
| 68 | + |
| 69 | +Failing annotations return no results for the feature type requested. |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + >>> from gcloud import vision |
| 74 | + >>> client = vision.Client() |
| 75 | + >>> image = client.image('./image.jpg') |
| 76 | + >>> logos = image.detect_logos(limit=10) |
| 77 | + >>> logos |
| 78 | + [] |
| 79 | +
|
| 80 | +
|
| 81 | +Manual Detection |
| 82 | +~~~~~~~~~~~~~~~~ |
| 83 | + |
| 84 | +You can call the detection method manually. |
| 85 | + |
| 86 | +.. code-block:: python |
| 87 | +
|
| 88 | + >>> from gcloud import vision |
| 89 | + >>> client = vision.Client() |
| 90 | + >>> image = client.image('gs://my-test-bucket/image.jpg') |
| 91 | + >>> faces = image.detect(type=vision.FACE_DETECTION, limit=10) |
| 92 | +
|
| 93 | +Face Detection |
| 94 | +~~~~~~~~~~~~~~ |
| 95 | + |
| 96 | +Detecting a face or faces in an image. |
| 97 | +For a list of the possible facial landmarks |
| 98 | +see: https://cloud.google.com/vision/reference/rest/v1/images/annotate#type_1 |
| 99 | + |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + >>> from gcloud import vision |
| 104 | + >>> client = vision.Client() |
| 105 | + >>> image = client.image('./image.jpg') |
| 106 | + >>> faces = image.detect_faces(limit=10) |
| 107 | + >>> faces[0].landmarks[0].type |
| 108 | + 'LEFT_EYE' |
| 109 | + >>> faces[0].landmarks[0].position.x |
| 110 | + 1301.2404 |
| 111 | + >>> faces[0].detection_confidence |
| 112 | + 0.9863683 |
| 113 | + >>> faces[0].joy_likelihood |
| 114 | + 0.54453093 |
| 115 | + >>> faces[0].anger_likelihood |
| 116 | + 0.02545464 |
| 117 | +
|
| 118 | +
|
| 119 | +
|
| 120 | +Label Detection |
| 121 | +~~~~~~~~~~~~~~~ |
| 122 | + |
| 123 | +Image labels are a way to help categorize the contents of an image. |
| 124 | +If you have an image with a car, person and a dog it, label detection will |
| 125 | +attempt to identify those objects. |
| 126 | + |
| 127 | +.. code-block:: python |
| 128 | +
|
| 129 | + >>> from gcloud import vision |
| 130 | + >>> client = vision.Client() |
| 131 | + >>> image = client.image('./image.jpg') |
| 132 | + >>> labels = image.detect_labels(limit=3) |
| 133 | + >>> labels[0].description |
| 134 | + 'automobile' |
| 135 | + >>> labels[0].score |
| 136 | + 0.9863683 |
| 137 | +
|
| 138 | +
|
| 139 | +Landmark Detection |
| 140 | +~~~~~~~~~~~~~~~~~~ |
| 141 | + |
| 142 | +The API will attemtp to detect landmarks such as Mount Rushmore and |
| 143 | +the Sydney Opera House. The API will also provide their known geographical |
| 144 | +locations if available. |
| 145 | + |
| 146 | +.. code-block:: python |
| 147 | +
|
| 148 | + >>> from gcloud import vision |
| 149 | + >>> client = vision.Client() |
| 150 | + >>> image = client.image('./image.jpg') |
| 151 | + >>> landmarks = image.detect_landmarks() |
| 152 | + >>> landmarks[0].description |
| 153 | + 'Sydney Opera House' |
| 154 | + >>> landmarks[0].locations[0].latitude |
| 155 | + -33.857123 |
| 156 | + >>> landmarks[0].locations[0].longitude |
| 157 | + 151.213921 |
| 158 | + >>> landmarks[0].bounding_poly.vertices[0].x |
| 159 | + 78 |
| 160 | + >>> landmarks[0].bounding_poly.vertices[0].y |
| 161 | + 162 |
| 162 | +
|
| 163 | +Logo Detection |
| 164 | +~~~~~~~~~~~~~~ |
| 165 | + |
| 166 | +Google Vision can also attempt to detect company and brand logos in images. |
| 167 | + |
| 168 | +.. code-block:: python |
| 169 | +
|
| 170 | + >>> from gcloud import vision |
| 171 | + >>> client = vision.Client() |
| 172 | + >>> image = client.image('./image.jpg') |
| 173 | + >>> logos = image.detect_logos(limit=1) |
| 174 | + >>> results.logos[0].description |
| 175 | + 'Google' |
| 176 | + >>> logos[0].score |
| 177 | + 0.9795432 |
| 178 | + >>> logos[0].bounding_poly.vertices[0].x |
| 179 | + 78 |
| 180 | + >>> logos[0].bounding_poly.vertices[0].y |
| 181 | + 62 |
| 182 | +
|
| 183 | +Safe Search Detection |
| 184 | +~~~~~~~~~~~~~~~~~~~~~ |
| 185 | + |
| 186 | +Detecting safe search properties of an image. |
| 187 | + |
| 188 | +.. code-block:: python |
| 189 | +
|
| 190 | + >>> from gcloud import vision |
| 191 | + >>> client = vision.Client() |
| 192 | + >>> image = client.image('./image.jpg') |
| 193 | + >>> safe_search = image.detect_safe_search() |
| 194 | + >>> safe_search.adult |
| 195 | + 'VERY_UNLIKELY' |
| 196 | + >>> safe_search.medical |
| 197 | + 'UNLIKELY' |
| 198 | +
|
| 199 | +Text Detection |
| 200 | +~~~~~~~~~~~~~~ |
| 201 | + |
| 202 | +Detecting text with ORC from an image. |
| 203 | + |
| 204 | +.. code-block:: python |
| 205 | +
|
| 206 | + >>> from gcloud import vision |
| 207 | + >>> client = vision.Client() |
| 208 | + >>> image = client.image('./image.jpg') |
| 209 | + >>> text = image.detect_text() |
| 210 | + >>> text.locale |
| 211 | + 'en' |
| 212 | + >>> text.description |
| 213 | + 'the full text of the image.' |
| 214 | +
|
| 215 | +Image Properties |
| 216 | +~~~~~~~~~~~~~~~~ |
| 217 | + |
| 218 | +Detecting image color properties. |
| 219 | + |
| 220 | +.. code-block:: python |
| 221 | +
|
| 222 | + >>> from gcloud import vision |
| 223 | + >>> client = vision.Client() |
| 224 | + >>> image = client.image('./image.jpg') |
| 225 | + >>> colors = image.detect_properties() |
| 226 | + >>> colors[0].red |
| 227 | + 244 |
| 228 | + >>> colors[0].blue |
| 229 | + 134 |
| 230 | + >>> colors[0].score |
| 231 | + 0.65519291 |
| 232 | + >>> colors[0].pixel_fraction |
| 233 | + 0.758658 |
0 commit comments