Create, Innovate, and Make Art with Machine Learning

栏目: IT技术 · 发布时间: 4年前

内容简介:DeepAI also has a Celebrity Recognition API that recognizes famous people from images.This JavaScript code classifies (and misclassifies)

Create, Innovate, and Make Art with Machine Learning

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.

Create, Innovate, and Make Art with Machine Learning

What is DeepAI?

DeepAI is a platform that aggregates guides such as Separate sampling: its effects and one potential solution , as well as research, data science, a glossary , a newsletter, job listings , 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 and take note of your API key which can be found on your dashboard .

Create, Innovate, and Make Art with Machine Learning

Then install the DeepAI npm module. At the top of a .js file (mine is called deepai.js) add the following lines:

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 ( complete book in .txt form found on GitHub here ). I saved it to an assets folder locally. You can save either one, but this post uses the unfinished paragraph .

(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

"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..."

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

Create, Innovate, and Make Art with Machine Learning

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

Create, Innovate, and Make Art with Machine Learning

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

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

DeepAI also has a Celebrity Recognition API that recognizes famous people from images. Create, Innovate, and Make Art with Machine Learning

This JavaScript code classifies (and misclassifies) this above image of Shaq and Kobe via URL. 

(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.

Create, Innovate, and Make Art with Machine Learning

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

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 .

Create, Innovate, and Make Art with Machine Learning

Given this JavaScript code and that image, DeepAI returns this URL to the colorized output image (shown below.)

(async function () {
resp = await deepai.callStandardApi("colorizer", {
        image: "https://www.terragalleria.com/images/black-white/us-ca/usca9303-bw.jpeg",
    });
    console.log(resp);
}();

Create, Innovate, and Make Art with Machine Learning

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.

Create, Innovate, and Make Art with Machine Learning

Given these two images above (including the Scream by Edvard Munch on the right), I added them to a folder called assets and then the code below returns this link containing the output image below.

(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:

Create, Innovate, and Make Art with Machine Learning

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. Let me know what you're building in the comments or online.

Authors


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JavaScript快速开发工具箱

JavaScript快速开发工具箱

Robin Nixon / 陈武、姚飞 / 清华大学出版社 / 2011-11 / 59.00元

《JavaScript快速开发工具箱:轻松解决JavaScript日常编程问题的100个插件工具》通透讲解100个现成的JavaScript插件,引导您使用这些利器得心应手地创建动态Web内容。《JavaScript快速开发工具箱:轻松解决JavaScript日常编程问题的100个插件工具》开篇讲解JavaScript、CSS和DOM,此后每章都列举一个完整示例,指导您将特定效果快速应用于网页。使......一起来看看 《JavaScript快速开发工具箱》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具