GitHub - oyeolamilekan/gemini-ai-tryon: An AI-powered virtual try-on app built with Next.js and Google Gemini. Upload your photo and a clothing item to see how it looks!

4 min read Original article ↗

AI Virtual Try-On Application

This is a web application built with Next.js that allows users to virtually try on clothing items using AI. Users upload a photo of themselves and a photo of a clothing item, and the application leverages the Google Gemini API to generate an image simulating the user wearing the clothing.

Features

  • 👤 User Image Upload: Upload a photo of yourself.
  • 👕 Clothing Image Upload: Upload a photo of a clothing item.
  • 🖼️ Image Previews: See previews of the uploaded images.
  • AI-Powered Try-On: Uses the Google Gemini API (specifically a flash experimental model) to generate the virtual try-on image.
  • Result Display: Shows the generated image result.
  • Loading State: Displays a loading indicator while the AI is processing.
  • 🔄 Reset Functionality: Easily clear all inputs and results.
  • 🎨 Simple UI: Clean interface built with Tailwind CSS.

Technology Stack

Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js (v18.x or later recommended)
  • npm (usually comes with Node.js)
  • A Google Gemini API Key: You can obtain one from Google AI Studio.

Getting Started

Follow these steps to set up and run the application locally:

  1. Clone the Repository (or Download Code):

    # Replace with your actual repository URL if applicable
    git clone https://github.com/oyeolamilekan/gemini-ai-tryon.git
    cd gemini-ai-tryon

    Alternatively, if you have the code locally, navigate to the project's root directory.

  2. Install Dependencies:

  3. Set Up Environment Variables:

    • Create a new file named .env.local in the root of your project directory.
    • Add your Google Gemini API Key to this file:
      # .env.local
      GEMINI_API_KEY=YOUR_GOOGLE_GEMINI_API_KEY
      
    • Replace YOUR_GOOGLE_GEMINI_API_KEY with the actual key you obtained from Google AI Studio.
  4. Run the Development Server:

  5. Open the Application: Open your web browser and navigate to http://localhost:3000.

Usage

  1. The application interface will load.
  2. Click the "Upload Your Photo" input section (or the button within it) and select an image file of a person. A preview will appear.
  3. Click the "Upload Clothing Item" input section and select an image file of a clothing item. A preview will appear.
  4. Click the "Try It On!" button.
  5. A loading indicator will appear while the AI generates the image. This may take several seconds.
  6. If successful, the resulting "try-on" image will be displayed below the form.
  7. If an error occurs (e.g., API issue, invalid input), an error message will be displayed. Check the browser console and backend terminal logs for more details.
  8. Click the "Reset" button at any time to clear the uploaded images, previews, results, and any error messages.

API Endpoint (/api/tryon)

  • Method: POST
  • Request Body: Expects FormData containing:
    • userImage: The File object for the user's photo.
    • clothingImage: The File object for the clothing item's photo.
  • Response Body (Success):
    {
      "image": "data:image/png;base64,...", // Base64 encoded data URL of the generated image
      "description": "Text description from the AI (if any)"
    }
  • Response Body (Error):
    {
      "error": "Error message string",
      "details": "Optional error details string"
    }

License

MIT License

Copyright (c) 2025 Oye Olamilekan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.