-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_v1.py
More file actions
307 lines (242 loc) · 11 KB
/
script_v1.py
File metadata and controls
307 lines (242 loc) · 11 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
import os
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import json
import logging
import time
from datetime import datetime
import imghdr
class RobustQRCodeDetector:
def __init__(self, debug=False):
"""
Initialize the QR code detector with comprehensive tracking
Args:
debug (bool): Enable detailed logging and intermediate image saving
"""
self.debug = debug
self.logger = self._setup_logger()
self.scan_stats = {
'scan_timestamp': datetime.now().isoformat(),
'total_images_scanned': 0,
'images_with_qr_codes': 0,
'images_without_qr_codes': 0,
'total_qr_codes_detected': 0,
'preprocessing_methods_performance': {},
'total_scan_time': 0,
'average_image_processing_time': 0,
'detection_details': []
}
def _setup_logger(self):
"""
Set up logging for the detector
Returns:
logging.Logger: Configured logger
"""
logger = logging.getLogger('AdvancedQRCodeDetector')
logger.setLevel(logging.DEBUG if self.debug else logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG if self.debug else logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
def preprocess_image(self, image):
"""
Advanced image preprocessing techniques
Args:
image (numpy.ndarray): Input image
Returns:
list of tuple: (method_name, preprocessed_image)
"""
preprocessed_images = []
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image.copy()
preprocessing_methods = [
('original_grayscale', gray),
('enhanced_contrast', self._enhance_contrast(gray)),
('adaptive_threshold', self._adaptive_threshold(gray)),
('otsu_threshold', self._otsu_threshold(gray)),
('gaussian_blur', self._gaussian_blur(gray)),
('median_blur', self._median_blur(gray)),
('bilateral_filter', self._bilateral_filter(gray)),
('equalized_histogram', self._equalize_histogram(gray)),
('morphological_opening', self._morphological_opening(gray)),
('laplacian_edge_enhancement', self._laplacian_edge_enhancement(gray))
]
if self.debug:
self._save_debug_images(preprocessing_methods)
return preprocessing_methods
def _enhance_contrast(self, image):
"""Enhance image contrast using CLAHE"""
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
return clahe.apply(image)
def _adaptive_threshold(self, image):
"""Apply adaptive thresholding"""
return cv2.adaptiveThreshold(
image, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2
)
def _otsu_threshold(self, image):
"""Apply Otsu's thresholding"""
_, thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return thresh
def _gaussian_blur(self, image):
"""Apply Gaussian blur"""
return cv2.GaussianBlur(image, (5, 5), 0)
def _median_blur(self, image):
"""Apply median blur"""
return cv2.medianBlur(image, 5)
def _bilateral_filter(self, image):
"""Apply bilateral filtering"""
return cv2.bilateralFilter(image, 9, 75, 75)
def _equalize_histogram(self, image):
"""Equalize image histogram"""
return cv2.equalizeHist(image)
def _morphological_opening(self, image):
"""Apply morphological opening"""
kernel = np.ones((3,3), np.uint8)
return cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
def _laplacian_edge_enhancement(self, image):
"""Enhance edges using Laplacian"""
laplacian = cv2.Laplacian(image, cv2.CV_64F)
return cv2.convertScaleAbs(laplacian)
def _save_debug_images(self, preprocessed_images):
"""
Save preprocessed images for debugging
Args:
preprocessed_images (list): List of (method_name, image) tuples
"""
debug_dir = os.path.join(os.getcwd(), 'debug_preprocessed')
os.makedirs(debug_dir, exist_ok=True)
for method_name, img in preprocessed_images:
cv2.imwrite(os.path.join(debug_dir, f'preprocessed_{method_name}.png'), img)
def detect_qr_codes(self, image_path):
"""
Detect QR codes using multiple preprocessing techniques
Args:
image_path (str): Path to the input image
Returns:
tuple: (detection_results, processing_time)
"""
start_time = time.time()
self.scan_stats['total_images_scanned'] += 1
try:
if not self._validate_image(image_path):
self.logger.warning(f"Invalid image: {image_path}")
return [], 0
image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
if image is None:
self.logger.error(f"Cannot read image: {image_path}")
return [], 0
preprocessed_images = self.preprocess_image(image)
all_detected_qr_codes = []
method_detection_counts = {}
for method_name, preprocessed_img in preprocessed_images:
decoded_objects = decode(preprocessed_img)
method_detection_counts[method_name] = len(decoded_objects)
for obj in decoded_objects:
qr_data = {
'filename': os.path.basename(image_path),
'data': obj.data.decode('utf-8'),
'type': obj.type,
'detection_method': method_name,
'polygon': [list(p) for p in obj.polygon],
'rect': [obj.rect.left, obj.rect.top, obj.rect.width, obj.rect.height]
}
all_detected_qr_codes.append(qr_data)
unique_qr_codes = list({qr['data']: qr for qr in all_detected_qr_codes}.values())
processing_time = time.time() - start_time
self.scan_stats['total_scan_time'] += processing_time
if unique_qr_codes:
self.scan_stats['images_with_qr_codes'] += 1
self.scan_stats['total_qr_codes_detected'] += len(unique_qr_codes)
self.scan_stats['detection_details'].extend(unique_qr_codes)
else:
self.scan_stats['images_without_qr_codes'] += 1
self.scan_stats['preprocessing_methods_performance'] = method_detection_counts
return unique_qr_codes, processing_time
except Exception as e:
self.logger.error(f"Error processing {image_path}: {e}")
return [], 0
def _validate_image(self, file_path):
"""
Validate if the file is a valid image
Args:
file_path (str): Path to the file
Returns:
bool: True if valid image, False otherwise
"""
try:
if not os.path.exists(file_path) or not os.path.isfile(file_path):
return False
image_type = imghdr.what(file_path)
return image_type is not None
except Exception:
return False
def save_detection_results(self, results, image_path):
"""
Save QR code detection results to a JSON file
Args:
results (list): QR code detection results
image_path (str): Path to the original image
"""
output_file = os.path.join(
os.getcwd(),
os.path.splitext(os.path.basename(image_path))[0] + "_qr_results.json"
)
existing_results = []
if os.path.exists(output_file):
with open(output_file, 'r') as f:
existing_results = json.load(f)
existing_results.extend(results)
with open(output_file, 'w') as f:
json.dump(existing_results, f, indent=4)
def save_scan_summary(self):
"""
Save comprehensive scan summary to a JSON file
"""
if self.scan_stats['total_images_scanned'] > 0:
self.scan_stats['detection_percentage'] = (
(self.scan_stats['images_with_qr_codes'] / self.scan_stats['total_images_scanned']) * 100
)
self.scan_stats['average_image_processing_time'] = (
self.scan_stats['total_scan_time'] / self.scan_stats['total_images_scanned']
)
summary_file = os.path.join(os.getcwd(), 'qr_scan_summary.json')
with open(summary_file, 'w') as f:
json.dump(self.scan_stats, f, indent=4)
print("\n--- QR Code Scan Summary ---")
print(f"Total Images Scanned: {self.scan_stats['total_images_scanned']}")
print(f"Images with QR Codes: {self.scan_stats['images_with_qr_codes']}")
print(f"Images without QR Codes: {self.scan_stats['images_without_qr_codes']}")
print(f"Total QR Codes Detected: {self.scan_stats['total_qr_codes_detected']}")
print(f"Detection Percentage: {self.scan_stats.get('detection_percentage', 0):.2f}%")
print(f"Total Scan Time: {self.scan_stats['total_scan_time']:.2f} seconds")
print(f"Average Image Processing Time: {self.scan_stats.get('average_image_processing_time', 0):.4f} seconds")
def process_images_in_directory(self, directory):
"""
Process all images in a given directory
Args:
directory (str): Path to the directory containing images
"""
supported_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.tiff')
for filename in os.listdir(directory):
if filename.lower().endswith(supported_extensions):
file_path = os.path.join(directory, filename)
self.logger.info(f"Processing image: {filename}")
qr_codes, processing_time = self.detect_qr_codes(file_path)
if qr_codes:
for qr in qr_codes:
print(f"QR Code Data: {qr['data']}")
self.save_detection_results(qr_codes, file_path)
def main():
detector = RobustQRCodeDetector(debug=False)
start_total_time = time.time()
current_directory = os.getcwd()
detector.process_images_in_directory(current_directory)
detector.save_scan_summary()
total_execution_time = time.time() - start_total_time
print(f"\nTotal Execution Time: {total_execution_time:.2f} seconds")
if __name__ == "__main__":
main()