Skip to content

Commit

Permalink
Scan image text to be searchable
Browse files Browse the repository at this point in the history
Fixes #992
  • Loading branch information
p0deje committed Dec 24, 2024
1 parent 1c6f694 commit 2255498
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
32 changes: 32 additions & 0 deletions Maccy/Models/HistoryItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AppKit
import Defaults
import Sauce
import SwiftData
import Vision

@Model
class HistoryItem {
Expand Down Expand Up @@ -73,6 +74,9 @@ class HistoryItem {

func generateTitle() -> String {
guard image == nil else {
Task {
self.performTextRecognition()
}
return ""
}

Expand Down Expand Up @@ -195,4 +199,32 @@ class HistoryItem {
.filter { types.contains(NSPasteboard.PasteboardType($0.type)) }
.compactMap { $0.value }
}

private func performTextRecognition() {
guard let cgImage = image?.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return
}

let requestHandler = VNImageRequestHandler(cgImage: cgImage)
let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
request.recognitionLevel = .fast

do {
try requestHandler.perform([request])
} catch {
print("Unable to perform the request: \(error).")
}
}

private func recognizeTextHandler(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNRecognizedTextObservation] else {
return
}

let recognizedStrings = observations.compactMap { observation in
return observation.topCandidates(1).first?.string
}

self.title = recognizedStrings.joined(separator: "\n")
}
}
6 changes: 3 additions & 3 deletions Maccy/Views/ListItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ struct ListItemView<Title: View>: View {
.padding(.leading, showIcons ? 5 : 10)
.padding(.trailing, 5)
.padding(.vertical, 5)
} else {
ListItemTitleView(attributedTitle: attributedTitle, title: title)
.padding(.leading, showIcons ? 0 : 5)
}

ListItemTitleView(attributedTitle: attributedTitle, title: title)
.padding(.leading, showIcons ? 0 : 5)

Spacer()

if !shortcuts.isEmpty {
Expand Down

0 comments on commit 2255498

Please sign in to comment.