Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/image-processing-and-computer-vision/matlab-recognize-text

matlab-recognize-text

Verified

Build OCR pipelines in MATLAB using the ocr() function. Use this skill when the user wants to read text from images, documents, signs, meters, displays, license plates, gauges, receipts, or seven-segment displays. Covers image preprocessing, text detection (CRAFT, MSER), ROI-based recognition, multi-language OCR, and custom model training. Use when: OCR, text recognition, extract text from image, character recognition, document scanning, meter reading, gauge reading, receipt scanning, digitize text from photo.

reference/training-guide.md

View on GitHub
# Custom OCR Model Training Guide

## When to Train a Custom Model

**Train only when ALL of these conditions are true:**

1. Default `ocr()` with preprocessing gives consistently poor results
2. Tuning `LayoutAnalysis`, `CharacterSet`, and preprocessing does not resolve issues
3. The font/character set is not covered by any built-in model (64 languages + seven-segment)
4. You have or can create labeled ground truth data (50+ images minimum)
5. The use case is repeated/high-volume enough to justify the effort

**Common scenarios requiring custom training:**
- Industrial seven-segment variants not covered by built-in model
- LED/LCD display fonts with unique styling
- Custom symbology (warehouse codes, proprietary markings)
- Degraded historical document fonts
- Domain-specific character sets (circuit labels, chemical formulas)

**Do NOT train when:**
- A different language model would work — try `Model="japanese"` etc. first
- Preprocessing would fix it — most failures are preprocessing problems
- The character set is standard but images are noisy — preprocess instead
- You only have a few images — insufficient data for training

## Training Workflow

### Step 1: Create Ground Truth Labels

Use the Image Labeler app to label text regions:

```matlab
% Launch Image Labeler
imageLabeler

% In the app:
% 1. Import images
% 2. Create a Rectangle ROI label named "TextROI"
% 3. Add a String attribute named "Text" to the label
% 4. Draw bounding boxes around each text region
% 5. Type the text content into the "Text" attribute
% 6. Export to workspace as 'gTruth'
```

**Labeling tips:**
- Label at word level (one box per word) for best results
- Include variety: different sizes, orientations, backgrounds
- Minimum 50 labeled images; 200+ recommended for robustness
- Include hard cases that the default model fails on

### Step 2: Extract Training Data

```matlab
% Extract datastores from ground truth
[imds, boxds, txtds] = ocrTrainingData(gTruth, "TextROI", "Text");

% Combine into single training datastore
trainingData = combine(imds, boxds, txtds);

% Optional: split for validation (80/20)
numImages = numel(imds.Files);
idx = randperm(numImages);
trainIdx = idx(1:round(0.8*numImages));
valIdx = idx(round(0.8*numImages)+1:end);
```

### Step 3: Configure Training Options

```matlab
options = ocrTrainingOptions( ...
    MaxEpochs=10, ...
    InitialLearnRate=0.001, ...
    Verbose=true, ...
    VerboseFrequency=50, ...
    CheckpointPath=tempdir, ...
    CheckpointFrequency=1);

% With validation for early stopping
options = ocrTrainingOptions( ...
    MaxEpochs=20, ...
    InitialLearnRate=0.001, ...
    ValidationData=validationData, ...
    ValidationFrequency=50, ...
    ValidationPatience=5, ...
    OutputNetwork="best-validation-loss");
```

### Step 4: Train

```matlab
% Fine-tune from English base model (transfer learning)
modelFile = trainOCR(trainingData, "myCustomModel", "english", options);

% Or fine-tune from another language base
modelFile = trainOCR(trainingData, "myCustomModel", "japanese", options);

% Resume from checkpoint if interrupted
modelFile = trainOCR(trainingData, "myCustomModel", checkpointPath, options);
```

### Step 5: Evaluate

```matlab
% Test on held-out images
results = ocr(testImage, Model=modelFile);
disp(results.Text)

% Formal evaluation with metrics
metrics = evaluateOCR(results, groundTruthDatastore);
disp(metrics.DataSetMetrics)  % Overall CER and WER
disp(metrics.ImageMetrics)    % Per-image breakdown
```

**Metrics explained:**
- **CER (Character Error Rate):** Edit distance between predicted and ground-truth characters / ground-truth length. Target: < 0.05
- **WER (Word Error Rate):** Edit distance between predicted and ground-truth words / ground-truth count. Target: < 0.10

### Step 6: Quantize for Speed (Optional)

```matlab
% Quantize for ~40% faster inference (small accuracy tradeoff)
fastModelFile = quantizeOCR(modelFile, "myCustomModel-fast");

% Use quantized model
results = ocr(I, Model=fastModelFile);
```

## Training Functions Reference

| Function | Purpose |
|----------|---------|
| `ocrTrainingData` | Extract training datastores from groundTruth object |
| `ocrTrainingOptions` | Configure hyperparameters (epochs, LR, solver, validation) |
| `trainOCR` | Train or fine-tune an OCR model |
| `evaluateOCR` | Compute CER/WER against ground truth |
| `ocrMetrics` | Object holding dataset and per-image error metrics |
| `quantizeOCR` | Reduce model to lower precision for speed |

## Key Training Parameters

| Parameter | Default | Guidance |
|-----------|---------|----------|
| `MaxEpochs` | 5 | Start with 10-20; use validation patience for early stopping |
| `InitialLearnRate` | 0.001 | Lower (0.0001) for fine-tuning, higher (0.01) for new domains |
| `SolverName` | `"adam"` | Adam works well; try `"sgdm"` if Adam oscillates |
| `CharacterSetSource` | `"auto"` | Use `"ground-truth-data"` for custom character sets |
| `ValidationPatience` | Inf | Set to 5-10 to enable early stopping |
| `OutputNetwork` | `"auto"` | Use `"best-validation-loss"` when validation data provided |

## Troubleshooting Training

| Problem | Likely Cause | Fix |
|---------|-------------|-----|
| Training loss not decreasing | Learning rate too low or data issue | Increase LR or check label quality |
| Validation loss increasing | Overfitting | Reduce epochs, add more data, or lower LR |
| Model worse than baseline | Insufficient or mismatched training data | Need more diverse examples |
| Training very slow | Large images | Crop to text regions before training |
| Out of memory | Images too large or batch too big | Resize images or reduce batch size |


----
Copyright 2026 The MathWorks, Inc.
----
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 319kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall