mcap/modules/OCR/module.jai
Musa Mahmood 775c2873e4 [0.0.10] Shutdown: say what it is waiting on, stop OCR mid-batch, no race with the frame loop
- The exit overlay says which step of shutdown is running and what it waits on ("Stopping the OCR
  scan: finishing the text lines it is reading.", the import, the viewer, the models), and how long
  that step has taken once it passes 2 s. It said "Saving..." for all of it.
- OCR stops mid-batch: ocr_process takes a cancel flag (Ocr_Engine.cancel), checked by its workers
  before each piece of work; a scan sets it to its own cancel. A cut-short batch stores nothing and
  counts no failures. Closing during a scan took minutes when a batch held large images; now well
  under a second.
- Once shutdown starts, the main thread stops running the app's frames (the viewer, search, import,
  OCR, Stats) and draws only the overlay: background_frame read the search thread group while the
  shutdown thread was tearing it down, an access violation on exit.
2026-10-09 23:37:35 +01:00

404 lines
17 KiB
Plaintext

// OCR: the text in images, with where each line is. A batch of RGBA images goes in; each comes back
// as its text lines, with a box and a confidence for each.
//
// engine, ok, error := ocr_create(models_folder, "ppocr-v5-mobile");
// results := ocr_process(engine, images);
//
// The models are listed in models_folder/models.config (fetch_models.jai downloads them) and run
// with onnxruntime (modules/onnxruntime), on an NVIDIA GPU through CUDA when it can, else on the
// CPU. ocr_gpu_status says why the GPU is not in use, and what to install so it can be.
//
// It runs PP-OCRv5's pipeline: detection (detect.jai) finds each line as a rectangle at any angle,
// then recognition (recognize.jai) reads it level. When the model lists an orientation classifier,
// lines that look upside down are read both ways; lines read downwards or upwards are read too.
Ocr_Provider :: enum { CPU; CUDA; }
// Tightly packed or not: stride is the bytes from one row to the next.
Ocr_Image :: struct {
pixels: *u8; // RGBA
width, height, stride: s32;
}
Ocr_Line :: struct {
x, y, width, height: s32; // in the image: the bounds of corners
text: string;
confidence: float; // 0 to 1: the mean probability of its characters
corners: [4] Vector2; // in the image's pixels: the text's top left, top right, bottom right, bottom left
}
Ocr_Result :: struct {
lines: [] Ocr_Line; // top to bottom, then left to right
failed: bool;
error: string;
}
Ocr_Engine :: struct {
model: Ocr_Model_Info;
provider: Ocr_Provider;
gpu_explanation: string; // why the GPU is not in use; "" when it is
env: *OrtEnv;
memory_info: *OrtMemoryInfo;
det, rec: Ocr_Session;
orientation: Ocr_Session; // the text line orientation classifier; none when its session is null
orientation_width, orientation_height: s32; // its input size
dictionary: [] string; // class n is dictionary[n - 1]
workers: s32; // threads ocr_process uses
rec_batch: s32; // text lines per recognizer run: the model's rec_batch_gpu or rec_batch_cpu
options: Ocr_Options;
stats: Ocr_Stats; // since creation, or the last ocr_reset_stats
// When set, and true, ocr_process stops starting work: the batch of lines or image each worker is
// on is finished, and then every image fails as "cancelled" (none is part read). Set by the caller.
cancel: *bool;
}
OCR_CANCELLED :: "cancelled";
ocr_cancelled :: (engine: *Ocr_Engine) -> bool {
return engine.cancel && engine.cancel.*;
}
// Time spent in each stage, summed over the worker threads (so it can exceed the elapsed time).
Ocr_Stats :: struct {
images: s64;
lines: s64;
detect_us: s64; // preparing images, the detector, and finding the boxes
recognize_us: s64; // cropping lines, the recognizer, and decoding the text
orientation_us: s64; // cropping lines and the orientation classifier
turned: s64; // lines that read better the other way up, or level as found
}
ocr_stats :: (engine: *Ocr_Engine) -> Ocr_Stats { return engine.stats; }
ocr_reset_stats :: (engine: *Ocr_Engine) { engine.stats = .{}; }
// How an engine runs on the GPU, beyond its model's models.config entry. The defaults are what ran
// fastest on the 20 sample captures (RTX 5080 laptop, onnxruntime 1.30.0): nearly every image and
// batch is a different size, and with onnxruntime's exact-size arena and cuDNN's largest workspaces
// GPU memory filled up and the server model ran several times slower.
Ocr_Options :: struct {
gpu_workers: s32 = 4; // threads preparing images and decoding results while the GPU runs the networks; more let the server model fill GPU memory
cuda_arena_extend_strategy := "kNextPowerOfTwo"; // reuses freed blocks for other sizes; "kSameAsRequested" grows by exactly what is asked
cudnn_max_workspace := false; // let cuDNN take as much working memory as it finds best (onnxruntime's own default)
rec_width_step: s32 = 64; // a recognizer batch's width is rounded up to a multiple of this, so shapes recur
}
// An engine for the model named in models_folder/models.config. With prefer_gpu it runs on CUDA when
// every library that needs is present; otherwise, or when CUDA fails to start, on the CPU.
ocr_create :: (models_folder: string, model_name: string, prefer_gpu := true, options := Ocr_Options.{}) -> *Ocr_Engine, ok: bool, error: string {
push_allocator(context.default_allocator);
loaded, load_error := ort_load();
if !loaded return null, false, load_error;
models, models_error := ocr_list_models(models_folder);
if models_error return null, false, models_error;
model: *Ocr_Model_Info;
for * models if it.name == model_name { model = it; break; }
if !model return null, false, tprint("% has no [model %].", OCR_MODELS_CONFIG, model_name);
engine := New(Ocr_Engine);
engine.model = model.*;
engine.options = options;
dictionary_text, dictionary_ok := read_entire_file(tprint("%/%", models_folder, model.dictionary), log_errors = false);
if !dictionary_ok { ocr_destroy(engine); return null, false, tprint("Cannot read the dictionary %/%.", models_folder, model.dictionary); }
entries := split(dictionary_text, "\n");
for * entries if it.count && it.*[it.count - 1] == #char "\r" it.count -= 1;
while entries.count && !entries[entries.count - 1].count entries.count -= 1; // the final newline
engine.dictionary = entries;
api := ort.api;
ok, error := ort_check(api.CreateEnv(.ORT_LOGGING_LEVEL_WARNING, "ocr", *engine.env));
if !ok { ocr_destroy(engine); return null, false, error; }
ort_check(api.CreateCpuMemoryInfo(.OrtArenaAllocator, .OrtMemTypeDefault, *engine.memory_info));
cores := cast(s32) max(get_number_of_processors(.ALL_LOGICAL), 1); // efficiency cores included
use_gpu := false;
if prefer_gpu {
available, explanation := ort_cuda_check("OCR"); // modules/onnxruntime/cuda.jai
use_gpu = available;
if !available engine.gpu_explanation = copy_string(explanation);
} else {
engine.gpu_explanation = copy_string("OCR runs on the CPU, as it was asked to.");
}
if use_gpu {
// A few CPU threads prepare images and decode results while the GPU runs the networks.
engine.workers = max(options.gpu_workers, 1);
ort_gpu_lock(); // one CUDA engine made or released at a time (modules/onnxruntime/session.jai)
sessions_ok, sessions_error := ocr_create_sessions(engine, models_folder, true, 2);
if sessions_ok sessions_ok, sessions_error = ocr_warm_up(engine);
if !sessions_ok ocr_release_sessions(engine);
ort_gpu_unlock();
if sessions_ok {
engine.provider = .CUDA;
engine.rec_batch = max(engine.model.rec_batch_gpu, 1);
} else {
engine.gpu_explanation = sprint("OCR runs on the CPU: the CUDA libraries were found, but onnxruntime could not start on the GPU:\n%", sessions_error);
use_gpu = false;
}
}
if !use_gpu {
// Several images at once, each network run spread over the rest of the cores.
engine.workers = clamp(cores / 4, 1, 8);
threads := max(cores / engine.workers, 1);
sessions_ok, sessions_error := ocr_create_sessions(engine, models_folder, false, threads);
if !sessions_ok { ocr_destroy(engine); return null, false, sessions_error; }
engine.provider = .CPU;
engine.rec_batch = max(engine.model.rec_batch_cpu, 1);
}
return engine, true, "";
}
ocr_provider :: (engine: *Ocr_Engine) -> Ocr_Provider {
return engine.provider;
}
// Whether the GPU is in use, and when not, why, and what to install so it can be.
ocr_gpu_status :: (engine: *Ocr_Engine) -> in_use: bool, explanation: string {
return engine.provider == .CUDA, engine.gpu_explanation;
}
ocr_destroy :: (engine: *Ocr_Engine) {
if !engine return;
if engine.provider == .CUDA ort_gpu_lock();
ocr_release_sessions(engine);
if engine.provider == .CUDA ort_gpu_unlock();
if engine.memory_info ort.api.ReleaseMemoryInfo(engine.memory_info);
if engine.env ort.api.ReleaseEnv(engine.env);
free(engine.gpu_explanation,, context.default_allocator);
free(engine,, context.default_allocator); // the dictionary and model strings are kept: small, and shared with ocr_list_models' caller
}
// The text in each image. Results (lines and their text) come from allocator. An image that fails
// is marked failed with the reason; the others are unaffected.
ocr_process :: (engine: *Ocr_Engine, images: [] Ocr_Image, allocator := context.allocator) -> [] Ocr_Result {
results := NewArray(images.count, Ocr_Result,, allocator);
if !images.count return results;
// Detection: images shared out among the workers.
per_image := NewArray(images.count, Image_Work,, context.default_allocator);
defer {
for * per_image array_free(it.boxes);
array_free(per_image,, context.default_allocator);
}
for * per_image it.image = *images[it_index];
run_on_workers(engine, per_image.count, (engine: *Ocr_Engine, index: s64, data: *void) {
work := cast(*Image_Work) data + index;
start := current_time_monotonic();
work.boxes, work.ok, work.error = ocr_detect(engine, work.image.*);
atomic_add(*engine.stats.detect_us, to_microseconds(current_time_monotonic() - start));
if work.error work.error = copy_string(work.error,, context.default_allocator);
}, per_image.data);
if ocr_cancelled(engine) return ocr_all_cancelled(results, allocator);
// Recognition: every line of every image, batched by width so little of each batch is padding.
jobs: [..] Line_Job;
jobs.allocator = context.default_allocator;
defer {
for jobs free(it.text,, context.default_allocator);
array_free(jobs);
}
rec_height := engine.model.rec_height;
turned: [..] bool; // per first job: whether detection turned its box a quarter
turned.allocator = context.default_allocator;
defer array_free(turned);
for work: per_image {
if !work.ok continue;
for work.boxes {
array_add(*jobs, .{image = work.image, corners = it.corners, width = ocr_line_width(it.corners, rec_height)});
array_add(*turned, it.turned);
}
}
// Other ways to read a line, recognized beside it, the more confident reading kept: upside down,
// where the orientation classifier finds it more likely than not; and level as it was found, for a
// box detection turned a quarter, since a narrow word such as "1" is as tall as a line read downwards.
first_count := jobs.count;
upside_down := NewArray(first_count, bool,, context.default_allocator);
defer array_free(upside_down,, context.default_allocator);
Orientation_Context :: struct { jobs: [] Line_Job; upside_down: [] bool; size: s64; failed: bool; error: string; }
classify := Orientation_Context.{jobs = jobs, upside_down = upside_down, size = max(engine.rec_batch, 1)};
if engine.orientation.session && first_count {
run_on_workers(engine, (first_count + classify.size - 1) / classify.size, (engine: *Ocr_Engine, index: s64, data: *void) {
c := cast(*Orientation_Context) data;
first := index * c.size;
count := min(c.size, c.jobs.count - first);
batch := NewArray(count, *Line_Job,, temp);
for * batch it.* = *c.jobs[first + it_index];
start := current_time_monotonic();
ok, error := ocr_classify_batch(engine, batch, array_view(c.upside_down, first, count));
atomic_add(*engine.stats.orientation_us, to_microseconds(current_time_monotonic() - start));
if !ok { c.failed = true; c.error = copy_string(error,, context.default_allocator); } // the last error wins
}, *classify);
}
defer free(classify.error,, context.default_allocator);
for n: 0..first_count - 1 {
job := jobs[n];
if upside_down[n] array_add(*jobs, .{image = job.image, corners = ocr_corners_turned(job.corners, 2), width = job.width, primary = n});
if turned[n] {
level := ocr_corners_turned(job.corners, 3);
array_add(*jobs, .{image = job.image, corners = level, width = ocr_line_width(level, rec_height), primary = n});
}
}
order := NewArray(jobs.count, *Line_Job,, context.default_allocator);
defer array_free(order,, context.default_allocator);
for * jobs order[it_index] = it;
quick_sort(order, (a: *Line_Job, b: *Line_Job) -> s64 { return a.width - b.width; });
batch_size := max(engine.rec_batch, 1);
batch_count := (order.count + batch_size - 1) / batch_size;
Batch_Context :: struct { order: [] *Line_Job; size: s64; failed: bool; error: string; }
batches := Batch_Context.{order = order, size = batch_size};
run_on_workers(engine, batch_count, (engine: *Ocr_Engine, index: s64, data: *void) {
b := cast(*Batch_Context) data;
first := index * b.size;
count := min(b.size, b.order.count - first);
start := current_time_monotonic();
ok, error := ocr_recognize_batch(engine, array_view(b.order, first, count));
atomic_add(*engine.stats.recognize_us, to_microseconds(current_time_monotonic() - start));
if !ok { b.failed = true; b.error = copy_string(error,, context.default_allocator); } // the last error wins; any is enough to say why
}, *batches);
if ocr_cancelled(engine) {
free(batches.error,, context.default_allocator);
return ocr_all_cancelled(results, allocator);
}
if classify.failed && !batches.failed {
batches.failed = true;
batches.error = copy_string(classify.error,, context.default_allocator);
}
// The more confident of each line's readings takes the first job's place.
for * other: array_view(jobs, first_count, jobs.count - first_count) {
first := *jobs[other.primary];
if other.confidence > first.confidence {
swap(*first.text, *other.text);
first.corners, first.confidence = other.corners, other.confidence;
engine.stats.turned += 1;
}
}
// Each image's lines, in reading order.
for work, image_index: per_image {
result := *results[image_index];
if !work.ok {
result.failed = true;
result.error = copy_string(work.error,, allocator);
free(work.error,, context.default_allocator);
continue;
}
if batches.failed {
result.failed = true;
result.error = copy_string(batches.error,, allocator);
continue;
}
lines: [..] Ocr_Line;
lines.allocator = allocator;
for job: array_view(jobs, 0, first_count) {
if job.image != work.image || !job.text.count continue;
x0, y0, x1, y1 := ocr_corner_bounds(job.corners, job.image.width, job.image.height);
array_add(*lines, .{x = x0, y = y0, width = x1 - x0, height = y1 - y0, text = copy_string(job.text,, allocator),
confidence = job.confidence, corners = job.corners});
}
// Rows by the middle of each line, then left to right within a row.
quick_sort(lines, (a: Ocr_Line, b: Ocr_Line) -> s64 {
ay, by := a.y + a.height / 2, b.y + b.height / 2;
if abs(ay - by) > min(a.height, b.height) / 2 return ifx ay < by then -1 else 1;
return a.x - b.x;
});
result.lines = lines;
engine.stats.lines += lines.count;
}
engine.stats.images += images.count;
free(batches.error,, context.default_allocator);
return results;
}
#scope_module
ocr_all_cancelled :: (results: [] Ocr_Result, allocator: Allocator) -> [] Ocr_Result {
for * results {
it.failed = true;
it.error = copy_string(OCR_CANCELLED,, allocator);
}
return results;
}
Image_Work :: struct {
image: *Ocr_Image;
boxes: [..] Detected_Box;
ok: bool;
error: string;
}
Work_Proc :: #type (engine: *Ocr_Engine, index: s64, data: *void);
// Calls proc for each index below count, on engine.workers threads at once.
run_on_workers :: (engine: *Ocr_Engine, count: s64, proc: Work_Proc, data: *void) {
if count <= 0 return;
Shared :: struct { engine: *Ocr_Engine; count: s64; next: s64; proc: Work_Proc; data: *void; }
shared := Shared.{engine = engine, count = count, proc = proc, data = data};
worker_proc :: (thread: *Thread) -> s64 {
// The context comes from the calling thread, allocator included, and that may be its temporary
// allocator, which belongs to that thread alone. Workers allocate from the heap, and use their
// own temporary storage.
context.allocator = context.default_allocator;
shared := cast(*Shared) thread.data;
while true {
if ocr_cancelled(shared.engine) break; // what is left fails as cancelled (ocr_process)
index := atomic_add(*shared.next, 1);
if index >= shared.count break;
shared.proc(shared.engine, index, shared.data);
reset_temporary_storage();
}
return 0;
}
// thread_init allocates the thread's temporary storage from the context allocator and tells it
// the thread is starting; the caller's may be a temporary allocator, which refuses that.
push_allocator(context.default_allocator);
thread_count := min(cast(s64) engine.workers, count);
threads := NewArray(thread_count, Thread,, context.default_allocator);
defer array_free(threads,, context.default_allocator);
started := 0;
for * threads {
if !thread_init(it, worker_proc, temporary_storage_size = 1024 * 1024) break;
thread_start(it, *shared); // sets thread.data
started += 1;
}
if !started { // no threads: do it here
here: Thread;
here.data = *shared;
worker_proc(*here);
}
for 0..started - 1 {
thread_is_done(*threads[it], -1);
thread_deinit(*threads[it]);
}
}
#load "models.jai";
#load "detect.jai";
#load "recognize.jai";
#import "Basic";
#import "String";
#import "File";
#import "File_Utilities";
#import "Math";
#import "Sort";
#import "System";
#import "Thread";
#import "Atomics";
#import "onnxruntime";
#if OS == .WINDOWS {
#import "Windows";
#import "Windows_Utf8";
} else {
#import "POSIX";
}