StegNet / README.md
Ankush
Fix gradio/huggingface_hub version conflict
5bf3a64

A newer version of the Gradio SDK is available: 6.11.0

Upgrade
metadata
title: StegNet
emoji: πŸ”’
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.44.0
python_version: '3.10'
app_file: app.py
pinned: false

StegNet β€” AI-Based Secure Image Steganography Platform

Context document for LLMs and AI agents working on this codebase. Read this fully before making any changes.


1. What This Project Does

StegNet is a Python-based web application that lets users:

  1. Hide a secret image inside a cover image using a deep learning CNN model (steganography)
  2. Reveal the hidden image from a steg image using a second CNN model
  3. Encrypt / Decrypt the steg image using AES or Blowfish (CBC mode, SHA-256 key hashing)
  4. Upscale any image 4Γ— using ESRGAN (Enhanced Super Resolution GAN)
  5. Generate images from text prompts via the HuggingFace Stable Diffusion 2.1 API

The web interface is built with Gradio and deployed on HuggingFace Spaces.


2. Repository Structure

StegNet/
β”œβ”€β”€ app.py                          # ENTRY POINT β€” Gradio web UI (6 tabs)
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ README.md                       # This file
└── app/
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ main_CLI_v1.py              # Legacy CLI script (not used in web)
    β”œβ”€β”€ models/
    β”‚   β”œβ”€β”€ DEEP_STEGO/
    β”‚   β”‚   β”œβ”€β”€ hide_image.py       # hide_image(cover_path, secret_path) β†’ steg_path
    β”‚   β”‚   β”œβ”€β”€ reveal_image.py     # reveal_image(steg_path) β†’ secret_path
    β”‚   β”‚   β”œβ”€β”€ train.py            # Model training script (not used at runtime)
    β”‚   β”‚   β”œβ”€β”€ models/
    β”‚   β”‚   β”‚   β”œβ”€β”€ hide.h5         # Trained hide network weights (TensorFlow/Keras)
    β”‚   β”‚   β”‚   └── reveal.h5       # Trained reveal network weights
    β”‚   β”‚   β”œβ”€β”€ checkpoints/
    β”‚   β”‚   β”‚   └── steg_model-06-0.03.hdf5
    β”‚   β”‚   └── Utils/
    β”‚   β”‚       β”œβ”€β”€ preprocessing.py        # normalize_batch / denormalize_batch
    β”‚   β”‚       β”œβ”€β”€ customLossWeight.py     # custom_loss_1, custom_loss_2 (MSE)
    β”‚   β”‚       β”œβ”€β”€ enhance.py
    β”‚   β”‚       β”œβ”€β”€ eval.py
    β”‚   β”‚       └── progressbarCL_animation.py
    β”‚   β”œβ”€β”€ ESRGAN/
    β”‚   β”‚   β”œβ”€β”€ upscale_image.py    # upscale_image(image_path) β†’ output_path
    β”‚   β”‚   β”œβ”€β”€ RRDBNet_arch.py     # RRDB network architecture (PyTorch)
    β”‚   β”‚   β”œβ”€β”€ model.py
    β”‚   β”‚   β”œβ”€β”€ net_intrep.py
    β”‚   β”‚   β”œβ”€β”€ test.py
    β”‚   β”‚   └── models/
    β”‚   β”‚       └── RRDB_ESRGAN_x4.pth   # Pretrained ESRGAN weights (PyTorch)
    β”‚   β”œβ”€β”€ encryption/
    β”‚   β”‚   β”œβ”€β”€ aes.py              # encrypt(path, key) / decrypt(path, key)
    β”‚   β”‚   └── blowfish.py         # encrypt(path, key) / decrypt(path, key)
    β”‚   β”œβ”€β”€ StableDiffusionAPI/
    β”‚   β”‚   └── StableDiffusionV2.py  # generate(text_prompt) β†’ PIL Image
    β”‚   └── StackGAN/               # Empty placeholder
    └── ui/
        └── main.py                 # Legacy PyQt5 desktop GUI (NOT used in web)

