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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0be0c56
 
 
c90b51c
0be0c56
 
 
c90b51c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c65498
c90b51c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/env python3
"""
Benchmark different OCR methods for play clock reading.

This script compares:
1. Tesseract (current method)
2. EasyOCR (deep learning based)
3. Template matching (custom digit templates)

Usage:
    python scripts/benchmark_ocr.py
"""

import logging
import sys
import time
from pathlib import Path
from typing import List, Tuple, Optional, Dict

import cv2
import numpy as np

from detection import DetectScoreBug

# Path reference for constants
PROJECT_ROOT = Path(__file__).parent.parent.parent

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)

# Constants
VIDEO_PATH = PROJECT_ROOT / "full_videos" / "OSU vs Tenn 12.21.24.mkv"
TEMPLATE_PATH = PROJECT_ROOT / "data" / "templates" / "scorebug_template_main.png"
CONFIG_PATH = PROJECT_ROOT / "data" / "config" / "play_clock_region.json"
DIGIT_TEMPLATES_DIR = PROJECT_ROOT / "data" / "templates" / "digits"

# Test segment - sample frames with known clock values (30 frames)
TEST_TIMESTAMPS = [2320.0 + i for i in range(30)]
# Expected values based on countdown pattern: 18->17->...->12->40->40->40->39->...
# This is approximate - the real test will use Tesseract as ground truth


def load_play_clock_config() -> Tuple[int, int, int, int]:
    """Load play clock region config."""
    import json

    with open(CONFIG_PATH, "r", encoding="utf-8") as f:
        data = json.load(f)
    return (data["x_offset"], data["y_offset"], data["width"], data["height"])


def extract_test_frames(video_path: Path, detector: DetectScoreBug, timestamps: List[float]) -> List[Tuple[float, np.ndarray, Tuple[int, int, int, int]]]:
    """Extract frames with scorebug for testing."""
    cap = cv2.VideoCapture(str(video_path))
    if not cap.isOpened():
        raise ValueError(f"Could not open video: {video_path}")

    fps = cap.get(cv2.CAP_PROP_FPS)
    frames = []

    for ts in timestamps:
        frame_number = int(ts * fps)
        cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
        ret, frame = cap.read()
        if not ret:
            continue

        detection = detector.detect(frame)
        if detection.detected and detection.bbox:
            frames.append((ts, frame, detection.bbox))

    cap.release()
    return frames


def extract_play_clock_region(frame: np.ndarray, scorebug_bbox: Tuple[int, int, int, int], config: Tuple[int, int, int, int]) -> np.ndarray:
    """Extract play clock region from frame."""
    sb_x, sb_y, _, _ = scorebug_bbox
    x_offset, y_offset, width, height = config

    pc_x = sb_x + x_offset
    pc_y = sb_y + y_offset

    return frame[pc_y : pc_y + height, pc_x : pc_x + width].copy()


