Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions Quicksilver/Code-QuickStepCore/QSObject_FileHandling.m
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,13 @@ - (id)initWithArray:(NSArray *)paths {
return self;
}

#define MAX_IMAGE_SCAN_SIZE 50 * 1024 * 1024 // 50MB

- (void)loadAdditionalSearchContext {
if ([[NSUserDefaults standardUserDefaults] boolForKey:kQSDisableExtractTextFromImages]) {
return;
}

if (@available(macOS 10.15, *)) {

// For images, use text recognition to extract any text and store as searchable context
Expand All @@ -709,45 +715,48 @@ - (void)loadAdditionalSearchContext {
return;
}

NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
if (!image) {
return;
}
NSURL *fileURL = [NSURL fileURLWithPath:path];

// Get the CGImage from NSImage
CGImageRef cgImage = [image CGImageForProposedRect:NULL context:NULL hints:nil];
if (!cgImage) {
if (!fileURL) {
return;
}

// Create a text recognition request
VNRecognizeTextRequest *request = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest *request, NSError *error) {
if (error) {
NSLog(@"Text recognition error: %@", error);
return;
}
NSMutableString *recognizedText = [NSMutableString string];
for (VNRecognizedTextObservation *observation in request.results) {
VNRecognizedText *text = [[observation topCandidates:1] firstObject];
if (text) {
[recognizedText appendFormat:@" %@", text.string];
// skip extracting text for images over 20MB
NSNumber *fileSize = nil;
[fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil];
if ([fileSize unsignedLongLongValue] > MAX_IMAGE_SCAN_SIZE) return;

__weak __typeof(self) weakSelf = self;
QSGCDAsync(^{
// Create a text recognition request
VNRecognizeTextRequest *request = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest *request, NSError *error) {
if (error) {
NSLog(@"Text recognition error: %@", error);
return;
}
}

NSMutableString *recognizedText = [NSMutableString string];
for (VNRecognizedTextObservation *observation in request.results) {
VNRecognizedText *text = [[observation topCandidates:1] firstObject];
if (text) {
[recognizedText appendFormat:@" %@", text.string];
}
}

if ([recognizedText length] > 0) {
[weakSelf setObject:recognizedText forCache:kAdditionalSearchContext];
}
}];

// Create an image request handler
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithURL:fileURL options:@{}];

if ([recognizedText length] > 0) {
[self setObject:recognizedText forCache:kAdditionalSearchContext];
NSError *error = nil;
[handler performRequests:@[request] error:&error];
if (error) {
NSLog(@"Error performing text recognition: %@", error);
}
}];

// Create an image request handler
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCGImage:cgImage options:@{}];

NSError *error = nil;
[handler performRequests:@[request] error:&error];
if (error) {
NSLog(@"Error performing text recognition: %@", error);
}
});
}
}

Expand Down
3 changes: 3 additions & 0 deletions Quicksilver/Code-QuickStepCore/QSPreferenceKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ Following lines are no longer used in this project.

// A boolean indicating whether the task viewer resizes automatically
#define kQSResizeTaskViewerAutomatically @"QSResizeTaskViewerAutomatically"

// A boolean indicating whether text from image extraction should be disabled or not
#define kQSDisableExtractTextFromImages @"QSDisableExtractTextFromImages"
2 changes: 2 additions & 0 deletions Quicksilver/Localized/en.lproj/QSAdvancedPrefPane.strings
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
"QSHistoryMaxLength:title" = "Number of items in Quicksilver’s history";

"QSCloseInterfaceOnSuccessfulDrop:title" = "Close the interface after a successful drag and drop";

"QSDisableExtractTextFromImages:title" = "Disable text extraction from images (disables searching for text in images)";
10 changes: 10 additions & 0 deletions Quicksilver/Resources/DefaultsMap.plist
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,15 @@
<key>type</key>
<string>checkbox</string>
</dict>
<dict>
<key>category</key>
<string>Command</string>
<key>default</key>
<string>QSDisableExtractTextFromImages</string>
<key>title</key>
<string>Disable text extraction from images (disables searching for text in images)</string>
<key>type</key>
<string>checkbox</string>
</dict>
</array>
</plist>
Loading