3. Key Function Signatures

Steganography

# app/models/DEEP_STEGO/hide_image.py
def hide_image(cover_image_filepath: str, secret_image_filepath: str) -> str:
    """Hides secret inside cover. Returns path to steg image (tempfile .png)."""

# app/models/DEEP_STEGO/reveal_image.py
def reveal_image(stego_image_filepath: str) -> str:
    """Extracts hidden image from steg. Returns path to revealed image (tempfile .png)."""

Encryption

# app/models/encryption/aes.py and blowfish.py
def encrypt(image_path: str, key: str) -> None:
    """Encrypts image file. Saves to image_path + '.enc'."""

def decrypt(encrypted_image_path: str, key: str) -> tuple[int, str | None]:
    """Decrypts. Returns (0, output_path) on success, (-1, None) on wrong key."""

Super Resolution

# app/models/ESRGAN/upscale_image.py
def upscale_image(image_filepath: str) -> str:
    """4Γ— upscale using ESRGAN. Returns output path (tempfile .png)."""

Image Generation

# app/models/StableDiffusionAPI/StableDiffusionV2.py
def generate(text_prompt: str) -> PIL.Image:
    """Calls HuggingFace Stable Diffusion 2.1 API. Requires HF_API_KEY env var."""

4. Model Architecture

Deep Steganography (TensorFlow/Keras)

  • Input: 224Γ—224 RGB images (cover + secret), normalized with ImageNet stats
  • Prepare Network: 3 parallel Conv2D branches (3Γ—3, 4Γ—4, 5Γ—5 kernels, 50 filters each) β†’ concatenated β†’ final branch
  • Hide Network: Concatenates cover + prepared secret features β†’ same multi-scale conv structure β†’ outputs 3-channel steg image
  • Reveal Network: Takes steg image (with Gaussian noise during training) β†’ recovers secret
  • Loss: Custom MSE β€” hide weight 1.0, reveal weight 0.75
  • Preprocessing: channel-wise z-score with ImageNet mean/std

ESRGAN (PyTorch)

  • RRDBNet(3, 3, 64, 23, gc=32) β€” 23 RRDB blocks, 64 features, 32 growth channels
  • 4Γ— spatial upscaling
  • Runs on CUDA if available, falls back to CPU automatically
  • Model loaded lazily (once, on first call)

5. Environment & Dependencies

tensorflow          # hide/reveal CNN models
torch               # ESRGAN
torchvision
opencv-python-headless   # image I/O (headless = no GUI deps)
Pillow              # image processing
pycryptodome        # AES + Blowfish encryption
gradio              # web UI
numpy
imageio             # saving numpy arrays as images
requests            # Stable Diffusion API calls

Python version: 3.10+ recommended
GPU: Optional. ESRGAN auto-detects CUDA; steganography runs on CPU fine.


6. Environment Variables

Variable Required Purpose
HF_API_KEY Optional HuggingFace API token for Stable Diffusion image generation tab

Set as a HuggingFace Space secret or local env variable.


7. Deployment

Live URL: https://huggingface.co/spaces/savetrees/StegNet
GitHub: https://github.com/savetree-1/stego-net
Platform: HuggingFace Spaces (Gradio SDK, CPU Basic, free tier)
Git LFS: All binary files (*.h5, *.pth, *.hdf5, *.npy, *.png, *.jpg) are tracked with Git LFS.

Run locally

git clone https://github.com/savetree-1/stego-net.git
cd stego-net
pip install -r requirements.txt
python app.py

Push updates

git add .
git commit -m "your message"
# HuggingFace
git push https://savetrees:HF_TOKEN@huggingface.co/spaces/savetrees/StegNet main
# GitHub
git push https://savetree-1:GH_TOKEN@github.com/savetree-1/stego-net.git main

