-
Notifications
You must be signed in to change notification settings - Fork 0
/
macos-comic-ocr.swift
194 lines (169 loc) · 7.23 KB
/
macos-comic-ocr.swift
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import Foundation
import Vision
import AppKit
func printHelp() {
print("""
Usage:
macos-comic-ocr [options]
Options:
-f, --file <file> Specify a single image file to process
-d, --directory <dir> Specify a directory containing images to process
-r, --recursive Recursively process images in the specified directory and subdirectories
-n, --rows <num> Specify the number of horizontal rows to split the images into (default: auto-detect based on aspect ratio)
-h, --help Display this help message
""")
}
func recognizeTextWithSentenceEndings(from imagePath: String, rows: Int?) -> String {
guard let image = NSImage(contentsOfFile: imagePath) else {
print("Image could not be loaded: \(imagePath)")
return ""
}
guard let tiffData = image.tiffRepresentation,
let ciImage = CIImage(data: tiffData) else {
print("Failed to extract TIFF representation for: \(imagePath)")
return ""
}
// Determine image width and height for aspect ratio
let imageWidth = ciImage.extent.width
let imageHeight = ciImage.extent.height
// Define ROIs based on specified rows or aspect ratio
var regionsOfInterest: [CGRect] = []
if let rows = rows, rows > 1 {
// Split the image into the specified number of rows, ensuring top-to-bottom order
let rowHeight = 1.0 / CGFloat(rows)
for i in 0..<rows {
let yPosition = 1.0 - rowHeight * CGFloat(i + 1) // Start from the top row
let region = CGRect(x: 0, y: yPosition, width: 1.0, height: rowHeight)
regionsOfInterest.append(region)
}
} else {
// Use aspect ratio to decide on 1 or 2 rows
let aspectRatio = imageWidth / imageHeight
if aspectRatio < 2.5 { // For images with ratio close to 2.25 (split horizontally)
let topHalf = CGRect(x: 0, y: 0.5, width: 1.0, height: 0.5)
let bottomHalf = CGRect(x: 0, y: 0, width: 1.0, height: 0.5)
regionsOfInterest = [topHalf, bottomHalf]
} else { // For images with ratio closer to 3.2 (process left-to-right as a single block)
let fullImage = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
regionsOfInterest = [fullImage]
}
}
var recognizedText = ""
// Process each region of interest
for roi in regionsOfInterest {
let requestHandler = VNImageRequestHandler(ciImage: ciImage, options: [:])
let request = VNRecognizeTextRequest { (request, error) in
if let error = error {
print("Text recognition failed: \(error)")
return
}
for observation in request.results as? [VNRecognizedTextObservation] ?? [] {
let boxWidth = observation.boundingBox.width
let boxHeight = observation.boundingBox.height
if boxWidth >= boxHeight * 0.4 || (boxWidth > 0.1 && boxHeight > 0.1) {
if let topCandidate = observation.topCandidates(1).first {
recognizedText += topCandidate.string
// Add a newline if the line ends with ., ?, or !
if topCandidate.string.hasSuffix(".") || topCandidate.string.hasSuffix("?") || topCandidate.string.hasSuffix("!") {
recognizedText += "\n"
}
recognizedText += "\n"
}
}
}
}
request.regionOfInterest = roi
do {
try requestHandler.perform([request])
} catch {
print("Failed to perform text recognition request: \(error)")
}
}
return recognizedText
}
func processFile(at filePath: String, rows: Int?) {
let recognizedText = recognizeTextWithSentenceEndings(from: filePath, rows: rows)
let outputFilePath = (filePath as NSString).deletingPathExtension + ".txt"
do {
try recognizedText.write(toFile: outputFilePath, atomically: true, encoding: .utf8)
print("Text recognition completed. Output saved to \(outputFilePath)")
} catch {
print("Failed to write output file for \(filePath): \(error)")
}
}
func processDirectory(at directoryPath: String, rows: Int?, recursive: Bool) {
let fileManager = FileManager.default
if recursive {
// Process files recursively
let enumerator = fileManager.enumerator(atPath: directoryPath)
while let element = enumerator?.nextObject() as? String {
let filePath = (directoryPath as NSString).appendingPathComponent(element)
do {
let attributes = try fileManager.attributesOfItem(atPath: filePath)
if attributes[.type] as? FileAttributeType == .typeRegular &&
(filePath.lowercased().hasSuffix(".jpg") || filePath.lowercased().hasSuffix(".jpeg") ||
filePath.lowercased().hasSuffix(".png") || filePath.lowercased().hasSuffix(".gif")) {
print("Processing \(filePath)...")
processFile(at: filePath, rows: rows)
}
} catch {
print("Failed to get attributes for \(filePath): \(error)")
}
}
} else {
// Process files only in the top-level directory
do {
let contents = try fileManager.contentsOfDirectory(atPath: directoryPath)
for element in contents {
let filePath = (directoryPath as NSString).appendingPathComponent(element)
let attributes = try fileManager.attributesOfItem(atPath: filePath)
if attributes[.type] as? FileAttributeType == .typeRegular &&
(filePath.lowercased().hasSuffix(".jpg") || filePath.lowercased().hasSuffix(".jpeg") ||
filePath.lowercased().hasSuffix(".png") || filePath.lowercased().hasSuffix(".gif")) {
print("Processing \(filePath)...")
processFile(at: filePath, rows: rows)
}
}
} catch {
print("Failed to list contents of directory \(directoryPath): \(error)")
}
}
}
// Main Command-line Argument Parsing
var filePath: String? = nil
var directoryPath: String? = nil
var recursive = false
var rows: Int? = nil
let args = CommandLine.arguments
if args.contains("-h") || args.contains("--help") || args.count == 1 {
printHelp()
exit(0)
}
var index = 1
while index < args.count {
switch args[index] {
case "-f", "--file":
index += 1
filePath = args[index]
case "-d", "--directory":
index += 1
directoryPath = args[index]
case "-r", "--recursive":
recursive = true
case "-n", "--rows":
index += 1
rows = Int(args[index]) ?? nil
default:
print("Unknown argument: \(args[index])")
printHelp()
exit(1)
}
index += 1
}
if let file = filePath {
processFile(at: file, rows: rows)
} else if let directory = directoryPath {
processDirectory(at: directory, rows: rows, recursive: recursive)
} else {
printHelp()
}