Abs6187 commited on
Commit
9a70891
Β·
verified Β·
1 Parent(s): 72729f3

Create model_comparison.py

Browse files
Files changed (1) hide show
  1. model_comparison.py +249 -0
model_comparison.py ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import pandas as pd
5
+ from advanced_ocr import AdvancedLicensePlateOCR, get_available_models
6
+ import os
7
+ from typing import List, Dict, Tuple
8
+
9
+ class OCRModelComparison:
10
+ def __init__(self):
11
+ self.ocr_system = AdvancedLicensePlateOCR()
12
+ self.results_cache = {}
13
+
14
+ def benchmark_single_image(self, image: Image.Image, model_keys: List[str]) -> Dict:
15
+ results = {
16
+ "image_size": image.size,
17
+ "models_tested": len(model_keys),
18
+ "results": []
19
+ }
20
+
21
+ for model_key in model_keys:
22
+ try:
23
+ start_time = time.time()
24
+
25
+ extraction_result = self.ocr_system.extract_text_with_model(
26
+ image, model_key, use_preprocessing=True
27
+ )
28
+
29
+ end_time = time.time()
30
+ processing_time = end_time - start_time
31
+
32
+ model_info = self.ocr_system.models[model_key]
33
+
34
+ result_entry = {
35
+ "model_key": model_key,
36
+ "model_name": model_info["name"],
37
+ "model_type": model_info["type"],
38
+ "processing_time": round(processing_time, 3),
39
+ "success": "error" not in extraction_result,
40
+ "best_result": extraction_result.get("best_result", "Error"),
41
+ "confidence": extraction_result.get("confidence", 0.0),
42
+ "extractions_count": len(extraction_result.get("extractions", [])),
43
+ "status": "βœ… Success" if "error" not in extraction_result else f"❌ {extraction_result.get('error', 'Unknown error')}"
44
+ }
45
+
46
+ results["results"].append(result_entry)
47
+
48
+ except Exception as e:
49
+ result_entry = {
50
+ "model_key": model_key,
51
+ "model_name": self.ocr_system.models.get(model_key, {}).get("name", "Unknown"),
52
+ "model_type": self.ocr_system.models.get(model_key, {}).get("type", "Unknown"),
53
+ "processing_time": 0.0,
54
+ "success": False,
55
+ "best_result": f"Exception: {str(e)}",
56
+ "confidence": 0.0,
57
+ "extractions_count": 0,
58
+ "status": f"❌ Exception: {str(e)}"
59
+ }
60
+ results["results"].append(result_entry)
61
+
62
+ return results
63
+
64
+ def create_comparison_table(self, benchmark_results: Dict) -> pd.DataFrame:
65
+ if not benchmark_results.get("results"):
66
+ return pd.DataFrame()
67
+
68
+ df_data = []
69
+ for result in benchmark_results["results"]:
70
+ df_data.append({
71
+ "Model": result["model_name"],
72
+ "Type": result["model_type"],
73
+ "Status": result["status"],
74
+ "Extracted Text": result["best_result"],
75
+ "Confidence": f"{result['confidence']:.2f}",
76
+ "Processing Time (s)": result["processing_time"],
77
+ "Variants Processed": result["extractions_count"]
78
+ })
79
+
80
+ return pd.DataFrame(df_data)
81
+
82
+ def get_best_model_recommendation(self, benchmark_results: Dict) -> str:
83
+ if not benchmark_results.get("results"):
84
+ return "No results available"
85
+
86
+ successful_results = [r for r in benchmark_results["results"] if r["success"]]
87
+
88
+ if not successful_results:
89
+ return "❌ No models succeeded in text extraction"
90
+
91
+ best_by_confidence = max(successful_results, key=lambda x: x["confidence"])
92
+ fastest = min(successful_results, key=lambda x: x["processing_time"])
93
+
94
+ recommendation = f"""
95
+ πŸ† **Best Results:**
96
+
97
+ **Highest Confidence:** {best_by_confidence['model_name']}
98
+ - Text: "{best_by_confidence['best_result']}"
99
+ - Confidence: {best_by_confidence['confidence']:.2f}
100
+ - Time: {best_by_confidence['processing_time']:.3f}s
101
+
102
+ **Fastest Processing:** {fastest['model_name']}
103
+ - Text: "{fastest['best_result']}"
104
+ - Time: {fastest['processing_time']:.3f}s
105
+ - Confidence: {fastest['confidence']:.2f}
106
+
107
+ **Recommendation:**
108
+ {"Use " + best_by_confidence['model_name'] + " for best accuracy" if best_by_confidence != fastest else "Best overall: " + best_by_confidence['model_name']}
109
+ """
110
+ return recommendation
111
+
112
+ def compare_ocr_models(image, selected_models):
113
+ if image is None:
114
+ return "Please upload an image", pd.DataFrame(), "No comparison performed"
115
+
116
+ if not selected_models:
117
+ return "Please select at least one model", pd.DataFrame(), "No models selected"
118
+
119
+ try:
120
+ comparator = OCRModelComparison()
121
+
122
+ if isinstance(image, str):
123
+ image = Image.open(image)
124
+
125
+ benchmark_results = comparator.benchmark_single_image(image, selected_models)
126
+ comparison_table = comparator.create_comparison_table(benchmark_results)
127
+ recommendation = comparator.get_best_model_recommendation(benchmark_results)
128
+
129
+ status_msg = f"βœ… Comparison completed! Tested {len(selected_models)} models on image size {benchmark_results['image_size']}"
130
+
131
+ return status_msg, comparison_table, recommendation
132
+
133
+ except Exception as e:
134
+ error_msg = f"❌ Error during comparison: {str(e)}"
135
+ return error_msg, pd.DataFrame(), "Comparison failed"
136
+
137
+ def create_model_comparison_app():
138
+ models = get_available_models()
139
+ model_choices = [(info["name"], key) for key, info in models.items()]
140
+
141
+ css = """
142
+ .model-comparison {
143
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
144
+ color: white;
145
+ padding: 20px;
146
+ border-radius: 10px;
147
+ text-align: center;
148
+ }
149
+ .recommendation-box {
150
+ background-color: #f8f9fa;
151
+ border: 2px solid #28a745;
152
+ border-radius: 8px;
153
+ padding: 15px;
154
+ margin: 10px 0;
155
+ }
156
+ """
157
+
158
+ with gr.Blocks(css=css, title="OCR Model Comparison Tool") as demo:
159
+ gr.HTML("""
160
+ <div class="model-comparison">
161
+ <h1>πŸ” License Plate OCR Model Comparison</h1>
162
+ <p>Compare different OCR models on your license plate images</p>
163
+ </div>
164
+ """)
165
+
166
+ with gr.Row():
167
+ with gr.Column(scale=1):
168
+ gr.Markdown("### Input")
169
+
170
+ input_image = gr.Image(
171
+ type="filepath",
172
+ label="Upload License Plate Image",
173
+ sources=["upload", "webcam"]
174
+ )
175
+
176
+ model_selector = gr.CheckboxGroup(
177
+ choices=model_choices,
178
+ value=["trocr_license", "easyocr"],
179
+ label="Select Models to Compare",
180
+ info="Choose which models to test (recommended: start with 2-3 models)"
181
+ )
182
+
183
+ compare_btn = gr.Button("πŸš€ Compare Models", variant="primary", size="lg")
184
+
185
+ gr.Markdown("### Available Models")
186
+ model_info_text = ""
187
+ for key, info in models.items():
188
+ model_info_text += f"**{info['name']}** ({info['type']})\n{info['description']}\n\n"
189
+
190
+ gr.Markdown(model_info_text)
191
+
192
+ with gr.Column(scale=2):
193
+ gr.Markdown("### Comparison Results")
194
+
195
+ status_output = gr.Textbox(
196
+ label="Status",
197
+ placeholder="Upload an image and select models to compare...",
198
+ interactive=False
199
+ )
200
+
201
+ comparison_table = gr.Dataframe(
202
+ label="Detailed Comparison",
203
+ headers=["Model", "Type", "Status", "Extracted Text", "Confidence", "Processing Time (s)", "Variants Processed"],
204
+ interactive=False
205
+ )
206
+
207
+ with gr.Group(elem_classes="recommendation-box"):
208
+ recommendation_output = gr.Markdown(
209
+ value="### 🎯 Recommendations will appear here after comparison",
210
+ label="Model Recommendation"
211
+ )
212
+
213
+ gr.Markdown("### Quick Start Guide")
214
+ gr.Markdown("""
215
+ 1. **Upload** a license plate image
216
+ 2. **Select** 2-3 models to compare (recommended combinations):
217
+ - `TrOCR License Plates + EasyOCR` (accuracy vs speed)
218
+ - `All TrOCR models` (compare TrOCR variants)
219
+ - `DETR + YOLO + EasyOCR` (different approaches)
220
+ 3. **Click Compare** and wait for results
221
+ 4. **Review** the recommendation for your use case
222
+
223
+ **Model Types:**
224
+ - **Transformers**: Modern AI models (TrOCR) - high accuracy, slower
225
+ - **Traditional**: Classic OCR (EasyOCR) - fast, reliable baseline
226
+ - **Object Detection**: End-to-end systems (DETR, YOLO) - detect + recognize
227
+ """)
228
+
229
+ compare_btn.click(
230
+ fn=compare_ocr_models,
231
+ inputs=[input_image, model_selector],
232
+ outputs=[status_output, comparison_table, recommendation_output]
233
+ )
234
+
235
+ gr.Examples(
236
+ examples=[
237
+ [["sample_1.jpg"], ["trocr_license", "easyocr"]],
238
+ [["sample_2.jpg"], ["trocr_license", "trocr_base", "easyocr"]]
239
+ ],
240
+ inputs=[input_image, model_selector],
241
+ outputs=[status_output, comparison_table, recommendation_output],
242
+ fn=compare_ocr_models
243
+ )
244
+
245
+ return demo
246
+
247
+ if __name__ == "__main__":
248
+ demo = create_model_comparison_app()
249
+ demo.launch(debug=True, share=True)