Lucid / cctv_preprocessor.py
BigJuls's picture
Update: DVR system with AI ReID integration
cb233ac
Raw History Blame Contribute Delete
9.06 kB
"""
CCTV Image Preprocessor for Enhanced Performance on Surveillance Footage
"""
import cv2
import numpy as np
from PIL import Image, ImageEnhance
import torch
from torchvision import transforms
class CCTVImagePreprocessor:
"""Preprocessor for enhancing CCTV images before analysis"""
def __init__(self):
"""Initialize preprocessor with enhancement models"""
self.enhancement_models = self._load_enhancement_models()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def _load_enhancement_models(self):
"""Load enhancement models for CCTV preprocessing"""
# Placeholder for enhancement models
# In practice, this would load:
# - Super-resolution models for low-quality images
# - Denoising models for compressed footage
# - Lighting normalization models
return {
'super_resolution': None,
'denoising': None,
'lighting_normalization': None
}
def preprocess_cctv_image(self, image):
"""
Preprocess CCTV image for better feature extraction
Args:
image: Input CCTV image (numpy array or PIL Image)
Returns:
Preprocessed image
"""
# Convert to numpy array if needed
if isinstance(image, Image.Image):
image = np.array(image)
# Apply preprocessing pipeline
image = self._normalize_resolution(image)
image = self._normalize_lighting(image)
image = self._reduce_noise(image)
image = self._enhance_contrast(image)
image = self._correct_color_balance(image)
return image
def _normalize_resolution(self, image):
"""Normalize image resolution for consistent processing"""
height, width = image.shape[:2]
# If image is too small, upscale it
if height < 100 or width < 50:
# Calculate scaling factor to reach minimum size
scale_factor = max(100/height, 50/width)
new_height = int(height * scale_factor)
new_width = int(width * scale_factor)
image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
# If image is too large, downscale it for performance
elif height > 1080 or width > 1920:
# Calculate scaling factor to reach maximum size
scale_factor = min(1080/height, 1920/width)
new_height = int(height * scale_factor)
new_width = int(width * scale_factor)
image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
return image
def _normalize_lighting(self, image):
"""Normalize lighting conditions in CCTV images"""
# Convert to LAB color space
lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
l, a, b = cv2.split(lab)
# Apply CLAHE to L channel
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
l = clahe.apply(l)
# Merge channels and convert back to RGB
lab = cv2.merge((l, a, b))
image = cv2.cvtColor(lab, cv2.COLOR_LAB2RGB)
return image
def _reduce_noise(self, image):
"""Reduce noise in CCTV images"""
# Apply bilateral filter for noise reduction while preserving edges
image = cv2.bilateralFilter(image, 9, 75, 75)
return image
def _enhance_contrast(self, image):
"""Enhance contrast in CCTV images"""
# Convert to PIL for enhancement
pil_image = Image.fromarray(image)
# Enhance contrast
enhancer = ImageEnhance.Contrast(pil_image)
pil_image = enhancer.enhance(1.2) # Slight contrast enhancement
# Enhance sharpness
enhancer = ImageEnhance.Sharpness(pil_image)
pil_image = enhancer.enhance(1.1) # Slight sharpness enhancement
return np.array(pil_image)
def _correct_color_balance(self, image):
"""Correct color balance in CCTV images"""
# Simple white balance correction
result = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
avg_a = np.average(result[:, :, 1])
avg_b = np.average(result[:, :, 2])
result[:, :, 1] = result[:, :, 1] - ((avg_a - 128) * (result[:, :, 0] / 255.0) * 1.1)
result[:, :, 2] = result[:, :, 2] - ((avg_b - 128) * (result[:, :, 0] / 255.0) * 1.1)
result = cv2.cvtColor(result, cv2.COLOR_LAB2RGB)
return result
def _is_low_quality(self, image):
"""Check if image is low quality"""
height, width = image.shape[:2]
return height < 100 or width < 50 # Thresholds for CCTV images
def _enhance_resolution(self, image):
"""Apply super-resolution enhancement"""
# Placeholder implementation
# In practice, this would use a trained super-resolution model
if self._is_low_quality(image):
height, width = image.shape[:2]
# Simple upscaling for demonstration
scale_factor = max(2, min(4, 200 // max(height, width)))
image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_CUBIC)
return image
def handle_occlusions(self, image, face_bbox=None):
"""
Handle partial occlusions in face images
Args:
image: Input image
face_bbox: Bounding box of face (x1, y1, x2, y2)
Returns:
Image with occlusions handled
"""
if face_bbox is None:
return image
# Extract face region
x1, y1, x2, y2 = face_bbox
face_region = image[y1:y2, x1:x2]
# Apply inpainting to handle minor occlusions
# This is a simplified approach - in practice, more sophisticated methods would be used
face_region = self._apply_inpainting(face_region)
# Replace face region in original image
image[y1:y2, x1:x2] = face_region
return image
def _apply_inpainting(self, image):
"""Apply inpainting to handle occlusions"""
# Convert to grayscale for mask creation
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Create a simple mask for inpainting (in practice, this would be more sophisticated)
# For demonstration, we'll create a mask for very dark regions that might be occlusions
_, mask = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY_INV)
# Apply inpainting
if np.any(mask):
image = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA)
return image
def estimate_image_quality(self, image):
"""
Estimate the quality of a CCTV image
Returns:
dict: Quality metrics
"""
# Calculate various quality metrics
blur_score = self._calculate_blur_score(image)
brightness_score = self._calculate_brightness_score(image)
contrast_score = self._calculate_contrast_score(image)
# Overall quality score
quality_score = (blur_score + brightness_score + contrast_score) / 3.0
return {
'blur_score': blur_score,
'brightness_score': brightness_score,
'contrast_score': contrast_score,
'overall_quality': quality_score
}
def _calculate_blur_score(self, image):
"""Calculate blur score using Laplacian variance"""
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
laplacian_var = cv2.Laplacian(gray, cv2.CV_64F).var()
# Normalize to 0-1 range (higher variance = less blur)
return min(1.0, laplacian_var / 1000.0)
def _calculate_brightness_score(self, image):
"""Calculate brightness score"""
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
mean_brightness = np.mean(gray)
# Ideal brightness is around 128, score decreases as we move away
brightness_score = 1.0 - abs(mean_brightness - 128) / 128.0
return max(0.0, brightness_score)
def _calculate_contrast_score(self, image):
"""Calculate contrast score"""
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
contrast = np.std(gray) / 255.0 # Normalize to 0-1
return min(1.0, contrast * 2) # Scale for better discrimination
# Example usage
if __name__ == "__main__":
# Create preprocessor
preprocessor = CCTVImagePreprocessor()
# Create a sample CCTV-style image
sample_image = np.random.randint(0, 255, (150, 100, 3), dtype=np.uint8)
# Preprocess the image
processed_image = preprocessor.preprocess_cctv_image(sample_image)
print(f"Original image shape: {sample_image.shape}")
print(f"Processed image shape: {processed_image.shape}")
print("CCTV preprocessing completed successfully!")