I have a problem with shortcuts. I try to build a shortcut for recognizing some words and numbers in JPEG files. I noticed that if I select all the text in Preview app some words can be found that Shortcuts text extraction command cannot return.
do you know if his is normal?
Is it possible to use preview extraction in shortcuts ?
Thanks
Post
Replies
Boosts
Views
Activity
hello
I am trying to detect the orientation of text in images. (each image has a label with a number but sometimes the the label is not in the right orientation and I would like two detect these cases and add a prefix to the image files)
this code is working well but when the text is upside down it considers that the text is well oriented
is it a way to distinguish the difference ?
thanks for your help !
import SwiftUI
import Vision
struct ContentView: View {
@State private var totalImages = 0
@State private var processedImages = 0
@State private var rotatedImages = 0
@State private var remainingImages = 0
var body: some View {
VStack {
Button(action: chooseDirectory) {
Text("Choisir le répertoire des images")
.padding()
}
Text("TOTAL: \(totalImages)")
Text("TRAITEES: \(processedImages)")
Text("ROTATION: \(rotatedImages)")
Text("RESTANT: \(remainingImages)")
}
.padding()
}
func chooseDirectory() {
let openPanel = NSOpenPanel()
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
openPanel.allowsMultipleSelection = false
openPanel.begin { response in
if response == .OK, let url = openPanel.url {
processImages(in: url)
}
}
}
func processImages(in directory: URL) {
DispatchQueue.global(qos: .userInitiated).async {
do {
let fileManager = FileManager.default
let urls = try fileManager.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)
let imageUrls = urls.filter { $0.pathExtension.lowercased() == "jpg" || $0.pathExtension.lowercased() == "png" }
DispatchQueue.main.async {
self.totalImages = imageUrls.count
self.processedImages = 0
self.rotatedImages = 0
self.remainingImages = self.totalImages
}
for url in imageUrls {
self.processImage(at: url)
}
} catch {
print("Error reading contents of directory: \(error.localizedDescription)")
}
}
}
func processImage(at url: URL) {
guard let image = NSImage(contentsOf: url), let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return
}
let request = VNRecognizeTextRequest { (request, error) in
if let error = error {
print("Error recognizing text: \(error.localizedDescription)")
return
}
if let results = request.results as? [VNRecognizedTextObservation], !results.isEmpty {
let orientationCorrect = self.isTextOrientationCorrect(results)
if !orientationCorrect {
self.renameFile(at: url)
DispatchQueue.main.async {
self.rotatedImages += 1
}
}
}
DispatchQueue.main.async {
self.processedImages += 1
self.remainingImages = self.totalImages - self.processedImages
}
}
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do {
try handler.perform([request])
} catch {
print("Error performing text recognition request: \(error.localizedDescription)")
}
}
func isTextOrientationCorrect(_ observations: [VNRecognizedTextObservation]) -> Bool {
// Placeholder for the logic to check text orientation
// This should be implemented based on your specific needs
for observation in observations {
if let recognizedText = observation.topCandidates(1).first {
let boundingBox = observation.boundingBox
let angle = atan2(boundingBox.height, boundingBox.width)
if abs(angle) > .pi / 4 {
return false
}
}
}
return true
}
func renameFile(at url: URL) {
let fileManager = FileManager.default
let directory = url.deletingLastPathComponent()
let newName = "ROTATION_" + url.lastPathComponent
let newURL = directory.appendingPathComponent(newName)
do {
try fileManager.moveItem(at: url, to: newURL)
} catch {
print("Error renaming file: \(error.localizedDescription)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}