Ankush commited on
Commit Β·
d2a9a00
1
Parent(s): 3a58f3a
Pin Python 3.10
Browse files
README.md
CHANGED
|
@@ -5,17 +5,222 @@ colorFrom: blue
|
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: "4.44.0"
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
| 13 |
-
StegNet : Deep Learning-Based Image Steganography with Encryption and Super Resolution
|
| 14 |
-
</h1>
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
<p align="center">
|
| 21 |
<strong>Hide secrets, enhance images!</strong>
|
|
|
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: "4.44.0"
|
| 8 |
+
python_version: "3.10"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# StegNet β AI-Based Secure Image Steganography Platform
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
> **Context document for LLMs and AI agents working on this codebase.**
|
| 16 |
+
> Read this fully before making any changes.
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## 1. What This Project Does
|
| 21 |
+
|
| 22 |
+
StegNet is a Python-based web application that lets users:
|
| 23 |
+
|
| 24 |
+
1. **Hide** a secret image inside a cover image using a deep learning CNN model (steganography)
|
| 25 |
+
2. **Reveal** the hidden image from a steg image using a second CNN model
|
| 26 |
+
3. **Encrypt / Decrypt** the steg image using AES or Blowfish (CBC mode, SHA-256 key hashing)
|
| 27 |
+
4. **Upscale** any image 4Γ using ESRGAN (Enhanced Super Resolution GAN)
|
| 28 |
+
5. **Generate** images from text prompts via the HuggingFace Stable Diffusion 2.1 API
|
| 29 |
+
|
| 30 |
+
The web interface is built with **Gradio** and deployed on **HuggingFace Spaces**.
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## 2. Repository Structure
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
StegNet/
|
| 38 |
+
βββ app.py # ENTRY POINT β Gradio web UI (6 tabs)
|
| 39 |
+
βββ requirements.txt # Python dependencies
|
| 40 |
+
βββ README.md # This file
|
| 41 |
+
βββ app/
|
| 42 |
+
βββ __init__.py
|
| 43 |
+
βββ main_CLI_v1.py # Legacy CLI script (not used in web)
|
| 44 |
+
βββ models/
|
| 45 |
+
β βββ DEEP_STEGO/
|
| 46 |
+
β β βββ hide_image.py # hide_image(cover_path, secret_path) β steg_path
|
| 47 |
+
β β βββ reveal_image.py # reveal_image(steg_path) β secret_path
|
| 48 |
+
β β βββ train.py # Model training script (not used at runtime)
|
| 49 |
+
β β βββ models/
|
| 50 |
+
β β β βββ hide.h5 # Trained hide network weights (TensorFlow/Keras)
|
| 51 |
+
β β β βββ reveal.h5 # Trained reveal network weights
|
| 52 |
+
β β βββ checkpoints/
|
| 53 |
+
β β β βββ steg_model-06-0.03.hdf5
|
| 54 |
+
β β βββ Utils/
|
| 55 |
+
β β βββ preprocessing.py # normalize_batch / denormalize_batch
|
| 56 |
+
β β βββ customLossWeight.py # custom_loss_1, custom_loss_2 (MSE)
|
| 57 |
+
β β βββ enhance.py
|
| 58 |
+
β β βββ eval.py
|
| 59 |
+
β β βββ progressbarCL_animation.py
|
| 60 |
+
β βββ ESRGAN/
|
| 61 |
+
β β βββ upscale_image.py # upscale_image(image_path) β output_path
|
| 62 |
+
β β βββ RRDBNet_arch.py # RRDB network architecture (PyTorch)
|
| 63 |
+
β β βββ model.py
|
| 64 |
+
β β βββ net_intrep.py
|
| 65 |
+
β β βββ test.py
|
| 66 |
+
β β βββ models/
|
| 67 |
+
β β βββ RRDB_ESRGAN_x4.pth # Pretrained ESRGAN weights (PyTorch)
|
| 68 |
+
β βββ encryption/
|
| 69 |
+
β β βββ aes.py # encrypt(path, key) / decrypt(path, key)
|
| 70 |
+
β β βββ blowfish.py # encrypt(path, key) / decrypt(path, key)
|
| 71 |
+
β βββ StableDiffusionAPI/
|
| 72 |
+
β β βββ StableDiffusionV2.py # generate(text_prompt) β PIL Image
|
| 73 |
+
β βββ StackGAN/ # Empty placeholder
|
| 74 |
+
βββ ui/
|
| 75 |
+
βββ main.py # Legacy PyQt5 desktop GUI (NOT used in web)
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
---
|
| 79 |
+
|
| 80 |
+
## 3. Key Function Signatures
|
| 81 |
+
|
| 82 |
+
### Steganography
|
| 83 |
+
```python
|
| 84 |
+
# app/models/DEEP_STEGO/hide_image.py
|
| 85 |
+
def hide_image(cover_image_filepath: str, secret_image_filepath: str) -> str:
|
| 86 |
+
"""Hides secret inside cover. Returns path to steg image (tempfile .png)."""
|
| 87 |
+
|
| 88 |
+
# app/models/DEEP_STEGO/reveal_image.py
|
| 89 |
+
def reveal_image(stego_image_filepath: str) -> str:
|
| 90 |
+
"""Extracts hidden image from steg. Returns path to revealed image (tempfile .png)."""
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
### Encryption
|
| 94 |
+
```python
|
| 95 |
+
# app/models/encryption/aes.py and blowfish.py
|
| 96 |
+
def encrypt(image_path: str, key: str) -> None:
|
| 97 |
+
"""Encrypts image file. Saves to image_path + '.enc'."""
|
| 98 |
+
|
| 99 |
+
def decrypt(encrypted_image_path: str, key: str) -> tuple[int, str | None]:
|
| 100 |
+
"""Decrypts. Returns (0, output_path) on success, (-1, None) on wrong key."""
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
### Super Resolution
|
| 104 |
+
```python
|
| 105 |
+
# app/models/ESRGAN/upscale_image.py
|
| 106 |
+
def upscale_image(image_filepath: str) -> str:
|
| 107 |
+
"""4Γ upscale using ESRGAN. Returns output path (tempfile .png)."""
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
### Image Generation
|
| 111 |
+
```python
|
| 112 |
+
# app/models/StableDiffusionAPI/StableDiffusionV2.py
|
| 113 |
+
def generate(text_prompt: str) -> PIL.Image:
|
| 114 |
+
"""Calls HuggingFace Stable Diffusion 2.1 API. Requires HF_API_KEY env var."""
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## 4. Model Architecture
|
| 120 |
+
|
| 121 |
+
### Deep Steganography (TensorFlow/Keras)
|
| 122 |
+
- Input: 224Γ224 RGB images (cover + secret), normalized with ImageNet stats
|
| 123 |
+
- **Prepare Network**: 3 parallel Conv2D branches (3Γ3, 4Γ4, 5Γ5 kernels, 50 filters each) β concatenated β final branch
|
| 124 |
+
- **Hide Network**: Concatenates cover + prepared secret features β same multi-scale conv structure β outputs 3-channel steg image
|
| 125 |
+
- **Reveal Network**: Takes steg image (with Gaussian noise during training) β recovers secret
|
| 126 |
+
- Loss: Custom MSE β hide weight 1.0, reveal weight 0.75
|
| 127 |
+
- Preprocessing: channel-wise z-score with ImageNet mean/std
|
| 128 |
+
|
| 129 |
+
### ESRGAN (PyTorch)
|
| 130 |
+
- `RRDBNet(3, 3, 64, 23, gc=32)` β 23 RRDB blocks, 64 features, 32 growth channels
|
| 131 |
+
- 4Γ spatial upscaling
|
| 132 |
+
- Runs on CUDA if available, falls back to CPU automatically
|
| 133 |
+
- Model loaded lazily (once, on first call)
|
| 134 |
+
|
| 135 |
+
---
|
| 136 |
+
|
| 137 |
+
## 5. Environment & Dependencies
|
| 138 |
+
|
| 139 |
+
```
|
| 140 |
+
tensorflow # hide/reveal CNN models
|
| 141 |
+
torch # ESRGAN
|
| 142 |
+
torchvision
|
| 143 |
+
opencv-python-headless # image I/O (headless = no GUI deps)
|
| 144 |
+
Pillow # image processing
|
| 145 |
+
pycryptodome # AES + Blowfish encryption
|
| 146 |
+
gradio # web UI
|
| 147 |
+
numpy
|
| 148 |
+
imageio # saving numpy arrays as images
|
| 149 |
+
requests # Stable Diffusion API calls
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
**Python version:** 3.10+ recommended
|
| 153 |
+
**GPU:** Optional. ESRGAN auto-detects CUDA; steganography runs on CPU fine.
|
| 154 |
+
|
| 155 |
+
---
|
| 156 |
+
|
| 157 |
+
## 6. Environment Variables
|
| 158 |
+
|
| 159 |
+
| Variable | Required | Purpose |
|
| 160 |
+
|----------|----------|---------|
|
| 161 |
+
| `HF_API_KEY` | Optional | HuggingFace API token for Stable Diffusion image generation tab |
|
| 162 |
+
|
| 163 |
+
Set as a HuggingFace Space secret or local env variable.
|
| 164 |
+
|
| 165 |
+
---
|
| 166 |
+
|
| 167 |
+
## 7. Deployment
|
| 168 |
+
|
| 169 |
+
**Live URL:** https://huggingface.co/spaces/savetrees/StegNet
|
| 170 |
+
**GitHub:** https://github.com/savetree-1/stego-net
|
| 171 |
+
**Platform:** HuggingFace Spaces (Gradio SDK, CPU Basic, free tier)
|
| 172 |
+
**Git LFS:** All binary files (`*.h5`, `*.pth`, `*.hdf5`, `*.npy`, `*.png`, `*.jpg`) are tracked with Git LFS.
|
| 173 |
+
|
| 174 |
+
### Run locally
|
| 175 |
+
```bash
|
| 176 |
+
git clone https://github.com/savetree-1/stego-net.git
|
| 177 |
+
cd stego-net
|
| 178 |
+
pip install -r requirements.txt
|
| 179 |
+
python app.py
|
| 180 |
+
```
|
| 181 |
+
|
| 182 |
+
### Push updates
|
| 183 |
+
```bash
|
| 184 |
+
git add .
|
| 185 |
+
git commit -m "your message"
|
| 186 |
+
# HuggingFace
|
| 187 |
+
git push https://savetrees:HF_TOKEN@huggingface.co/spaces/savetrees/StegNet main
|
| 188 |
+
# GitHub
|
| 189 |
+
git push https://savetree-1:GH_TOKEN@github.com/savetree-1/stego-net.git main
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
---
|
| 193 |
+
|
| 194 |
+
## 8. Important Implementation Notes
|
| 195 |
+
|
| 196 |
+
- **Model paths** are resolved relative to each file using `os.path.dirname(os.path.abspath(__file__))` β never hardcoded absolute paths
|
| 197 |
+
- **Models are lazy-loaded** (loaded once on first inference call, cached in module-level `_model` variable)
|
| 198 |
+
- **Output files** use `tempfile.mkstemp(suffix='.png')` β caller receives the path
|
| 199 |
+
- **Encryption output** saves to `original_path + '.enc'`; decryption saves to `original_path.dec.png`
|
| 200 |
+
- The `app/ui/main.py` PyQt5 desktop GUI is **not used** in the web deployment β ignore it
|
| 201 |
+
- The `app/main_CLI_v1.py` CLI script is **not used** in the web deployment β ignore it
|
| 202 |
+
- `imageio.imsave` is used instead of PIL/cv2 for saving numpy uint8 arrays from model output
|
| 203 |
+
|
| 204 |
+
---
|
| 205 |
+
|
| 206 |
+
## 9. Known Issues & Limitations
|
| 207 |
+
|
| 208 |
+
- ESRGAN is slow on CPU (HuggingFace free tier) β expect 30β60s for upscaling
|
| 209 |
+
- Steganography models resize all input to 224Γ224 β output is always 224Γ224
|
| 210 |
+
- HuggingFace free tier sleeps after inactivity β first request may be slow
|
| 211 |
+
- Image generation requires `HF_API_KEY` env var β disabled if not set
|
| 212 |
+
|
| 213 |
+
---
|
| 214 |
+
|
| 215 |
+
## 10. Original Project
|
| 216 |
+
|
| 217 |
+
This project was originally called **InvisiCipher** (by Asirwad). It has been:
|
| 218 |
+
- Rebranded to **StegNet**
|
| 219 |
+
- Converted from a PyQt5 desktop app to a Gradio web app
|
| 220 |
+
- Fixed hardcoded Windows paths replaced with portable relative paths
|
| 221 |
+
- Added CPU fallback for ESRGAN
|
| 222 |
+
- Added model lazy-loading
|
| 223 |
+
- Removed hardcoded API key file dependency (now uses env var)
|
| 224 |
|
| 225 |
<p align="center">
|
| 226 |
<strong>Hide secrets, enhance images!</strong>
|