| --- |
| 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 |
| ```python |
| # 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 |
| ```python |
| # 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 |
| ```python |
| # app/models/ESRGAN/upscale_image.py |
| def upscale_image(image_filepath: str) -> str: |
| """4Γ upscale using ESRGAN. Returns output path (tempfile .png).""" |
| ``` |
|
|
| ### Image Generation |
| ```python |
| # 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 |
| ```bash |
| git clone https://github.com/savetree-1/stego-net.git |
| cd stego-net |
| pip install -r requirements.txt |
| python app.py |
| ``` |
|
|
| ### Push updates |
| ```bash |
| 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) |
|
|
| <p align="center"> |
| <strong>Hide secrets, enhance images!</strong> |
| </p> |
|
|
| ## 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 |
|
|
| <p align="center"> |
| <img src="app/ui/assets/readme_assets/main_window.png" alt="Welcome" width="1000"> |
| </p> |
|
|
| ## Image hide |
|
|
| <p align="center"> |
| <img src="app/ui/assets/readme_assets/hide.png" alt="Image hide" width="1000"> |
| </p> |
|
|
| ## Image reveal |
|
|
| <p align="center"> |
| <img src="app/ui/assets/readme_assets/reveal.png" alt="Image reveal" width="1000"> |
| </p> |
|
|
| ## Super resolution |
|
|
| <p align="center"> |
| <img src="app/ui/assets/readme_assets/superres.png" alt="Super resolution" width="1000"> |
| </p> |
|
|
| ## 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: |
|
|
| - <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/Tensorflow_logo.svg/1915px-Tensorflow_logo.svg.png" alt="TensorFlow" width="26" align="center"> TensorFlow: [βοΈ](https://www.tensorflow.org/) |
| - <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/10/PyTorch_logo_icon.svg/1200px-PyTorch_logo_icon.svg.png" |
| alt="PyTorch" width="25" align="center"> PyTorch: [βοΈ](https://pytorch.org/) |
| - <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Python_and_Qt.svg/800px-Python_and_Qt.svg.png" |
| alt="PyQt" width="25" align="center"> PyQt: [βοΈ](https://www.qt.io/qt-for-python) |
|
|
| ## Contact |
|
|
| For any questions or inquiries, please contact us at [asirwadsali@gmail.com](mailto:asirwadsali@gmail.com). |
|
|