This is Deepsplash!



Messing around with stupid artificial intelligence

So I found this API called DeepAI and it has a thing called Neural Talk. It's description says "Summarizes the content of an image in a one sentence description." so I thought I'd give it a whirl. We need images...

import requests

img = requests.get('https://source.unsplash.com/random/1200x800',
          allow_redirects=False).headers['Location']

print(img)

Which prints out the location of an image from Unsplash.

https://images.unsplash.com/photo-1553231915-7279274eb20d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1200&h=800&fit=crop&ixid=eyJhcHBfaWQiOjF9

Let's push that img variable through the DeepAI API, parse the output from the returned json and see what happens. You'll have to make an account with DeepAI at https://deepai.org and get a key.

response = requests.post(
    "https://api.deepai.org/api/neuraltalk",
    data = {'image': img},
    headers = {'api-key': 'XXXX'}
)

print(response.json()['output'])

It'll print some words out. like "a plate of food with meat and vegetables" or something. We don't know what it's looking at so let's get the image at the same time.

def get_img(url):
    img = requests.get(url).content
    with open('image.jpg', 'wb') as f:
        f.write(img)

get_img(img)

Great. Now we can see which image it's trying to explain. As you can see it's not always spot on and not very useful. Maybes try some images of your own. Anyway... Here's the whole thing cleaned up and in functions. Have fun!

import requests

def get_img(url):
    img = requests.get(url).content
    with open('image.jpg', 'wb') as f:
        f.write(img)

def deepsplash():
    img = requests.get('https://source.unsplash.com/random/1200x800',
              allow_redirects=False).headers['Location']

    response = requests.post(
        "https://api.deepai.org/api/neuraltalk",
        data = {'image': img},
        headers = {'api-key': 'XXXX'}
    )
    return response.json()['output'], img

def main():
    response, img = deepsplash()
    response = response.capitalize().strip()
    print(response)
    get_img(img)

if __name__ == '__main__':
    main()

A man sitting on a rock with a snowboard

A man sitting on a rock with a snowboard

Thanks for reading. x

Resources