def preprocess_for_ocr(region: np.ndarray) -> np.ndarray:
    """Standard preprocessing for OCR."""
    # Convert to grayscale
    gray = cv2.cvtColor(region, cv2.COLOR_BGR2GRAY)

    # Scale up
    scale_factor = 4
    scaled = cv2.resize(gray, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)

    # Otsu's threshold
    _, binary = cv2.threshold(scaled, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    # Invert if needed (dark text on light background)
    if np.mean(binary) < 128:
        binary = cv2.bitwise_not(binary)

    return binary


# ============================================================
# OCR Method 1: Tesseract (current baseline)
# ============================================================
def ocr_tesseract(region: np.ndarray) -> Tuple[Optional[int], float]:
    """Read digits using Tesseract."""
    import pytesseract

    preprocessed = preprocess_for_ocr(region)

    # Add padding
    padding = 10
    preprocessed = cv2.copyMakeBorder(preprocessed, padding, padding, padding, padding, cv2.BORDER_CONSTANT, value=255)

    config = "--psm 7 -c tessedit_char_whitelist=0123456789"

    try:
        data = pytesseract.image_to_data(preprocessed, config=config, output_type=pytesseract.Output.DICT)

        best_text = ""
        best_conf = 0.0

        for i, text in enumerate(data["text"]):
            conf = float(data["conf"][i])
            if conf > best_conf and text.strip():
                best_text = text.strip()
                best_conf = conf

        if best_text and best_text.isdigit():
            value = int(best_text)
            if 0 <= value <= 40:
                return value, best_conf / 100.0

    except Exception as e:
        logger.debug(f"Tesseract error: {e}")

    return None, 0.0


# ============================================================
# OCR Method 2: EasyOCR
# ============================================================
_easyocr_reader = None


def get_easyocr_reader():
    """Lazy-load EasyOCR reader."""
    global _easyocr_reader
    if _easyocr_reader is None:
        try:
            import easyocr

            _easyocr_reader = easyocr.Reader(["en"], gpu=False)  # CPU mode for fair comparison
            logger.info("EasyOCR reader initialized")
        except ImportError:
            logger.warning("EasyOCR not installed. Install with: pip install easyocr")
            return None
    return _easyocr_reader


def ocr_easyocr(region: np.ndarray) -> Tuple[Optional[int], float]:
    """Read digits using EasyOCR."""
    reader = get_easyocr_reader()
    if reader is None:
        return None, 0.0

    preprocessed = preprocess_for_ocr(region)

    try:
        # EasyOCR expects BGR or grayscale
        results = reader.readtext(preprocessed, allowlist="0123456789", detail=1)

        if results:
            # Get highest confidence result
            best_result = max(results, key=lambda x: x[2])
            text = best_result[1].strip()
            conf = best_result[2]

            if text.isdigit():
                value = int(text)
                if 0 <= value <= 40:
                    return value, conf

    except Exception as e:
        logger.debug(f"EasyOCR error: {e}")

    return None, 0.0


# ============================================================
# OCR Method 3: Template Matching for Digits
# ============================================================


class DigitTemplateMatcher:
    """Fast digit recognition using template matching."""

    def __init__(self):
        self.digit_templates: Dict[str, np.ndarray] = {}
        self._calibrated = False

    def calibrate_from_tesseract(self, regions: List[np.ndarray]) -> bool:
        """
        Calibrate digit templates using Tesseract as ground truth on first few frames.

        This extracts individual digit images from frames where Tesseract successfully reads values.
        """
        logger.info("Calibrating digit templates from Tesseract readings...")

        for region in regions:
            # Get Tesseract reading as ground truth
            value, conf = ocr_tesseract(region)
            if value is None or conf < 0.7:
                continue

            # Preprocess and extract digit regions
            preprocessed = preprocess_for_ocr(region)
            h, w = preprocessed.shape

            # Find digit contours
            contours, _ = cv2.findContours(cv2.bitwise_not(preprocessed), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

            if not contours:
                continue

            # Get bounding boxes sorted left-to-right
            boxes = [cv2.boundingRect(c) for c in contours]
            boxes = [(x, y, bw, bh) for x, y, bw, bh in boxes if bh > h * 0.3]  # Filter small noise
            boxes.sort(key=lambda b: b[0])  # Sort by x position

            # Extract digits based on value
            value_str = str(value)
            if len(boxes) != len(value_str):
                continue  # Mismatch, skip

            for i, (x, y, bw, bh) in enumerate(boxes):
                digit = value_str[i]
                # Add padding around digit
                pad = 4
                x1 = max(0, x - pad)
                y1 = max(0, y - pad)
                x2 = min(w, x + bw + pad)
                y2 = min(h, y + bh + pad)

                digit_img = preprocessed[y1:y2, x1:x2]

                # Store template (keep best quality one per digit)
                if digit not in self.digit_templates or digit_img.shape[0] * digit_img.shape[1] > self.digit_templates[digit].shape[0] * self.digit_templates[digit].shape[1]:
                    self.digit_templates[digit] = digit_img.copy()

            # Check if we have all digits we need (0-4 for tens, 0-9 for ones)
            if all(str(d) in self.digit_templates for d in range(10)):
                break

        logger.info(f"  Calibrated templates for digits: {sorted(self.digit_templates.keys())}")
        self._calibrated = len(self.digit_templates) >= 5  # At least 0-4 for play clock

        return self._calibrated

    def read(self, region: np.ndarray) -> Tuple[Optional[int], float]:
        """Read digits using template matching."""
        if not self._calibrated:
            return None, 0.0

        preprocessed = preprocess_for_ocr(region)
        h, w = preprocessed.shape

        # Find digit contours
        contours, _ = cv2.findContours(cv2.bitwise_not(preprocessed), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        if not contours:
            return None, 0.0

        # Get bounding boxes sorted left-to-right
        boxes = [cv2.boundingRect(c) for c in contours]
        boxes = [(x, y, bw, bh) for x, y, bw, bh in boxes if bh > h * 0.3]  # Filter noise
        boxes.sort(key=lambda b: b[0])

        if not boxes:
            return None, 0.0

        # Match each digit region to templates
        digits = []
        total_conf = 0.0

        for x, y, bw, bh in boxes:
            # Extract digit with padding
            pad = 4
            x1 = max(0, x - pad)
            y1 = max(0, y - pad)
            x2 = min(w, x + bw + pad)
            y2 = min(h, y + bh + pad)

            digit_img = preprocessed[y1:y2, x1:x2]

            # Match against all templates
            best_digit = None
            best_conf = 0.0

            for digit, template in self.digit_templates.items():
                # Resize template to match digit height
                if template.shape[0] == 0 or digit_img.shape[0] == 0:
                    continue

                scale = digit_img.shape[0] / template.shape[0]
                new_w = max(1, int(template.shape[1] * scale))
                resized = cv2.resize(template, (new_w, digit_img.shape[0]), interpolation=cv2.INTER_LINEAR)

                # Pad smaller image to match sizes for comparison
                digit_img_padded = digit_img
                if resized.shape[1] < digit_img.shape[1]:
                    diff = digit_img.shape[1] - resized.shape[1]
                    resized = cv2.copyMakeBorder(resized, 0, 0, diff // 2, diff - diff // 2, cv2.BORDER_CONSTANT, value=255)
                elif digit_img.shape[1] < resized.shape[1]:
                    diff = resized.shape[1] - digit_img.shape[1]
                    digit_img_padded = cv2.copyMakeBorder(digit_img, 0, 0, diff // 2, diff - diff // 2, cv2.BORDER_CONSTANT, value=255)

                # Ensure same size
                min_h = min(resized.shape[0], digit_img_padded.shape[0])
                min_w = min(resized.shape[1], digit_img_padded.shape[1])
                resized = resized[:min_h, :min_w]
                digit_img_padded = digit_img_padded[:min_h, :min_w]

                # Calculate normalized cross-correlation
                if resized.size == 0 or digit_img_padded.size == 0:
                    continue

                # Simple pixel difference score
                diff = np.abs(resized.astype(float) - digit_img_padded.astype(float))
                score = 1.0 - (np.mean(diff) / 255.0)

                if score > best_conf:
                    best_conf = score
                    best_digit = digit

            if best_digit is not None and best_conf > 0.5:
                digits.append(best_digit)
                total_conf += best_conf

        if not digits:
            return None, 0.0

        # Combine digits into number
        try:
            value = int("".join(digits))
            avg_conf = total_conf / len(digits)
            if 0 <= value <= 40:
                return value, avg_conf
        except ValueError:
            pass

        return None, 0.0


_digit_matcher = None


def get_digit_matcher() -> DigitTemplateMatcher:
    """Get or create digit template matcher."""
    global _digit_matcher
    if _digit_matcher is None:
        _digit_matcher = DigitTemplateMatcher()
    return _digit_matcher


def ocr_template_matching(region: np.ndarray) -> Tuple[Optional[int], float]:
    """Read digits using template matching."""
    matcher = get_digit_matcher()
    return matcher.read(region)


# ============================================================
# Benchmark Runner
# ============================================================
def run_benchmark(frames: List[Tuple[float, np.ndarray, Tuple[int, int, int, int]]], config: Tuple[int, int, int, int]) -> None:
    """Run benchmark comparing OCR methods."""
    logger.info("=" * 60)
    logger.info("OCR BENCHMARK")
    logger.info("=" * 60)
    logger.info(f"Testing {len(frames)} frames")

    # Extract play clock regions
    regions = []
    for ts, frame, scorebug_bbox in frames:
        region = extract_play_clock_region(frame, scorebug_bbox, config)
        regions.append((ts, region))

    # Method 1: Tesseract (baseline - also used for ground truth)
    logger.info("")
    logger.info("-" * 60)
    logger.info("Method 1: Tesseract (baseline)")
    logger.info("-" * 60)

    tesseract_results = []
    t_start = time.perf_counter()
    for ts, region in regions:
        value, conf = ocr_tesseract(region)
        tesseract_results.append((ts, value, conf))
    tesseract_time = time.perf_counter() - t_start

    tesseract_success = sum(1 for _, v, _ in tesseract_results if v is not None)
    logger.info(f"  Success rate: {tesseract_success}/{len(regions)} ({100*tesseract_success/len(regions):.1f}%)")
    logger.info(f"  Total time: {tesseract_time:.3f}s")
    logger.info(f"  Per-frame: {1000*tesseract_time/len(regions):.1f}ms")
    logger.info(f"  Values: {[v for _, v, _ in tesseract_results]}")

    # Use Tesseract results as ground truth for accuracy comparison
    ground_truth = {ts: v for ts, v, _ in tesseract_results if v is not None}

    # Method 2: EasyOCR
    logger.info("")
    logger.info("-" * 60)
    logger.info("Method 2: EasyOCR")
    logger.info("-" * 60)

    reader = get_easyocr_reader()
    easyocr_time = 0
    easyocr_success = 0
    easyocr_accuracy = 0

    if reader:
        easyocr_results = []
        t_start = time.perf_counter()
        for ts, region in regions:
            value, conf = ocr_easyocr(region)
            easyocr_results.append((ts, value, conf))
        easyocr_time = time.perf_counter() - t_start

        easyocr_success = sum(1 for _, v, _ in easyocr_results if v is not None)
        # Calculate accuracy vs ground truth
        easyocr_correct = sum(1 for ts, v, _ in easyocr_results if ts in ground_truth and v == ground_truth[ts])
        easyocr_accuracy = easyocr_correct / len(ground_truth) * 100 if ground_truth else 0

        logger.info(f"  Success rate: {easyocr_success}/{len(regions)} ({100*easyocr_success/len(regions):.1f}%)")
        logger.info(f"  Accuracy vs Tesseract: {easyocr_correct}/{len(ground_truth)} ({easyocr_accuracy:.1f}%)")
        logger.info(f"  Total time: {easyocr_time:.3f}s")
        logger.info(f"  Per-frame: {1000*easyocr_time/len(regions):.1f}ms")
        logger.info(f"  Speedup vs Tesseract: {tesseract_time/easyocr_time:.2f}x")
        logger.info(f"  Values: {[v for _, v, _ in easyocr_results]}")
    else:
        logger.info("  SKIPPED (EasyOCR not installed)")

    # Method 3: Template Matching
    logger.info("")
    logger.info("-" * 60)
    logger.info("Method 3: Template Matching")
    logger.info("-" * 60)

    matcher = get_digit_matcher()

    # Calibrate using first 10 regions (not counted in benchmark time)
    calibration_regions = [r for _, r in regions[:10]]
    if matcher.calibrate_from_tesseract(calibration_regions):
        template_results = []
        t_start = time.perf_counter()
        for ts, region in regions:
            value, conf = ocr_template_matching(region)
            template_results.append((ts, value, conf))
        template_time = time.perf_counter() - t_start

        template_success = sum(1 for _, v, _ in template_results if v is not None)
        template_correct = sum(1 for ts, v, _ in template_results if ts in ground_truth and v == ground_truth[ts])
        template_accuracy = template_correct / len(ground_truth) * 100 if ground_truth else 0

        logger.info(f"  Success rate: {template_success}/{len(regions)} ({100*template_success/len(regions):.1f}%)")
        logger.info(f"  Accuracy vs Tesseract: {template_correct}/{len(ground_truth)} ({template_accuracy:.1f}%)")
        logger.info(f"  Total time: {template_time:.3f}s")
        logger.info(f"  Per-frame: {1000*template_time/len(regions):.1f}ms")
        logger.info(f"  Speedup vs Tesseract: {tesseract_time/template_time:.2f}x")
        logger.info(f"  Values: {[v for _, v, _ in template_results]}")
    else:
        logger.info("  SKIPPED (calibration failed)")
        template_time = 0
        template_success = 0
        template_accuracy = 0

    # Summary
    logger.info("")
    logger.info("=" * 60)
    logger.info("SUMMARY")
    logger.info("=" * 60)
    logger.info(f"{'Method':<20} {'Time/frame':<12} {'Success':<12} {'Accuracy':<12} {'Speedup':<10}")
    logger.info("-" * 66)
    logger.info(f"{'Tesseract':<20} {f'{1000*tesseract_time/len(regions):.1f}ms':<12} {f'{tesseract_success}/{len(regions)}':<12} {'(baseline)':<12} {'1.00x':<10}")
    if reader and easyocr_time > 0:
        logger.info(
            f"{'EasyOCR':<20} {f'{1000*easyocr_time/len(regions):.1f}ms':<12} {f'{easyocr_success}/{len(regions)}':<12} {f'{easyocr_accuracy:.1f}%':<12} {f'{tesseract_time/easyocr_time:.2f}x':<10}"
        )
    if template_time > 0:
        logger.info(
            f"{'Template Matching':<20} {f'{1000*template_time/len(regions):.1f}ms':<12} {f'{template_success}/{len(regions)}':<12} {f'{template_accuracy:.1f}%':<12} {f'{tesseract_time/template_time:.2f}x':<10}"
        )


def main():
    """Main entry point."""
    logger.info("OCR Benchmark Tool")
    logger.info("=" * 60)

    # Verify paths
    if not VIDEO_PATH.exists():
        logger.error(f"Video not found: {VIDEO_PATH}")
        return 1

    if not TEMPLATE_PATH.exists():
        logger.error(f"Template not found: {TEMPLATE_PATH}")
        return 1

    if not CONFIG_PATH.exists():
        logger.error(f"Config not found: {CONFIG_PATH}")
        return 1

    # Load config
    config = load_play_clock_config()
    logger.info(f"Play clock config: {config}")

    # Initialize scorebug detector
    detector = DetectScoreBug(template_path=str(TEMPLATE_PATH))

    # Extract test frames
    logger.info(f"Extracting {len(TEST_TIMESTAMPS)} test frames...")
    frames = extract_test_frames(VIDEO_PATH, detector, TEST_TIMESTAMPS)
    logger.info(f"Extracted {len(frames)} frames with scorebug")

    if not frames:
        logger.error("No frames with scorebug found!")
        return 1

    # Run benchmark
    run_benchmark(frames, config)

    return 0


if __name__ == "__main__":
    sys.exit(main())