AIception, paint me like one of your French girls

Here is a nice thought:

What if one of the great painters (Picasso, van Gogh, Monet) was still alive?

Wouldn’t you be really excited to ask him/her to paint you?

 

Thanks to modern technology you can!

Morgan Starry

 

AIception has a public endpoint https://aiception.com/api/v2.1/artistic_image that will help “paint” any image in the “style” of any great painter.

 

Let’s get started!

We wish to implement it in Python but you can use your favorite programming language.

1. Create the following directory structure:

/blog_aiception_artistic
    aiception.py
    picasso_painted_me.py

2. Get aiception.py  <from the github here>.

3. Download and install Anaconda ( a great package, dependency and environment manager)

https://www.continuum.io/downloads

4. Create a virtual environment with Python 3.*

conda create -n blog_aiception_artistic python=3.5

5. Activate your new environment

source activate blog_aiception_artistic

6. Install extra packages like the requests HTTP library

pip install requests

7. Do your first API call

from aiception import *
from pprint import pprint

token = "YOUR_TOKEN_HERE"
image_url = "https://upload.wikimedia.org/wikipedia/commons/0/06/Morgan_Freeman,_2006_(cropped).jpg"

# call AIception API
r = face_age(token, image_url)
pprint(r)

You get the following JSON response from the server:

{'answer': {'age': 60.5},
 'created_at': 'Fri, 17 Feb 2017 19:16:20 GMT',
 'image_url': 'https://upload.wikimedia.org/wikipedia/commons/0/06/Morgan_Freeman,_2006_(cropped).jpg',
 'state': 'done',
 'this_url': 'https://aiception.com/api/v2.1/face_age/2547',
 'type': 'face_age'}

Great! So the age of the man in the picture is 60.5 years old…wait…that’s not what we want… we want to get a painting!

Let’s use the correct endpoint now, artistic_image, and we need an extra parameter for the style of the image.

8. Call the artistic_image endpoint

from aiception import *


token = "YOUR_TOKEN_HERE"
image_url = "https://upload.wikimedia.org/wikipedia/commons/0/06/Morgan_Freeman,_2006_(cropped).jpg"
style_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1280px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg"

# call AIception API (takes around 55 seconds)
r = artistic_image(token, image_url, style_url)
print(r)

It will take a while (around 55 seconds) so please be patient.

You can’t rush art!

We receive the following JSON response that is a bit more complex than the first one:

{  
   'answer':{  
      'urls':[  
         {  
            'iteration':'100',
            'image_url':'https://s3.eu-central-1.amazonaws.com/aiception.com/2017/02/task-113-N3agK7gftHq3GxfByN7tLyT16yy0Tqds71YcxQtlUs5a3AhirqjydCbMd2kF.png'
         },
         {  
            'iteration':'300',
            'image_url':'https://s3.eu-central-1.amazonaws.com/aiception.com/2017/02/task-113-9U9wNXug1TeUqolEgxEau7fyFlwBLH16HBtRadkZ4uGtTSXk0ik27S9meT9L.png'
         },
         {  
            'iteration':'200',
            'image_url':'https://s3.eu-central-1.amazonaws.com/aiception.com/2017/02/task-113-NKLC9n2yI1RcCoMukBkwpCTCf7CFY1SaBxBP9TBFIZfCNaEuCYWuCnMEKekb.png'
         }
      ]
   },
   'image_url':'https://upload.wikimedia.org/wikipedia/commons/0/06/Morgan_Freeman,_2006_(cropped).jpg',
   'created_at':'Fri, 17 Feb 2017 18:56:39 GMT',
   'type':'artistic_image',
   'style_url':'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1280px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg',
   'state':'done',
   'this_url':'https://aiception.com/api/v2.1/artistic_image/113'
}

The JSON contains 3 urls.

Each is the same stylized image at different stages (iterations) in the digital painting process. You cannot control this parameter (yet).

Nice!

9. We want to be able to automatically download the received images.

We picked the one at iteration 300 because it looks better.

import shutil

# select the image at iteration '300'
image_url = [elem['image_url'] for elem in r['answer']['urls'] if elem['iteration'] == '300'][0]

# download the image
file_name = "my_image.png"
response = requests.get(image_url, stream=True)
with open(file_name, 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)

Great!

 

Now it’s your turn!

Use this technology in your next great

  • mobile app
  • facebook app
  •  unity game

People will want their own photos/cats/dogs stylized.

Try the other endpoints:

r = face_age(token, image_url)
r = detect_object(token, image_url)
r = adult_content(token, image_url)
r = faces(token, image_url)

Mix and match the eye to catch…

 

Publish your own crazy idea that uses AIception API and if we love it we will share it on this blog as well!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *