Tesseract OCR iOS图像格式
我使用Tesseract OCR iOS扫描文本,并将其与项目中包含的照片一起使用 但是当从UIImagePickerController向其传递UIImage时,它不起作用。我设置了这个简单的测试:Tesseract OCR iOS图像格式,ios,objective-c,uiimage,tesseract,Ios,Objective C,Uiimage,Tesseract,我使用Tesseract OCR iOS扫描文本,并将其与项目中包含的照片一起使用 但是当从UIImagePickerController向其传递UIImage时,它不起作用。我设置了这个简单的测试: 从选择器获取原始图像,并将其馈送给tesseract:Doesnotwork 将UIImage另存为JPEG,从应用程序容器中复制,将其包含在项目中并将其馈送到tesseract:Doesnot工作 在photoshop中打开保存的UIImage,然后再次保存(默认JPEG质量设置为12时不做任何
- (void)recognizeWithImage:(UIImage *)image {
G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] initWithLanguage:@"dan"];
operation.tesseract.image = image;
self.imageView.image = image;
operation.recognitionCompleteBlock = ^(G8Tesseract *recognizedTesseract) {
NSLog(@"Result:\n%@", [recognizedTesseract recognizedText]);
};
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
以下是从相机获取图像的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
NSData *dataForJPEGFile = UIImageJPEGRepresentation(originalImage, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [paths[0] stringByAppendingPathComponent:@"temp_ocr_image_orig.jpg"];
[dataForJPEGFile writeToFile:filePath atomically:YES];
[self recognizeWithImage:originalImage];
}
以及两个图像文件的测试:
[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_orig.jpg"]];
[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_photoshopped.jpg"]];
图像
方向
对于两个图像都是不同的。当您将图像加载到引擎中时:在您的情况下,两个图像都是以不同方向的图像生成到引擎中:
以下是它们在发动机前方的外观:
原始图像:
Photoshop图像:
如果仔细观察,它们的呈现方式都不同。我相信uiimagejpegresentation
正在做一些疯狂的事情,或者当您将图像
写入容器
时,图像会进入不同的方向
您需要一种方法来修改从选择器或容器中获取的图像的方向
我做了一些组合以获得photoshop图像的正确方向:
//image is the original image
UIImage *imageToDisplay =[UIImage imageWithCGImage:[image CGImage]
scale:1.0
orientation: UIImageOrientationRight];
UIImage *newImage= [UIImage imageWithCGImage:[imageToDisplay CGImage]
scale:1.0
orientation: UIImageOrientationDown];
UIImage *newImage2= [UIImage imageWithCGImage:[newImage CGImage]
scale:1.0
orientation: UIImageOrientationLeft];
//Now I get the correct orientation
// Set the image on which Tesseract should perform recognition
operation.tesseract.image = newImage2 ;
现在,您可以按预期从OCR获取文本
您应该尝试在一行代码中获得正确的方向。我在这里使用了3个旋转 图像大小如何?当您将这两个图像传递给方法识别图像时,它们的大小是否相同?请尝试使用不同的引擎设置,然后尝试png图像。对于更多信息,大小相同,并且两个图像在UIImageView中都能很好地显示。不同的引擎设置和语言包没有区别。PNG对这两种情况都没有帮助所以@“结果:\n%@”是什么?错:I,1591;02195591这就是问题所在!我还没有弄清楚原因,但只需将方向设置为左侧就足够了。原始图像是右向的。我还没有尝试过在其他方向拍照,但如果我找到更好的方法来处理它,我会在这里发表评论。