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


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

查看所有标签

猜你喜欢:

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

机械设计实践

机械设计实践

村洋太郎(日) / 王启义/等 / 机械工业出版社 / 1998-08 / 36.00

本书记述了各种设计过程的思考方法和具体作法以及必要的知识和具 体数据。介绍了设计中要决定的内容和相应的制约条件。如功能、机构、 构造、形状、力和强度、尺寸加工工艺、工具、材料、机械要素等。最后 介绍了具体设计实例。本书的目的在于即使不看其他的书和参考书就能设 计出所需要的具体机械。 本书供从事机械设计的有关技术人员及大专院校相关专业的师生使 用。一起来看看 《机械设计实践》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具