Compare commits

...
2 Commits
Author SHA1 Message Date
TZGyn 04b84e188a 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
2026-09-30 11:44:26 +08:00
TZGyn 2e971d5f8d Darken the screen in frozen capture mode
Frozen mode showed the still image at full brightness until a selection
existed, unlike live mode's dark veil. It now draws the same 35% veil over
everything and re-draws the selected area from the snapshot, keeping the
selection bright. The hint text matches the mode.

💘 Generated with Crush

Assisted-by: Crush:deepseek-v4-flash-0731
2026-09-25 17:43:59 +08:00
4 changed files with 84 additions and 50 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")
+58 -26
View File
@@ -8,34 +8,66 @@ final class OCRService {
func recognize(_ cgImage: CGImage, completion: @escaping (String) -> Void) { func recognize(_ cgImage: CGImage, completion: @escaping (String) -> Void) {
queue.async { queue.async {
let request = VNRecognizeTextRequest() let text = Self.perform(cgImage)
request.recognitionLevel = .accurate // Tiny text (small UI labels) often reads as nothing at native
request.usesLanguageCorrection = true // size; retry upscaled and keep whichever pass read more.
request.recognitionLanguages = ["en-US", "zh-Hans", "zh-Hant", "ja-JP", "ko-KR"] guard text.count < 3 else {
// Without this, Vision defaults to the first listed language and completion(text)
// renders CJK as Latin garbage (verified: 你好世界 -> "177WR").
if #available(macOS 13.0, *) {
request.automaticallyDetectsLanguage = true
}
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do {
try handler.perform([request])
} catch {
completion("")
return return
} }
var best = text
let observations = request.results ?? [] for factor in [CGFloat(2), CGFloat(3)] {
let lines = observations guard let upscaled = Self.upscaled(cgImage, factor: factor) else { continue }
.sorted { a, b in let candidate = Self.perform(upscaled)
let ay = a.boundingBox.midY if candidate.count > best.count { best = candidate }
let by = b.boundingBox.midY if best.count >= 3 { break }
if abs(ay - by) > 0.02 { return ay > by } }
return a.boundingBox.minX < b.boundingBox.minX completion(best)
}
.compactMap { $0.topCandidates(1).first?.string }
completion(lines.joined(separator: "\n"))
} }
} }
private static func perform(_ cgImage: CGImage) -> String {
let request = VNRecognizeTextRequest()
request.recognitionLevel = .accurate
request.usesLanguageCorrection = true
request.recognitionLanguages = ["en-US", "zh-Hans", "zh-Hant", "ja-JP", "ko-KR"]
// Without this, Vision defaults to the first listed language and
// renders CJK as Latin garbage (verified: 你好世界 -> "177WR").
if #available(macOS 13.0, *) {
request.automaticallyDetectsLanguage = true
}
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do {
try handler.perform([request])
} catch {
return ""
}
let observations = request.results ?? []
let lines = observations
.sorted { a, b in
let ay = a.boundingBox.midY
let by = b.boundingBox.midY
if abs(ay - by) > 0.02 { return ay > by }
return a.boundingBox.minX < b.boundingBox.minX
}
.compactMap { $0.topCandidates(1).first?.string }
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()
}
} }
+11 -14
View File
@@ -97,7 +97,9 @@ final class OverlayView: NSView {
} }
// Esc hint — the only way out once clicks are consumed by the tap. // Esc hint — the only way out once clicks are consumed by the tap.
let hint = "Drag to select · hover + click a window to capture it · Esc to cancel" let hint = frozenImage != nil
? "Frozen screen · drag to select · Esc to cancel"
: "Drag to select · hover + click a window to capture it · Esc to cancel"
let hintAttrs: [NSAttributedString.Key: Any] = [ let hintAttrs: [NSAttributedString.Key: Any] = [
.font: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium), .font: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium),
.foregroundColor: NSColor.white.withAlphaComponent(0.85), .foregroundColor: NSColor.white.withAlphaComponent(0.85),
@@ -115,22 +117,17 @@ final class OverlayView: NSView {
guard let controller else { return } guard let controller else { return }
// Frozen mode: no window highlight; the marquee is cut straight out // Frozen mode: same look as live mode — a dark veil over everything,
// of the still image so it shows the frozen pixels. // with the selection showing the frozen pixels undimmed.
if frozenImage != nil { if let frozenImage {
NSColor.black.withAlphaComponent(0.35).setFill()
NSBezierPath(rect: bounds).fill()
if let sel = controller.marqueeRect, sel.width > 0, sel.height > 0 { if let sel = controller.marqueeRect, sel.width > 0, sel.height > 0 {
let vf = viewRect(sel) let vf = viewRect(sel)
// Dim everything except the selection (even-odd fill).
let maskPath = CGMutablePath()
maskPath.addRect(bounds)
maskPath.addRect(vf)
NSGraphicsContext.current?.saveGraphicsState() NSGraphicsContext.current?.saveGraphicsState()
let ctx = NSGraphicsContext.current?.cgContext NSBezierPath(rect: vf).addClip()
ctx?.addPath(maskPath) drawFrozen(frozenImage)
ctx?.fillPath(using: .evenOdd)
NSColor.black.withAlphaComponent(0.35).setFill()
ctx?.addPath(maskPath)
ctx?.fillPath(using: .evenOdd)
NSGraphicsContext.current?.restoreGraphicsState() NSGraphicsContext.current?.restoreGraphicsState()
let border = NSBezierPath(rect: vf) let border = NSBezierPath(rect: vf)
+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.0</string> <string>1.10.2</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>100</string> <string>102</string>
<key>LSUIElement</key> <key>LSUIElement</key>
<true/> <true/>
<key>NSHighResolutionCapable</key> <key>NSHighResolutionCapable</key>