This tutorial is designed to guide you through building an AI-powered web application and showcase the potential of AI in everyday web development. Artificial Intelligence (AI) is revolutionizing modern web technology, making it more innovative and responsive. By incorporating AI, developers can enhance user experiences through features like real-time data analysis, personalized content recommendations, and advanced image recognition.
\ Next.js is a robust Reach framework that enables developers to quickly build server-side rendered and static web applications. It offers excellent performance, scalability, and a seamless developer experience. TensorFlow.js, on the other hand, TensorFlow.js is a Javascript library that allows you to train and run machine learning models directly in the browser.
\ By combining Next.js and TensorFlow.js, you can create sophisticated web applications that leverage the power of AI without needing extensive backend infrastructure.
\ By the end of this tutorial, you will have built a fully functional AI-powered web application capable of performing image recognition tasks. You'll gain hands-on experience with Next.js and TensorFlow.js, learning how to integrate machine learning models into a modern web framework.
\ This tutorial will equip you with the skills to start incorporating AI features into your projects, opening up new possibilities for innovation and user engagement.
Setting Up the Environment Prerequisites:\
\
It's common to place your projects in a Projects directory within your home directory.
Open the VS Code at the top bar, select View scroll down to the terminal, and create a folder called Projects this into the terminal:
mkdir -p ~/Projects
\ and move to the project's folder by running:
cd Projects\ Your projects would then be located in paths like:/home/your-username/Projects/my_project (Linux)/Users/your-username/Projects/my_project (Mac)
\ Windows Using Linux subsystem WSL
\
If you haven't installed Next.js yet, you can create a new Next.js project using the following command:
Installing Next.js: npx create-next-app ai-web-app\ Test that the app is working as for now:
npm run dev\ You will see the Next.js app on the page http://localhost:3000. If it works, we can proceed.
Installing TensorFlow.js: npm install @tensorflow/tfjs @tensorflow-models/mobilenet\
Project Structure ai-web-app/ ├── node_modules/ ├── public/ ├── src/ │ ├── pages/ │ │ ├── api/ │ │ │ └── hello.js │ │ ├── _app.js │ │ ├── _document.js │ │ ├── index.js │ ├── styles/ │ │ ├── globals.css │ │ ├── Home.module.css │ ├── utils/ │ │ └── imageProcessing.js ├── .gitignore ├── package.json ├── README.md\ So, we have to add the following file:
\ Erase all the code and add the following ones:
Part 1: Imports and State Initializationmodel: State to store the loaded TensorFlow model.
predictions: State to store the predictions made by the model.
import Head from "next/head"; import styles from "../styles/Home.module.css"; import { useState } from "react"; import { loadModel, loadImage } from "@/utils/imageProcessing";
export default function Home() { const [model, setModel] = useState(null); const [predictions, setPredictions] = useState([]);
\
\
\
Sets the predictions state with the results.
const handleAnalyzeClick = async () => { const fileInput = document.getElementById("image-upload"); const imageFile = fileInput.files[0];
\
Retrieving the Uploaded Image File:
const fileInput = document.getElementById("image-upload"); const imageFile = fileInput.files[0];
\
\
Checking if an Image File is Uploaded:
if (!imageFile) { alert("Please upload an image file."); return; }
\
Loading the Image and Classifying It:
try { const image = await loadImage(imageFile); const predictions = await model.classify(image); setPredictions(predictions); } catch (error) { console.error('Error analyzing the image:', error); }
try { ... } catch (error) { ... }: The try-catch The block handles any errors during the image loading and classification process.
Loading the Image:
const image = await loadImage(imageFile);
\
\
\ setPredictions(predictions): This updates the predictions State with the new classification results. This triggers a re-render of the component, displaying the predictions to the user.
Handling Errors:
catch (error) { console.error('Error analyzing the image:', error); }
\ catch (error) { … }: This block catches any errors that occur during the try block.console.error('Error analyzing the image:', error);: If an error occurs, it logs the error message to the console for debugging purposes.
Part 3: Loading the TensorFlow ModelUses a useState Hook to load the model when the component mounts.
Sets the loaded model into the state.
useState(() => { (async () => { try { const loadedModel = await loadModel(); setModel(loadedModel); } catch (error) { console.error('Error loading the model:', error); } })(); }, []);
\
To begin building our AI-powered web application with Next.js and TensorFlow.js, we'll set up a basic layout using Next.js components. This initial structure will be the foundation for our application's user interface.
Part 4: Rendering the UI 5. Rendering:\ <> ... >: This React Fragment allows multiple elements to be grouped without adding extra nodes to the DOM.
2. Container Div\
\
4. Main Content\
Using Next.js and TensorFlow.js to show some AI model.
\
...
: This paragraph provides a brief description and is styled using the styles.description Class.\
\
\ Edit the Styles for the index.js file in Home.module.css erase all the code, and add the following one:
.container { min-height: 100vh; padding: 0 0.5rem; display: flex; flex-direction: column; justify-content: center; align-items: center; } .main { padding: 5rem 0; flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; } .title { margin: 0; line-height: 1.15; font-size: 4rem; text-align: center; } .description { margin: 4rem 0; line-height: 1.5; font-size: 1.5rem; text-align: center; } #ouput-area { margin-top: 2rem; } .li { margin-top: 10px; font-size: 20px; } .button { margin-top: 1rem; padding: 0.5rem 1rem; font-size: 1rem; cursor:pointer; background-color: #0070f3; color: white; border: none; border-radius: 5px; } .button:hover { background-color: #005bb5; }\ Once you have done the previous steps, check to see something like this: \n
Now, let's work with the brain of the app. imageProcessing.js File:
Part 1: Loading the Model Function: loadModel import * as tf from "@tensorflow/tfjs"; import * as mobilenet from "@tensorflow-models/mobilenet"; export async function loadModel() { try { const model = await mobilenet.load(); return model; } catch (error) { console.error("Error loading the model:", error); throw error; } }\ This Function loads the MobileNet model using TensorFlow.js. Here's a step-by-step explanation:
\ This function preprocesses an image in the format required by MobileNet. Here's a step-by-step explanation:
\ This Function loads an image file and returns an HTML Image element. Here's a step-by-step explanation:
\ Now, you can test this final project by uploading images to the project's page and seeing the final results; if you have any problems, please check the provided link to clone the project from Github:
ConclusionThis tutorial taught you how to build an AI-powered web application using Next.js and TensorFlow.js. We covered:
\ By combining Next.js and TensorFlow.js, you can create sophisticated web applications that leverage the power of AI, enhancing user experiences with features like image recognition.
Next StepsTo further improve your application, consider exploring these additional features:
Ivan Duarte is a backend developer with experience working freelance. He is passionate about web development and artificial intelligence and enjoys sharing their knowledge through tutorials and articles. Follow me on X, Github, and LinkedIn for more insights and updates.
All Rights Reserved. Copyright , Central Coast Communications, Inc.