Publish reference layouts as API-authored text and image notes
wiki CI and deployment / verify-and-deploy (push) Successful in 16m28s
wiki CI and deployment / verify-and-deploy (push) Successful in 16m28s
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
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")
|
||||
Reference in New Issue
Block a user