-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
236 lines (195 loc) · 6.97 KB
/
preprocess.py
File metadata and controls
236 lines (195 loc) · 6.97 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
from __future__ import annotations
import os
from dataclasses import dataclass
from typing import List, Tuple
import cv2
import numpy as np
@dataclass(slots=True)
class PreprocessDebugConfig:
"""Configuration for saving and visualising intermediate preprocessing steps."""
save: bool = False
prefix: str = "test_images/out/pre"
show: bool = False
delay_ms: int = 500
@property
def enabled(self) -> bool:
return self.save or self.show
@dataclass(slots=True)
class PreprocessConfig:
"""Tunable parameters for the preprocessing pipeline."""
min_width: int = 640
blur_kernel_size: Tuple[int, int] = (3, 3)
clahe_clip_limit: float = 2.0
clahe_grid_size: Tuple[int, int] = (8, 8)
tophat_kernel_size: Tuple[int, int] = (17, 3)
resize_interpolation: int = cv2.INTER_CUBIC
class Preprocessor:
"""Runs a deterministic sequence of OpenCV transforms tailored for licence plates."""
def __init__(self, config: PreprocessConfig | None = None) -> None:
self.config = config or PreprocessConfig()
self._clahe = cv2.createCLAHE(
clipLimit=self.config.clahe_clip_limit,
tileGridSize=self.config.clahe_grid_size,
)
self._tophat_kernel = cv2.getStructuringElement(
cv2.MORPH_RECT,
self.config.tophat_kernel_size,
)
def run(
self,
image: np.ndarray,
*,
debug: PreprocessDebugConfig | None = None,
) -> np.ndarray:
"""
Apply preprocessing to an image.
Parameters
----------
image:
Source image (RGB, RGBA, BGR, or grayscale).
debug:
Optional debugging configuration; when provided, intermediate steps are
persisted to disk or shown interactively.
"""
dbg = debug or resolve_debug_config()
frames: List[Tuple[str, np.ndarray]] = []
gray = _to_grayscale(image)
_record_frame(dbg, frames, "00_gray", gray)
blurred = cv2.GaussianBlur(gray, self.config.blur_kernel_size, 0)
_record_frame(dbg, frames, "01_blur", blurred)
equalised = self._clahe.apply(blurred)
_record_frame(dbg, frames, "02_clahe", equalised)
normalised = cv2.normalize(equalised, None, 0, 255, cv2.NORM_MINMAX)
_record_frame(dbg, frames, "03_norm", normalised)
high_pass = cv2.morphologyEx(normalised, cv2.MORPH_TOPHAT, self._tophat_kernel)
_record_frame(dbg, frames, "04_tophat", high_pass)
processed = _ensure_min_width(
high_pass,
min_width=self.config.min_width,
interpolation=self.config.resize_interpolation,
dbg=dbg,
frames=frames,
)
if dbg.show:
_show_sequence(frames, delay_ms=dbg.delay_ms)
return processed
def resolve_debug_config(
*,
save_debug: bool | None = None,
prefix: str = "test_images/out/pre",
show: bool = False,
delay_ms: int = 500,
) -> PreprocessDebugConfig:
"""Derive the debug configuration, honouring the AUTOSENTINEL_DEBUG env flag."""
env_debug = os.getenv("AUTOSENTINEL_DEBUG", "0") == "1"
save = (save_debug if save_debug is not None else False) or env_debug
return PreprocessDebugConfig(save=save, prefix=prefix, show=show or False, delay_ms=delay_ms)
def preprocess(
image: np.ndarray,
*,
save_debug: bool = False,
debug_prefix: str = "test_images/out/pre",
show: bool = False,
delay_ms: int = 500,
config: PreprocessConfig | None = None,
) -> np.ndarray:
"""Convenience functional wrapper around ``Preprocessor`` for backwards compatibility."""
debug_cfg = PreprocessDebugConfig(
save=save_debug or os.getenv("AUTOSENTINEL_DEBUG", "0") == "1",
prefix=debug_prefix,
show=show,
delay_ms=delay_ms,
)
return Preprocessor(config=config).run(image, debug=debug_cfg)
def _record_frame(
debug: PreprocessDebugConfig,
frames: List[Tuple[str, np.ndarray]],
name: str,
image: np.ndarray,
) -> None:
frames.append((name, image))
if debug.save:
_save_frame(debug.prefix, name, image)
def _save_frame(prefix: str, stage: str, image: np.ndarray) -> None:
out_dir = os.path.dirname(prefix)
if out_dir:
os.makedirs(out_dir, exist_ok=True)
ext = "png" if image.ndim == 2 else "jpg"
path = f"{prefix}_{stage}.{ext}"
to_write = image
if image.ndim == 3:
if image.shape[2] == 3:
to_write = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
elif image.shape[2] == 4:
to_write = cv2.cvtColor(image, cv2.COLOR_RGBA2BGRA)
cv2.imwrite(path, to_write)
def _ensure_min_width(
image: np.ndarray,
*,
min_width: int,
interpolation: int,
dbg: PreprocessDebugConfig,
frames: List[Tuple[str, np.ndarray]],
) -> np.ndarray:
height, width = image.shape[:2]
if width >= min_width:
_record_frame(dbg, frames, "05_keep", image)
return image
scale = min_width / float(width)
resized = cv2.resize(
image,
(int(width * scale), int(height * scale)),
interpolation=interpolation,
)
_record_frame(dbg, frames, "05_resize", resized)
return resized
def _show_sequence(frames: List[Tuple[str, np.ndarray]], *, delay_ms: int) -> None:
window_name = "AutoSentinel Preprocess"
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
for name, frame in frames:
vis = frame
if frame.ndim == 3 and frame.shape[2] == 3:
vis = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
elif frame.ndim == 3 and frame.shape[2] == 4:
vis = cv2.cvtColor(frame, cv2.COLOR_RGBA2BGRA)
vis = vis.copy()
cv2.putText(
vis,
name,
(12, 28),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
255 if frame.ndim == 2 else (255, 255, 255),
2,
cv2.LINE_AA,
)
cv2.imshow(window_name, vis)
if cv2.waitKey(delay_ms) & 0xFF == 27:
break
cv2.destroyWindow(window_name)
def _to_grayscale(image: np.ndarray) -> np.ndarray:
"""Convert input to single-channel grayscale."""
if image.ndim == 2:
return image.copy()
channels = image.shape[2]
if channels == 3:
# Assume RGB input produced by Pillow; fall back to BGR if needed.
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
if _is_low_contrast(gray):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return gray
if channels == 4:
gray = cv2.cvtColor(image, cv2.COLOR_RGBA2GRAY)
if _is_low_contrast(gray):
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
return gray
raise ValueError(f"Unsupported image shape {image.shape}")
def _is_low_contrast(image: np.ndarray, threshold: float = 15.0) -> bool:
return float(image.max() - image.min()) < threshold
__all__ = [
"PreprocessConfig",
"PreprocessDebugConfig",
"Preprocessor",
"preprocess",
"resolve_debug_config",
]