forked from tisfeng/Raycast-Easydict
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use execa to run swift directly
- Loading branch information
Showing
5 changed files
with
106 additions
and
26 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// The Swift Programming Language | ||
// https://docs.swift.org/swift-book | ||
// swift build -c release | ||
|
||
import Cocoa | ||
import Vision | ||
|
||
let recognitionLevel = VNRequestTextRecognitionLevel.accurate | ||
let usesLanguageCorrection = true | ||
|
||
func captureScreen() -> CGImage? { | ||
let screenRect = NSScreen.main?.frame ?? .zero | ||
let imageRef = CGWindowListCreateImage(screenRect, .optionOnScreenOnly, kCGNullWindowID, .bestResolution) | ||
return imageRef | ||
} | ||
|
||
func captureSelectedArea() -> CGImage? { | ||
let task = Process() | ||
task.launchPath = "/usr/sbin/screencapture" | ||
task.arguments = ["-i", "-c"] | ||
task.launch() | ||
task.waitUntilExit() | ||
|
||
guard let pasteboard = NSPasteboard.general.pasteboardItems?.first, | ||
let fileType = pasteboard.types.first, | ||
let data = pasteboard.data(forType: fileType), | ||
let image = NSImage(data: data) else { | ||
fputs("Error: failed to capture selected area\n", stderr) | ||
return nil | ||
} | ||
|
||
var proposedRect = NSRect.zero | ||
guard let imgRef = image.cgImage(forProposedRect: &proposedRect, context: nil, hints: nil) else { | ||
fputs("Error: failed to convert NSImage to CGImage for captured area\n", stderr) | ||
return nil | ||
} | ||
|
||
return imgRef | ||
} | ||
|
||
func main() -> Int32 { | ||
let imgRef: CGImage? = captureSelectedArea() | ||
|
||
guard let capturedImage = imgRef else { | ||
fputs("Error: failed to capture image\n", stderr) | ||
return 1 | ||
} | ||
|
||
let request = VNRecognizeTextRequest() | ||
request.recognitionLevel = recognitionLevel | ||
request.usesLanguageCorrection = usesLanguageCorrection | ||
if #available(macOS 13.0, *) { | ||
request.automaticallyDetectsLanguage = true | ||
} | ||
|
||
// "en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR", "zh-Hans", "zh-Hant", "yue-Hans", "yue-Hant", "ko-KR", "ja-JP", "ru-RU", "uk-UA" | ||
request.recognitionLanguages = ["zh-Hans", "zh-Hant", "en-US", "ja-JP", "fr-FR", "de-DE", "es-ES", "pt-BR", "it-IT", "ko-KR", "ru-RU", "uk-UA"] | ||
|
||
do { | ||
try VNImageRequestHandler(cgImage: capturedImage, options: [:]).perform([request]) | ||
} catch { | ||
fputs("Error: \(error.localizedDescription)", stderr) | ||
return 1 | ||
} | ||
|
||
guard let observations = request.results else { | ||
fputs("Error: could not get text recognition results", stderr) | ||
return 1 | ||
} | ||
|
||
// Currently, we just join all recognized text by '\n' | ||
// TODO: use the bounding box frame and the end character of each line to determine how to join the text. | ||
let obs = observations | ||
.map { $0.topCandidates(1).first?.string ?? "" } | ||
.joined(separator: "\n") | ||
|
||
print(obs) | ||
|
||
return 0 | ||
} | ||
|
||
exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters