实时文本识别 (OCR)

Live Text Recognition (OCR)

我想知道在 iPhone 实时相机模式下是否可以在不拍照的情况下操作 OCR?字母数字文本遵循可预测或有时固定的组合(类似于序列号)。

我已经尝试过 OpenCV 和 Tesseract,但我无法弄清楚如何在实时摄像头馈送上进行一些图像处理。

我只是不知道我必须识别我期待的文本的部分!我可以使用任何其他库来完成这部分吗?

您可以使用 TesseractOCR 并使用 AVCaptureSession 来实现这一点。

@interface YourClass()
{
    BOOL canScanFrame;
    BOOL isScanning;
}
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation YourClass
//...
- (void)prepareToScan
{
    //Prepare capture session, preview layer and so on
    //...

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerTicked) userInfo:nil repeats:YES];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;
{
    if (canScanFrame) {
        canScanFrame = NO;

        CGImageRef imageRef = [self imageFromSampleBuffer:sampleBuffer];
        UIImage *image = [UIImage imageWithCGImage:imageRef scale:1 orientation:UIImageOrientationRight];
        CGImageRelease(imageRef);

        [self.scanner setImage:image];

        isScanning = YES;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"scan start");
            [self.scanner recognize];
            NSLog(@"scan stop");
            dispatch_async(dispatch_get_main_queue(), ^{
                isScanning = NO;
                NSString *text = [self.scanner recognizedText];
                //do something with text                     
            });
        });
    }
}

- (CGImageRef) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer // Create a CGImageRef from sample buffer data
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);        // Lock the image buffer

    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);   // Get information of the image
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
    CGContextRelease(newContext);

    CGColorSpaceRelease(colorSpace);
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    return newImage;
}
- (void)timerTicked
{
    if (!isScanning) {
        canScanFrame = YES;
    }
}

@结束