Skip to content

Commit

Permalink
Fixes ocr concatenation and adds ocr from input file
Browse files Browse the repository at this point in the history
  • Loading branch information
xenodium committed Jul 17, 2024
1 parent a60818a commit 119e5bc
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import ArgumentParser
import Cocoa
import Vision

let packageVersion = "0.7.2"
let packageVersion = "0.7.3"

var recorder: WindowRecorder?

Expand Down Expand Up @@ -59,6 +59,13 @@ struct RecordCommand: ParsableCommand {
)
var record: String?

@Option(
name: .shortAndLong,
help: ArgumentHelp(
"Input image file (for --ocr only).", valueName: "input image file")
)
var input: String?

@Flag(name: [.customShort("c"), .long], help: "Select and recognize text in screen region.")
var ocr: Bool = false

Expand Down Expand Up @@ -124,9 +131,19 @@ struct RecordCommand: ParsableCommand {
print("Error: --output file must end in .txt")
Darwin.exit(1)
}
if let image = captureScreenImage() {
recognizeText(in: image, useClipboard: clipboard, saveToFile: output)

if let input = input,
let capturedImage = NSImage(contentsOfFile: input)
{
recognizeText(in: capturedImage, useClipboard: clipboard, saveToFile: output)
Darwin.exit(0)
}

if let capturedImage = captureScreenImage() {
recognizeText(in: capturedImage, useClipboard: clipboard, saveToFile: output)
Darwin.exit(0)
}

Darwin.exit(0)
}

Expand All @@ -136,6 +153,11 @@ struct RecordCommand: ParsableCommand {
Darwin.exit(1)
}

if input != nil {
print("Error: can't use --screenshot with --input")
Darwin.exit(1)
}

if mov || gif {
print("Error: can't use --screenshot with --mov or --gif")
Darwin.exit(1)
Expand Down Expand Up @@ -170,6 +192,11 @@ struct RecordCommand: ParsableCommand {
Darwin.exit(1)
}

if input != nil {
print("Error: can't use --record with --input")
Darwin.exit(1)
}

if mov {
if let output = output,
URL(fileURLWithPath: output).pathExtension != "mov"
Expand Down Expand Up @@ -434,7 +461,8 @@ class WindowRecorder {

guard
let destinationGIF = CGImageDestinationCreateWithURL(
url as NSURL, kUTTypeGIF, images.count, nil)
url as NSURL,
kUTTypeGIF, images.count, nil)
else {
print("Error: No destination GIF")
exit(1)
Expand Down Expand Up @@ -708,27 +736,29 @@ func recognizeText(in image: NSImage, useClipboard: Bool, saveToFile outputPath:
Darwin.exit(1)
}

var recognizedText = ""
if let observations = request.results as? [VNRecognizedTextObservation] {
for observation in observations {
if let topCandidate = observation.topCandidates(1).first {
if useClipboard {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(topCandidate.string, forType: .string)
}
if let outputPath = outputPath {
let outputURL = URL(fileURLWithPath: outputPath)
do {
try topCandidate.string.write(to: outputURL, atomically: true, encoding: .utf8)
} catch {
print(String(describing: error))
Darwin.exit(1)
}
} else {
print(topCandidate.string)
}

recognizedText += topCandidate.string + "\n"
}
}
recognizedText = recognizedText.trimmingCharacters(in: .whitespaces)
if useClipboard {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(recognizedText, forType: .string)
}
if let outputPath = outputPath {
let outputURL = URL(fileURLWithPath: outputPath)
do {
try recognizedText.write(to: outputURL, atomically: true, encoding: .utf8)
} catch {
print(String(describing: error))
Darwin.exit(1)
}
} else {
print(recognizedText)
}
}
}
Expand Down

0 comments on commit 119e5bc

Please sign in to comment.