Skip to content

FMXExpress/AI-Image-Masker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Image Masker with Positive/Negative Prompting – DelphiFMX Python GUI

This project provides a graphical user interface (GUI) for creating AI-powered image masks based on positive and negative prompts using the Replicate API.
Built with DelphiFMX for Python, this application enables users to generate masks and inverted masks for uploaded images. It runs background processes asynchronously, ensuring the UI remains smooth and responsive during processing.


Features

  • AI Mask Generation: Generate masks for selected images using advanced AI models.
  • Positive and Negative Prompts: Control the areas to include or exclude in masks using descriptive text prompts.
  • Dual Outputs: View both the mask and inverted mask generated from the original image.
  • Real-Time Status Updates: Live updates on image processing progress.
  • Background Processing: Runs AI calls asynchronously, so the interface never freezes.

Screenshot

Below is an example screenshot of the application interface:

App Screenshot


Prerequisites


Installation

  1. Clone this repository:

    git clone https://github.com/your-username/ai-image-masker.git
    cd ai-image-masker
  2. Install required dependencies:

    pip install delphifmx replicate
  3. Set your Replicate API token:

    export REPLICATE_API_TOKEN=your_replicate_api_token
  4. Run the application:

    python main.py

Usage

  1. Upload an Image

    • Click the "Select Image" button to choose an image from your computer.
    • Supported formats: .png, .jpg, .jpeg
  2. Enter Prompts

    • Positive Prompt: Specifies the elements you want to include in the mask.
    • Negative Prompt: Specifies the elements you want to exclude from the mask.
    • Prompts should be comma-separated (e.g., dog, person, tree).
  3. Processing

    • The app automatically sends the image and prompts to the Grounded SAM model hosted on Replicate.
    • Progress updates appear in the status bar at the bottom of the app.
  4. View Results

    • Once processing is complete, both the mask and inverted mask will be saved locally and displayed in the GUI.

Project Structure

ai-image-masker/
│
├── main.py          # Main Python script containing GUI and API logic
├── Air.style        # Custom DelphiFMX style file for the GUI
├── mask.png         # Generated mask image (output)
├── inverted_mask.png # Generated inverted mask image (output)
└── screenshot.png   # Example screenshot of the app

Code Overview

Import Section

import os
import replicate
import urllib.request
from delphifmx import *
  • Replicate API: Handles AI model execution for mask generation.
  • DelphiFMX: Powers the cross-platform GUI.
  • urllib.request: Downloads the mask and inverted mask images.

How It Works

  1. Image Selection

    • User selects an image via the GUI file dialog.
  2. Prompt Input

    • Users provide positive and/or negative prompts to guide the AI.
  3. Replicate Prediction

    • The app sends the image and prompts to the Replicate model using this code:

      self.prediction = replicate.predictions.create(
          version="ee871c19efb1941f55f66a3d7d960428c8a5afcb77449547fe8e5a3ab9ebc21c",
          input={
              "image": open(self.original_image_path, "rb"),
              "mask_prompt": positive_prompt,
              "negative_mask_prompt": negative_prompt,
              "adjustment_factor": -15
          }
      )
    • Processing runs in the background to keep the UI responsive.

  4. Results Display

    • Final images (mask.png and inverted_mask.png) are downloaded and displayed in the application.

Example Workflow

  1. Select an image of a person standing with a dog.
  2. Enter:
    • Positive Prompt: person
    • Negative Prompt: dog
  3. Click Process.
  4. The app generates:
    • Mask: Only the person is highlighted.
    • Inverted Mask: Everything except the person is highlighted.

Troubleshooting

  • Error: "Invalid API Key"

    • Ensure your Replicate API token is correctly set:
      echo $REPLICATE_API_TOKEN
  • Empty Mask Output

    • Refine your positive and negative prompts for better results.
  • Slow Processing

    • Processing speed depends on network speed and Replicate server load.

Contributing

Contributions are welcome!
Submit a pull request or open an issue for bug reports, feature requests, or enhancements.


License

This project is licensed under the MIT License.


Links

About

AI image masker with Grounded SAM using a Python GUI desktop app.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages