Art Gallery with Neural Style Transfer in NuxtJS

Moronfolu Olufunke

Ever wished to paint like Michelangelo or Leonardo Da Vinci? - Now, it is possible using the Neural Style Transfer. In this post, we will be looking at creating a simple art gallery using the Neural style transfer.

The Neural Style Transfer uses neural networks to transfer and blend the styles from one image (source image) into another image (target image) while still maintaining the content of both images. Primarily, the Neural Style Transfer repaints and transforms the target image in the style of the source image using an intricate network.

Sandbox

We completed this project in a CodeSandbox. Fork and run it to quickly get started.

Github

Check out the complete source code here.

Prerequisite

Understanding this article requires the following:

  • Installation of Node.js.
  • Basic knowledge of JavaScript.
  • Familiarity with Nuxt.js

Use the npx create-nuxt-app <project-name> to create a new Nuxt project. The process of scaffolding the project should bring a list of options which should look like this:

After successful project creation, we start our application by running the following command.

1cd <project name>
2 npm run dev

Nuxt will, in turn, start a hot-reloading development environment which is accessible by default at http://localhost:3000

Cloudinary Setup

We can achieve the Neural Style Transfer in our Nuxt application using Cloudinary, a cloud-based service that provides solutions for asset management (images, videos). To use Cloudinary, we will need to create an account and also install the Cloudinary Nuxt SDK, like so: npm install @nuxtjs/cloudinary

Cloudinary uses the Neural Artwork Style Transfer add-on **to perform the Neural Style Transfer. Click on the Add-ons** tab to access the add-on page.

The Add-ons page has several add-ons from which we can pick our desired add-on, in this case, the Neural Artwork Style Transfer add-on:

Configuring nuxt.config for Cloudinary

We need to configure our Nuxt application in the nuxt.config file before we can use Cloudinary. To do this, we will need to do the following:

  1. Create a .env file in the root of our project and add our Cloudinary Cloud name, API Key, and the API Secret. We can find these by navigating to the Cloudinary dashboard.
  2. Register these properties in our .env file like so:
1cloudName='***'
2 apiKey='***'
3 apiSecret='***'
  1. Register Cloudinary component for global use in the nuxt.config file by adding '@nuxtjs/cloudinary' in the modules section.
  2. Add a cloudinary section in the nuxt.config like so:
1cloudinary: {
2 cloudName: process.env.cloudName,
3 apiKey: process.env.apiKey,
4 apiSecret: process.env.apiSecret,
5 useComponent: true,
6 },

NB: Endeavor to replace all occurrences of “****" with the correct values.

Voila! We are ready to start building and using our Cloudinary components in our project.

Uploading Images to Cloudinary

With our Nuxt app scaffolding completed, we’ll start by uploading sixteen images from Unsplash to Cloudinary.

Cloudinary allows us to upload images via the dashboard by clicking the Media Library tab.

After uploading the images, we will need to copy the public Id as this is required to pick each uploaded image from Cloudinary.

Setting Up the Image Store

We will need to call our images from the Nuxt application by creating a utils folder at the root of our application. We will create an images.json file within this folder to hold the individual image information for each image occurrence in our application.

A sample snippet of our images.json file should look like this:

1[
2 {
3 "id": 1,
4 "publicId": "rita-burza-unsplash",
5 "overlay": "micheile-visual-stories-unsplash"
6 },
7 ]

Building the Project

Setting up and configuring Cloudinary for usage in our application means we can now use Cloudinary’s cld-image and cld-transformation component in our application like we would use the native <img> tag in an HTML document.

As a result, we will create a Gallery.vue file in our components folder. This file will contain all the code needed for the art gallery, which will result from neural style transfer for this tutorial.

To show the result in our browser view, we will need to import the Gallery.vue component into our pages/index.vue like so:

1<template>
2 <div class="ph3">
3 <Gallery />
4 </div>
5 </template>

Cloudinary provides for style transfer using different transformation properties attached to the component. The required setup for neural style transfer in Cloudinary is:

  • The target image: The image we would use as the base for the styles to get transferred to
  • The source image: The image we intend to transfer its style to the target image.
  • The input image: The repainted generated image which is a blend of both the target and the source image

The public-id will represent the target image, while our overlay will be the source image. We will also need to specify the overlay effect which is denoted as style_transfer in the Gallery.vue component.

1<template>
2 <div>
3 <div v-for="image in images" :key="image.id" class="fl w-50 w-25-l pa1 overflow-hidden">
4 <cld-image :public-id="image.publicId" class="grow" sign-url="true">
5 <cld-transformation height="750" width="750" crop="crop" />
6 <cld-transformation :overlay="image.overlay" />
7 <cld-transformation effect="style_transfer" flags="layer_apply" />
8 </cld-image>
9 </div>
10 </div>
11 </template>
12 <script>
13 import images from "~/utils/images.json";
14 export default {
15 data() {
16 return {
17 images
18 }
19 }
20 }
21 </script>

The snippet above renders the images from the images.json by looping through each dataset using v-for. The public-id is then set to the value of the publicId in our json file. Consequently, the overlay props in the cld-transformation are also set to the value of the overlay in our json file.

Cloudinary transformations are pretty powerful and may prove costly if others access unaccounted URLs that apply the neural art style or other forms of image transformations. Hence, our URL is needed to be signed by Cloudinary’s authenticated API.

As seen in the code snippet above, we can sign our Cloudinary URL by setting the sign_url props to true.

At this point, our result should look like this:

From the screenshot above, we can see the images have transformed. The styles from the source image get imprinted on the target image. We have a new painting that combines two paintings whose content is still intact.

Conclusion

This post explains how to build an art gallery in Nuxt with the Neural style transfer using Cloudinary transformations.

References

Moronfolu Olufunke

Frontend Engineer | Tecnical Writer | Story Teller

Olufunke is a Frontend Engineer and technical writer who enjoys building stuff and writing about them.