ascota_classification.color¶
The classification color module provides functionality for classifying
pottery sherds based on their color attributes. It includes methods for
classifying colors into predefined categories, extracting dominant colors from
images, and visualizing color distributions using 3 different methods. The methods
include lab thresholding, k-means clustering, and CLIP-ViT based classification. This
ensures that pottery sherds can be systematically categorized based on their color.
Color classification module for pottery images with transparent backgrounds.
This module provides three methods for classifying pottery color: 1. lab_threshold: Uses CIELAB color space thresholds 2. kmeans_lab: K-means clustering in CIELAB space 3. clip_vit: CLIP-based image classification
_postprocess ¶
_postprocess(output)
Postprocess CLIP model output to convert to dictionary format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
list
|
Raw output from CLIP pipeline |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary mapping labels to confidence scores |
Source code in src/ascota_classification/color.py
17 18 19 20 21 22 23 24 25 26 27 | |
_get_lab_values ¶
_get_lab_values(image)
Convert PIL image to CIELAB color space and extract valid pottery pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL Image with transparency (RGBA) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (lab_pixels, valid_mask) where lab_pixels is Nx3 array of Lab* values |
ndarray
|
and valid_mask is boolean array indicating valid pottery pixels |
Source code in src/ascota_classification/color.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
_classify_by_lab_threshold ¶
_classify_by_lab_threshold(image, debug=False)
Classify pottery color using CIELAB thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL Image with transparent background |
required |
debug
|
bool
|
If True, print debug information |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Classification label: "Red Pottery", "Black Pottery", or "Uncertain" |
Source code in src/ascota_classification/color.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
_classify_by_kmeans_lab ¶
_classify_by_kmeans_lab(image, n_clusters=2, debug=False)
Classify pottery color using K-means clustering in CIELAB space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL Image with transparent background |
required |
n_clusters
|
int
|
Number of clusters for K-means (default: 2) |
2
|
debug
|
bool
|
If True, print debug information |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Classification label: "Red Pottery", "Black Pottery", "Mixed", or "Uncertain" |
Source code in src/ascota_classification/color.py
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
_classify_by_clip_vit ¶
_classify_by_clip_vit(image, candidate_labels='Red Pottery, Black Pottery', debug=False)
Classify pottery color using CLIP vit image classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL Image with transparent background |
required |
candidate_labels
|
str
|
Comma-separated string of candidate labels |
'Red Pottery, Black Pottery'
|
debug
|
bool
|
If True, print debug information |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary mapping labels to confidence scores |
Source code in src/ascota_classification/color.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
classify_pottery_color ¶
classify_pottery_color(
image,
method="lab_threshold",
candidate_labels="Red Pottery, Black Pottery",
n_clusters=2,
debug=False,
)
Classify pottery color from an image with transparent background.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL Image with transparent background (RGBA format) |
required |
method
|
Literal['lab_threshold', 'kmeans_lab', 'clip_vit']
|
Classification method to use: - "lab_threshold": CIELAB color space threshold-based classification - "kmeans_lab": K-means clustering in CIELAB space - "clip_vit": CLIP-based classification |
'lab_threshold'
|
candidate_labels
|
str
|
Comma-separated candidate labels for CLIP method |
'Red Pottery, Black Pottery'
|
n_clusters
|
int
|
Number of clusters for kmeans_lab method (default: 2) |
2
|
debug
|
bool
|
If True, print debug information |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, any]
|
Dictionary containing: - "label": Classification result - "method": Method used - "scores": (Optional) Confidence scores for CLIP method |
Raises:
| Type | Description |
|---|---|
ValueError
|
If image doesn't have alpha channel or method is invalid |
Examples:
>>> from PIL import Image
>>> img = Image.open("pottery.png")
>>> result = classify_pottery_color(img, method="lab_threshold", debug=True)
>>> print(result["label"])
'Red Pottery'
>>> result = classify_pottery_color(img, method="clip_vit",
... candidate_labels="Red, Black, White")
>>> print(result["scores"])
{'Red': 0.85, 'Black': 0.10, 'White': 0.05}
Source code in src/ascota_classification/color.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |