Files
reims-vgpu/scripts/internal/vm_ocr.swift
T
Anees Iqbal 28aecd0f04 🎨 Move the development scripts to scripts/internal
scripts/ mixed the one script a person installing reims-ctl runs,
install-ctl.sh, with eight that only developing the project runs: the worktree
and build environment, the GPU locks, the VM boot and tour drivers, the window
walk and the IOSurface qtest. Those move to scripts/internal, so scripts/ holds
what users run. run_vmapple.sh, walk_window_macos.sh and vm_drive.py find the
repository from their own path, one level deeper now; every reference in the
docs, the Makefile, the video crates' tools and the code's comments follows.
2026-10-26 14:08:39 +02:00

29 lines
1.2 KiB
Swift

// Text on a screendump, for scripts/internal/vm_drive.py: prints one line per
// piece of text Vision recognises, "x y w h text", in the image's pixels with
// the origin at the top left. vm_drive.py compiles this once (swiftc) and runs
// it on each screendump to tell which pane the guest shows and where its
// buttons are.
//
// vm_ocr IMAGE.png
import AppKit
import Vision
guard CommandLine.arguments.count == 2,
let image = NSImage(contentsOfFile: CommandLine.arguments[1]),
let cg = image.cgImage(forProposedRect: nil, context: nil, hints: nil)
else {
FileHandle.standardError.write("usage: vm_ocr IMAGE.png\n".data(using: .utf8)!)
exit(2)
}
let w = CGFloat(cg.width), h = CGFloat(cg.height)
let request = VNRecognizeTextRequest()
request.recognitionLevel = .accurate
request.usesLanguageCorrection = false
try VNImageRequestHandler(cgImage: cg, options: [:]).perform([request])
for o in request.results ?? [] {
guard let t = o.topCandidates(1).first else { continue }
let b = o.boundingBox
print(String(format: "%d %d %d %d ", Int(b.minX * w), Int((1 - b.maxY) * h),
Int(b.width * w), Int(b.height * h)) + t.string)
}