-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdamaged_poster_detector.py
More file actions
1151 lines (885 loc) · 42.5 KB
/
damaged_poster_detector.py
File metadata and controls
1151 lines (885 loc) · 42.5 KB
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Damaged Poster Detection System
==============================
A production-ready system for detecting various types of damage in posters
using computer vision and deep learning techniques.
Author: AI Assistant
Date: June 2025
"""
import os
import sys
import logging
from pathlib import Path
from typing import Dict, List, Tuple, Optional, Union
import warnings
import time
import psutil
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import multiprocessing
from functools import lru_cache
import cv2
import numpy as np
from PIL import Image, ImageEnhance, ImageFilter, ExifTags
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights
import albumentations as A
from albumentations.pytorch import ToTensorV2
import yaml
import json
from dataclasses import dataclass
from datetime import datetime
import redis
from prometheus_client import Counter, Histogram, Gauge, start_http_server
# Advanced image processing imports
from skimage import morphology, segmentation, filters, measure, feature
from skimage.restoration import denoise_bilateral, denoise_wavelet
from scipy import ndimage
from scipy.spatial.distance import pdist, squareform
import matplotlib.pyplot as plt
import seaborn as sns
# Suppress warnings for cleaner output
warnings.filterwarnings("ignore")
# Setup comprehensive logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('damage_detector.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Production metrics
DETECTION_COUNTER = Counter('poster_detections_total', 'Total poster detections')
DETECTION_LATENCY = Histogram('poster_detection_duration_seconds', 'Detection duration')
DAMAGE_TYPE_COUNTER = Counter('damage_types_detected_total', 'Damage types detected', ['damage_type'])
ERROR_COUNTER = Counter('detection_errors_total', 'Detection errors', ['error_type'])
SYSTEM_MEMORY_GAUGE = Gauge('system_memory_usage_bytes', 'System memory usage')
@dataclass
class DamageConfig:
"""Enhanced configuration for damage detection parameters"""
# Image preprocessing
target_size: Tuple[int, int] = (512, 512)
contrast_threshold: float = 0.15
brightness_threshold: float = 0.2
# Advanced corner case handling
max_image_size: int = 4096 # Maximum image dimension
min_image_size: int = 64 # Minimum image dimension
supported_formats: List[str] = None
enable_exif_rotation: bool = True
denoise_strength: float = 0.1
# Production settings
enable_caching: bool = True
cache_ttl: int = 3600 # Cache time-to-live in seconds
max_workers: int = None # Auto-detect based on CPU cores
memory_threshold: float = 0.85 # Memory usage threshold
timeout_seconds: int = 30 # Processing timeout
# Enhanced damage detection thresholds
tear_morphology_kernel: int = 5
crease_line_threshold: float = 0.3
stain_color_deviation: float = 30
fade_brightness_variance: float = 0.25
burn_texture_threshold: float = 0.4
water_blob_area_threshold: int = 100
# New damage types
fold_detection_enabled: bool = True
scratch_detection_enabled: bool = True
discoloration_detection_enabled: bool = True
perforation_detection_enabled: bool = True
def __post_init__(self):
if self.supported_formats is None:
self.supported_formats = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp']
if self.max_workers is None:
self.max_workers = min(multiprocessing.cpu_count(), 4)
class AdvancedImageProcessor:
"""Advanced image preprocessing with corner case handling"""
def __init__(self, config: DamageConfig):
self.config = config
self.redis_client = None
if config.enable_caching:
try:
self.redis_client = redis.Redis(host='localhost', port=6379, db=0)
self.redis_client.ping()
except:
logger.warning("Redis not available, caching disabled")
self.redis_client = None
@lru_cache(maxsize=128)
def get_cached_result(self, image_hash: str) -> Optional[Dict]:
"""Get cached detection result"""
if not self.redis_client:
return None
try:
cached = self.redis_client.get(f"detection:{image_hash}")
if cached:
return json.loads(cached)
except Exception as e:
logger.warning(f"Cache retrieval error: {e}")
return None
def cache_result(self, image_hash: str, result: Dict):
"""Cache detection result"""
if not self.redis_client:
return
try:
self.redis_client.setex(
f"detection:{image_hash}",
self.config.cache_ttl,
json.dumps(result, default=str)
)
except Exception as e:
logger.warning(f"Cache storage error: {e}")
def handle_exif_rotation(self, image: Image.Image) -> Image.Image:
"""Handle EXIF rotation data"""
if not self.config.enable_exif_rotation:
return image
try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
break
exif = dict(image._getexif().items()) if image._getexif() else {}
if orientation in exif:
if exif[orientation] == 3:
image = image.rotate(180, expand=True)
elif exif[orientation] == 6:
image = image.rotate(270, expand=True)
elif exif[orientation] == 8:
image = image.rotate(90, expand=True)
except (AttributeError, KeyError, TypeError):
pass # No EXIF data or unsupported format
return image
def validate_image(self, image_path: Union[str, Path]) -> Tuple[bool, str]:
"""Comprehensive image validation"""
try:
path = Path(image_path)
# Check file existence
if not path.exists():
return False, "File does not exist"
# Check file size
file_size = path.stat().st_size
if file_size == 0:
return False, "Empty file"
# Check file extension
if path.suffix.lower() not in self.config.supported_formats:
return False, f"Unsupported format: {path.suffix}"
# Try to open and validate image
with Image.open(path) as img:
# Check image dimensions
width, height = img.size
max_dim = max(width, height)
min_dim = min(width, height)
if max_dim > self.config.max_image_size:
return False, f"Image too large: {max_dim}px > {self.config.max_image_size}px"
if min_dim < self.config.min_image_size:
return False, f"Image too small: {min_dim}px < {self.config.min_image_size}px"
# Check if image is corrupted
img.verify()
return True, "Valid"
except Exception as e:
return False, f"Validation error: {str(e)}"
def smart_resize(self, image: np.ndarray) -> np.ndarray:
"""Intelligent resizing preserving aspect ratio and quality"""
h, w = image.shape[:2]
target_h, target_w = self.config.target_size
# Calculate aspect ratio preserving dimensions
aspect_ratio = w / h
target_aspect = target_w / target_h
if aspect_ratio > target_aspect:
# Image is wider
new_w = target_w
new_h = int(target_w / aspect_ratio)
else:
# Image is taller
new_h = target_h
new_w = int(target_h * aspect_ratio)
# Resize with high quality interpolation
resized = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_LANCZOS4)
# Pad to target size if necessary
if new_h < target_h or new_w < target_w:
top = (target_h - new_h) // 2
bottom = target_h - new_h - top
left = (target_w - new_w) // 2
right = target_w - new_w - left
resized = cv2.copyMakeBorder(
resized, top, bottom, left, right,
cv2.BORDER_CONSTANT, value=[0, 0, 0]
)
return resized
def advanced_noise_removal(self, image: np.ndarray) -> np.ndarray:
"""Advanced noise removal for better damage detection"""
# Bilateral filter for noise reduction while preserving edges
denoised = cv2.bilateralFilter(image, 9, 75, 75)
# Wavelet denoising for texture preservation
if len(image.shape) == 3:
# Color image
for i in range(3):
denoised[:, :, i] = denoise_wavelet(
denoised[:, :, i],
method='BayesShrink',
mode='soft',
rescale_sigma=True
)
else:
# Grayscale image
denoised = denoise_wavelet(
denoised,
method='BayesShrink',
mode='soft',
rescale_sigma=True
)
return (denoised * 255).astype(np.uint8) if denoised.dtype != np.uint8 else denoised
class FeatureExtractor:
"""Extract various features for damage detection"""
def __init__(self, config: DamageConfig):
self.config = config
def extract_texture_features(self, image: np.ndarray) -> Dict[str, float]:
"""Extract texture-based features for damage detection"""
# Gray Level Co-occurrence Matrix (GLCM) features
from skimage.feature import graycomatrix, graycoprops
# Ensure image is uint8
if image.dtype != np.uint8:
image = (image * 255).astype(np.uint8)
# Compute GLCM
glcm = graycomatrix(
image,
distances=[1, 2, 3],
angles=[0, np.pi/4, np.pi/2, 3*np.pi/4],
levels=256,
symmetric=True,
normed=True
)
# Extract GLCM properties
features = {
'contrast': np.mean(graycoprops(glcm, 'contrast')),
'dissimilarity': np.mean(graycoprops(glcm, 'dissimilarity')),
'homogeneity': np.mean(graycoprops(glcm, 'homogeneity')),
'energy': np.mean(graycoprops(glcm, 'energy')),
'correlation': np.mean(graycoprops(glcm, 'correlation')),
'asm': np.mean(graycoprops(glcm, 'ASM'))
}
return features
def extract_color_features(self, image: np.ndarray) -> Dict[str, float]:
"""Extract color-based features"""
# Convert to different color spaces
lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
features = {
'mean_r': np.mean(image[:, :, 0]),
'mean_g': np.mean(image[:, :, 1]),
'mean_b': np.mean(image[:, :, 2]),
'std_r': np.std(image[:, :, 0]),
'std_g': np.std(image[:, :, 1]),
'std_b': np.std(image[:, :, 2]),
'mean_l': np.mean(lab[:, :, 0]),
'mean_a': np.mean(lab[:, :, 1]),
'mean_b_lab': np.mean(lab[:, :, 2]),
'mean_h': np.mean(hsv[:, :, 0]),
'mean_s': np.mean(hsv[:, :, 1]),
'mean_v': np.mean(hsv[:, :, 2]),
'color_variance': np.var(image),
}
return features
def extract_edge_features(self, edges: np.ndarray) -> Dict[str, float]:
"""Extract edge-based features"""
# Edge density
edge_density = np.sum(edges > 0) / edges.size
# Hough lines for detecting tears/creases
lines = cv2.HoughLinesP(
edges, 1, np.pi/180, threshold=50,
minLineLength=30, maxLineGap=10
)
line_count = len(lines) if lines is not None else 0
# Contour analysis
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
features = {
'edge_density': edge_density,
'line_count': line_count,
'contour_count': len(contours),
'total_contour_area': sum(cv2.contourArea(c) for c in contours),
'max_contour_area': max([cv2.contourArea(c) for c in contours]) if contours else 0
}
return features
def extract_all_features(self, image: np.ndarray) -> Dict[str, Dict[str, float]]:
"""Extract all features for comprehensive damage detection"""
# Convert to grayscale for texture analysis
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Extract features
texture_features = self.extract_texture_features(gray_image)
color_features = self.extract_color_features(image)
# Edge detection (Canny)
edges = cv2.Canny(gray_image, 50, 150)
edge_features = self.extract_edge_features(edges)
# Combine all features
all_features = {
'texture': texture_features,
'color': color_features,
'edges': edge_features
}
return all_features
class DamageClassifier(nn.Module):
"""Deep learning model for damage classification"""
def __init__(self, num_classes: int = 7): # 6 damage types + no damage
super(DamageClassifier, self).__init__()
# Use EfficientNet as backbone
self.backbone = efficientnet_b0(weights=EfficientNet_B0_Weights.IMAGENET1K_V1)
# Replace classifier
self.backbone.classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(1280, 512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(256, num_classes)
)
# Add attention mechanism
self.attention = nn.MultiheadAttention(embed_dim=1280, num_heads=8)
def forward(self, x):
# Extract features
features = self.backbone.features(x)
# Global average pooling
features = self.backbone.avgpool(features)
features = torch.flatten(features, 1)
# Apply attention (simplified)
attended_features, _ = self.attention(
features.unsqueeze(0),
features.unsqueeze(0),
features.unsqueeze(0)
)
attended_features = attended_features.squeeze(0)
# Classification
output = self.backbone.classifier(attended_features)
return output
class TraditionalCVDetector:
"""Traditional computer vision-based damage detection"""
def __init__(self, config: DamageConfig):
self.config = config
def detect_tears(self, edges: np.ndarray, gray: np.ndarray) -> float:
"""Detect tears using edge analysis and morphological operations"""
# Apply morphological operations to enhance tear-like structures
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 15))
vertical_tears = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 3))
horizontal_tears = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
# Combine vertical and horizontal tear detections
tears = cv2.bitwise_or(vertical_tears, horizontal_tears)
# Calculate tear score based on tear area and intensity
tear_area = np.sum(tears > 0) / tears.size
# Use Hough lines to detect straight tear lines
lines = cv2.HoughLinesP(
tears, 1, np.pi/180, threshold=30,
minLineLength=50, maxLineGap=5
)
line_score = len(lines) / 100.0 if lines is not None else 0
return min(tear_area * 10 + line_score, 1.0)
def _detect_creases(self, gray: np.ndarray) -> float:
"""Detect creases using directional filters and ridge detection"""
# Apply Gaussian derivative filters
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
# Calculate gradient magnitude and direction
magnitude = np.sqrt(sobel_x**2 + sobel_y**2)
# Detect ridges (creases appear as ridges in gradient)
from skimage.filters import frangi
ridges = frangi(gray, sigmas=range(1, 4, 1), beta1=0.5, beta2=15)
# Combine gradient and ridge information
crease_map = magnitude * ridges
# Calculate crease score
crease_score = np.mean(crease_map) / 255.0
return min(crease_score * 5, 1.0)
def _detect_stains(self, lab: np.ndarray, original: np.ndarray) -> float:
"""Detect stains using color deviation analysis"""
# Analyze color uniformity in LAB space
l_channel = lab[:, :, 0]
a_channel = lab[:, :, 1]
b_channel = lab[:, :, 2]
# Calculate local standard deviation
from scipy.ndimage import uniform_filter
kernel_size = 15
l_mean = uniform_filter(l_channel.astype(float), size=kernel_size)
l_sqr_mean = uniform_filter(l_channel.astype(float)**2, size=kernel_size)
l_std = np.sqrt(l_sqr_mean - l_mean**2)
# Detect areas with high color variation (potential stains)
stain_mask = l_std > np.percentile(l_std, 85)
# Analyze color clustering
from sklearn.cluster import KMeans
pixels = original.reshape(-1, 3)
kmeans = KMeans(n_clusters=5, random_state=42, n_init=10)
labels = kmeans.fit_predict(pixels)
# Calculate color dispersion
color_dispersion = np.std([np.std(pixels[labels == i], axis=0) for i in range(5)])
stain_area = np.sum(stain_mask) / stain_mask.size
stain_score = stain_area * 3 + color_dispersion / 100
return min(stain_score, 1.0)
def _detect_fading(self, original: np.ndarray, gray: np.ndarray) -> float:
"""Detect fading using brightness and contrast analysis"""
# Calculate global brightness
brightness = np.mean(gray) / 255.0
# Calculate local contrast
contrast = np.std(gray) / 255.0
# Calculate color saturation
hsv = cv2.cvtColor(original, cv2.COLOR_RGB2HSV)
saturation = np.mean(hsv[:, :, 1]) / 255.0
# Fading is characterized by low contrast and low saturation
fade_score = (1 - contrast) * 0.5 + (1 - saturation) * 0.5
# Adjust for extreme brightness (overexposed fading)
if brightness > 0.8:
fade_score += 0.2
return min(fade_score, 1.0)
def _detect_burns(self, original: np.ndarray, gray: np.ndarray) -> float:
"""Detect burn damage using color and texture analysis"""
# Convert to HSV for better burn detection
hsv = cv2.cvtColor(original, cv2.COLOR_RGB2HSV)
# Detect brown/black regions (typical burn colors)
lower_brown = np.array([5, 50, 20])
upper_brown = np.array([20, 255, 150])
brown_mask = cv2.inRange(hsv, lower_brown, upper_brown)
lower_black = np.array([0, 0, 0])
upper_black = np.array([180, 255, 50])
black_mask = cv2.inRange(hsv, lower_black, upper_black)
burn_mask = cv2.bitwise_or(brown_mask, black_mask)
# Analyze texture irregularity in potential burn areas
from skimage.feature import local_binary_pattern
lbp = local_binary_pattern(gray, P=8, R=1, method='uniform')
texture_variance = np.var(lbp[burn_mask > 0]) if np.sum(burn_mask) > 0 else 0
burn_area = np.sum(burn_mask > 0) / burn_mask.size
burn_score = burn_area * 5 + texture_variance / 1000
return min(burn_score, 1.0)
def _detect_water_damage(self, lab: np.ndarray, gray: np.ndarray) -> float:
"""Detect water damage using blob analysis and texture changes"""
# Water damage often creates irregular blob-like patterns
from skimage.feature import blob_doh
blobs = blob_doh(gray, min_sigma=10, max_sigma=50, threshold=0.1)
# Analyze brightness variations (water damage creates uneven drying patterns)
from scipy.ndimage import gaussian_filter
smoothed = gaussian_filter(gray.astype(float), sigma=5)
brightness_variation = np.std(gray - smoothed)
# Calculate water damage score
blob_score = len(blobs) / 100.0
variation_score = brightness_variation / 50.0
water_score = blob_score * 0.6 + variation_score * 0.4
return min(water_score, 1.0)
def detect_all_damage_types(self, image: np.ndarray) -> Dict[str, Dict[str, float]]:
"""Detect all damage types using traditional CV methods"""
# Convert to grayscale for processing
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
results = {}
# 1. Tear Detection (using edge analysis and contours)
tear_score = self.detect_tears(cv2.Canny(gray_image, 50, 150), gray_image)
results['tear'] = {
'confidence': tear_score,
'detected': tear_score > self.config.tear_threshold
}
# 2. Crease Detection (using directional filters)
crease_score = self._detect_creases(gray_image)
results['crease'] = {
'confidence': crease_score,
'detected': crease_score > self.config.crease_threshold
}
# 3. Stain Detection (using color analysis)
stain_score = self._detect_stains(lab_image, image)
results['stain'] = {
'confidence': stain_score,
'detected': stain_score > self.config.stain_threshold
}
# 4. Fade Detection (using brightness and contrast analysis)
fade_score = self._detect_fading(image, gray_image)
results['fade'] = {
'confidence': fade_score,
'detected': fade_score > self.config.fade_threshold
}
# 5. Burn Detection (using color and texture analysis)
burn_score = self._detect_burns(image, gray_image)
results['burn'] = {
'confidence': burn_score,
'detected': burn_score > self.config.burn_threshold
}
# 6. Water Damage Detection (using blob analysis)
water_score = self._detect_water_damage(lab_image, gray_image)
results['water_damage'] = {
'confidence': water_score,
'detected': water_score > self.config.water_damage_threshold
}
return results
class DamagedPosterDetector:
"""Main class for detecting damaged posters"""
def __init__(self, config_path: Optional[str] = None):
"""
Initialize the damaged poster detector
Args:
config_path: Path to configuration file
"""
self.config = DamageConfig()
if config_path and os.path.exists(config_path):
self._load_config(config_path)
self.preprocessor = ImagePreprocessor(self.config)
self.feature_extractor = FeatureExtractor(self.config)
# Initialize models
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.damage_classifier = DamageClassifier().to(self.device)
# Damage types
self.damage_types = [
'no_damage', 'tear', 'crease', 'stain', 'fade', 'burn', 'water_damage'
]
logger.info(f"Initialized DamagedPosterDetector on {self.device}")
def _load_config(self, config_path: str):
"""Load configuration from YAML file"""
with open(config_path, 'r') as f:
config_dict = yaml.safe_load(f)
for key, value in config_dict.items():
if hasattr(self.config, key):
setattr(self.config, key, value)
def detect_damage(self, image_path: str) -> Dict:
"""
Detect damage in a poster image
Args:
image_path: Path to the poster image
Returns:
Dictionary containing damage detection results
"""
try:
# Load image
image = cv2.imread(image_path)
if image is None:
raise ValueError(f"Could not load image from {image_path}")
# Preprocess image
processed_images = self.preprocessor.preprocess_image(image)
# Extract features
texture_features = self.feature_extractor.extract_texture_features(
processed_images['gray']
)
color_features = self.feature_extractor.extract_color_features(
processed_images['original']
)
edge_features = self.feature_extractor.extract_edge_features(
processed_images['edges']
)
# Traditional CV-based damage detection
cv_results = self._detect_damage_traditional(processed_images)
# Deep learning-based damage detection
dl_results = self._detect_damage_deep_learning(processed_images['original'])
# Ensemble results
final_results = self._ensemble_results(cv_results, dl_results)
# Generate detailed report
report = self._generate_damage_report(
final_results, texture_features, color_features, edge_features, image_path
)
return report
except Exception as e:
logger.error(f"Error detecting damage in {image_path}: {str(e)}")
return {
'error': str(e),
'image_path': image_path,
'timestamp': datetime.now().isoformat()
}
def _detect_damage_traditional(self, processed_images: Dict[str, np.ndarray]) -> Dict:
"""Traditional computer vision-based damage detection"""
results = {}
gray = processed_images['gray']
original = processed_images['original']
edges = processed_images['edges']
lab = processed_images['lab']
# 1. Tear Detection (using edge analysis and contours)
tear_score = self._detect_tears(edges, gray)
results['tear'] = {
'confidence': tear_score,
'detected': tear_score > self.config.tear_threshold
}
# 2. Crease Detection (using directional filters)
crease_score = self._detect_creases(gray)
results['crease'] = {
'confidence': crease_score,
'detected': crease_score > self.config.crease_threshold
}
# 3. Stain Detection (using color analysis)
stain_score = self._detect_stains(lab, original)
results['stain'] = {
'confidence': stain_score,
'detected': stain_score > self.config.stain_threshold
}
# 4. Fade Detection (using brightness and contrast analysis)
fade_score = self._detect_fading(original, gray)
results['fade'] = {
'confidence': fade_score,
'detected': fade_score > self.config.fade_threshold
}
# 5. Burn Detection (using color and texture analysis)
burn_score = self._detect_burns(original, gray)
results['burn'] = {
'confidence': burn_score,
'detected': burn_score > self.config.burn_threshold
}
# 6. Water Damage Detection (using blob analysis)
water_score = self._detect_water_damage(lab, gray)
results['water_damage'] = {
'confidence': water_score,
'detected': water_score > self.config.water_damage_threshold
}
return results
def _detect_tears(self, edges: np.ndarray, gray: np.ndarray) -> float:
"""Detect tears using edge analysis and morphological operations"""
# Apply morphological operations to enhance tear-like structures
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 15))
vertical_tears = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 3))
horizontal_tears = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
# Combine vertical and horizontal tear detections
tears = cv2.bitwise_or(vertical_tears, horizontal_tears)
# Calculate tear score based on tear area and intensity
tear_area = np.sum(tears > 0) / tears.size
# Use Hough lines to detect straight tear lines
lines = cv2.HoughLinesP(
tears, 1, np.pi/180, threshold=30,
minLineLength=50, maxLineGap=5
)
line_score = len(lines) / 100.0 if lines is not None else 0
return min(tear_area * 10 + line_score, 1.0)
def _detect_creases(self, gray: np.ndarray) -> float:
"""Detect creases using directional filters and ridge detection"""
# Apply Gaussian derivative filters
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
# Calculate gradient magnitude and direction
magnitude = np.sqrt(sobel_x**2 + sobel_y**2)
# Detect ridges (creases appear as ridges in gradient)
from skimage.filters import frangi
ridges = frangi(gray, sigmas=range(1, 4, 1), beta1=0.5, beta2=15)
# Combine gradient and ridge information
crease_map = magnitude * ridges
# Calculate crease score
crease_score = np.mean(crease_map) / 255.0
return min(crease_score * 5, 1.0)
def _detect_stains(self, lab: np.ndarray, original: np.ndarray) -> float:
"""Detect stains using color deviation analysis"""
# Analyze color uniformity in LAB space
l_channel = lab[:, :, 0]
a_channel = lab[:, :, 1]
b_channel = lab[:, :, 2]
# Calculate local standard deviation
from scipy.ndimage import uniform_filter
kernel_size = 15
l_mean = uniform_filter(l_channel.astype(float), size=kernel_size)
l_sqr_mean = uniform_filter(l_channel.astype(float)**2, size=kernel_size)
l_std = np.sqrt(l_sqr_mean - l_mean**2)
# Detect areas with high color variation (potential stains)
stain_mask = l_std > np.percentile(l_std, 85)
# Analyze color clustering
from sklearn.cluster import KMeans
pixels = original.reshape(-1, 3)
kmeans = KMeans(n_clusters=5, random_state=42, n_init=10)
labels = kmeans.fit_predict(pixels)
# Calculate color dispersion
color_dispersion = np.std([np.std(pixels[labels == i], axis=0) for i in range(5)])
stain_area = np.sum(stain_mask) / stain_mask.size
stain_score = stain_area * 3 + color_dispersion / 100
return min(stain_score, 1.0)
def _detect_fading(self, original: np.ndarray, gray: np.ndarray) -> float:
"""Detect fading using brightness and contrast analysis"""
# Calculate global brightness
brightness = np.mean(gray) / 255.0
# Calculate local contrast
contrast = np.std(gray) / 255.0
# Calculate color saturation
hsv = cv2.cvtColor(original, cv2.COLOR_RGB2HSV)
saturation = np.mean(hsv[:, :, 1]) / 255.0
# Fading is characterized by low contrast and low saturation
fade_score = (1 - contrast) * 0.5 + (1 - saturation) * 0.5
# Adjust for extreme brightness (overexposed fading)
if brightness > 0.8:
fade_score += 0.2
return min(fade_score, 1.0)
def _detect_burns(self, original: np.ndarray, gray: np.ndarray) -> float:
"""Detect burn damage using color and texture analysis"""
# Convert to HSV for better burn detection
hsv = cv2.cvtColor(original, cv2.COLOR_RGB2HSV)
# Detect brown/black regions (typical burn colors)
lower_brown = np.array([5, 50, 20])
upper_brown = np.array([20, 255, 150])
brown_mask = cv2.inRange(hsv, lower_brown, upper_brown)
lower_black = np.array([0, 0, 0])
upper_black = np.array([180, 255, 50])
black_mask = cv2.inRange(hsv, lower_black, upper_black)
burn_mask = cv2.bitwise_or(brown_mask, black_mask)
# Analyze texture irregularity in potential burn areas
from skimage.feature import local_binary_pattern
lbp = local_binary_pattern(gray, P=8, R=1, method='uniform')
texture_variance = np.var(lbp[burn_mask > 0]) if np.sum(burn_mask) > 0 else 0
burn_area = np.sum(burn_mask > 0) / burn_mask.size
burn_score = burn_area * 5 + texture_variance / 1000
return min(burn_score, 1.0)
def _detect_water_damage(self, lab: np.ndarray, gray: np.ndarray) -> float:
"""Detect water damage using blob analysis and texture changes"""
# Water damage often creates irregular blob-like patterns
from skimage.feature import blob_doh
blobs = blob_doh(gray, min_sigma=10, max_sigma=50, threshold=0.1)
# Analyze brightness variations (water damage creates uneven drying patterns)
from scipy.ndimage import gaussian_filter
smoothed = gaussian_filter(gray.astype(float), sigma=5)
brightness_variation = np.std(gray - smoothed)
# Calculate water damage score
blob_score = len(blobs) / 100.0
variation_score = brightness_variation / 50.0
water_score = blob_score * 0.6 + variation_score * 0.4
return min(water_score, 1.0)
def detect_all_damage_types(self, image: np.ndarray) -> Dict[str, Dict[str, float]]:
"""Detect all damage types using traditional CV methods"""
# Convert to grayscale for processing
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
results = {}
# 1. Tear Detection (using edge analysis and contours)
tear_score = self.detect_tears(cv2.Canny(gray_image, 50, 150), gray_image)
results['tear'] = {
'confidence': tear_score,
'detected': tear_score > self.config.tear_threshold
}
# 2. Crease Detection (using directional filters)
crease_score = self._detect_creases(gray_image)
results['crease'] = {
'confidence': crease_score,
'detected': crease_score > self.config.crease_threshold
}
# 3. Stain Detection (using color analysis)
stain_score = self._detect_stains(lab_image, image)
results['stain'] = {
'confidence': stain_score,
'detected': stain_score > self.config.stain_threshold
}
# 4. Fade Detection (using brightness and contrast analysis)
fade_score = self._detect_fading(image, gray_image)
results['fade'] = {
'confidence': fade_score,
'detected': fade_score > self.config.fade_threshold
}
# 5. Burn Detection (using color and texture analysis)
burn_score = self._detect_burns(image, gray_image)
results['burn'] = {
'confidence': burn_score,
'detected': burn_score > self.config.burn_threshold
}
# 6. Water Damage Detection (using blob analysis)
water_score = self._detect_water_damage(lab_image, gray_image)
results['water_damage'] = {
'confidence': water_score,
'detected': water_score > self.config.water_damage_threshold
}
return results