Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- modules/dataset_loader.py +36 -0
- modules/enhance.py +68 -0
- modules/metrics.py +40 -0
- modules/parallel.py +27 -0
modules/dataset_loader.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
def get_lol_dataset_samples(base_path="dataset/LOLdataset"):
|
| 6 |
+
"""
|
| 7 |
+
Crawls the LOL dataset directory for low-light and normal-light image pairs.
|
| 8 |
+
LOL dataset usually has 'our485/low' and 'our485/high' subfolders.
|
| 9 |
+
"""
|
| 10 |
+
low_path = os.path.join(base_path, "low")
|
| 11 |
+
high_path = os.path.join(base_path, "high")
|
| 12 |
+
|
| 13 |
+
if not os.path.exists(low_path):
|
| 14 |
+
return []
|
| 15 |
+
|
| 16 |
+
samples = []
|
| 17 |
+
for file_name in os.listdir(low_path):
|
| 18 |
+
if file_name.endswith(('.png', '.jpg', '.jpeg')):
|
| 19 |
+
samples.append({
|
| 20 |
+
"name": file_name,
|
| 21 |
+
"low": os.path.join(low_path, file_name),
|
| 22 |
+
"high": os.path.join(high_path, file_name)
|
| 23 |
+
})
|
| 24 |
+
return samples
|
| 25 |
+
|
| 26 |
+
def load_lol_pair(sample):
|
| 27 |
+
"""
|
| 28 |
+
Loads a pair of images from the LOL dataset.
|
| 29 |
+
"""
|
| 30 |
+
low_img = cv2.imread(sample['low'])
|
| 31 |
+
high_img = cv2.imread(sample['high'])
|
| 32 |
+
|
| 33 |
+
low_img = cv2.cvtColor(low_img, cv2.COLOR_BGR2RGB)
|
| 34 |
+
high_img = cv2.cvtColor(high_img, cv2.COLOR_BGR2RGB)
|
| 35 |
+
|
| 36 |
+
return low_img, high_img
|
modules/enhance.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def detect_brightness(img):
|
| 5 |
+
"""
|
| 6 |
+
Detects the average brightness of an image.
|
| 7 |
+
Returns: Average pixel value (0-255).
|
| 8 |
+
"""
|
| 9 |
+
if len(img.shape) == 3:
|
| 10 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 11 |
+
else:
|
| 12 |
+
gray = img
|
| 13 |
+
return np.mean(gray)
|
| 14 |
+
|
| 15 |
+
def apply_gamma_correction(img, gamma=1.0):
|
| 16 |
+
"""
|
| 17 |
+
Applies gamma correction to improve intensity.
|
| 18 |
+
"""
|
| 19 |
+
invGamma = 1.0 / gamma
|
| 20 |
+
table = np.array([((i / 255.0) ** invGamma) * 255
|
| 21 |
+
for i in np.arange(256)]).astype("uint8")
|
| 22 |
+
return cv2.LUT(img, table)
|
| 23 |
+
|
| 24 |
+
def apply_clahe(img, clip_limit=2.0, tile_grid_size=(8, 8)):
|
| 25 |
+
"""
|
| 26 |
+
Contrast Limited Adaptive Histogram Equalization.
|
| 27 |
+
"""
|
| 28 |
+
lab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
|
| 29 |
+
l, a, b = cv2.split(lab)
|
| 30 |
+
|
| 31 |
+
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
|
| 32 |
+
cl = clahe.apply(l)
|
| 33 |
+
|
| 34 |
+
limg = cv2.merge((cl, a, b))
|
| 35 |
+
return cv2.cvtColor(limg, cv2.COLOR_LAB2RGB)
|
| 36 |
+
|
| 37 |
+
def apply_denoising(img, h=10):
|
| 38 |
+
"""
|
| 39 |
+
Non-local Means Denoising.
|
| 40 |
+
"""
|
| 41 |
+
return cv2.fastNlMeansDenoisingColored(img, None, h, h, 7, 21)
|
| 42 |
+
|
| 43 |
+
def apply_sharpening(img):
|
| 44 |
+
"""
|
| 45 |
+
Laplacian-based sharpening.
|
| 46 |
+
"""
|
| 47 |
+
kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
|
| 48 |
+
return cv2.filter2D(img, -1, kernel)
|
| 49 |
+
|
| 50 |
+
def enhancement_pipeline(img, config):
|
| 51 |
+
"""
|
| 52 |
+
Full sequential enhancement pipeline.
|
| 53 |
+
"""
|
| 54 |
+
processed = img.copy()
|
| 55 |
+
|
| 56 |
+
if config.get('gamma_enable', True):
|
| 57 |
+
processed = apply_gamma_correction(processed, config.get('gamma', 1.5))
|
| 58 |
+
|
| 59 |
+
if config.get('clahe_enable', True):
|
| 60 |
+
processed = apply_clahe(processed, config.get('clahe_clip', 3.0))
|
| 61 |
+
|
| 62 |
+
if config.get('denoise_enable', True):
|
| 63 |
+
processed = apply_denoising(processed, config.get('denoise_h', 10))
|
| 64 |
+
|
| 65 |
+
if config.get('sharpen_enable', True):
|
| 66 |
+
processed = apply_sharpening(processed)
|
| 67 |
+
|
| 68 |
+
return processed
|
modules/metrics.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def calculate_execution_metrics(sequential_fn, parallel_fn, img, config):
|
| 5 |
+
"""
|
| 6 |
+
Compares timing and performance metrics.
|
| 7 |
+
"""
|
| 8 |
+
# Hot run to ensure cache/init doesn't skew small images (optional)
|
| 9 |
+
# sequential_fn(img, config)
|
| 10 |
+
|
| 11 |
+
# Sequential Test
|
| 12 |
+
start_seq = time.perf_counter()
|
| 13 |
+
res_seq = sequential_fn(img, config)
|
| 14 |
+
time_seq = time.perf_counter() - start_seq
|
| 15 |
+
|
| 16 |
+
# Parallel Test
|
| 17 |
+
start_par = time.perf_counter()
|
| 18 |
+
res_par = parallel_fn(img, config)
|
| 19 |
+
time_par = time.perf_counter() - start_par
|
| 20 |
+
|
| 21 |
+
speedup = time_seq / time_par if time_par > 0 else 1.0
|
| 22 |
+
|
| 23 |
+
return {
|
| 24 |
+
"sequential_time": time_seq,
|
| 25 |
+
"parallel_time": time_par,
|
| 26 |
+
"speedup": speedup,
|
| 27 |
+
"result": res_par # Use parallel result as final
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
def get_image_info(img):
|
| 31 |
+
"""
|
| 32 |
+
Returns resolution and channel info.
|
| 33 |
+
"""
|
| 34 |
+
h, w, c = img.shape
|
| 35 |
+
return {
|
| 36 |
+
"width": w,
|
| 37 |
+
"height": h,
|
| 38 |
+
"channels": c,
|
| 39 |
+
"megapixels": (w * h) / 1e6
|
| 40 |
+
}
|
modules/parallel.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from multiprocessing import Pool, cpu_count
|
| 3 |
+
from modules.enhance import enhancement_pipeline
|
| 4 |
+
|
| 5 |
+
def process_image_chunk(args):
|
| 6 |
+
"""
|
| 7 |
+
Worker function to process a horizontal slice of the image.
|
| 8 |
+
"""
|
| 9 |
+
chunk, config = args
|
| 10 |
+
return enhancement_pipeline(chunk, config)
|
| 11 |
+
|
| 12 |
+
def parallel_enhancement(img, config):
|
| 13 |
+
"""
|
| 14 |
+
Orchestrates parallel image processing using multiprocessing Pool.
|
| 15 |
+
"""
|
| 16 |
+
cores = max(1, cpu_count() - 1)
|
| 17 |
+
# Split image into horizontal chunks based on number of logical cores
|
| 18 |
+
chunks = np.array_split(img, cores, axis=0)
|
| 19 |
+
|
| 20 |
+
# Prepare arguments for each worker
|
| 21 |
+
worker_args = [(chunk, config) for chunk in chunks]
|
| 22 |
+
|
| 23 |
+
with Pool(processes=cores) as pool:
|
| 24 |
+
processed_chunks = pool.map(process_image_chunk, worker_args)
|
| 25 |
+
|
| 26 |
+
# Reassemble chunks
|
| 27 |
+
return np.vstack(processed_chunks)
|