-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathocr
More file actions
executable file
·78 lines (63 loc) · 1.73 KB
/
Copy pathocr
File metadata and controls
executable file
·78 lines (63 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env swift
//
// Extract text from images (OCR) using Apple’s text recognition API.
//
// - Recognize text in an image(s):
//
// `ocr {{file1}} {{file2}}`
//
// ---
// Source: https://evanhahn.com/mac-ocr-script/
import Foundation
import Vision
struct ProcessResult {
let path: URL
let index: Int
let texts: [String]?
}
func die(_ msg: String) -> Never {
fputs("\(msg)\n", stderr)
exit(1)
}
func process(_ path: URL) async -> [String]? {
var recognizeTextRequest = RecognizeTextRequest()
recognizeTextRequest.automaticallyDetectsLanguage = true
recognizeTextRequest.usesLanguageCorrection = true
recognizeTextRequest.recognitionLevel = .accurate
guard let observations = try? await recognizeTextRequest.perform(on: path) else {
return nil
}
return observations.compactMap { $0.topCandidates(1).first?.string }
}
guard CommandLine.arguments.count > 1 else {
die("usage: ocr /path/to/image1.jpg [/path/to/image2.png ...]")
}
let paths = CommandLine.arguments.dropFirst().map { URL(fileURLWithPath: $0) }
Task {
let results = await withTaskGroup(of: ProcessResult.self) { group in
for (index, path) in paths.enumerated() {
group.addTask {
.init(path: path, index: index, texts: await process(path))
}
}
var results = [ProcessResult]()
for await result in group {
results.append(result)
}
return results
}
var allSuccess = true
for result in results.sorted(by: { $0.index < $1.index }) {
if result.index != 0 {
print()
}
if let texts = result.texts {
for text in texts { print(text) }
} else {
fputs("Failed to process \(result.path)\n", stderr)
allSuccess = false
}
}
exit(allSuccess ? 0 : 1)
}
dispatchMain()