File size: 17,170 Bytes
3e23f63
 
 
 
 
 
 
 
 
 
 
 
 
 
8702db7
69c9fd9
644f8c6
f9b53d5
 
 
 
3e23f63
34f268d
644f8c6
 
3e23f63
644f8c6
3e23f63
 
 
 
 
f9b53d5
 
3e23f63
f9b53d5
 
 
 
 
3ed4400
 
 
 
3e23f63
 
 
635d31b
3e23f63
635d31b
 
 
 
 
3e23f63
 
635d31b
 
3e23f63
635d31b
 
3e23f63
 
 
 
 
635d31b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e23f63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644f8c6
 
 
 
3e23f63
69c9fd9
 
 
3e23f63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635d31b
3e23f63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ed4400
 
 
 
 
3e23f63
 
 
 
 
 
ed66c76
3e23f63
ed66c76
 
 
 
 
 
3e23f63
 
 
 
 
 
 
 
 
 
445a45a
3e23f63
 
3ed4400
 
 
 
 
3e23f63
3ed4400
3e23f63
 
 
 
3ed4400
635d31b
 
 
 
 
 
 
 
 
3ed4400
635d31b
3ed4400
3e23f63
 
 
 
3ed4400
3e23f63
445a45a
 
 
3e23f63
 
635d31b
 
 
445a45a
 
 
3ed4400
635d31b
 
 
 
 
 
 
 
3e23f63
 
 
 
 
3ed4400
3e23f63
445a45a
 
 
3ed4400
445a45a
3e23f63
 
 
 
 
445a45a
3e23f63
445a45a
 
 
 
3ed4400
445a45a
 
3ed4400
 
 
3e23f63
3ed4400
445a45a
 
3e23f63
 
 
 
635d31b
3e23f63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644f8c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b89eed5
644f8c6
 
 
f9b53d5
644f8c6
 
3e23f63
 
 
 
 
 
 
 
f9b53d5
3e23f63
644f8c6
3e23f63
f9b53d5
 
644f8c6
635d31b
 
 
 
 
644f8c6
f9b53d5
 
 
 
644f8c6
f9b53d5
 
644f8c6
3e23f63
644f8c6
 
 
 
3e23f63
644f8c6
f9b53d5
 
644f8c6
 
3e23f63
635d31b
 
3e23f63
 
 
7b912e5
 
 
 
 
 
 
3e23f63
 
 
3ed4400
 
 
 
 
da9351a
3e23f63
 
da9351a
3e23f63
da9351a
 
7b912e5
3e23f63
7b912e5
da9351a
 
 
 
 
 
3e23f63
da9351a
7b912e5
da9351a
 
69c9fd9
 
 
da9351a
 
69c9fd9
da9351a
3e23f63
7b912e5
3e23f63
7b912e5
3e23f63
 
 
 
 
 
7b912e5
3e23f63
3ed4400
ed66c76
 
 
 
 
 
 
 
 
3ed4400
69c9fd9
 
 
 
 
 
 
 
 
 
 
 
 
b89eed5
 
 
69c9fd9
3e23f63
 
69c9fd9
3ed4400
3e23f63
 
 
 
 
 
 
 
3ed4400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e23f63
3ed4400
 
3e23f63
 
 
 
 
 
b89eed5
3e23f63
 
 
 
 
 
 
 
 
 
d850478
7b912e5
3e23f63
 
 
 
 
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
import gradio as gr
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from PIL import Image, ImageFont, ImageDraw
import numpy as np
import os
import string
import cv2
from torchvision.transforms.functional import to_pil_image
import matplotlib.pyplot as plt
import math
from datetime import datetime
import re
from termcolor import colored
from pyctcdecode import BeamSearchDecoderCTC, Alphabet
from difflib import SequenceMatcher


# --------- Globals --------- #
CHARS = string.ascii_letters + string.digits + string.punctuation
CHAR2IDX = {c: i + 1 for i, c in enumerate(CHARS)}  # Start from 1
CHAR2IDX["<BLANK>"] = 0  # CTC blank
IDX2CHAR = {v: k for k, v in CHAR2IDX.items()}
BLANK_IDX = 0
IMAGE_HEIGHT = 32
IMAGE_WIDTH = 128
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
font_path = None
ocr_model = None
# Create vocabulary list (ensure order matches your model’s output indices!)
labels = [IDX2CHAR.get(i, "") for i in range(len(IDX2CHAR))]

