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? { func renderTestImage(_ text: String, fontSize: CGFloat = 56, width: CGFloat = 1900, height: CGFloat = 220) -> CGImage? {
let size = NSSize(width: 1900, height: 220) let size = NSSize(width: width, height: height)
guard let rep = NSBitmapImageRep( guard let rep = NSBitmapImageRep(
bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height), bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, 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), .font: NSFont.systemFont(ofSize: fontSize),
.foregroundColor: NSColor.black, .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() NSGraphicsContext.restoreGraphicsState()
return rep.cgImage return rep.cgImage
} }
func runOCRSelfTest() { func runOCRSelfTest() {
let cases: [(expected: String, contains: String)] = [ let cases: [(expected: String, contains: String, fontSize: CGFloat, width: CGFloat, height: CGFloat)] = [
("Kapture OCR test 1234, hello world", "Kapture OCR test 1234"), ("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 var index = 0
@@ -154,9 +159,9 @@ func runOCRSelfTest() {
guard index < cases.count else { exit(0) } guard index < cases.count else { exit(0) }
let current = cases[index] let current = cases[index]
index += 1 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 OCRService.shared.recognize(image) { recognized in
print("case: \(current.expected)") print("case: \(current.expected) @\(Int(current.fontSize))pt")
print("got: \(recognized)") print("got: \(recognized)")
let ok = recognized.contains(current.contains) let ok = recognized.contains(current.contains)
print(ok ? "PASS" : "FAIL") print(ok ? "PASS" : "FAIL")
+35 -3
View File
@@ -8,6 +8,25 @@ final class OCRService {
func recognize(_ cgImage: CGImage, completion: @escaping (String) -> Void) { func recognize(_ cgImage: CGImage, completion: @escaping (String) -> Void) {
queue.async { queue.async {
let text = Self.perform(cgImage)
// Tiny text (small UI labels) often reads as nothing at native
// size; retry upscaled and keep whichever pass read more.
guard text.count < 3 else {
completion(text)
return
}
var best = text
for factor in [CGFloat(2), CGFloat(3)] {
guard let upscaled = Self.upscaled(cgImage, factor: factor) else { continue }
let candidate = Self.perform(upscaled)
if candidate.count > best.count { best = candidate }
if best.count >= 3 { break }
}
completion(best)
}
}
private static func perform(_ cgImage: CGImage) -> String {
let request = VNRecognizeTextRequest() let request = VNRecognizeTextRequest()
request.recognitionLevel = .accurate request.recognitionLevel = .accurate
request.usesLanguageCorrection = true request.usesLanguageCorrection = true
@@ -22,8 +41,7 @@ final class OCRService {
do { do {
try handler.perform([request]) try handler.perform([request])
} catch { } catch {
completion("") return ""
return
} }
let observations = request.results ?? [] let observations = request.results ?? []
@@ -35,7 +53,21 @@ final class OCRService {
return a.boundingBox.minX < b.boundingBox.minX return a.boundingBox.minX < b.boundingBox.minX
} }
.compactMap { $0.topCandidates(1).first?.string } .compactMap { $0.topCandidates(1).first?.string }
completion(lines.joined(separator: "\n")) return lines.joined(separator: "\n")
} }
private static func upscaled(_ cgImage: CGImage, factor: CGFloat) -> CGImage? {
let width = Int(CGFloat(cgImage.width) * factor)
let height = Int(CGFloat(cgImage.height) * factor)
guard width > 0, height > 0, width <= 20000, height <= 20000 else { return nil }
guard let ctx = CGContext(
data: nil, width: width, height: height,
bitsPerComponent: 8, bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
) else { return nil }
ctx.interpolationQuality = .high
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))
return ctx.makeImage()
} }
} }
+2 -2
View File
@@ -108,9 +108,9 @@ cat > "$APP/Contents/Info.plist" <<'PLIST'
<key>CFBundleIconFile</key> <key>CFBundleIconFile</key>
<string>Icon</string> <string>Icon</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>1.10.1</string> <string>1.10.2</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>101</string> <string>102</string>
<key>LSUIElement</key> <key>LSUIElement</key>
<true/> <true/>
<key>NSHighResolutionCapable</key> <key>NSHighResolutionCapable</key>