Files
probably-rendering-one-more…/scripts/recognize-layout-text.swift
T
codex-bot 38ec492ca3
wiki CI and deployment / verify-and-deploy (push) Successful in 16m28s
Publish reference layouts as API-authored text and image notes
2026-10-26 17:20:57 +08:00

23 lines
1.1 KiB
Swift

import Foundation
import Vision
import AppKit
let folder=URL(fileURLWithPath:CommandLine.arguments[1])
var result:[[String:Any]]=[]
for url in try FileManager.default.contentsOfDirectory(at:folder,includingPropertiesForKeys:nil).filter({$0.pathExtension=="png"}).sorted(by:{$0.lastPathComponent<$1.lastPathComponent}) {
let request=VNRecognizeTextRequest()
request.recognitionLevel = .accurate
request.recognitionLanguages = ["zh-Hans","en-US"]
request.usesLanguageCorrection = false
request.minimumTextHeight = 0.004
try VNImageRequestHandler(url:url).perform([request])
let lines=(request.results ?? []).compactMap { observation -> [String:Any]? in
guard let candidate=observation.topCandidates(1).first else{return nil}
let b=observation.boundingBox
return ["text":candidate.string,"confidence":candidate.confidence,"box":[b.minX,1-b.maxY,b.width,b.height]]
}
result.append(["file":url.lastPathComponent,"lines":lines])
}
let data=try JSONSerialization.data(withJSONObject:result,options:[.prettyPrinted,.sortedKeys])
try data.write(to:URL(fileURLWithPath:CommandLine.arguments[2]))
print("OCR processed \(result.count) panels")