Visit the following webpage: https://archive.org/details/National_Geographic_Wallpapers This webpage hosts a collection of 506 images from National Geographic Magazine with most of these images previously being part of an international photography contest. Your task is to randomly choose 5 images and identify the objects within those images using Google’s vision API. Submit your iPython notebook code, and screenshots of output as shown below. You may need to reactivate your Google vision API account (or billing information for trial cycles) if you haven’t used it recently. Here is the sample code import base64 import urllib import os import io import PIL from IPython.display import display, Image GOOGLE_API_KEY = '' #Use your Google API key here pip install google-api-python-client from googleapiclient.discovery import build service = build('vision', 'v1', developerKey=GOOGLE_API_KEY) cat = 'C:\\Users\\Instagram and neural networks\\cat.jpg' def label_image(path=None, URL=None, max_results=5): if URL is not None: image_content=base64.b64encode(urllib.request.urlopen(URL).read()) else: image_content=base64.b64encode(open(path,'rb').read()) service_request=service.images().annotate(body={ 'requests':[{ 'image':{ 'content': image_content.decode('UTF-8') }, 'features': [{ 'type': 'LABEL_DETECTION', 'maxResults': max_results }] }] }) labels = service_request.execute()['responses'][0]['labelAnnotations'] if URL is not None: display(Image(url=URL)) else: display(Image(path)) for label in labels: print('[{0:3.0f}%]: {1}'.format(label['score']*100, label['description'])) return label_image(cat)
Visit the following webpage: https://archive.org/details/National_Geographic_Wallpapers
This webpage hosts a collection of 506 images from National Geographic Magazine with most of these
images previously being part of an international photography contest. Your task is to randomly choose 5
images and identify the objects within those images using Google’s vision API. Submit your iPython
notebook code, and screenshots of output as shown below.
You may need to reactivate your Google vision API account (or billing information for trial cycles) if you
haven’t used it recently.
Here is the sample code
import base64
import urllib
import os
import io
import PIL
from IPython.display import display, Image
GOOGLE_API_KEY = '' #Use your Google API key here
pip install google-api-python-client
from googleapiclient.discovery import build
service = build('vision', 'v1', developerKey=GOOGLE_API_KEY)
cat = 'C:\\Users\\Instagram and neural networks\\cat.jpg'
def label_image(path=None, URL=None, max_results=5):
if URL is not None:
image_content=base64.b64encode(urllib.request.urlopen(URL).read())
else:
image_content=base64.b64encode(open(path,'rb').read())
service_request=service.images().annotate(body={
'requests':[{
'image':{
'content': image_content.decode('UTF-8')
},
'features': [{
'type': 'LABEL_DETECTION',
'maxResults': max_results
}]
}]
})
labels = service_request.execute()['responses'][0]['labelAnnotations']
if URL is not None:
display(Image(url=URL))
else:
display(Image(path))
for label in labels:
print('[{0:3.0f}%]: {1}'.format(label['score']*100, label['description']))
return
label_image(cat)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps