forked from PatrickLib/captcha_recognize
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredictor.py
More file actions
317 lines (250 loc) · 10.2 KB
/
Copy pathpredictor.py
File metadata and controls
317 lines (250 loc) · 10.2 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
"""
Modern prediction module for CAPTCHA recognition.
"""
import tensorflow as tf
from tensorflow import keras
import numpy as np
from pathlib import Path
from typing import List, Tuple, Optional, Dict, Any
import logging
import argparse
import sys
from datetime import datetime
import json
from PIL import Image
from .config import Config, ModelConfig
from .model import load_model
from .data_loader import CaptchaDataLoader
logger = logging.getLogger(__name__)
class CaptchaPredictor:
"""
Predictor class for CAPTCHA recognition.
This class handles loading trained models and making predictions
on new CAPTCHA images.
"""
def __init__(self, config: Config, model_path: Optional[str] = None):
self.config = config
self.data_loader = CaptchaDataLoader(config.data, config.model)
self.model = None
if model_path:
self.load_model(model_path)
def load_model(self, model_path: str):
"""
Load a trained model from disk.
Args:
model_path: Path to the saved model
"""
try:
self.model = load_model(model_path, self.config.model)
logger.info(f"Model loaded successfully from {model_path}")
except Exception as e:
logger.error(f"Failed to load model: {e}")
raise
def preprocess_image(self, image_path: str) -> np.ndarray:
"""
Preprocess a single image for prediction.
Args:
image_path: Path to the image file
Returns:
Preprocessed image array
"""
try:
# Load image
image = Image.open(image_path)
image_gray = image.convert('L') # Convert to grayscale
image_resized = image_gray.resize(
(self.config.model.image_width, self.config.model.image_height)
)
image.close()
# Convert to numpy array and normalize
image_array = np.array(image_resized, dtype=np.float32)
image_array = (image_array / 255.0) - 0.5
image_array = np.expand_dims(image_array, axis=-1) # Add channel dimension
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
return image_array
except Exception as e:
logger.error(f"Failed to preprocess image {image_path}: {e}")
raise
def predict_single(self, image_path: str) -> Tuple[str, float]:
"""
Predict CAPTCHA text for a single image.
Args:
image_path: Path to the image file
Returns:
Tuple of (predicted_text, confidence_score)
"""
if self.model is None:
raise ValueError("Model not loaded. Call load_model() first.")
# Preprocess image
image_array = self.preprocess_image(image_path)
# Make prediction
predictions = self.model.predict(image_array, verbose=0)
# Process predictions
predicted_text = self._decode_predictions(predictions)
confidence = self._calculate_confidence(predictions)
return predicted_text, confidence
def predict_batch(self, image_dir: Optional[str] = None) -> List[Tuple[str, str, float]]:
"""
Predict CAPTCHA text for multiple images.
Args:
image_dir: Directory containing images (uses config default if None)
Returns:
List of tuples: (filename, predicted_text, confidence_score)
"""
if self.model is None:
raise ValueError("Model not loaded. Call load_model() first.")
# Load test images
images, filenames = self.data_loader.load_test_images(image_dir)
# Make predictions
predictions = self.model.predict(images, verbose=1)
# Process results
results = []
for i, filename in enumerate(filenames):
predicted_text = self._decode_predictions([pred[i] for pred in predictions])
confidence = self._calculate_confidence([pred[i] for pred in predictions])
results.append((filename, predicted_text, confidence))
return results
def _decode_predictions(self, predictions: List[np.ndarray]) -> str:
"""
Decode model predictions to text.
Args:
predictions: List of prediction arrays for each character position
Returns:
Decoded text string
"""
decoded_text = ""
for char_pred in predictions:
# Get the character with highest probability
char_index = np.argmax(char_pred)
decoded_text += self.config.model.char_set[char_index]
return decoded_text
def _calculate_confidence(self, predictions: List[np.ndarray]) -> float:
"""
Calculate confidence score for predictions.
Args:
predictions: List of prediction arrays for each character position
Returns:
Average confidence score
"""
confidences = []
for char_pred in predictions:
# Get the probability of the predicted character
char_index = np.argmax(char_pred)
confidence = char_pred[char_index]
confidences.append(confidence)
return np.mean(confidences)
def evaluate_accuracy(self,
test_dir: str,
ground_truth: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
"""
Evaluate prediction accuracy on test data.
Args:
test_dir: Directory containing test images
ground_truth: Dictionary mapping filenames to true labels (optional)
Returns:
Dictionary containing evaluation metrics
"""
if self.model is None:
raise ValueError("Model not loaded. Call load_model() first.")
# Get predictions
results = self.predict_batch(test_dir)
# Calculate accuracy
total_count = len(results)
correct_count = 0
for filename, predicted_text, confidence in results:
if ground_truth and filename in ground_truth:
true_text = ground_truth[filename]
if predicted_text == true_text:
correct_count += 1
else:
# Try to extract true text from filename
# This assumes filename contains the true CAPTCHA text
filename_without_ext = Path(filename).stem
if predicted_text.lower() in filename_without_ext.lower():
correct_count += 1
accuracy = correct_count / total_count if total_count > 0 else 0.0
# Calculate average confidence
avg_confidence = np.mean([conf for _, _, conf in results])
evaluation_results = {
'total_samples': total_count,
'correct_predictions': correct_count,
'accuracy': accuracy,
'average_confidence': avg_confidence,
'predictions': results
}
logger.info(f"Evaluation results: {correct_count}/{total_count} correct ({accuracy:.3f})")
logger.info(f"Average confidence: {avg_confidence:.3f}")
return evaluation_results
def save_predictions(self,
results: List[Tuple[str, str, float]],
output_path: str):
"""
Save prediction results to a file.
Args:
results: List of prediction results
output_path: Path to save the results
"""
try:
with open(output_path, 'w') as f:
f.write("filename,predicted_text,confidence\n")
for filename, predicted_text, confidence in results:
f.write(f"{filename},{predicted_text},{confidence:.4f}\n")
logger.info(f"Predictions saved to {output_path}")
except Exception as e:
logger.error(f"Failed to save predictions: {e}")
raise
def main():
"""Main prediction function."""
parser = argparse.ArgumentParser(description='Predict CAPTCHA text from images')
parser.add_argument(
'--model_path',
type=str,
required=True,
help='Path to the trained model'
)
parser.add_argument(
'--image_dir',
type=str,
default='./data/test_data',
help='Directory containing test images'
)
parser.add_argument(
'--output_file',
type=str,
default='./predictions.csv',
help='Output file for predictions'
)
parser.add_argument(
'--single_image',
type=str,
default=None,
help='Path to single image for prediction (optional)'
)
args = parser.parse_args()
# Create configuration
config = Config()
# Create predictor
predictor = CaptchaPredictor(config, args.model_path)
try:
if args.single_image:
# Single image prediction
predicted_text, confidence = predictor.predict_single(args.single_image)
print(f"Image: {args.single_image}")
print(f"Predicted text: {predicted_text}")
print(f"Confidence: {confidence:.4f}")
else:
# Batch prediction
results = predictor.predict_batch(args.image_dir)
# Print results
for filename, predicted_text, confidence in results:
print(f"{filename}: {predicted_text} (confidence: {confidence:.4f})")
# Save results
predictor.save_predictions(results, args.output_file)
# Evaluate accuracy
evaluation = predictor.evaluate_accuracy(args.image_dir)
print(f"\nAccuracy: {evaluation['accuracy']:.3f}")
except Exception as e:
logger.error(f"Prediction failed: {e}")
sys.exit(1)
if __name__ == '__main__':
main()