http://www.perlmonks.org?node_id=938554

priyaviswam has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I was trying to randomly capture the picture through HD Camera and convert it to the Tiff file. So that it can be used for OCR. In this Situation, I tried to OCR an image which has no image content, perl interpreter crashes @$miDoc->{Images}->Item(0)->{Layout}{Text} Please find the code I have tried:
sub ApplyOCR{ my $TiffImageLocation = $_[0]; my $LogFilePath = $_[1]; if (-f $LogFilePath){ unlink $LogFilePath; } Win32::OLE::Const->Load("Microsoft Office Document Imaging 12\.0 T +ype Library") or die "Cannot use the Office 2007 OCR API"; my $miDoc = Win32::OLE->new('MODI.Document') or die "Cannot creat +e a MODI object"; $miDoc->Create("$TiffImageLocation"); $miDoc->OCR(9,1,1); my $OCRResult = $miDoc->{Images}->Item(0)->{Layout}{Text}; open (LOG, ">>$LogFilePath") or die $!; if ($Image){ print LOG "Image Name : $Image\n"; print LOG "==========\n\n"; } print LOG "OCRResult : \n"; print LOG "$OCRResult\n\n"; close LOG; }
Please tell me how can I find that the object $miDoc has not text content. So that I can avoid this crash.

  • Comment on Perl intepreter crash when tyring to get the text from OCRed output
  • Download Code

Replies are listed 'Best First'.
Re: Perl intepreter crash when tyring to get the text from OCRed output
by DrHyde (Prior) on Nov 17, 2011 at 10:26 UTC

    First of all, the interpreter doesn't crash "@$miDoc->{Images}->Item(0)->{Layout}{Text}", it crashes "at $miDoc...". The difference is important, as the '@' character could easily be part of code. By correctly spelling the word 'at' and using <code> tags your question would be made clearer.

    How do you know it crashes there? The only relevant-looking diagnostics I can see are three lines earlier (your die) and a coupla lines later when you write to the log file.

    Why the inconsistent use (and non-use) of -> in that line? That indicates to me that you may be merely cutting and pasting bits of code without any understanding of what you're doing, in which case it's much harder to help you.

    What EXACTLY are the error messages you're seeing? And are you using strict and warnings?

      HI, How do you know it crashes there? After performaing the OCR() at line
      $miDoc->OCR(9,1,1);
      We get the result from the following line :
      my $OCRResult = $miDoc->{Images}->Item(0)->{Layout}{Text};
      The above line represents the following : Ocr - ed image contain Image object. This Image object contains a Layout object and Layout object's Text property represents the text for that image. It crashed at this line where the text property is not present in the Ocr-ed image. So I got an pop saying "Perl interpreter has stopped working" And "Yes im using strict and warning" Thanks
Re: Perl intepreter crash when tyring to get the text from OCRed output
by marto (Cardinal) on Nov 17, 2011 at 11:15 UTC

    Have you ensured that the tiff compression scheme used is supported by the application? As mentioned in your previous thread convert jpg to tiff file.

      Yes, When I tried with an image which has some text content it was able to give me a good result

        Yes, when trying to OCR an image it helps if you use one which actually contains text.

Re: Perl intepreter crash when tyring to get the text from OCRed output
by ww (Archbishop) on Nov 17, 2011 at 11:09 UTC
    ... and what on earth is the meaning of the "image which has no image content...." that you're trying to OCR?

      My mistake..It is image which has no text content

Re: Perl intepreter crash when tyring to get the text from OCRed output
by sundialsvc4 (Abbot) on Nov 17, 2011 at 13:40 UTC

    Ummm... it doesn’t really mean anything, not to a programmer anyway, merely to say that “it crashed.”   Give us specific details:   exactly what you saw, after doing what, and under exactly what conditions.

      As refered to the issue posted by me, Please Check the latest code

      sub ApplyOCR{ my $TiffImageLocation = $_[0]; my $OCRResult; print "InitOnce : $InitOnce\n"; if ($InitOnce == 0) { $InitOnce = 1; #### To make it one time initialization $miDoc = Win32::OLE->new('MODI.Document') or die "Cannot crea +te a MODI object"; print "miDoc : $miDoc\n"; } #### Creat the new document $miDoc->Create("$TiffImageLocation"); $miDoc->OCR(9,1,1); $miDoc->Save(); # print "Save object\n"; # print Dumper ($miDoc); # print "\n"; try{ my $ImageDoc = $miDoc->{Images}; if (defined $ImageDoc){ $OCRResult = $miDoc->{Images}->Item(0)->{Layout}{Text}; #### Close the opened document $miDoc->Close(); return $OCRResult; }else{ if ($DebugMsg == 1){ print "OCR cannot be done on this image in $TiffImageL +ocation\n"; #comment to debug- this is the message and } return 1; } }catch{ my $err = shift; warn "Some other kind of error in try: $err\n"; throw $err; if ($DebugMsg == 1){ print "OCR cannot be done on this image in $TiffImageLocat +ion\n"; #comment to debug- this is the message and } return 1; } }

      For the tiff image which has not text content in it, then the code breaks at line 25. Since it has no text content the $miDoc don't have the property item(0). Please help me how to capture this error/exception.