# Wrap in Alphabet
alphabet = Alphabet.build_alphabet(labels)

# Now initialize decoder correctly
decoder = BeamSearchDecoderCTC(alphabet)
# Ensure required directories exist at startup
os.makedirs("./fonts", exist_ok=True)
os.makedirs("./models", exist_ok=True)
os.makedirs("./labels", exist_ok=True)

# --------- Dataset --------- #
class OCRDataset(Dataset):
    def __init__(self, font_path, size=1000, label_length_range=(4, 7)):
        self.font = ImageFont.truetype(font_path, 32)
        self.label_length_range = label_length_range
        self.samples = [
            "".join(np.random.choice(list(CHARS), np.random.randint(*self.label_length_range)))
            for _ in range(size)
        ]

        self.transform = transforms.Compose([
            transforms.ToTensor(),  # must be first
            transforms.Normalize((0.5,), (0.5,)),
            transforms.Resize((IMAGE_HEIGHT, IMAGE_WIDTH)),
            transforms.RandomApply([transforms.GaussianBlur(kernel_size=3)], p=0.3),
            transforms.RandomApply([transforms.RandomAffine(degrees=10, translate=(0.1, 0.1))], p=0.3),
        ])
    def __len__(self):
        return len(self.samples)

    def __getitem__(self, idx):
        label = self.samples[idx]

        # Create an image with padding
        pad = 8
        w = self.font.getlength(label)
        h = self.font.size
        img_w, img_h = int(w + 2 * pad), int(h + 2 * pad)
        img = Image.new("L", (img_w, img_h), 255)
        draw = ImageDraw.Draw(img)
        draw.text((pad, pad), label, font=self.font, fill=0)

        img = self.transform(img)
        label_encoded = torch.tensor([CHAR2IDX[c] for c in label], dtype=torch.long)
        label_length = torch.tensor(len(label_encoded), dtype=torch.long)

        return img, label_encoded, label_length



    def render_text(self, text):
        img = Image.new("L", (IMAGE_WIDTH, IMAGE_HEIGHT), color=255)
        draw = ImageDraw.Draw(img)
        bbox = self.font.getbbox(text)
        w, h = bbox[2] - bbox[0], bbox[3] - bbox[1]
        draw.text(((IMAGE_WIDTH - w) // 2, (IMAGE_HEIGHT - h) // 2), text, font=self.font, fill=0)
        return img


# --------- Model --------- #
class OCRModel(nn.Module):
    def __init__(self, num_classes):
        super().__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(1, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d((2, 2), (2, 1)),  # height↓2, width↓1
            nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.MaxPool2d((2, 2), (2, 1))  # height↓2 again, width↓1
        )


        self.rnn = nn.LSTM(64 * 8, 128, bidirectional=True, num_layers=2, batch_first=True)
        self.fc = nn.Linear(256, num_classes)
        with torch.no_grad():
            self.fc.bias[0] = -5.0  # discourage blank early on


    def forward(self, x):
        b, c, h, w = x.size()
        x = self.conv(x)
        x = x.permute(0, 3, 1, 2)
        x = x.reshape(b, x.size(1), -1)
        x, _ = self.rnn(x)
        x = self.fc(x)
        return x
def color_char(c, conf):
    color_levels = ['\033[31m', '\033[33m', '\033[32m', '\033[36m', '\033[34m', '\033[35m', '\033[0m']
    idx = min(int(conf * (len(color_levels) - 1)), len(color_levels) - 1)
    return f"{color_levels[idx]}{c}\033[0m"

def sanitize_filename(name):
    return re.sub(r'[^a-zA-Z0-9_-]', '_', name)

def greedy_decode(log_probs):
    # log_probs shape: (T, B, C)
    # Usually, B=1 during inference
    pred = log_probs.argmax(2).squeeze(1).tolist()  # this should give a list of ints
    print(f"Decoded indices: {pred}")  # debug print
    
    decoded = []
    prev = BLANK_IDX
    for p in pred:
        if p != prev and p != BLANK_IDX:
            decoded.append(IDX2CHAR.get(p, ""))
        prev = p
    return ''.join(decoded)




# --------- Custom Collate --------- #
def custom_collate_fn(batch):
    images, labels, _ = zip(*batch)
    images = torch.stack(images, 0)

    flat_labels = []
    label_lengths = []

    for label in labels:
        flat_labels.append(label)
        label_lengths.append(len(label))

    targets = torch.cat(flat_labels)
    return images, targets, torch.tensor(label_lengths, dtype=torch.long)


# --------- Model Save/Load --------- #
def list_saved_models():
    model_dir = "./models"
    if not os.path.exists(model_dir):
        return []
    return [f for f in os.listdir(model_dir) if f.endswith(".pth")]



def save_model(model, path):
    torch.save(model.state_dict(), path)


def load_model(filename):
    global ocr_model
    model_dir = "./models"
    path = os.path.join(model_dir, filename)
    
    if not os.path.exists(path):
        return f"Model file '{path}' does not exist."

    model = OCRModel(num_classes=len(CHAR2IDX))
    model.load_state_dict(torch.load(path, map_location=device))
    model.to(device)
    model.eval()
    ocr_model = model
    return f"Model '{path}' loaded."


# --------- Gradio Functions --------- #
def train_model(font_file, epochs=100, learning_rate=0.001):
    import time
    global font_path, ocr_model

    # Ensure directories exist
    os.makedirs("./fonts", exist_ok=True)
    os.makedirs("./models", exist_ok=True)

    # Save uploaded font to ./fonts
    font_name = os.path.splitext(os.path.basename(font_file.name))[0]
    font_path = f"./fonts/{font_name}.ttf"
    with open(font_file.name, "rb") as uploaded:
        with open(font_path, "wb") as f:
            f.write(uploaded.read())

    # Curriculum learning: label length grows over time
    def get_dataset_for_epoch(epoch):
        if epoch < epochs // 3:
            label_len = (3, 4)
        elif epoch < 2 * epochs // 3:
            label_len = (4, 6)
        else:
            label_len = (5, 7)
        return OCRDataset(font_path, label_length_range=label_len)

    # Visualize one sample
    dataset = get_dataset_for_epoch(0)
    img, label, _ = dataset[0]
    print("Label:", ''.join([IDX2CHAR[i.item()] for i in label]))
    plt.imshow(img.permute(1, 2, 0).squeeze(), cmap='gray')
    plt.show()

    # Model setup
    model = OCRModel(num_classes=len(CHAR2IDX)).to(device)
    criterion = nn.CTCLoss(blank=BLANK_IDX)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=5)

    for epoch in range(epochs):
        dataset = get_dataset_for_epoch(epoch)
        dataloader = DataLoader(dataset, batch_size=16, shuffle=True, collate_fn=custom_collate_fn)

        model.train()
        running_loss = 0.0

        # Warmup learning rate
        if epoch < 5:
            warmup_lr = learning_rate * 0.2
            for param_group in optimizer.param_groups:
                param_group['lr'] = warmup_lr
        else:
            for param_group in optimizer.param_groups:
                param_group['lr'] = learning_rate

        for img, targets, target_lengths in dataloader:
            img = img.to(device)
            targets = targets.to(device)
            target_lengths = target_lengths.to(device)

            output = model(img)
            seq_len = output.size(1)
            batch_size = img.size(0)
            input_lengths = torch.full((batch_size,), seq_len, dtype=torch.long).to(device)

            log_probs = output.log_softmax(2).transpose(0, 1)
            loss = criterion(log_probs, targets, input_lengths, target_lengths)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

        avg_loss = running_loss / len(dataloader)
        scheduler.step(avg_loss)
        print(f"[{epoch + 1}/{epochs}] Loss: {avg_loss:.4f}")

    # Save the model to ./models
    timestamp = time.strftime("%Y%m%d%H%M%S")
    model_name = f"{font_name}_{epochs}ep_lr{learning_rate:.0e}_{timestamp}.pth"
    model_path = os.path.join("./models", model_name)
    save_model(model, model_path)

    ocr_model = model
    return f"✅ Training complete! Model saved as '{model_path}'"







def preprocess_image(image: Image.Image):
    img_cv = np.array(image.convert("L"))

    img_bin = cv2.adaptiveThreshold(img_cv, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                    cv2.THRESH_BINARY_INV, 25, 15)

    # Invert if background is dark
    white_px = (img_bin == 255).sum()
    black_px = (img_bin == 0).sum()
    if black_px > white_px:
        img_bin = 255 - img_bin

    # Resize and pad/crop to (IMAGE_HEIGHT, IMAGE_WIDTH)
    h, w = img_bin.shape
    scale = IMAGE_HEIGHT / h
    new_w = int(w * scale)
    resized = cv2.resize(img_bin, (new_w, IMAGE_HEIGHT), interpolation=cv2.INTER_AREA)

    if new_w < IMAGE_WIDTH:
        pad_width = IMAGE_WIDTH - new_w
        padded = np.pad(resized, ((0, 0), (0, pad_width)), constant_values=255)
    else:
        padded = resized[:, :IMAGE_WIDTH]

    return to_pil_image(padded)




# ROYGBIV color ramp (low → high confidence)
CONFIDENCE_COLORS = [
    "#FF0000",  # Red
    "#FF7F00",  # Orange
    "#FFFF00",  # Yellow
    "#00FF00",  # Green
    "#00BFFF",  # Sky Blue
    "#0000FF",  # Blue
    "#8B00FF",  # Violet
]

def confidence_to_color(conf):
    """
    Map confidence (0.0–1.0) to a ROYGBIV-style hex color.
    """
    index = min(int(conf * (len(CONFIDENCE_COLORS) - 1)), len(CONFIDENCE_COLORS) - 1)
    return CONFIDENCE_COLORS[index]

def color_char(c, conf):
    """
    Wrap character `c` in a span tag with color mapped from `conf`.
    """
    color = confidence_to_color(conf)
    return f'<span style="color:{color}; font-size:12pt; font-weight:bold;">{c}</span>'





def predict_text(image: Image.Image, ground_truth: str = None, debug: bool = False):
    if ocr_model is None:
        return "Please load or train a model first."

    processed = preprocess_image(image)
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5,), (0.5,))
    ])
    img_tensor = transform(processed).unsqueeze(0).to(device)  # (1, C, H, W)

    ocr_model.eval()
    with torch.no_grad():
        output = ocr_model(img_tensor)           # (1, T, C)
        log_probs = output.log_softmax(2)[0]     # (T, C)

        # Decode best beam path (string)
        pred_text_raw = decoder.decode(log_probs.cpu().numpy())
        pred_chars = pred_text_raw.replace("<BLANK>", "")
        # Remove <BLANK> tokens if present (assuming <BLANK> is in vocab)
        pred_text = ''.join([c for c in pred_chars if c != "<BLANK>"])

        # Confidence: mean max prob per timestep
        probs = log_probs.exp()
        max_probs = probs.max(dim=1)[0]
        avg_conf = max_probs.mean().item()

    # Color each character (uniform confidence for now)
    colorized_chars = [color_char(c, avg_conf) for c in pred_text]
    pretty_output = ''.join(colorized_chars)

    sim_score = ""
    if ground_truth:
        similarity = SequenceMatcher(None, ground_truth, pred_text).ratio()
        sim_score = f"<br><strong>Levenshtein Similarity:</strong> {similarity:.2%}"

    if debug:
        print("Decoded Text:", pred_text)
        print("Average Confidence:", avg_conf)
        if ground_truth:
            print("Ground Truth:", ground_truth)

    return f"<strong>Prediction:</strong> <strong>{pretty_output}</strong><br><strong>Confidence:</strong> {avg_conf:.2%}{sim_score}"



