Parte 4: Image Intelligence Introduce l'architettura delle onde ImageSummarizer e gli schemi più ampi. Questo articolo profondoM SK1 si immerge nel OCR sottosistema—tre livelli di extrazione del testoM SK1 router intelligente, e l'ottimizzazione delle piste di film che raggiunge la riduzione dei token per i GIF animati .
Perché un articolo separato? Il tubo di OCR si è evoluto da "Tesseract con Vision LLM fallback" ad un sofisticato sistema a tre livelliM SK2con ML - basato sull'OCRMska4 multiMske5votazione frammentaleM Ske6 testoMiske7estrazione solo di strisceM Ska8 e costiMске9trasmissione consapevoli*. È sufficientmente complesso per garantirsi la propria decomposizione dettagliataMSKA12
Article connessi:
L'OCR su immagini reali-world fallisce in modi prevedibili:
Appoggio tradizionale: "Run Tesseract, se non usa Vision LLMM SK3
Il problema.: Questo o perde il testo stilizzato (Tesseract fallisce) o costa troppo ( usa sempre Vision LLM
Soluzione: Aggiungere un livello intermedio (FlorenceM SK2 ONNX) che gestisce le fonti stilizzate localmente
Il sistema gestisce le onde in ordine prioritario (in numero più alto = esecuzione successiva):
Wave Priority Order:
40: TextLikelinessWave → Heuristic text detection
50: OcrWave → Tesseract OCR (if text-likely)
51: MlOcrWave → Florence-2 ML OCR (if Tesseract low confidence)
55: Florence2Wave → Florence-2 captions (optional)
80: VisionLlmWave → Vision LLM (escalation)
| Priorità | Velocità | Costo | Perfetta | Limiti | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 50 | ~50ms | gratis MSC3 testo pulitoM SK4 contrasto alto, caratteri standard SSL6 caratteri stilizzatiMSL7 bassa qualitàMST8 testo ruotato S |
I segnali emetti:
ocr.text - testo estrattoocr.confidence - Tesseract punteggio medio di fiducia| Priorità | Velocità | Costo | Perfetta | Limiti | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 51 | ~200ms | Gratis Stilizzati caratteriM SK4 memes, testo decorativo R | Diagrammi complessiMSC7 testo ruotato M |
I segnali emetti:
ocr.ml.text - SingleM SK1frame Florence-2 OCRocr.ml.multiframe_text - MultiM SK1testo GIF per fotogrammi ( preferito per le animazioni)ocr.ml.confidence - Numero di fiducia del modello| Priorità | Velocità | Costo | Perfetto per | Contrattivi | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 80 | ~1-5s | $0.001-0.01 | Tutto, scene particolarmente complesse | Dobbiamo rispettare i segnali deterministici |
I segnali emetti:
ocr.vision.text - Extrazione del testo di Vision LLM OCRocr.vision.confidence - Confidenza LLM ( tipicamente 0.95)caption.text - Copertina descrittiva opzionale separata dall'OCRPrima di immergersi nei tre livelli OCR, lasciateM SK1s ricoprire I modelli deterministici ML che alimentano il sistema. Tutti i modelli funzionano localmente tramite ONNX Runtime—no chiamate APIM SK2 nessuna dipendenza dalla nuvolaMST3 nessun costoMst4
* Minore avvertimento: I fornitori di esecuzione GPU possono introdurre floating negligibili-point nondeterminismM SK2 Il contratto di segnale (limiti di fiduciaMSC4 logica di routingMST5 rimane completamente deterministicoMSM6
Nota.: Le dimensioni sono approssimative e variano in base alla varianteM SK1quantizzazione. Le normali dimensioni di download mostrate sotto .
| Modello | Circa . Tasso S | Oggetto M | velocità R | Tipo del modello D |
|---|---|---|---|---|
| L'AST | ||||
| CRAFT | ~150MB SMK2 CharacterM SK3Detezione del testo nella regione | |||
| Florence-2 | ||||
| Real-ESRGAN | ||||
| CLIP |
Spazio totale del disco: ~1.0-1.5GB a seconda delle varianti del modello sceltoM SK2
Detettore efficace e accurato del testo della scena - trova regioni di testo nelle scene naturali.
// EAST detects text bounding boxes with confidence scores
var result = await textDetector.RunEastDetectionAsync(imagePath);
// Output: List of BoundingBox with coordinates + confidence
// Example: [BoundingBox(x1:50, y1:100, x2:300, y2:150, confidence:0.92)]
Come funziona?:
Perché deterministico?
< 0.5 → escalate)dettagli tecnici:
// EAST preprocessing (from implementation)
- Input size: 320×320 (must be multiple of 32)
- Format: BGR with mean subtraction [123.68, 116.78, 103.94]
- Output stride: 4 (downsampled 4×)
- Score threshold: 0.5
- NMS IoU threshold: 0.4
Esempio di output:
Input: meme.png (800×600)
EAST detection: 15 text regions found
Region 1: (50, 480, 750, 580) - confidence 0.87 [bottom subtitle area]
Region 2: (100, 50, 300, 90) - confidence 0.62 [top text]
Region 3: ...
Route decision: ANIMATED (subtitle pattern in bottom 30%)
Detezione di testo a livello di carattere- - è eccezionale per i curviM SK1 artistici, e per il testo stilizzato .
// CRAFT finds character-level regions, then groups into words
var result = await textDetector.RunCraftDetectionAsync(imagePath);
// Better than EAST for: decorative fonts, curved text, logos
Come funziona?:
Quando si usa CRAFT.:
dettagli tecnici:
// CRAFT preprocessing
- Max dimension: 1280px (maintains aspect ratio)
- Format: RGB normalized with ImageNet stats
- Mean: [0.485, 0.456, 0.406]
- Std: [0.229, 0.224, 0.225]
- Output stride: 2 (downsampled 2×)
- Threshold: 0.4 for character regions
EAST vs CRAFT comparazione:
| Carattolo | Est | CRAFT ≥ | |
|---|---|---|---|
| livello di rilevamento | parolaM SK2 linea | carattere S | |
| Velocità | ~20ms S | ||
| Perfetto per | testo standard, sottotitoli | caratteri decorativi | |
| testo curvato | Limitato | Eccellente S | |
| dimensione del modello | 100MB S |
Impara immagini di bassa - qualità prima dell'OCR - 4× innalzamento per buioM SK2 testo piccolo.
// Upscale low-quality image before running OCR
if (quality.Sharpness < 30) // Laplacian variance threshold
{
var upscaled = await esrganService.UpscaleAsync(imagePath, scale: 4);
// Now run OCR on the enhanced image
}
Quando è usato':
Esempio:
Input: 100×75 screenshot with tiny text
Laplacian variance: 18 (very blurry)
ESRGAN: Upscale to 400×300 (~500ms)
New Laplacian variance: 87 (sharp)
OCR: Tesseract confidence: 0.92 (vs 0.42 before upscaling)
Text: "Click here to continue" (vs garbled before)
dettagli tecnici:
// Real-ESRGAN processing
- Input: Any size (processed in 128×128 tiles if large)
- Output: 4× scaled (200×150 → 800×600)
- Model: x4plus variant (general photos)
- Processing: ~500ms for 800×600 image
- Memory: ~2GB peak (tiles reduce this)
L'economia dei simboli:
Scenario: Screenshot with tiny text
Option 1: Send low-res to Vision LLM
Image: 100×75 = ~20 tokens
LLM can't read tiny text → fails
Cost: $0.0002 (wasted)
Option 2: Upscale with ESRGAN, use Tesseract
ESRGAN: Free (local), 500ms
Tesseract: Free (local), 50ms
Success: 92% confidence
Cost: $0
Result: ESRGAN + local OCR beats Vision LLM for low-res images
Inserzioni multimodali per la ricerca di immagini semantiche - progetti immagini e testo nello spazio vettore condiviso.
// Generate embedding for semantic search
var embedding = await clipService.GenerateEmbeddingAsync(imagePath);
// Returns: float[512] vector
// Later: semantic search across thousands of images
var similarImages = await vectorDb.SearchAsync(queryEmbedding, topK: 10);
Come funziona?:
Usare casi:
dettagli tecnici:
// CLIP visual encoder
- Model: ViT-B/32 (Vision Transformer)
- Input: 224×224 RGB (center crop + resize)
- Output: 512-dimensional embedding
- Normalized: L2 norm = 1.0
- Speed: ~100ms per image
Esempio:
Input images:
cat_on_couch.jpg → [0.23, -0.51, 0.88, ...]
dog_on_couch.jpg → [0.19, -0.48, 0.91, ...]
car_photo.jpg → [-0.67, 0.33, -0.12, ...]
Query: "animals on furniture"
Text embedding → [0.21, -0.50, 0.89, ...]
Cosine similarity:
cat_on_couch: 0.94 (very similar!)
dog_on_couch: 0.91 (similar)
car_photo: 0.12 (not similar)
Result: Returns cat and dog images
Osservate la sezione di livello 2 per i dettagli completo sulla Florence-2 ONNX OCR e i sottotitoliM SK2
Tutti i modelli vengono scaricati automaticamente durante il primo uso:
$ imagesummarizer image.png --pipeline auto
[First run]
Downloading EAST scene text detector (~100MB)...
Progress: ████████████████████ 100% (102.4 MB)
Downloading Florence-2 base model (~250MB)...
Progress: ████████████████████ 100% (248.7 MB)
Downloading CLIP ViT-B/32 visual (~350MB)...
Progress: ████████████████████ 100% (347.2 MB)
Models saved to: ~/.mostlylucid/models/
Total disk space: 1.16 GB
[Subsequent runs]
All models cached, analysis starts immediately
Degradazione graziosa:
// If ONNX model download fails, system falls back gracefully
EAST unavailable → Try CRAFT → Fall back to Tesseract PSM
Real-ESRGAN unavailable → Skip upscaling, use original image
CLIP unavailable → Skip embeddings, OCR still works
Florence-2 unavailable → Use Tesseract → Vision LLM escalation
Ogni fallimento del modello ONNX viene registrato con il percorso di fallback, assicurando che il sistema non crolla mai a causa di modelli mancante.
Nota di prezzo: Gli esempi di costi sotto usano un'immaginazione dei prezzi (~$0.005/immagine per Vision LLM). I costi reali dell'API variano secondo il provider e il modelloM SK3 L'intuizione centraleMSC4il processo locale elimina la maggior parte delle chiamate APIMST5differenze a prescindere dal prezzo specificoMSST6
Senza modelli ONNX (bassa linea):
Every image → Send to Vision LLM
Cost: ~$0.005/image (example pricing)
Time: ~2s network + inference
100 images = ~$0.50, ~200s
Con i modelli ONNX (locale-primaM SK2
85 images → EAST + Florence-2 (local)
Cost: $0
Time: ~200ms
10 images → EAST + Tesseract (local)
Cost: $0
Time: ~50ms
5 images → EAST + Vision LLM (escalation)
Cost: ~$0.025 (5 × $0.005)
Time: ~2s each
100 images = ~$0.025, ~30s total
risparmio: ~95% riduzione dei costiM SK2 ~85% più veloce, Il routing deterministico..
I modelli ONNX trasformano il sistema da "probabilistico fino alla base deterministica " a "escensione probabilistica solo quando è necessario."
La linea di base. veloceM SK1 determinista, funziona benissimo per un testo pulitoMSC3
public class OcrWave : IAnalysisWave
{
public string Name => "OcrWave";
public int Priority => 60; // After color/identity
public async Task<IEnumerable<Signal>> AnalyzeAsync(
string imagePath,
AnalysisContext context,
CancellationToken ct)
{
var signals = new List<Signal>();
// Get preprocessed image from cache
var image = context.GetCached<Image<Rgba32>>("image");
// Run Tesseract OCR
using var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default);
using var page = engine.Process(image);
var text = page.GetText();
var confidence = page.GetMeanConfidence();
signals.Add(new Signal
{
Key = "ocr.text", // Tesseract OCR result
Value = text,
Confidence = confidence,
Source = Name,
Tags = new List<string> { "ocr", "text" },
Metadata = new Dictionary<string, object>
{
["engine"] = "tesseract",
["mean_confidence"] = confidence,
["word_count"] = text.Split(' ').Length
}
});
signals.Add(new Signal
{
Key = "ocr.confidence",
Value = confidence,
Confidence = 1.0,
Source = Name
});
return signals;
}
}
I segnali chiave:
ocr.full_text - Il testo estrattoocr.early_exit - Signale per saltare il livello 2/3 se la fiducia è altaMicrosoft's FlorenceM SK1 è un modello linguistico di visione- che è eccezionale per la catturatura densa e l'OCRMST3 La versione ONNX funziona localmente senza costi APIMSC4
public class MlOcrWave : IAnalysisWave
{
private readonly Florence2OnnxModel _model;
public string Name => "MlOcrWave";
public int Priority => 51; // Runs AFTER Tesseract (priority 50)
public async Task<IEnumerable<Signal>> AnalyzeAsync(
string imagePath,
AnalysisContext context,
CancellationToken ct)
{
var signals = new List<Signal>();
// Check if Tesseract already succeeded with high confidence
var tesseractConfidence = context.GetValue<double>("ocr.confidence");
if (tesseractConfidence >= 0.95)
{
signals.Add(new Signal
{
Key = "ocr.ml.skipped", // Consistent namespace: ocr.ml.*
Value = true,
Confidence = 1.0,
Source = Name,
Metadata = new Dictionary<string, object>
{
["reason"] = "tesseract_high_confidence",
["tesseract_confidence"] = tesseractConfidence
}
});
return signals;
}
// Run Florence-2 OCR
var result = await _model.ExtractTextAsync(imagePath, ct);
signals.Add(new Signal
{
Key = "ocr.ml.text", // Florence-2 ML OCR text
Value = result.Text,
Confidence = result.Confidence,
Source = Name,
Tags = new List<string> { "ocr", "text", "ml" },
Metadata = new Dictionary<string, object>
{
["model"] = "florence2-base",
["inference_time_ms"] = result.InferenceTime,
["token_count"] = result.TokenCount
}
});
// For animated GIFs, extract all unique frames
if (context.GetValue<int>("identity.frame_count") > 1)
{
var frameResults = await ExtractMultiFrameTextAsync(
imagePath,
maxFrames: 10,
ct);
signals.Add(new Signal
{
Key = "ocr.ml.multiframe_text",
Value = frameResults.CombinedText,
Confidence = frameResults.AverageConfidence,
Source = Name,
Metadata = new Dictionary<string, object>
{
["frames_processed"] = frameResults.FrameCount,
["unique_text_segments"] = frameResults.UniqueSegments,
["deduplication_method"] = "levenshtein_85"
}
});
}
return signals;
}
}
Per i GIF animati, FlorenceM SK1 processi fino a 10 frammenti campionati in parallelo :
private async Task<MultiFrameResult> ExtractMultiFrameTextAsync(
string imagePath,
int maxFrames,
CancellationToken ct)
{
// Load GIF and extract frames
using var image = await Image.LoadAsync<Rgba32>(imagePath, ct);
var frames = new List<Image<Rgba32>>();
int frameCount = image.Frames.Count;
int step = Math.Max(1, frameCount / maxFrames);
for (int i = 0; i < frameCount; i += step)
{
frames.Add(image.Frames.CloneFrame(i));
}
// Process all frames in parallel (bounded concurrency to avoid thrashing)
var semaphore = new SemaphoreSlim(4); // Max 4 concurrent inferences
var tasks = frames.Select(async frame =>
{
await semaphore.WaitAsync(ct);
try
{
var result = await _model.ExtractTextAsync(frame, ct);
return result;
}
finally
{
semaphore.Release();
}
});
var results = await Task.WhenAll(tasks);
semaphore.Dispose();
// Deduplicate using Levenshtein distance
var uniqueTexts = DeduplicateByLevenshtein(
results.Select(r => r.Text).ToList(),
threshold: 0.85);
return new MultiFrameResult
{
CombinedText = string.Join("\n", uniqueTexts),
FrameCount = frames.Count,
UniqueSegments = uniqueTexts.Count,
AverageConfidence = results.Average(r => r.Confidence)
};
}
private List<string> DeduplicateByLevenshtein(
List<string> texts,
double threshold)
{
var unique = new List<string>();
foreach (var text in texts)
{
bool isDuplicate = false;
foreach (var existing in unique)
{
var distance = LevenshteinDistance(text, existing);
var maxLen = Math.Max(text.Length, existing.Length);
var similarity = 1.0 - (distance / (double)maxLen);
if (similarity >= threshold)
{
isDuplicate = true;
break;
}
}
if (!isDuplicate)
{
unique.Add(text);
}
}
return unique;
}
Esempio: 93-frame GIF → | | 10 frami campionati S→ \2 risultati di testo unico
Frame 1-45: "I'm not even mad."
Frame 46-93: "That's amazing."
Detezione del testo OpenCV (~5-20ms) determina il percorso da seguireM SK2
public class TextDetectionService
{
public TextDetectionResult DetectText(Image<Rgba32> image)
{
// Use OpenCV EAST text detector
var (regions, confidence) = RunEastDetector(image);
return new TextDetectionResult
{
HasText = regions.Count > 0,
RegionCount = regions.Count,
Confidence = confidence,
Route = SelectRoute(regions, confidence, image)
};
}
private ProcessingRoute SelectRoute(
List<TextRegion> regions,
double confidence,
Image<Rgba32> image)
{
// No text detected
if (regions.Count == 0)
return ProcessingRoute.NoOcr;
// Animated GIF with subtitle pattern
if (image.Frames.Count > 1 && HasSubtitlePattern(regions))
return ProcessingRoute.AnimatedFilmstrip;
// High confidence, standard text
if (confidence >= 0.8 && HasStandardTextCharacteristics(regions))
return ProcessingRoute.Fast; // Florence-2 only
// Moderate confidence
if (confidence >= 0.5)
return ProcessingRoute.Balanced; // Florence-2 + Tesseract voting
// Low confidence, complex image
return ProcessingRoute.Quality; // Full pipeline + Vision LLM
}
private bool HasSubtitlePattern(List<TextRegion> regions)
{
// Subtitles are typically in bottom 30% of frame
var bottomRegions = regions.Where(r =>
r.BoundingBox.Y > r.ImageHeight * 0.7);
return bottomRegions.Count() >= regions.Count * 0.5;
}
}
| Route | Triggers When | Processing S | Time M | Costo R |
|---|---|---|---|---|
| FAST | alta fiducia (>0.8), testo standard | Florence-2 solo M | ~100ms P | gratis R |
| BALANCED | Confidenza moderata (0.5-0.8) | FlorenceM SK3 ♫+ Vote tesserattico ♫ | ~300ms ♫ | |
| QUALItà | Bassa fiducia (<0.5), complesso | Multi -frame MESK4 Vision LLM S | ESK6s SESK7 $0.001-0.01 \ESK9 | |
| ANIMATED | GIF con schemi di sottotitoli | testo-per filmato solo | S~2-3s |
L'ottimizzazione rivoluzionaria dei sottotitoli GIF: estratto Solo le regioni di testo., non fotogrammi completi.
Appoggio tradizionale per un GIF 93-frame con sottotitoli:
Option 1: Process every frame
93 frames × 300×185 × ~150 tokens/frame = 13,950 tokens
Cost: ~$0.14 @ $0.01/1K tokens
Time: ~27 seconds
Option 2: Sample 10 frames
10 frames × 300×185 × ~150 tokens/frame = 1,500 tokens
Cost: ~$0.015
Time: ~3 seconds
Problem: Might miss subtitle changes
Estratto solo i quadrati di text bounding, eliminando pixel di fondo:
2 text regions × 250×50 × ~25 tokens/region = 50 tokens
Cost: ~$0.0005
Time: ~2 seconds
Token reduction: 30×
public class FilmstripService
{
public async Task<TextOnlyStrip> CreateTextOnlyStripAsync(
string imagePath,
CancellationToken ct)
{
using var gif = await Image.LoadAsync<Rgba32>(imagePath, ct);
// 1. Detect subtitle region (bottom 30% of frames)
var subtitleRegion = DetectSubtitleRegion(gif);
// 2. Extract frames with text changes
var uniqueFrames = ExtractUniqueTextFrames(gif, subtitleRegion);
// 3. Extract tight bounding boxes around text
var textRegions = ExtractTextBoundingBoxes(uniqueFrames);
// 4. Create horizontal strip of text-only regions
var strip = CreateHorizontalStrip(textRegions);
return new TextOnlyStrip
{
Image = strip,
RegionCount = textRegions.Count,
TotalTokens = EstimateTokens(strip),
OriginalTokens = EstimateTokens(gif),
Reduction = CalculateReduction(strip, gif)
};
}
private Rectangle DetectSubtitleRegion(Image<Rgba32> gif)
{
// Analyze bottom 30% of frame for text patterns
int subtitleHeight = (int)(gif.Height * 0.3);
int subtitleY = gif.Height - subtitleHeight;
return new Rectangle(0, subtitleY, gif.Width, subtitleHeight);
}
private List<Image<Rgba32>> ExtractUniqueTextFrames(
Image<Rgba32> gif,
Rectangle subtitleRegion)
{
var uniqueFrames = new List<Image<Rgba32>>();
Image<Rgba32>? previousFrame = null;
for (int i = 0; i < gif.Frames.Count; i++)
{
var frame = gif.Frames.CloneFrame(i);
var subtitleCrop = frame.Clone(ctx =>
ctx.Crop(subtitleRegion));
// Compare with previous frame
if (previousFrame == null ||
HasTextChanged(subtitleCrop, previousFrame, threshold: 0.05))
{
uniqueFrames.Add(subtitleCrop);
previousFrame = subtitleCrop;
}
}
return uniqueFrames;
}
private bool HasTextChanged(
Image<Rgba32> current,
Image<Rgba32> previous,
double threshold)
{
// Threshold bright pixels (white/yellow text on dark background)
var currentBright = CountBrightPixels(current);
var previousBright = CountBrightPixels(previous);
// Calculate Jaccard similarity of bright pixels
var intersection = currentBright.Intersect(previousBright).Count();
var union = currentBright.Union(previousBright).Count();
var similarity = union > 0 ? intersection / (double)union : 1.0;
// Text changed if similarity drops below threshold
return similarity < (1.0 - threshold);
}
// Helper type for bounding box + crop
private record TextCrop
{
public required Image<Rgba32> CroppedImage { get; init; }
public required Rectangle Bounds { get; init; }
}
private List<TextCrop> ExtractTextBoundingBoxes(
List<Image<Rgba32>> frames)
{
var textCrops = new List<TextCrop>();
foreach (var frame in frames)
{
// Threshold to get text mask
var mask = ThresholdBrightPixels(frame, minValue: 200);
// Find connected components (text regions)
var components = FindConnectedComponents(mask);
// Get tight bounding box around all components
var bbox = GetTightBoundingBox(components);
// Add padding
bbox.Inflate(5, 5);
// Clone the region (dispose properly in production!)
var cropped = frame.Clone(ctx => ctx.Crop(bbox));
textCrops.Add(new TextCrop
{
CroppedImage = cropped,
Bounds = bbox
});
}
return textCrops;
}
private Image<Rgba32> CreateHorizontalStrip(
List<TextCrop> textCrops)
{
// Calculate strip dimensions
int totalWidth = textCrops.Sum(c => c.Bounds.Width);
int maxHeight = textCrops.Max(c => c.Bounds.Height);
// Create blank canvas
var strip = new Image<Rgba32>(totalWidth, maxHeight);
// Paste text regions horizontally
int xOffset = 0;
foreach (var crop in textCrops)
{
strip.Mutate(ctx => ctx.DrawImage(
crop.CroppedImage,
new Point(xOffset, 0),
opacity: 1.0f));
xOffset += crop.Bounds.Width;
// Dispose crop after use (important!)
crop.CroppedImage.Dispose();
}
return strip;
}
}
Input: anchorman-not-even-mad.gif (93 frames, 300×185)
Trattura:
1. Detect subtitle region: bottom 30% (300×55)
2. Extract unique frames: 93 frames → 2 text changes
3. Extract tight bounding boxes:
- Frame 1-45: "I'm not even mad." → 252×49 bbox
- Frame 46-93: "That's amazing." → 198×49 bbox
4. Create horizontal strip: 450×49 total
Output: testo-unica striscia (450×49)

Token Economics:
30× riduzione mantenendo tutti i testi sottotitoli.
Quando sia Tesseract che Florence-2 falliscono o producono risultati bassi- risultati di fiduciaM SK2 si espandeno ad un Vision LLM (GPTMST4oMSSK5 Claude MSC6 SonnetMSV7 Gemini Pro VisionMSP8 o modelli Ollama come minicpmMSL9vMSS10
public class OcrQualityWave : IAnalysisWave
{
private readonly SpellChecker _spellChecker;
public string Name => "OcrQualityWave";
public int Priority => 58; // After Florence-2 and Tesseract
public async Task<IEnumerable<Signal>> AnalyzeAsync(
string imagePath,
AnalysisContext context,
CancellationToken ct)
{
var signals = new List<Signal>();
// Get best OCR result from earlier waves (priority order)
string? ocrText =
context.GetValue<string>("ocr.ml.text") ?? // Florence-2 (priority 51)
context.GetValue<string>("ocr.text"); // Tesseract (priority 50)
if (string.IsNullOrWhiteSpace(ocrText))
{
signals.Add(new Signal
{
Key = "ocr.quality.no_text",
Value = true,
Confidence = 1.0,
Source = Name
});
return signals;
}
// Run spell check (deterministic quality assessment)
var spellResult = _spellChecker.CheckTextQuality(ocrText);
// Additional quality signals to avoid false positives
var alphanumRatio = CalculateAlphanumericRatio(ocrText); // Letters/digits vs junk
var avgTokenLength = CalculateAverageTokenLength(ocrText);
signals.Add(new Signal
{
Key = "ocr.quality.spell_check_score",
Value = spellResult.CorrectWordsRatio,
Confidence = 1.0,
Source = Name,
Metadata = new Dictionary<string, object>
{
["total_words"] = spellResult.TotalWords,
["correct_words"] = spellResult.CorrectWords,
["garbled_words"] = spellResult.GarbledWords,
["alphanum_ratio"] = alphanumRatio,
["avg_token_length"] = avgTokenLength
}
});
// Deterministic escalation threshold
// NOTE: Spellcheck alone can false-trigger on proper nouns, memes, brand names.
// Use additional signals (alphanum ratio, token length) to reduce false escalations.
bool isGarbled = spellResult.CorrectWordsRatio < 0.5 &&
alphanumRatio > 0.7; // Mostly valid characters, just not in dictionary
signals.Add(new Signal
{
Key = "ocr.quality.is_garbled",
Value = isGarbled,
Confidence = 1.0,
Source = Name
});
// Signal Vision LLM escalation
if (isGarbled)
{
signals.Add(new Signal
{
Key = "ocr.quality.escalation_required",
Value = true,
Confidence = 1.0,
Source = Name,
Tags = new List<string> { "action_required", "escalation" },
Metadata = new Dictionary<string, object>
{
["reason"] = "spell_check_below_threshold",
["quality_score"] = spellResult.CorrectWordsRatio,
["threshold"] = 0.5,
["target_tier"] = "vision_llm"
}
});
// Cache garbled text for Vision LLM to access
context.SetCached("ocr.garbled_text", ocrText);
}
return signals;
}
}
L'escalazione è determinista.: punteggio di controllo dello spelling < 50% ≥→ escalate. Nessun giudizio probabilisticoM SK5
Quando è attivata l'escalazione per i GIF animati, usa il testo-only stripM SK2
public class VisionLlmWave : IAnalysisWave
{
private readonly IVisionLlmClient _client;
public string Name => "VisionLlmWave";
public int Priority => 50;
public async Task<IEnumerable<Signal>> AnalyzeAsync(
string imagePath,
AnalysisContext context,
CancellationToken ct)
{
var signals = new List<Signal>();
// Check if escalation is required
var escalationRequired = context.GetValue<bool>(
"ocr.quality.escalation_required");
if (!escalationRequired)
{
signals.Add(new Signal
{
Key = "vision.llm.skipped",
Value = true,
Confidence = 1.0,
Source = Name,
Metadata = new Dictionary<string, object>
{
["reason"] = "no_escalation_required"
}
});
return signals;
}
// For animated GIFs, use text-only strip
string imageToProcess = imagePath;
bool usedFilmstrip = false;
if (context.GetValue<int>("identity.frame_count") > 1)
{
var filmstrip = await CreateTextOnlyStripAsync(imagePath, ct);
imageToProcess = filmstrip.Path;
usedFilmstrip = true;
signals.Add(new Signal
{
Key = "vision.filmstrip.created",
Value = true,
Confidence = 1.0,
Source = Name,
Metadata = new Dictionary<string, object>
{
["mode"] = "text_only",
["region_count"] = filmstrip.RegionCount,
["token_reduction"] = filmstrip.Reduction,
["original_tokens"] = filmstrip.OriginalTokens,
["final_tokens"] = filmstrip.TotalTokens
}
});
}
// Build constrained prompt
var prompt = BuildConstrainedPrompt(context);
// Call Vision LLM
var result = await _client.ExtractTextAsync(
imageToProcess,
prompt,
ct);
// Emit OCR text signal (Vision LLM tier)
signals.Add(new Signal
{
Key = "ocr.vision.text", // Vision LLM OCR result
Value = result.Text,
Confidence = 0.95, // High but not 1.0 - still probabilistic
Source = Name,
Tags = new List<string> { "ocr", "vision", "llm" },
Metadata = new Dictionary<string, object>
{
["model"] = result.Model,
["used_filmstrip"] = usedFilmstrip,
["inference_time_ms"] = result.InferenceTime,
["token_count"] = result.TokenCount,
["cost_usd"] = result.Cost
}
});
// Optionally emit caption if requested (separate from OCR)
if (result.Caption != null)
{
signals.Add(new Signal
{
Key = "caption.text", // Descriptive caption, not OCR
Value = result.Caption,
Confidence = 0.90,
Source = Name,
Tags = new List<string> { "caption", "description" }
});
}
return signals;
}
private string BuildConstrainedPrompt(AnalysisContext context)
{
var sb = new StringBuilder();
sb.AppendLine("Extract all text from this image.");
sb.AppendLine();
sb.AppendLine("CONSTRAINTS:");
sb.AppendLine("- Only extract text that is actually visible");
sb.AppendLine("- Preserve formatting and line breaks");
sb.AppendLine("- If no text is present, return empty string");
sb.AppendLine();
// Add context from earlier waves
var garbledText = context.GetCached<string>("ocr.garbled_text");
if (!string.IsNullOrEmpty(garbledText))
{
sb.AppendLine("CONTEXT:");
sb.AppendLine("Traditional OCR detected garbled text:");
sb.AppendLine($" \"{garbledText}\"");
sb.AppendLine("Use this as a hint for stylized or unusual fonts.");
sb.AppendLine();
}
sb.AppendLine("Return only the extracted text, no commentary.");
return sb.ToString();
}
}
Quando tutti i livelli sono terminati, la selezione finale del testo usa un ordine di priorità rigido:
public static string? GetFinalText(DynamicImageProfile profile)
{
// Priority chain (highest to lowest quality)
// NOTE: This selects ONE source, but the ledger exposes ALL sources
// with confidence scores for downstream inspection
// 1. Vision LLM OCR (best for complex/garbled text)
var visionText = profile.GetValue<string>("ocr.vision.text");
if (!string.IsNullOrEmpty(visionText))
return visionText;
// 2. Florence-2 multi-frame GIF OCR (best for animations)
var florenceMultiText = profile.GetValue<string>("ocr.ml.multiframe_text");
if (!string.IsNullOrEmpty(florenceMultiText))
return florenceMultiText;
// 3. Florence-2 single-frame ML OCR (good for stylized fonts)
var florenceText = profile.GetValue<string>("ocr.ml.text");
if (!string.IsNullOrEmpty(florenceText))
return florenceText;
// 4. Tesseract OCR (reliable for clean standard text)
var tesseractText = profile.GetValue<string>("ocr.text");
if (!string.IsNullOrEmpty(tesseractText))
return tesseractText;
// 5. Fallback (empty)
return string.Empty;
}
Ogni livello ha caratteristiche conosciute.:
| Source | Key di segnale | Perfetto per M | Confidenza D | Costo R | velocità P | ||
|---|---|---|---|---|---|---|---|
| Vision LLM OCR | ocr.vision.text |
Diagrammi complessiM SK1 testo rotto, frammentato | 0.95 | \ $0.001-0.01 ♫ ♫ | ♫ | ||
Florenza-2 (GIFM SK3 ocr.ml.multiframe_text |
GIF animati con sottotitoli | 0.85-0.92 | |||||
Florenza-2 (singleM SK3 ocr.ml.text |
caratteri stilizzatiM SK1 memes, testo decorativo | 0.85-0.90 | gratis | |||||
| Tesseract | ocr.text |
testo standard pulitoM SK1 contrasto alto | Varie | gratis SSK4 S~50ms M |
100 immaginiM SK1 tutti usando Vision LLM:
100 images × $0.005/image = $0.50
Total time: 100 × 2s = 200 seconds
Distribuzione delle rotte ( tipica):
Cost:
60 × $0 = $0
25 × $0 = $0
10 × $0.005 = $0.05
5 × $0.002 = $0.01
Total: $0.06
Time:
60 × 0.1s = 6s
25 × 0.3s = 7.5s
10 × 2s = 20s
5 × 2.5s = 12.5s
Total: 46 seconds
Savings:
Cost: 88% reduction ($0.50 → $0.06)
Time: 77% reduction (200s → 46s)
Il livello medio (Florence-2) gestisce 85% di immagini a costo zero
Qui è il flusso completo di un meme GIF con sottotitoli.
1. Load image: anchorman-not-even-mad.gif (93 frames)
2. IdentityWave (priority 10):
→ identity.frame_count = 93
→ identity.format = "gif"
→ identity.is_animated = true
3. TextLikelinessWave (priority 40, ~10ms):
→ Heuristic text detection: 15 regions in bottom 30%
→ Subtitle pattern: DETECTED
→ text.likeliness = 0.85
4. OcrWave (priority 50, ~60ms):
→ Run Tesseract OCR on first frame
→ ocr.text = "I'm not emn mad." (garbled)
→ ocr.confidence = 0.62
5. MlOcrWave (priority 51, ~180ms):
→ Tesseract confidence < 0.95, run Florence-2
→ Sample 10 frames (animated GIF)
→ Run Florence-2 on each frame (parallel)
→ Deduplicate: 10 results → 2 unique texts
→ ocr.ml.multiframe_text = "I'm not even mad.\nThat's amazing."
→ ocr.ml.confidence = 0.91
6. OcrQualityWave (priority 58, ~5ms):
→ Check Florence-2 result
→ Spell check: 6/6 words correct (100%)
→ ocr.quality.is_garbled = false
→ ocr.quality.escalation_required = false
7. VisionLlmWave (priority 80, SKIPPED):
→ No escalation required (Florence-2 succeeded)
Final output:
Text: "I'm not even mad.\nThat's amazing."
Source: ocr.ml.multiframe_text
Confidence: 0.91
Cost: $0 (local processing)
Time: ~250ms total (Tesseract + Florence-2)
Se Florence-2 fosse andata storta M SK1confidenza < 0.5), il flusso sarebbe continuato:
6. OcrQualityWave:
→ Spell check: 2/6 words correct (33%)
→ ocr.quality.is_garbled = true
→ ocr.quality.escalation_required = true
7. VisionLlmWave:
→ Create text-only filmstrip (2 regions, 450×49)
→ Send to Vision LLM: "Extract all text from this strip"
→ vision.llm.text = "I'm not even mad.\nThat's amazing."
→ Confidence: 0.95
→ Cost: ~$0.002 (30× token reduction vs full frames)
→ Time: ~2.3s
Il sistema a tre livelli è completamente configurabile.
{
"DocSummarizer": {
"Ocr": {
"Tesseract": {
"Enabled": true,
"DataPath": "/usr/share/tesseract-ocr/4.00/tessdata",
"Languages": ["eng"],
"EarlyExitThreshold": 0.95
},
"Florence2": {
"Enabled": true,
"ModelPath": "models/florence2-base",
"ConfidenceThreshold": 0.85,
"MaxFrames": 10,
"DeduplicationMethod": "levenshtein",
"LevenshteinThreshold": 0.85
},
"Quality": {
"SpellCheckThreshold": 0.5,
"EscalationEnabled": true
}
},
"VisionLlm": {
"Enabled": true,
"Provider": "ollama",
"OllamaUrl": "http://localhost:11434",
"Model": "minicpm-v:8b",
"MaxRetries": 3,
"TimeoutSeconds": 30
},
"Filmstrip": {
"TextOnlyMode": true,
"SubtitleRegionPercent": 0.3,
"BrightPixelThreshold": 200,
"TextChangeThreshold": 0.05
},
"Routing": {
"FastRouteConfidence": 0.8,
"BalancedRouteConfidence": 0.5,
"TextDetectionEnabled": true
}
}
}
| Esplosione | Detezione | |||||||
|---|---|---|---|---|---|---|---|---|
| Tesseract fallisce. | Confidenza < 0.7 O controllo di ortografia | 3 | 4 | 5 | Andare a Firenze | 6 | 7 | |
| Florence-2 fallisce | Confidenza < 0.5 O controllo di ortografia | 3 | 4 | 5 | Escalare a Vision LLM | 6 | ||
| La fine del tempo di Vision LLM | La richiesta supera 30s | Torniamo al miglior risultato OCR disponibile MSC3 | ||||||
| Tutti i livelli falliscono. | Tutti i risultati vuoti o spazzaturati | Rendere la sequenza vuota con fiducia | ||||||
| Limito di costo dell'API | Budget giornaliero superato | Disable Vision LLMM SK2 usare Florence-2 solo | ||||||
| Il modello non è disponibile. | FlorenceM SK1Vision LLM offline | Skip tier, continue to next |
Ogni fallimento è deterministico e registrato con la provenienza completa.
For each image:
1. Run Tesseract
2. If looks wrong, manually fix or skip
Problems:
- No middle tier (binary: works or doesn't)
- Manual intervention required
- No cost optimization
For each image:
1. Send to GPT-4o/Claude
2. Pay $0.005-0.01 per image
Problems:
- Expensive (85% of images could be free)
- Slow (network latency)
- Still hallucinates without constraints
For each image:
1. OpenCV text detection (5-20ms, free)
2. Route to appropriate tier
3. Florence-2 handles 85% locally (200ms, free)
4. Vision LLM only for complex cases (2-5s, $0.001-0.01)
Benefits:
- 88% cost reduction
- 77% faster (most images process locally)
- Deterministic escalation (auditable)
- Filmstrip optimization (30× token reduction)
- Constrained by deterministic signals
Il tubo di 3 -tier OCR dimostra che Costo-aware routing e local-processo iniziale può migliorare drasticamente sia la performance che l'economia senza sacrificare la qualità.
Principali scoperte:
Le scale del modello: Analyse deterministica locale → modello locale di ML → escalazione della nuvola, ogni livello con caratteristiche conosciute e scambio di costi -offsM SK2
Questo è l'incertezza limitata applicata ai segnali deterministici OCR: ( controllo spellingoM SK2 rilevamento del testo) modelli probabilistici di limitazione MSC4FlorenzaMST5 Vision LLMMst6 e il risultato finale aggrega le fonti secondo la qualitàMSSK7
| Parte | Pattern | Focus S |
|---|---|---|
| 1 | Fuzzinesse limitata | Componente unica |
| 2 | MoM Fuzzy Constretto | Numerosi componenti |
| 3 | Dragging Contexto | Tempo / memoria |
| 4 | Intelligenza dell'immagine | Architectura delle ondeM SK1 modelli |
| 4.1 | Il Trio Pipeline -Tier OCR (Questo articolo) | OCR, Modelli ONNX, filmtrip |
Più avanti: Parte 5 mostrerà come ImageSummarizer DocSummarizer, e DataSummarizer comporsi in grafico multi-modal RAG con LucidRAG.
Tutte le parti segueno la stessa invariante: Componenti probabilistice propose; sistemi deterministici persistono.
© 2026 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.