Make Art with Machine Learning
Tags: machinelearning, deepai, javascript, npm
Published: 2020-03-31 18:34:42

---
title: Make Art with Machine Learning
published: true
description: Use DeepAI to generate text, recognize celebrities in images, perform neural style transfer, colorize black-and-white images, and more.
tags: ml, machine-learning, deepai, js, npm
---
![header img](https://twilio-cms-prod.s3.amazonaws.com/images/artblog_Ovbh9Q0.width-1616.png)

One thing I love about programming is the chance to get creative. There are many cool APIs that help developers make art. This post will go over how to generate text from another text or image, recognize celebrities' faces, colorize photos and videos, and perform neural style transfer using the machine learning library DeepAI.

![example image with neural img transfer](https://twilio-cms-prod.s3.amazonaws.com/images/htflf7kjj5v91WACiw9MhLZDbR4dKHCreFZ6QSnVlf2CT.width-1600.png)

### What is DeepAI?
[DeepAI](https://deepai.org/) is a platform that aggregates guides such as [Separate sampling: its effects and one potential solution](https://deepai.org/guide/separate-sampling-its-effects-and-one-conservative-solution-3635170014), as well as research, data science, a [glossary](https://deepai.org/definitions), a newsletter, [job listings](https://deepai.org/jobs), and more to increase AI accessibility to both consumers and developers. This post will look at some of its machine learning models and APIs that can help you get creative juices flowing.

They offer language support for JavaScript, Python, Ruby, and C#, and you can also hit the APIs with a cURL request.

### Prerequisites

To follow along with this post, [make an account on DeepAI.org](https://deepai.org/) and take note of your API key which can be found on your [dashboard](https://deepai.org/dashboard/profile).

![Profile](https://twilio-cms-prod.s3.amazonaws.com/images/deepai.width-500.png)

Then install the [DeepAI npm module](https://www.npmjs.com/package/deepai). At the top of a <em>.js file</em> (mine is called <em>deepai.js</em>) add the following lines:

```js
const fs = require('fs');
const deepai = require('deepai');
deepai.setApiKey('REPLACE-WITH-YOUR-DEEPAI-API-KEY');
```
Now it's time to start building art with machine learning.

### Generating fiction with  DeepAI

I love reading and writing, especially fiction. DeepAI's text-generator API lets you generate text according to an input string, also known as seed text in natural language processing (NLP.)

Below is an example of JavaScript code using DeepAI's text-generator API to complete an [unfinished paragraph from Harry Potter and the Sorcerer's Stone](https://github.com/elizabethsiegle/deepai_demo_art/blob/master/assets/hp11st2ps.txt) ([complete book in .txt form found on GitHub here](https://github.com/formcept/whiteboard/blob/master/nbviewer/notebooks/data/harrypotter/Book%201%20-%20The%20Philosopher's%20Stone.txt)). I saved it to an `assets` folder locally. You can save either one, but this post uses the [unfinished paragraph](https://github.com/elizabethsiegle/deepai_demo_art/blob/master/assets/hp11st2ps.txt).

```js


(async function () {
    resp = await deepai.callStandardApi("text-generator", {
        text: fs.createReadStream("assets/hp11st2ps.txt"),
    });
    fs.writeFile('assets/gentext.txt', JSON.stringify(resp), (err) => {
        //throw error
        if (err) throw err;
        //file saved
        console.log('image saved!');
    });
    console.log(resp);
})();


```

Given the first one-and-a-half sentences of Harry Potter, the returned text is

<em>"Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much. They were the last people you'd expect to be involved in  ̈the \"first-ever\" team meeting. They said it was a great opportunity for you to spend your entire time in hockey, and they even took the time to meet with this guy who had already done a lot to help me improve my game as a coach..." </em>

In addition to a locally-hosted file, you could generate text from a URL, text string, and other forms as well.

### Generating text from an image
Something else you can do with DeepAI is generate text from an image with their Neural Talk 2 API. The code below takes in this [image of Detective Pikachu dancing](https://www.syfy.com/sites/syfy/files/2019/05/untitled.png)

![detective pikachu dancing](https://twilio-cms-prod.s3.amazonaws.com/images/TFXwtmsTF5_Qm_6CXA9YsVWTmUQFNbc5ESoAizMH_4uMr.width-1000.png)

and outputs "a teddy bear sitting on top of a table."

![detective pikachu generated text from image](https://twilio-cms-prod.s3.amazonaws.com/images/4umNLV8OUX1J38eEyc2fgobkLyZhs8_A6JnS1SuiUHwDT.width-1000.png)

Why yes, that is an extremely cute prediction summary of Pikachu, but it's not quite the most accurate! Here's the code:

```js
(async function () {
resp = await deepai.callStandardApi("neuraltalk", {
        image: "https://www.syfy.com/sites/syfy/files/2019/05/untitled.png",
    });
    console.log(resp);
})();
```

### Recognizing Celebrities in an image
DeepAI also has a Celebrity Recognition API that recognizes famous people from images.

![Kobe and Shaq](https://www.thewrap.com/wp-content/uploads/2020/01/ShaqKobeBryant.jpg)

This JavaScript code classifies (and misclassifies) [this above image of Shaq and Kobe via URL](https://www.thewrap.com/wp-content/uploads/2020/01/ShaqKobeBryant.jpg). 
```js
(async function () {
resp = await deepai.callStandardApi("celebrity-recognition", {
        image: "https://pbs.twimg.com/media/BhxWutnCEAAtEQ6.jpg",
    });
    console.log(JSON.stringify(resp));
})();
```
It is fairly confident one of them is Kobe (and it classified another image of Kobe as well), but even more confident that Shaq is Joe Fraiser.
![Kobe and Shaq output](https://twilio-cms-prod.s3.amazonaws.com/images/Acc7w4uTR7YBZm4r3F30lZuTVXXR0OeJ0Gh73sgek_pab.width-1000.png)

Alternatively you could instead pass in images hosted locally, from a website, and more.

### Colorizing Images

Perhaps a more useful API DeepAI offers is image colorization. This allows you to add color to old or historic pictures or video and perform photo restoration as well, like on [this black-and-white image of San Francisco Chinatown](https://www.terragalleria.com/images/black-white/us-ca/usca9303-bw.jpeg).
![Chinatown black and white image](https://www.terragalleria.com/images/black-white/us-ca/usca9303-bw.jpeg)

Given this JavaScript code and that image, DeepAI returns [this URL to the colorized output image](https://api.deepai.org/job-view-file/ee2cac74-4ff0-47f2-98cb-b48c38c8f376/outputs/output.jpg) (shown below.)

```js
(async function () {
resp = await deepai.callStandardApi("colorizer", {
        image: "https://www.terragalleria.com/images/black-white/us-ca/usca9303-bw.jpeg",
    });
    console.log(resp);
}();
```
![colorized output image no longer b and w](https://api.deepai.org/job-view-file/ee2cac74-4ff0-47f2-98cb-b48c38c8f376/outputs/output.jpg)

### Performing Neural Style Transfer
Last but not least, the Fast Style Transfer API lets you generate art using deep learning. It quickly performs neural style transfer which is an optimization technique that takes in two images: one content image and one style image (ie. a famous painting). The transfer combines the two pictures together and returns an output image with the content of the content image and the style of the style image.
![2 side by side images, 1 is the Scream](https://twilio-cms-prod.s3.amazonaws.com/images/Screen_Shot_2020-03-30_at_10.53.02_AM.width-1000.png)
Given these two images above (including <em>the Scream</em> by Edvard Munch on the right), I added them to a folder called assets and then the code below returns [this link](https://api.deepai.org/job-view-file/816dbef4-ef83-424a-9fe1-6eede909e4c8/outputs/output.png) containing the output image below.
```js
(async function () {
 resp = await deepai.callStandardApi("fast-style-transfer", {
        content: fs.createReadStream("assets/avatar.png"),
        style: "https://cdn.kastatic.org/ka-perseus-images/6a6151155fbde50cec7b9616661c1448d3374fa7.jpg",
    });
    console.log(resp);
})();
```

That code outputs the first image in the style of the second:

![neural style transfer image](https://api.deepai.org/job-view-file/816dbef4-ef83-424a-9fe1-6eede909e4c8/outputs/output.png)

Now that's neural style transfer!

### Go forth and make art with code

If you're stuck at home, now is the perfect time to experiment with machine learning libraries like DeepAI and to creatively make art, whether generating text from other text or image, recognizing celebrities' faces, colorizing photos or videos, or performing neural style transfer. Again, DeepAI lets you do all these tasks and more with JavaScript, Python, Ruby, C#, or a cURL request.