# New helper function: generate label images grid
CHARS = string.ascii_letters + string.digits + string.punctuation

FONT_SIZE = 32
PADDING = 8
LABEL_DIR = "./labels"

def generate_labels(font_file=None, num_labels: int = 25):
    global font_path

    try:
        if font_file and font_file != "None":
            font_path = os.path.abspath(font_file)
        else:
            font_path = None

        if font_path is None or not os.path.exists(font_path):
            font = ImageFont.load_default()
        else:
            font = ImageFont.truetype(font_path, 32)

        os.makedirs("./labels", exist_ok=True)
        labels = ["".join(np.random.choice(list(CHARS), np.random.randint(4, 7))) for _ in range(num_labels)]
        images = []

        for label in labels:
            bbox = font.getbbox(label)
            text_w = bbox[2] - bbox[0]
            text_h = bbox[3] - bbox[1]
            pad = 8
            img_w = text_w + pad * 2
            img_h = text_h + pad * 2

            img = Image.new("L", (img_w, img_h), color=255)
            draw = ImageDraw.Draw(img)
            draw.text((pad, pad), label, font=font, fill=0)

            safe_label = sanitize_filename(label)
            timestamp = datetime.now().strftime("%Y%m%d%H%M%S%f")
            label_dir = os.path.join("./labels", safe_label)
            os.makedirs(label_dir, exist_ok=True)

            filepath = os.path.join(label_dir, f"{timestamp}.png")
            img.save(filepath)

            images.append(img)

        return images

    except Exception as e:
        print("Error in generate_labels:", e)
        error_img = Image.new("RGB", (512, 128), color=(255, 255, 255))
        draw = ImageDraw.Draw(error_img)
        draw.text((10, 50), f"Error: {str(e)}", fill=(255, 0, 0))
        return [error_img]

