← BACK TO PORTFOLIO

AI ADAPTIVE IMAGE COMPRESSION

Content-aware compression: a neural network decides which parts of an image matter, so text stays sharp while everything else is compressed hard.

RoleML Engineer
Timeline2024
ModelMobileNetV2 + OCR branch
StackPython, TensorFlow/Keras, OpenCV, Tesseract, Streamlit

OVERVIEW

Standard compression treats every pixel the same, so a screenshot or a scanned document loses legibility in exactly the places people need to read. This project splits an image into tiles, predicts how important each tile is, and spends quality only where it matters.

AI Image Compression Result 1
AI Image Compression Result 2
AI Image Compression Result 3
AI Image Compression Result 4

THE PIPELINE

01TileThe uploaded image is cut into 64×64 tiles; edge tiles are zero-padded.
02Find textTesseract runs on an Otsu-binarized copy, and every detected word box is painted into a binary mask — a fourth channel next to RGB.
03ScoreEach tile (resized to 128×128×4) goes through the classifier, which returns an importance probability.
04AdaptTiles above the threshold keep full resolution with Lanczos interpolation; the rest are downscaled (40% by default) and scaled back with cheaper linear interpolation.
05Rebuild & encodeTiles are stitched back together and saved as JPEG at the chosen quality; the app reports the size reduction and draws an importance heatmap.

THE MODEL

Visual branch
MobileNetV2 pretrained on ImageNet, with only its last 30 layers fine-tuned, followed by global average pooling.
Text branch
A small convolutional branch (16 filters, max-pooling) reads the OCR mask on its own.
Fusion head
Both feature vectors are concatenated, then Dense(256) → Dropout(0.4) → Dense(64) → sigmoid.
Training
Adam (1e-4), binary cross-entropy, balanced class weights, early stopping, and learning-rate reduction on plateau.

HOW THE TRAINING DATA WAS LABELED

Labeling a whole image dataset by hand was not realistic, so labels are produced automatically. A preprocessing script runs OCR on each image and measures the share of its area covered by text: 1% or more is labeled “important”, anything less “not important”. The CNN then learns to generalize that signal from the pixels plus the mask.

INTERACTIVE APP

  • Tunable in real time: sliders for the importance threshold (default 0.6), the scale for important and unimportant tiles, and the final JPEG quality (default 85).
  • Side-by-side comparison of the original and compressed image, with both file sizes and the percentage saved.
  • Heatmap overlay showing which tiles the model considered important.
  • One-click download of the compressed result.

File size drops by up to 60–80%, measured against the same image saved as a quality-100 JPEG, while text stays readable.

LIMITATIONS

  • “Important” currently means “contains text”, because that is what the labels encode. Faces and other objects are the next target.
  • Importance is binary per tile, not a continuous quality level.
  • OCR quality limits the labels: blurry or stylized text can be missed.

RUN IT

git clone https://github.com/AAFLY99/ai-adaptive-image-compression.git
cd ai-adaptive-image-compression
pip install -r requirements.txt   # also install Tesseract OCR on your system
python classify.py                 # label the dataset with OCR
python train.py                    # train the tile classifier
streamlit run app.py

The dataset and trained models are excluded from the repository because of their size.