Retry OCR on upscaled images when nothing is read

Tiny UI text (around 7pt) reads as nothing at native size — verified
locally. When a pass returns fewer than three characters, OCR retries at
2x and 3x and keeps the fullest result. The smoke test covers a tiny-text
case so the fallback stays working.

💘 Generated with Crush

Assisted-by: Crush:deepseek-v4-flash-0731
This commit is contained in:
2026-09-30 11:44:26 +08:00
parent 2e971d5f8d
commit 04b84e188a
3 changed files with 73 additions and 36 deletions
+13 -8
View File
@@ -123,8 +123,8 @@ enum LoginItem {
}
}
func renderTestImage(_ text: String, fontSize: CGFloat = 56) -> CGImage? {
let size = NSSize(width: 1900, height: 220)
func renderTestImage(_ text: String, fontSize: CGFloat = 56, width: CGFloat = 1900, height: CGFloat = 220) -> CGImage? {
let size = NSSize(width: width, height: height)
guard let rep = NSBitmapImageRep(
bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
@@ -138,15 +138,20 @@ func renderTestImage(_ text: String, fontSize: CGFloat = 56) -> CGImage? {
.font: NSFont.systemFont(ofSize: fontSize),
.foregroundColor: NSColor.black,
]
text.draw(at: NSPoint(x: 60, y: 75), withAttributes: attrs)
// Centre the text vertically so tiny canvases still contain it.
let textHeight = ("Ag" as NSString).size(withAttributes: attrs).height
let y = max(4, (size.height - textHeight) / 2)
text.draw(at: NSPoint(x: 20, y: y), withAttributes: attrs)
NSGraphicsContext.restoreGraphicsState()
return rep.cgImage
}
func runOCRSelfTest() {
let cases: [(expected: String, contains: String)] = [
("Kapture OCR test 1234, hello world", "Kapture OCR test 1234"),
("你好世界,这是一个测试", "你好世界"),
let cases: [(expected: String, contains: String, fontSize: CGFloat, width: CGFloat, height: CGFloat)] = [
("Kapture OCR test 1234, hello world", "Kapture OCR test 1234", 56, 1900, 220),
("你好世界,这是一个测试", "你好世界", 56, 1900, 220),
// Tiny text: needs the upscale retry to read anything at all.
("会话导出并发冲突报错 export fail", "导出", 7, 320, 40),
]
var index = 0
@@ -154,9 +159,9 @@ func runOCRSelfTest() {
guard index < cases.count else { exit(0) }
let current = cases[index]
index += 1
guard let image = renderTestImage(current.expected) else { exit(1) }
guard let image = renderTestImage(current.expected, fontSize: current.fontSize, width: current.width, height: current.height) else { exit(1) }
OCRService.shared.recognize(image) { recognized in
print("case: \(current.expected)")
print("case: \(current.expected) @\(Int(current.fontSize))pt")
print("got: \(recognized)")
let ok = recognized.contains(current.contains)
print(ok ? "PASS" : "FAIL")