forked from PatrickLib/captcha_recognize
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_usage.py
More file actions
170 lines (132 loc) · 5.34 KB
/
example_usage.py
File metadata and controls
170 lines (132 loc) · 5.34 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
#!/usr/bin/env python3
"""
Example usage of the refactored CAPTCHA Recognizer system.
"""
import sys
from pathlib import Path
# Add the current directory to Python path
sys.path.insert(0, str(Path(__file__).parent))
def example_basic_usage():
"""Demonstrate basic usage of the system."""
print("🔧 Basic Usage Example")
print("=" * 50)
try:
from captcha_recognizer import Config, CaptchaModel, create_model
# Create configuration
config = Config()
print(f"✓ Created configuration with image size: {config.model.input_shape}")
# Create model
model = create_model(config.model)
print(f"✓ Created model with {model.count_params()} parameters")
# Print model summary
print("\nModel Summary:")
model.summary()
return True
except Exception as e:
print(f"✗ Basic usage example failed: {e}")
return False
def example_custom_configuration():
"""Demonstrate custom configuration."""
print("\n🔧 Custom Configuration Example")
print("=" * 50)
try:
from captcha_recognizer import Config, ModelConfig, TrainingConfig
# Create custom configuration
custom_config = Config(
model=ModelConfig(
image_height=64,
image_width=256,
chars_num=6
),
training=TrainingConfig(
batch_size=64,
learning_rate=0.001,
epochs=150
)
)
print(f"✓ Custom configuration created:")
print(f" - Image size: {custom_config.model.input_shape}")
print(f" - Characters: {custom_config.model.chars_num}")
print(f" - Batch size: {custom_config.training.batch_size}")
print(f" - Learning rate: {custom_config.training.learning_rate}")
print(f" - Epochs: {custom_config.training.epochs}")
return True
except Exception as e:
print(f"✗ Custom configuration example failed: {e}")
return False
def example_data_loader():
"""Demonstrate data loader usage."""
print("\n🔧 Data Loader Example")
print("=" * 50)
try:
from captcha_recognizer import Config, CaptchaDataLoader
config = Config()
data_loader = CaptchaDataLoader(config.data, config.model)
print("✓ Data loader created successfully")
# Create synthetic dataset for demonstration
synthetic_dataset = data_loader.create_synthetic_dataset(5)
print(f"✓ Created synthetic dataset with 5 samples")
# Show dataset structure
for batch_idx, (images, labels) in enumerate(synthetic_dataset):
print(f" Batch {batch_idx + 1}:")
print(f" Images shape: {images.shape}")
print(f" Labels shape: {labels.shape}")
if batch_idx >= 1: # Just show first 2 batches
break
return True
except Exception as e:
print(f"✗ Data loader example failed: {e}")
return False
def example_environment_config():
"""Demonstrate environment variable configuration."""
print("\n🔧 Environment Configuration Example")
print("=" * 50)
try:
import os
from captcha_recognizer import Config
# Set environment variables
os.environ['CAPTCHA_BATCH_SIZE'] = '32'
os.environ['CAPTCHA_LEARNING_RATE'] = '0.0005'
os.environ['CAPTCHA_EPOCHS'] = '200'
# Create configuration from environment
config = Config.from_env()
print(f"✓ Configuration loaded from environment:")
print(f" - Batch size: {config.training.batch_size}")
print(f" - Learning rate: {config.training.learning_rate}")
print(f" - Epochs: {config.training.epochs}")
return True
except Exception as e:
print(f"✗ Environment configuration example failed: {e}")
return False
def main():
"""Run all examples."""
print("🚀 CAPTCHA Recognizer - Example Usage\n")
examples = [
("Basic Usage", example_basic_usage),
("Custom Configuration", example_custom_configuration),
("Data Loader", example_data_loader),
("Environment Configuration", example_environment_config),
]
passed = 0
total = len(examples)
for example_name, example_func in examples:
try:
if example_func():
passed += 1
else:
print(f"✗ {example_name} failed")
except Exception as e:
print(f"✗ {example_name} failed with exception: {e}")
print(f"\n{'='*60}")
print(f"Example Results: {passed}/{total} examples completed successfully")
if passed == total:
print("🎉 All examples completed successfully!")
print("\n💡 You can now use the system:")
print(" - Run training: python -m trainer")
print(" - Make predictions: python -m predictor --model_path ./models/model.h5")
print(" - Test the system: python test_basic.py")
else:
print("❌ Some examples failed. Please check the errors above.")
return 0 if passed == total else 1
if __name__ == "__main__":
sys.exit(main())