8. Important Implementation Notes

  • Model paths are resolved relative to each file using os.path.dirname(os.path.abspath(__file__)) β€” never hardcoded absolute paths
  • Models are lazy-loaded (loaded once on first inference call, cached in module-level _model variable)
  • Output files use tempfile.mkstemp(suffix='.png') β€” caller receives the path
  • Encryption output saves to original_path + '.enc'; decryption saves to original_path.dec.png
  • The app/ui/main.py PyQt5 desktop GUI is not used in the web deployment β€” ignore it
  • The app/main_CLI_v1.py CLI script is not used in the web deployment β€” ignore it
  • imageio.imsave is used instead of PIL/cv2 for saving numpy uint8 arrays from model output

9. Known Issues & Limitations

  • ESRGAN is slow on CPU (HuggingFace free tier) β€” expect 30–60s for upscaling
  • Steganography models resize all input to 224Γ—224 β€” output is always 224Γ—224
  • HuggingFace free tier sleeps after inactivity β€” first request may be slow
  • Image generation requires HF_API_KEY env var β€” disabled if not set

10. Original Project

This project was originally called InvisiCipher (by Asirwad). It has been:

  • Rebranded to StegNet
  • Converted from a PyQt5 desktop app to a Gradio web app
  • Fixed hardcoded Windows paths replaced with portable relative paths
  • Added CPU fallback for ESRGAN
  • Added model lazy-loading
  • Removed hardcoded API key file dependency (now uses env var)

Hide secrets, enhance images!

Overview

Welcome to our Steganography and Super Resolution project! This project combines the power of steganography techniques and super-resolution using deep learning models. Our goal is to hide a secret image within a cover image using advanced convolutional neural networks (CNNs) and then enhance the quality of the hidden image using an Enhanced Super Resolution Generative Adversarial Network (ESRGAN). We also provide an option to encrypt the steg image using various chaos encryption algorithms for added security.

Features

✨ Interactive Hiding: Utilize our intuitive hide network powered by CNNs to embed secret images within cover images effortlessly.

πŸ”’ Secure Encryption: Choose from multiple chaos encryption algorithms such as AES, Blowfish to encrypt your steg image and protect your secrets.

🌟 Enhanced Super Resolution: Witness the magic of our ESRGAN model as it enhances the resolution and quality of the hidden image, revealing every detail.

🎨 Easy-to-Use: Our project provides a user-friendly interface and simple scripts to perform hiding, encryption, decryption, and image enhancement with just a few lines of code.

Project Architecture

The project architecture consists of the following components:

  1. Prepare Network: A CNN-based network that prepares the secret image for hiding by extracting essential features and encoding it.

  2. Hide Network: Another CNN-based network that embeds the prepared secret image within the cover image, producing the steg image.

  3. Chaos Encryption: Choose between AES encryption, Blowfish encryption to secure your steg image.

  4. Chaos Decryption: Decrypt the encrypted steg image using the corresponding decryption algorithm to retrieve the steg image.

  5. Reveal Network: A CNN-based network that extracts the secret image from the steg image by decoding the hidden information.

  6. ESRGAN: Our Enhanced Super Resolution Generative Adversarial Network (ESRGAN) model enhances the quality and resolution of the extracted secret image.

Getting Started

To get started with our project, follow these steps:

  1. Clone the Repository: git clone https://github.com/Asirwad/InvisiCipher.git

  2. Install Dependencies: Install the required dependencies by running pip install -r requirements.txt.

  3. Prepare Your Dataset: Organize your cover and secret images dataset and place them in the appropriate directories.

  4. Customize Configuration: Modify the configuration files to set the desired parameters for the models and encryption algorithms.

  5. Train the Models: Run the training script to train the CNN models and ESRGAN: python train.py.

  6. Explore the Scripts: Utilize the provided scripts to hide, encrypt, reveal, and enhance images based on your specific requirements.

Welcome screen

Welcome

Image hide

Image hide

Image reveal

Image reveal

Super resolution

Super resolution

Contributing

We welcome contributions from the open source community. If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.

Acknowledgements

We would like to acknowledge the following resources and libraries used in this project:

Contact

For any questions or inquiries, please contact us at asirwadsali@gmail.com.