Spaces:
Sleeping
Sleeping
Update readme
Browse files
readme
CHANGED
|
@@ -1,225 +1,241 @@
|
|
| 1 |
-
Image Color Classifier — README
|
| 2 |
-
|
| 3 |
-
Classify an image as rust, zinc, or normal using simple, fast color-space heuristics (OpenCV + NumPy).
|
| 4 |
-
You get three ways to use it:
|
| 5 |
-
|
| 6 |
-
a FastAPI server (/classify/ endpoint)
|
| 7 |
-
|
| 8 |
-
a Gradio web UI for quick testing
|
| 9 |
-
|
| 10 |
-
CLI tools to generate color reports and classify offline
|
| 11 |
-
|
| 12 |
-
Features
|
| 13 |
-
|
| 14 |
-
Heuristic color analysis in CIELab:
|
| 15 |
-
|
| 16 |
-
rustish_ratio → fraction of pixels with elevated a* (reddish/brownish)
|
| 17 |
-
|
| 18 |
-
zincish_ratio → fraction of pixels with elevated b* (yellowish)
|
| 19 |
-
|
| 20 |
-
Dominant colors via K-Means (palette + relative shares)
|
| 21 |
-
|
| 22 |
-
Three thresholds you can tune: rust_thr, zinc_thr, and lab_delta
|
| 23 |
-
|
| 24 |
-
Repository Layout
|
| 25 |
-
.
|
| 26 |
-
├─ api.py # FastAPI app exposing /classify/
|
| 27 |
-
├─ app.py # Gradio demo UI
|
| 28 |
-
├─ classify.py # (named 'app.py' in your message) classify from *_color_report.json
|
| 29 |
-
├─ color.py # Report generator (stats + palette + heuristics)
|
| 30 |
-
├─ main.py # CLI variant (stats + palette + heuristics + classification)
|
| 31 |
-
├─ requirements.txt # Python deps (fix the typos—see below)
|
| 32 |
-
└─ color_out/ # (created at runtime) reports and palette images
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
⚠️ Fix your requirements.txt:
|
| 36 |
-
|
| 37 |
-
Replace:
|
| 38 |
-
|
| 39 |
-
"fastapi[all]"
|
| 40 |
-
opencv-python-headless
|
| 41 |
-
numpy
|
| 42 |
-
gardio
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
With:
|
| 46 |
-
|
| 47 |
-
fastapi[all]
|
| 48 |
-
uvicorn
|
| 49 |
-
opencv-python-headless
|
| 50 |
-
numpy
|
| 51 |
-
gradio
|
| 52 |
-
|
| 53 |
-
Quickstart
|
| 54 |
-
1) Environment
|
| 55 |
-
python -m venv .venv
|
| 56 |
-
# Windows
|
| 57 |
-
.venv\Scripts\activate
|
| 58 |
-
# macOS/Linux
|
| 59 |
-
source .venv/bin/activate
|
| 60 |
-
|
| 61 |
-
pip install -r requirements.txt
|
| 62 |
-
|
| 63 |
-
2) Run the FastAPI server
|
| 64 |
-
uvicorn api:app --host 127.0.0.1 --port 8000
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
Endpoint: POST /classify/
|
| 68 |
-
|
| 69 |
-
Body: multipart form with file field file (image)
|
| 70 |
-
|
| 71 |
-
Query params (optional):
|
| 72 |
-
|
| 73 |
-
k (int, default 3) — number of dominant colors
|
| 74 |
-
|
| 75 |
-
rust_thr (float, default 0.01)
|
| 76 |
-
|
| 77 |
-
zinc_thr (float, default 0.02)
|
| 78 |
-
|
| 79 |
-
lab_delta (float, default 6.0)
|
| 80 |
-
|
| 81 |
-
cURL example:
|
| 82 |
-
|
| 83 |
-
curl -X POST "http://127.0.0.1:8000/classify/?k=3&rust_thr=0.01&zinc_thr=0.02&lab_delta=6.0" \
|
| 84 |
-
-F "file=@/path/to/sample.jpg"
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
Response (JSON):
|
| 88 |
-
|
| 89 |
-
{
|
| 90 |
-
"filename": "sample.jpg",
|
| 91 |
-
"classification": "rust",
|
| 92 |
-
"rustish_ratio": 0.0342,
|
| 93 |
-
"zincish_ratio": 0.0051,
|
| 94 |
-
"top_colors_rgb": [[183, 98, 72], [220, 210, 205], [120, 85, 70]],
|
| 95 |
-
"top_colors_share": [0.62, 0.25, 0.13]
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
OpenAPI docs at: http://127.0.0.1:8000/docs
|
| 100 |
-
|
| 101 |
-
3) Run the Gradio demo
|
| 102 |
-
python gradio_app.py
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
Upload an image, tweak sliders, and see the JSON output instantly.
|
| 106 |
-
|
| 107 |
-
Parameters mirrored: k, rust_thr, zinc_thr, lab_delta.
|
| 108 |
-
|
| 109 |
-
4) CLI workflows
|
| 110 |
-
|
| 111 |
-
You have two report generators and one “classify from JSON” helper.
|
| 112 |
-
Pick either color.py or main.py to generate a report.
|
| 113 |
-
|
| 114 |
-
A) Generate a detailed report with color.py
|
| 115 |
-
|
| 116 |
-
Creates:
|
| 117 |
-
|
| 118 |
-
Stats in RGB/HSV/Lab
|
| 119 |
-
|
| 120 |
-
Dominant color palette image
|
| 121 |
-
|
| 122 |
-
Heuristic ratios
|
| 123 |
-
|
| 124 |
-
python color.py --img /path/to/img.jpg --k 3 --resize_max 1200 --outdir color_out \
|
| 125 |
-
--rust_thr 0.01 --zinc_thr 0.02 --lab_delta 6.0
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
Outputs:
|
| 129 |
-
|
| 130 |
-
color_out/<name>_palette.png
|
| 131 |
-
|
| 132 |
-
color_out/<name>_color_report.json (includes classification)
|
| 133 |
-
|
| 134 |
-
B) Alternative generator main.py
|
| 135 |
-
|
| 136 |
-
Similar to color.py, also prints a short console summary.
|
| 137 |
-
|
| 138 |
-
python main.py --img /path/to/img.jpg --k 3 --resize_max 1200 --outdir color_out
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
Add your preferred license (e.g., MIT) as LICENSE.
|
|
|
|
| 1 |
+
Image Color Classifier — README
|
| 2 |
+
|
| 3 |
+
Classify an image as rust, zinc, or normal using simple, fast color-space heuristics (OpenCV + NumPy).
|
| 4 |
+
You get three ways to use it:
|
| 5 |
+
|
| 6 |
+
a FastAPI server (/classify/ endpoint)
|
| 7 |
+
|
| 8 |
+
a Gradio web UI for quick testing
|
| 9 |
+
|
| 10 |
+
CLI tools to generate color reports and classify offline
|
| 11 |
+
|
| 12 |
+
Features
|
| 13 |
+
|
| 14 |
+
Heuristic color analysis in CIELab:
|
| 15 |
+
|
| 16 |
+
rustish_ratio → fraction of pixels with elevated a* (reddish/brownish)
|
| 17 |
+
|
| 18 |
+
zincish_ratio → fraction of pixels with elevated b* (yellowish)
|
| 19 |
+
|
| 20 |
+
Dominant colors via K-Means (palette + relative shares)
|
| 21 |
+
|
| 22 |
+
Three thresholds you can tune: rust_thr, zinc_thr, and lab_delta
|
| 23 |
+
|
| 24 |
+
Repository Layout
|
| 25 |
+
.
|
| 26 |
+
├─ api.py # FastAPI app exposing /classify/
|
| 27 |
+
├─ app.py # Gradio demo UI
|
| 28 |
+
├─ classify.py # (named 'app.py' in your message) classify from *_color_report.json
|
| 29 |
+
├─ color.py # Report generator (stats + palette + heuristics)
|
| 30 |
+
├─ main.py # CLI variant (stats + palette + heuristics + classification)
|
| 31 |
+
├─ requirements.txt # Python deps (fix the typos—see below)
|
| 32 |
+
└─ color_out/ # (created at runtime) reports and palette images
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
⚠️ Fix your requirements.txt:
|
| 36 |
+
|
| 37 |
+
Replace:
|
| 38 |
+
|
| 39 |
+
"fastapi[all]"
|
| 40 |
+
opencv-python-headless
|
| 41 |
+
numpy
|
| 42 |
+
gardio
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
With:
|
| 46 |
+
|
| 47 |
+
fastapi[all]
|
| 48 |
+
uvicorn
|
| 49 |
+
opencv-python-headless
|
| 50 |
+
numpy
|
| 51 |
+
gradio
|
| 52 |
+
|
| 53 |
+
Quickstart
|
| 54 |
+
1) Environment
|
| 55 |
+
python -m venv .venv
|
| 56 |
+
# Windows
|
| 57 |
+
.venv\Scripts\activate
|
| 58 |
+
# macOS/Linux
|
| 59 |
+
source .venv/bin/activate
|
| 60 |
+
|
| 61 |
+
pip install -r requirements.txt
|
| 62 |
+
|
| 63 |
+
2) Run the FastAPI server
|
| 64 |
+
uvicorn api:app --host 127.0.0.1 --port 8000
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
Endpoint: POST /classify/
|
| 68 |
+
|
| 69 |
+
Body: multipart form with file field file (image)
|
| 70 |
+
|
| 71 |
+
Query params (optional):
|
| 72 |
+
|
| 73 |
+
k (int, default 3) — number of dominant colors
|
| 74 |
+
|
| 75 |
+
rust_thr (float, default 0.01)
|
| 76 |
+
|
| 77 |
+
zinc_thr (float, default 0.02)
|
| 78 |
+
|
| 79 |
+
lab_delta (float, default 6.0)
|
| 80 |
+
|
| 81 |
+
cURL example:
|
| 82 |
+
|
| 83 |
+
curl -X POST "http://127.0.0.1:8000/classify/?k=3&rust_thr=0.01&zinc_thr=0.02&lab_delta=6.0" \
|
| 84 |
+
-F "file=@/path/to/sample.jpg"
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
Response (JSON):
|
| 88 |
+
|
| 89 |
+
{
|
| 90 |
+
"filename": "sample.jpg",
|
| 91 |
+
"classification": "rust",
|
| 92 |
+
"rustish_ratio": 0.0342,
|
| 93 |
+
"zincish_ratio": 0.0051,
|
| 94 |
+
"top_colors_rgb": [[183, 98, 72], [220, 210, 205], [120, 85, 70]],
|
| 95 |
+
"top_colors_share": [0.62, 0.25, 0.13]
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
OpenAPI docs at: http://127.0.0.1:8000/docs
|
| 100 |
+
|
| 101 |
+
3) Run the Gradio demo
|
| 102 |
+
python gradio_app.py
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
Upload an image, tweak sliders, and see the JSON output instantly.
|
| 106 |
+
|
| 107 |
+
Parameters mirrored: k, rust_thr, zinc_thr, lab_delta.
|
| 108 |
+
|
| 109 |
+
4) CLI workflows
|
| 110 |
+
|
| 111 |
+
You have two report generators and one “classify from JSON” helper.
|
| 112 |
+
Pick either color.py or main.py to generate a report.
|
| 113 |
+
|
| 114 |
+
A) Generate a detailed report with color.py
|
| 115 |
+
|
| 116 |
+
Creates:
|
| 117 |
+
|
| 118 |
+
Stats in RGB/HSV/Lab
|
| 119 |
+
|
| 120 |
+
Dominant color palette image
|
| 121 |
+
|
| 122 |
+
Heuristic ratios
|
| 123 |
+
|
| 124 |
+
python color.py --img /path/to/img.jpg --k 3 --resize_max 1200 --outdir color_out \
|
| 125 |
+
--rust_thr 0.01 --zinc_thr 0.02 --lab_delta 6.0
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
Outputs:
|
| 129 |
+
|
| 130 |
+
color_out/<name>_palette.png
|
| 131 |
+
|
| 132 |
+
color_out/<name>_color_report.json (includes classification)
|
| 133 |
+
|
| 134 |
+
B) Alternative generator main.py
|
| 135 |
+
|
| 136 |
+
Similar to color.py, also prints a short console summary.
|
| 137 |
+
|
| 138 |
+
python main.py --img /path/to/img.jpg --k 3 --resize_max 1200 --outdir color_out
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
Outputs:
|
| 145 |
+
|
| 146 |
+
color_out/<name>_palette.png
|
| 147 |
+
|
| 148 |
+
color_out/<name>_color_report.json
|
| 149 |
+
|
| 150 |
+
C) Classify an existing report with classify.py
|
| 151 |
+
|
| 152 |
+
(Your message labeled this file as app.py—the code shows it’s a JSON classifier.)
|
| 153 |
+
|
| 154 |
+
python classify.py --report color_out/<name>_color_report.json \
|
| 155 |
+
--rust_thr 0.01 --zinc_thr 0.02
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
Console output:
|
| 162 |
+
|
| 163 |
+
{
|
| 164 |
+
"file": "img.jpg",
|
| 165 |
+
"rustish_ratio": 0.0342,
|
| 166 |
+
"zincish_ratio": 0.0051,
|
| 167 |
+
"classification": "rust"
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
How it works (short)
|
| 171 |
+
|
| 172 |
+
Convert image to CIELab: L*, a*, b* (OpenCV uses a scaled 8-bit Lab).
|
| 173 |
+
|
| 174 |
+
Compute medians of a* and b*, add lab_delta (e.g., 6.0) to form thresholds.
|
| 175 |
+
|
| 176 |
+
Rustish = fraction of pixels with a* > median(a*) + lab_delta.
|
| 177 |
+
Zincish = fraction with b* > median(b*) + lab_delta.
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
Decision rule:
|
| 184 |
+
|
| 185 |
+
If zincish_ratio > zinc_thr → zinc
|
| 186 |
+
|
| 187 |
+
Else if rustish_ratio > rust_thr → rust
|
| 188 |
+
|
| 189 |
+
Else → normal
|
| 190 |
+
|
| 191 |
+
These are simple heuristics for quick screening, not robust material detection.
|
| 192 |
+
|
| 193 |
+
Tuning tips
|
| 194 |
+
|
| 195 |
+
Increase lab_delta to be stricter (fewer pixels count as rustish/zincish).
|
| 196 |
+
|
| 197 |
+
Decrease thresholds rust_thr / zinc_thr to make classification more sensitive.
|
| 198 |
+
|
| 199 |
+
For large images, set --resize_max to speed up K-Means and stats.
|
| 200 |
+
|
| 201 |
+
Notes & gotchas
|
| 202 |
+
|
| 203 |
+
Color spaces: OpenCV reads images as BGR; conversions are handled internally.
|
| 204 |
+
|
| 205 |
+
Lighting & WB: Results vary with illumination and white balance. Try to keep input lighting consistent.
|
| 206 |
+
|
| 207 |
+
Performance: k=3 works well for speed. Increase for richer palettes at a compute cost.
|
| 208 |
+
|
| 209 |
+
Headless environments: Using opencv-python-headless avoids GUI dependencies.
|
| 210 |
+
|
| 211 |
+
Example programmatic use (Python)
|
| 212 |
+
import cv2 as cv
|
| 213 |
+
from api import rust_zinc_indicators, classify_from_ratios, dominant_colors_kmeans
|
| 214 |
+
|
| 215 |
+
bgr = cv.imread("sample.jpg", cv.IMREAD_COLOR)
|
| 216 |
+
inds = rust_zinc_indicators(bgr, delta=6.0)
|
| 217 |
+
label = classify_from_ratios(inds["rustish_ratio"], inds["zincish_ratio"],
|
| 218 |
+
rust_thr=0.01, zinc_thr=0.02)
|
| 219 |
+
palette = dominant_colors_kmeans(bgr, k=3)
|
| 220 |
+
print(label, inds, palette[:1])
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
Troubleshooting
|
| 227 |
+
|
| 228 |
+
Invalid image file. Could not decode image.
|
| 229 |
+
The upload wasn’t a valid image. Try a supported format (JPG/PNG).
|
| 230 |
+
|
| 231 |
+
ModuleNotFoundError / import errors:
|
| 232 |
+
Re-check your virtualenv and pip install -r requirements.txt.
|
| 233 |
+
|
| 234 |
+
Uvicorn not found:
|
| 235 |
+
Add uvicorn to requirements.txt and pip install -r requirements.txt.
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
License
|
| 240 |
+
|
| 241 |
Add your preferred license (e.g., MIT) as LICENSE.
|