-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
67 lines (56 loc) · 2.43 KB
/
main.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
import Foundation
import Vision
import CoreGraphics
// Access command line arguments
let arguments = CommandLine.arguments
// Ensure the correct number of arguments is provided
guard arguments.count == 3, let imagePath = arguments.dropFirst().first, let boxDataPath = arguments.dropFirst(2).first else {
print("Usage: ./<executable> <image_path> <box_data_text_file_path>")
exit(0)
}
// Load the image from the provided path
let imageURL = URL(fileURLWithPath: imagePath)
guard let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, nil),
let cgImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil) else {
print("Failed to load the image from the provided path.")
exit(0)
}
// Perform text recognition using Apple's Vision framework
let request = VNRecognizeTextRequest { request, error in
guard let observations = request.results as? [VNRecognizedTextObservation], error == nil else {
print("Text recognition failed with error: \(error?.localizedDescription ?? "Unknown error")")
exit(0)
}
var boundingBoxData: [[String: Any]] = []
for (_, observation) in observations.enumerated() {
guard let topCandidate = observation.topCandidates(1).first else { continue }
print(topCandidate.string)
// Extract bounding box data
let boundingBox = observation.boundingBox
let data: [String: Any] = [
"word_num": topCandidate.string.split(separator: " ").count,
"left": (boundingBox.origin.x),
"top": (boundingBox.origin.y),
"width": (boundingBox.size.width),
"height": (boundingBox.size.height),
"conf": observation.confidence, // Replace with actual confidence value if available
"text": topCandidate.string
]
boundingBoxData.append(data)
}
// Write bounding box data to file
let boxDataFileURL = URL(fileURLWithPath: boxDataPath)
do {
let jsonData = try JSONSerialization.data(withJSONObject: boundingBoxData, options: .prettyPrinted)
try jsonData.write(to: boxDataFileURL, options: .atomic)
} catch {
print("Failed to write bounding box data to file: \(error.localizedDescription)")
}
}
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do {
try handler.perform([request])
} catch {
print("Failed to perform text recognition with error: \(error.localizedDescription)")
exit(0)
}