Initial work on using Apple's Vision framework to OCR subtitles
This commit is contained in:
6 files changed
+116
-13
No files matched your search
@@ -20,7 +20,8 @@
|
||||
#import "MP42PrivateUtilities.h"
|
||||
#import "MP42SubtitleTrack.h"
|
||||
|
||||
#import "MP42OCRWrapper.h"
|
||||
#import "MP42TesseractOCR.h"
|
||||
#import "MP42VisionOCR.h"
|
||||
#import "MP42SubUtilities.h"
|
||||
|
||||
#include "FFmpegUtils.h"
|
||||
@@ -32,7 +33,7 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
{
|
||||
NSThread *decoderThread;
|
||||
|
||||
MP42OCRWrapper *_ocr;
|
||||
id<MP42TextRecognizer> _ocr;
|
||||
CIContext *_imgContext;
|
||||
AVCodec *avCodec;
|
||||
AVCodecContext *avContext;
|
||||
@@ -426,7 +427,8 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
|
||||
srcMagicCookie = [track.importer magicCookieForTrack:track];
|
||||
|
||||
_ocr = [[MP42OCRWrapper alloc] initWithLanguage:track.language];
|
||||
_ocr = [[MP42TesseractOCR alloc] initWithLanguage:track.language];
|
||||
// _ocr = [[MP42VisionOCR alloc] initWithLanguage:track.language];
|
||||
|
||||
if (format == kMP42SubtitleCodecType_VobSub) {
|
||||
// Launch the vobsub decoder thread.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// SBOcr.h
|
||||
// MP42TesseractOCR.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 27/03/11.
|
||||
@@ -8,15 +8,12 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MP42Utilities.h"
|
||||
#import "MP42TextRecognizer.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
MP42_OBJC_DIRECT_MEMBERS
|
||||
@interface MP42OCRWrapper : NSObject
|
||||
|
||||
- (instancetype)initWithLanguage:(NSString *)language;
|
||||
- (nullable NSString *)performOCROnCGImage:(CGImageRef)image;
|
||||
|
||||
@interface MP42TesseractOCR : NSObject<MP42TextRecognizer>
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,12 +1,12 @@
|
||||
//
|
||||
// SBOcr.mm
|
||||
// MP42TesseractOCR.mm
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 27/03/11.
|
||||
// Copyright 2022 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MP42OCRWrapper.h"
|
||||
#import "MP42TesseractOCR.h"
|
||||
#import "MP42Languages.h"
|
||||
|
||||
// Tesseract OCR
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
if (base_path) {
|
||||
path = base_path.fileSystemRepresentation;
|
||||
} else {
|
||||
path = [[NSBundle bundleForClass:[MP42OCRWrapper class]].bundlePath stringByAppendingString:@"/Versions/A/Resources/tessdata/"].fileSystemRepresentation;
|
||||
path = [[NSBundle bundleForClass:[MP42TesseractOCR class]].bundlePath stringByAppendingString:@"/Versions/A/Resources/tessdata/"].fileSystemRepresentation;
|
||||
}
|
||||
|
||||
int result = tess_base_api.Init(path, lang.UTF8String, mode);
|
||||
@@ -49,7 +49,7 @@ protected:
|
||||
};
|
||||
|
||||
MP42_OBJC_DIRECT_MEMBERS
|
||||
@implementation MP42OCRWrapper {
|
||||
@implementation MP42TesseractOCR {
|
||||
OCRWrapper *tess_base;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// MP42TextRecognizer.h
|
||||
// MP42Foundation
|
||||
//
|
||||
// Created by Damiano Galassi on 29/09/2026.
|
||||
// Copyright © 2026 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol MP42TextRecognizer <NSObject>
|
||||
|
||||
- (instancetype)initWithLanguage:(NSString *)language;
|
||||
- (nullable NSString *)performOCROnCGImage:(CGImageRef)image;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// MP42VisionOCR.h
|
||||
// MP42Foundation
|
||||
//
|
||||
// Created by Damiano Galassi on 29/09/2026.
|
||||
// Copyright © 2026 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MP42TextRecognizer.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
API_AVAILABLE(macos(13))
|
||||
@interface MP42VisionOCR : NSObject<MP42TextRecognizer>
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// MP42VisionOCR.m
|
||||
// MP42Foundation
|
||||
//
|
||||
// Created by Damiano Galassi on 29/09/2026.
|
||||
// Copyright © 2026 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MP42VisionOCR.h"
|
||||
|
||||
@import Vision;
|
||||
|
||||
@interface MP42VisionOCR ()
|
||||
@property (nonatomic, readonly) NSString *language;
|
||||
@property (nonatomic, readonly) dispatch_semaphore_t done;
|
||||
@end
|
||||
|
||||
@implementation MP42VisionOCR
|
||||
|
||||
- (nonnull instancetype)initWithLanguage:(nonnull NSString *)language
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_language = [language copy];
|
||||
_done = dispatch_semaphore_create(0);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (nullable NSString *)performOCROnCGImage:(nonnull CGImageRef)image
|
||||
{
|
||||
__block NSMutableString *text = [[NSMutableString alloc] init];
|
||||
|
||||
VNImageRequestHandler *requestHandler = [[VNImageRequestHandler alloc] initWithCGImage:image options:@{}];
|
||||
|
||||
VNRecognizeTextRequest *textRequest = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) {
|
||||
NSArray<VNRecognizedTextObservation *> *observations = request.results;
|
||||
if (observations.count) {
|
||||
for (VNRecognizedTextObservation *currentObservation in request.results) {
|
||||
NSArray<VNRecognizedText *> *topCandidate = [currentObservation topCandidates:1];
|
||||
VNRecognizedText *recognizedText = topCandidate.firstObject;
|
||||
if (recognizedText) {
|
||||
if (text.length) {
|
||||
[text appendString:@"\n"];
|
||||
}
|
||||
[text appendString:recognizedText.string];
|
||||
}
|
||||
}
|
||||
}
|
||||
dispatch_semaphore_signal(self->_done);
|
||||
}];
|
||||
textRequest.recognitionLevel = VNRequestTextRecognitionLevelAccurate;
|
||||
textRequest.recognitionLanguages = @[_language];
|
||||
textRequest.revision = VNRecognizeTextRequestRevision3;
|
||||
textRequest.usesLanguageCorrection = YES;
|
||||
|
||||
NSError *error;
|
||||
[requestHandler performRequests:@[textRequest] error:&error];
|
||||
|
||||
dispatch_semaphore_wait(_done, DISPATCH_TIME_FOREVER);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user