Spaces:
Sleeping
Sleeping
File size: 32,527 Bytes
1d177b0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 71 72 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 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 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 255 256 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 292 293 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"!pip install rembg[gpu] opencv-python-headless pillow tqdm onnxruntime"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mzMbPGrmcntQ",
"outputId": "88a9177b-793b-4dae-f666-72e440e74acc"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.11/dist-packages (4.11.0.86)\n",
"Requirement already satisfied: pillow in /usr/local/lib/python3.11/dist-packages (11.2.1)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (4.67.1)\n",
"Collecting onnxruntime\n",
" Downloading onnxruntime-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (4.5 kB)\n",
"Collecting rembg[gpu]\n",
" Downloading rembg-2.0.66-py3-none-any.whl.metadata (21 kB)\n",
"Requirement already satisfied: jsonschema in /usr/local/lib/python3.11/dist-packages (from rembg[gpu]) (4.24.0)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from rembg[gpu]) (2.0.2)\n",
"Requirement already satisfied: pooch in /usr/local/lib/python3.11/dist-packages (from rembg[gpu]) (1.8.2)\n",
"Collecting pymatting (from rembg[gpu])\n",
" Downloading pymatting-1.1.14-py3-none-any.whl.metadata (7.7 kB)\n",
"Requirement already satisfied: scikit-image in /usr/local/lib/python3.11/dist-packages (from rembg[gpu]) (0.25.2)\n",
"Requirement already satisfied: scipy in /usr/local/lib/python3.11/dist-packages (from rembg[gpu]) (1.15.3)\n",
"Collecting onnxruntime-gpu (from rembg[gpu])\n",
" Downloading onnxruntime_gpu-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (4.9 kB)\n",
"Collecting coloredlogs (from onnxruntime)\n",
" Downloading coloredlogs-15.0.1-py2.py3-none-any.whl.metadata (12 kB)\n",
"Requirement already satisfied: flatbuffers in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (25.2.10)\n",
"Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (24.2)\n",
"Requirement already satisfied: protobuf in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (5.29.5)\n",
"Requirement already satisfied: sympy in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (1.13.1)\n",
"Collecting humanfriendly>=9.1 (from coloredlogs->onnxruntime)\n",
" Downloading humanfriendly-10.0-py2.py3-none-any.whl.metadata (9.2 kB)\n",
"Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.11/dist-packages (from jsonschema->rembg[gpu]) (25.3.0)\n",
"Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.11/dist-packages (from jsonschema->rembg[gpu]) (2025.4.1)\n",
"Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.11/dist-packages (from jsonschema->rembg[gpu]) (0.36.2)\n",
"Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.11/dist-packages (from jsonschema->rembg[gpu]) (0.25.1)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in /usr/local/lib/python3.11/dist-packages (from pooch->rembg[gpu]) (4.3.8)\n",
"Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.11/dist-packages (from pooch->rembg[gpu]) (2.32.3)\n",
"Requirement already satisfied: numba!=0.49.0 in /usr/local/lib/python3.11/dist-packages (from pymatting->rembg[gpu]) (0.60.0)\n",
"Requirement already satisfied: networkx>=3.0 in /usr/local/lib/python3.11/dist-packages (from scikit-image->rembg[gpu]) (3.5)\n",
"Requirement already satisfied: imageio!=2.35.0,>=2.33 in /usr/local/lib/python3.11/dist-packages (from scikit-image->rembg[gpu]) (2.37.0)\n",
"Requirement already satisfied: tifffile>=2022.8.12 in /usr/local/lib/python3.11/dist-packages (from scikit-image->rembg[gpu]) (2025.6.11)\n",
"Requirement already satisfied: lazy-loader>=0.4 in /usr/local/lib/python3.11/dist-packages (from scikit-image->rembg[gpu]) (0.4)\n",
"Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy->onnxruntime) (1.3.0)\n",
"Requirement already satisfied: llvmlite<0.44,>=0.43.0dev0 in /usr/local/lib/python3.11/dist-packages (from numba!=0.49.0->pymatting->rembg[gpu]) (0.43.0)\n",
"Requirement already satisfied: typing-extensions>=4.4.0 in /usr/local/lib/python3.11/dist-packages (from referencing>=0.28.4->jsonschema->rembg[gpu]) (4.14.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->pooch->rembg[gpu]) (3.4.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->pooch->rembg[gpu]) (3.10)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->pooch->rembg[gpu]) (2.4.0)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->pooch->rembg[gpu]) (2025.6.15)\n",
"Downloading onnxruntime-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (16.4 MB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m16.4/16.4 MB\u001b[0m \u001b[31m68.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading onnxruntime_gpu-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (283.2 MB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m283.2/283.2 MB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading pymatting-1.1.14-py3-none-any.whl (54 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m54.7/54.7 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading rembg-2.0.66-py3-none-any.whl (41 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m41.7/41.7 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m6.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: humanfriendly, pymatting, coloredlogs, onnxruntime-gpu, onnxruntime, rembg\n",
"Successfully installed coloredlogs-15.0.1 humanfriendly-10.0 onnxruntime-1.22.0 onnxruntime-gpu-1.22.0 pymatting-1.1.14 rembg-2.0.66\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HKYwL78egwwZ",
"outputId": "af67d853-0f86-4b1f-b4be-a09bcb1b2c97"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "c311ZPEYb0wH",
"outputId": "75c250d4-9dc8-4aed-ff4f-fe39c82633a3"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"=== PRΓPROCESSING DES EMPREINTES DIGITALES ===\n",
"Dossier d'entrΓ©e: /content/drive/MyDrive/DATA\n",
"Dossier de sortie: /content/processed_fingerprints_structured\n",
"Taille cible: (128, 128)\n",
"Compatible VGG16: True\n",
"\n",
"=== ANALYSE DE LA STRUCTURE DU DOSSIER ===\n",
"Total d'images trouvΓ©es: 2100\n",
"RΓ©partition par dossier:\n",
" LANDRY: 210 images\n",
" DIVINE: 210 images\n",
" JOEL: 210 images\n",
" MAURICE: 210 images\n",
" NATHANAEL: 210 images\n",
" WILSON: 210 images\n",
" LONTSI: 210 images\n",
" DANILO: 210 images\n",
" KETSIA: 210 images\n",
" PATRICE: 210 images\n",
"Traitement de 2100 images...\n",
"Structure de dossiers prΓ©servΓ©e: /content/drive/MyDrive/DATA -> /content/processed_fingerprints_structured/processed_with_structure\n",
"Mode VGG16 : Images en RGB avec taille personnalisΓ©e\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"\rTraitement des images: 0%| | 0/2100 [00:00<?, ?it/s]Downloading data from 'https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx' to file '/root/.u2net/u2net.onnx'.\n",
"\n",
" 0%| | 0.00/176M [00:00<?, ?B/s]\u001b[A\n",
" 0%| | 80.9k/176M [00:00<04:27, 656kB/s]\u001b[A\n",
" 0%| | 392k/176M [00:00<01:28, 1.97MB/s]\u001b[A\n",
" 1%|β | 1.78M/176M [00:00<00:27, 6.27MB/s]\u001b[A\n",
" 1%|β | 2.48M/176M [00:00<00:27, 6.28MB/s]\u001b[A\n",
" 3%|β | 4.59M/176M [00:00<00:15, 11.1MB/s]\u001b[A\n",
" 6%|ββ | 9.76M/176M [00:00<00:06, 24.0MB/s]\u001b[A\n",
" 9%|ββββ | 16.0M/176M [00:00<00:04, 36.0MB/s]\u001b[A\n",
" 13%|βββββ | 22.3M/176M [00:00<00:03, 44.2MB/s]\u001b[A\n",
" 17%|βββββββ | 29.2M/176M [00:00<00:02, 51.8MB/s]\u001b[A\n",
" 20%|ββββββββ | 35.1M/176M [00:01<00:02, 54.0MB/s]\u001b[A\n",
" 24%|βββββββββ | 41.6M/176M [00:01<00:02, 57.4MB/s]\u001b[A\n",
" 27%|βββββββββββ | 47.6M/176M [00:01<00:02, 58.4MB/s]\u001b[A\n",
" 30%|ββββββββββββ | 53.6M/176M [00:01<00:02, 58.7MB/s]\u001b[A\n",
" 34%|βββββββββββββ | 59.5M/176M [00:01<00:02, 56.3MB/s]\u001b[A\n",
" 37%|βββββββββββββββ | 65.9M/176M [00:01<00:01, 58.4MB/s]\u001b[A\n",
" 41%|ββββββββββββββββ | 72.0M/176M [00:01<00:01, 59.1MB/s]\u001b[A\n",
" 44%|βββββββββββββββββ | 78.2M/176M [00:01<00:01, 60.1MB/s]\u001b[A\n",
" 48%|βββββββββββββββββββ | 84.9M/176M [00:01<00:01, 62.3MB/s]\u001b[A\n",
" 52%|ββββββββββββββββββββ | 91.5M/176M [00:01<00:01, 63.4MB/s]\u001b[A\n",
" 56%|ββββββββββββββββββββββ | 98.3M/176M [00:02<00:01, 64.6MB/s]\u001b[A\n",
" 60%|ββββββββββββββββββββββββ | 105M/176M [00:02<00:01, 65.8MB/s]\u001b[A\n",
" 64%|βββββββββββββββββββββββββ | 112M/176M [00:02<00:00, 67.0MB/s]\u001b[A\n",
" 68%|βββββββββββββββββββββββββββ | 119M/176M [00:02<00:00, 58.7MB/s]\u001b[A\n",
" 71%|ββββββββββββββββββββββββββββ | 125M/176M [00:02<00:01, 45.8MB/s]\u001b[A\n",
" 74%|βββββββββββββββββββββββββββββ | 130M/176M [00:02<00:01, 42.8MB/s]\u001b[A\n",
" 77%|ββββββββββββββββββββββββββββββ | 135M/176M [00:02<00:00, 42.4MB/s]\u001b[A\n",
" 79%|βββββββββββββββββββββββββββββββ | 139M/176M [00:03<00:00, 39.9MB/s]\u001b[A\n",
" 81%|ββββββββββββββββββββββββββββββββ | 143M/176M [00:03<00:00, 40.1MB/s]\u001b[A\n",
" 84%|βββββββββββββββββββββββββββββββββ | 148M/176M [00:03<00:00, 40.9MB/s]\u001b[A\n",
" 86%|ββββββββββββββββββββββββββββββββββ | 152M/176M [00:03<00:00, 41.4MB/s]\u001b[A\n",
" 89%|βββββββββββββββββββββββββββββββββββ | 156M/176M [00:03<00:00, 41.9MB/s]\u001b[A\n",
" 91%|ββββββββββββββββββββββββββββββββββββ | 160M/176M [00:03<00:00, 41.0MB/s]\u001b[A\n",
" 94%|βββββββββββββββββββββββββββββββββββββ | 165M/176M [00:03<00:00, 39.9MB/s]\u001b[A\n",
" 96%|ββββββββββββββββββββββββββββββββββββββ | 169M/176M [00:03<00:00, 41.2MB/s]\u001b[A\n",
" 99%|βββββββββββββββββββββββββββββββββββββββ| 174M/176M [00:03<00:00, 42.3MB/s]\u001b[A\n",
"100%|βββββββββββββββββββββββββββββββββββββββ| 176M/176M [00:00<00:00, 65.3GB/s]\n",
"Traitement des images: 18%|ββ | 376/2100 [23:59<2:12:56, 4.63s/it]"
]
}
],
"source": [
"import os\n",
"import cv2\n",
"import numpy as np\n",
"from PIL import Image\n",
"import zipfile\n",
"from rembg import remove\n",
"from tqdm import tqdm\n",
"import shutil\n",
"from google.colab import files\n",
"import io\n",
"\n",
"def get_all_image_files_with_structure(folder_path):\n",
" \"\"\"\n",
" Récupère tous les fichiers d'images dans un dossier en préservant la structure\n",
" Retourne une liste de tuples (chemin_complet, chemin_relatif)\n",
" \"\"\"\n",
" supported_formats = ('.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.tif')\n",
" image_files = []\n",
"\n",
" for root, dirs, files in os.walk(folder_path):\n",
" for file in files:\n",
" if file.lower().endswith(supported_formats):\n",
" full_path = os.path.join(root, file)\n",
" relative_path = os.path.relpath(full_path, folder_path)\n",
" image_files.append((full_path, relative_path))\n",
"\n",
" return image_files\n",
"\n",
"def resize_image(image, target_size=(224, 224)):\n",
" \"\"\"\n",
" Redimensionne l'image Γ la taille cible (224x224 pour VGG16)\n",
" \"\"\"\n",
" if isinstance(image, Image.Image):\n",
" return image.resize(target_size, Image.Resampling.LANCZOS)\n",
" else:\n",
" return cv2.resize(image, target_size, interpolation=cv2.INTER_LANCZOS4)\n",
"\n",
"def enhance_contrast_histogram_equalization(image, keep_rgb=True):\n",
" \"\"\"\n",
" AmΓ©liore le contraste avec Γ©galisation d'histogramme\n",
" Garde les 3 canaux RGB pour VGG16 si keep_rgb=True\n",
" \"\"\"\n",
" if isinstance(image, Image.Image):\n",
" img_array = np.array(image)\n",
"\n",
" if keep_rgb and len(img_array.shape) == 3:\n",
" # Appliquer CLAHE sur chaque canal RGB sΓ©parΓ©ment\n",
" img_enhanced = np.zeros_like(img_array)\n",
" clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))\n",
"\n",
" for i in range(3): # Pour chaque canal RGB\n",
" img_enhanced[:, :, i] = clahe.apply(img_array[:, :, i])\n",
"\n",
" return Image.fromarray(img_enhanced)\n",
" else:\n",
" # Version originale pour niveaux de gris\n",
" if len(img_array.shape) == 3:\n",
" img_gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)\n",
" else:\n",
" img_gray = img_array\n",
" clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))\n",
" img_enhanced = clahe.apply(img_gray)\n",
" return Image.fromarray(img_enhanced)\n",
" else:\n",
" if keep_rgb and len(image.shape) == 3:\n",
" img_enhanced = np.zeros_like(image)\n",
" clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))\n",
"\n",
" for i in range(3):\n",
" img_enhanced[:, :, i] = clahe.apply(image[:, :, i])\n",
"\n",
" return img_enhanced\n",
" else:\n",
" if len(image.shape) == 3:\n",
" img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
" else:\n",
" img_gray = image\n",
" clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))\n",
" img_enhanced = clahe.apply(img_gray)\n",
" return img_enhanced\n",
"\n",
"def ensure_rgb_format(image):\n",
" \"\"\"\n",
" S'assure que l'image est en format RGB 3 canaux pour VGG16\n",
" \"\"\"\n",
" if isinstance(image, Image.Image):\n",
" if image.mode == 'L': # Niveaux de gris\n",
" return image.convert('RGB')\n",
" elif image.mode == 'RGBA': # Avec canal alpha\n",
" return image.convert('RGB')\n",
" else:\n",
" return image\n",
" else:\n",
" if len(image.shape) == 2: # Niveaux de gris\n",
" return cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)\n",
" elif image.shape[2] == 4: # RGBA\n",
" return cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)\n",
" else:\n",
" return image\n",
"\n",
"def remove_background_rembg(image):\n",
" \"\"\"\n",
" Supprime le fond de l'image avec rembg\n",
" \"\"\"\n",
" if not isinstance(image, Image.Image):\n",
" if len(image.shape) == 2:\n",
" image = Image.fromarray(image).convert('RGB')\n",
" else:\n",
" image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n",
"\n",
" image_no_bg = remove(image)\n",
"\n",
" # Convertir RGBA vers RGB avec fond blanc pour VGG16\n",
" if image_no_bg.mode == 'RGBA':\n",
" background = Image.new('RGB', image_no_bg.size, (255, 255, 255)) # Fond blanc\n",
" background.paste(image_no_bg, mask=image_no_bg.split()[-1]) # Utiliser canal alpha comme masque\n",
" return background\n",
"\n",
" return image_no_bg\n",
"\n",
"def process_single_image_with_structure(image_path, output_folder, relative_path,\n",
" target_size=(224, 224), vgg16_compatible=True):\n",
" \"\"\"\n",
" Traite une seule image en prΓ©servant sa position dans la structure de dossiers\n",
" \"\"\"\n",
" try:\n",
" image = Image.open(image_path)\n",
"\n",
" # Redimensionnement\n",
" image_resized = resize_image(image, target_size)\n",
"\n",
" # AmΓ©lioration du contraste (garde RGB si compatible VGG16)\n",
" image_enhanced = enhance_contrast_histogram_equalization(\n",
" image_resized, keep_rgb=vgg16_compatible\n",
" )\n",
"\n",
" # Suppression du fond\n",
" image_no_bg = remove_background_rembg(image_enhanced)\n",
"\n",
" # S'assurer du format RGB pour VGG16\n",
" if vgg16_compatible:\n",
" image_no_bg = ensure_rgb_format(image_no_bg)\n",
"\n",
" # CrΓ©er la structure de dossiers de sortie\n",
" output_dir = os.path.dirname(os.path.join(output_folder, relative_path))\n",
" os.makedirs(output_dir, exist_ok=True)\n",
"\n",
" # Changer l'extension en .png pour uniformiser\n",
" base_name = os.path.splitext(os.path.basename(relative_path))[0]\n",
" output_relative_path = os.path.join(os.path.dirname(relative_path), f\"{base_name}.png\")\n",
" output_path = os.path.join(output_folder, output_relative_path)\n",
"\n",
" # Sauvegarde\n",
" image_no_bg.save(output_path, \"PNG\")\n",
"\n",
" return output_path, True\n",
" except Exception as e:\n",
" print(f\"Erreur lors du traitement de {image_path}: {str(e)}\")\n",
" return None, False\n",
"\n",
"def process_folder_with_structure(input_folder, output_folder,\n",
" target_size=(224, 224), vgg16_compatible=True):\n",
" \"\"\"\n",
" Traite toutes les images d'un dossier en prΓ©servant la structure\n",
" \"\"\"\n",
" os.makedirs(output_folder, exist_ok=True)\n",
" image_files = get_all_image_files_with_structure(input_folder)\n",
"\n",
" if not image_files:\n",
" print(f\"Aucune image trouvΓ©e dans {input_folder}\")\n",
" return []\n",
"\n",
" print(f\"Traitement de {len(image_files)} images...\")\n",
" print(f\"Structure de dossiers prΓ©servΓ©e: {input_folder} -> {output_folder}\")\n",
" if vgg16_compatible:\n",
" print(\"Mode VGG16 : Images en RGB avec taille personnalisΓ©e\")\n",
"\n",
" processed_files = []\n",
" successful_count = 0\n",
"\n",
" for image_path, relative_path in tqdm(image_files, desc=\"Traitement des images\"):\n",
" output_path, success = process_single_image_with_structure(\n",
" image_path, output_folder, relative_path, target_size, vgg16_compatible\n",
" )\n",
" if success:\n",
" processed_files.append(output_path)\n",
" successful_count += 1\n",
"\n",
" print(f\"Traitement terminé: {successful_count}/{len(image_files)} images traitées avec succès\")\n",
" return processed_files\n",
"\n",
"def create_zip_archive(folder_path, zip_filename):\n",
" \"\"\"\n",
" CrΓ©e une archive ZIP du dossier traitΓ© et la rend tΓ©lΓ©chargeable\n",
" \"\"\"\n",
" try:\n",
" with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
" for root, dirs, files in os.walk(folder_path):\n",
" for file in files:\n",
" file_path = os.path.join(root, file)\n",
" arcname = os.path.relpath(file_path, folder_path)\n",
" zipf.write(file_path, arcname)\n",
" print(f\"Archive ZIP créée: {zip_filename}\")\n",
" files.download(zip_filename)\n",
" return True\n",
" except Exception as e:\n",
" print(f\"Erreur lors de la crΓ©ation de l'archive ZIP: {str(e)}\")\n",
" return False\n",
"\n",
"def analyze_folder_structure(input_folder):\n",
" \"\"\"\n",
" Analyse et affiche la structure du dossier d'entrΓ©e\n",
" \"\"\"\n",
" print(\"\\n=== ANALYSE DE LA STRUCTURE DU DOSSIER ===\")\n",
" structure_info = {}\n",
" total_images = 0\n",
"\n",
" for root, dirs, files in os.walk(input_folder):\n",
" relative_path = os.path.relpath(root, input_folder)\n",
" if relative_path == '.':\n",
" relative_path = 'racine'\n",
"\n",
" image_count = sum(1 for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.tif')))\n",
" if image_count > 0:\n",
" structure_info[relative_path] = image_count\n",
" total_images += image_count\n",
"\n",
" print(f\"Total d'images trouvΓ©es: {total_images}\")\n",
" print(\"RΓ©partition par dossier:\")\n",
" for folder, count in structure_info.items():\n",
" print(f\" {folder}: {count} images\")\n",
"\n",
" return structure_info\n",
"\n",
"def preprocess_fingerprints_complete(input_folder=\"/content/drive/MyDrive/DATA\",\n",
" output_base_folder=\"/content/processed_fingerprints\",\n",
" target_size=(224, 224),\n",
" vgg16_compatible=True,\n",
" create_zip=True,\n",
" cleanup_temp=False):\n",
" \"\"\"\n",
" Fonction principale pour le prΓ©processing complet des empreintes digitales\n",
" PrΓ©serve la structure de dossiers originale\n",
" \"\"\"\n",
" print(\"=== PRΓPROCESSING DES EMPREINTES DIGITALES ===\")\n",
" print(f\"Dossier d'entrΓ©e: {input_folder}\")\n",
" print(f\"Dossier de sortie: {output_base_folder}\")\n",
" print(f\"Taille cible: {target_size}\")\n",
" print(f\"Compatible VGG16: {vgg16_compatible}\")\n",
"\n",
" if not os.path.exists(input_folder):\n",
" print(f\"Erreur: Le dossier {input_folder} n'existe pas\")\n",
" return False\n",
"\n",
" # Analyser la structure du dossier d'entrΓ©e\n",
" analyze_folder_structure(input_folder)\n",
"\n",
" os.makedirs(output_base_folder, exist_ok=True)\n",
" temp_folder = os.path.join(output_base_folder, \"processed_with_structure\")\n",
"\n",
" processed_files = process_folder_with_structure(input_folder, temp_folder,\n",
" target_size, vgg16_compatible)\n",
"\n",
" if not processed_files:\n",
" print(\"Aucune image traitée avec succès\")\n",
" return False\n",
"\n",
" print(f\"\\n=== STRUCTURE PRΓSERVΓE ===\")\n",
" print(f\"Les images traitΓ©es conservent la mΓͺme organisation que le dossier original\")\n",
"\n",
" if create_zip:\n",
" zip_filename = os.path.join(output_base_folder, \"fingerprints_processed_structured.zip\")\n",
" zip_success = create_zip_archive(temp_folder, zip_filename)\n",
"\n",
" if zip_success:\n",
" zip_size = os.path.getsize(zip_filename) / (1024 * 1024)\n",
" print(f\"Taille de l'archive: {zip_size:.2f} MB\")\n",
" if cleanup_temp:\n",
" shutil.rmtree(temp_folder)\n",
" print(\"Dossier temporaire supprimΓ©\")\n",
" print(f\"Archive avec structure prΓ©servΓ©e: {zip_filename}\")\n",
" return zip_filename\n",
" else:\n",
" print(f\"Images traitΓ©es avec structure prΓ©servΓ©e dans: {temp_folder}\")\n",
" return temp_folder\n",
"\n",
"def main():\n",
" \"\"\"\n",
" Fonction principale pour exΓ©cution dans Colab - Version avec structure prΓ©servΓ©e\n",
" \"\"\"\n",
" # Paramètres\n",
" output_folder = \"/content/processed_fingerprints_structured\"\n",
" target_size = (128, 128) # Modifiable selon les besoins\n",
"\n",
" # ExΓ©cuter le preprocessing avec prΓ©servation de structure\n",
" result = preprocess_fingerprints_complete(\n",
" input_folder=\"/content/drive/MyDrive/DATA\",\n",
" output_base_folder=output_folder,\n",
" target_size=target_size,\n",
" vgg16_compatible=True,\n",
" create_zip=True,\n",
" cleanup_temp=False # Garder les dossiers pour vΓ©rification\n",
" )\n",
"\n",
" if result:\n",
" print(f\"\\nβ
Préprocessing terminé avec succès!\")\n",
" print(f\"Structure prΓ©servΓ©e: {result}\")\n",
" print(\"\\nCaractΓ©ristiques des images:\")\n",
" print(\"- Format: PNG RGB (3 canaux)\")\n",
" print(\"- Fond: Blanc (après suppression)\")\n",
" print(\"- Contraste: AmΓ©liorΓ© par canal\")\n",
" print(\"- Structure: Identique au dossier original\")\n",
" print(\"\\nποΈ La hiΓ©rarchie de dossiers a Γ©tΓ© conservΓ©e intΓ©gralement\")\n",
" else:\n",
" print(\"\\nβ Γchec du prΓ©processing\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
]
}
]
} |