实时文本识别(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;
    }
}

@结束

相关内容

  • 没有找到相关文章

最新更新