def list_fonts():
    font_dir = "./fonts"
    if not os.path.exists(font_dir):
        return ["None"]
    fonts = [
        (f, os.path.join(font_dir, f)) for f in os.listdir(font_dir)
        if f.lower().endswith((".ttf", ".otf"))
    ]
    return [("None", "None")] + fonts


custom_css = """
#label-gallery .gallery-item img {
    height: 43px;           /* 32pt ≈ 43px */
    width: auto;
    object-fit: contain;
    padding: 4px;
}

#label-gallery {
    flex-grow: 1;
    overflow-y: auto;
    height: 100%;
}
#output-text {
    font-size: 12pt;
}
"""

# --------- Updated Gradio UI with new tab --------- #
with gr.Blocks(css=custom_css) as demo:
    with gr.Tab("【Train OCR Model】"):
        font_file = gr.File(label="Upload .ttf or .otf font", file_types=[".ttf", ".otf"])
        epochs_input = gr.Slider(minimum=1, maximum=4096, value=256, step=1, label="Epochs")
        lr_input = gr.Slider(minimum=0.001, maximum=0.1, value=0.05, step=0.001, label="Learning Rate")
        train_button = gr.Button("Train OCR Model")
        train_status = gr.Textbox(label="Status")

    train_button.click(fn=train_model, inputs=[font_file, epochs_input, lr_input], outputs=train_status)

    with gr.Tab("【Generate Labels】"):
        font_file_labels = gr.Dropdown(
            choices=list_fonts(),
            label="Optional font for label image",
            interactive=True,
        )
        num_labels = gr.Number(value=20, label="Number of labels to generate", precision=0, interactive=True)
        gen_button = gr.Button("Generate Label Grid")

        gen_button.click(
            fn=generate_labels,
            inputs=[font_file_labels, num_labels],
            outputs=gr.Gallery(
                label="Generated Labels",
                columns=16,                  # 16 tiles per row
                object_fit="contain",       # Maintain aspect ratio
                height="100%",              # Allow full app height
                elem_id="label-gallery"     # For CSS targeting
            )

        )
    with gr.Tab("【Recognize Text】"):
        model_list = gr.Dropdown(choices=list_saved_models(), label="Select OCR Model")
        refresh_btn = gr.Button("🔄 Refresh Models")
        load_model_btn = gr.Button("Load Model")  # <-- new button

        image_input = gr.Image(type="pil", label="Upload word strip")
        predict_btn = gr.Button("Predict")
        output_text = gr.HTML(label="Recognized Text", elem_id="output-text")
        model_status = gr.Textbox(label="Model Load Status")

        # Refresh dropdown choices
        refresh_btn.click(fn=lambda: gr.update(choices=list_saved_models()), outputs=model_list)

        # Load model on button click, NOT dropdown change
        load_model_btn.click(fn=load_model, inputs=model_list, outputs=model_status)

        predict_btn.click(fn=predict_text, inputs=image_input, outputs=output_text)






if __name__ == "__main__":
    demo.launch()