Print OCR results to text file

14 views (last 30 days)
Laura Elder
Laura Elder on 5 Jul 2019
Answered: Laura Elder on 7 Jul 2019
Hello,
I have read the other posts and examples for printing your OCR results into a text file but am running into an error.
Error using fprintf
Function is not defined for 'cell' inputs.
Error in REMOVEDallEXTRAbits (line 142)
fprintf(fileID, '%s\r\n', text);
ocrtxt = ocr(BW2, textBBoxes, 'Language', 'C:\Users\e362609\Documents\MATLAB\Examples\vision\TextDetectionExample\try50\tessdata\try50.traineddata');
% [ocrtxt.Text];
fileID = fopen('trytext.txt');
fprintf(fileID, '%s\r\n', text);
fclose(fileID);
Any help is much apreciated!
  2 Comments
Star Strider
Star Strider on 7 Jul 2019
I don’t have the Computer Vision Toolbox, so I have no personal experience with it.
However (according to the online documentation on ocr), it appears that the information you want is either in the structure fields:
ocrtext.Text
that appears to return a character array, or:
ocrtext.Words
that returns a cell array.
Consider that:
ocrtxt.Words = {'This','is','a','test'}; % Create Something Similar To What I Believe ‘ocr’ Returns
fprintf(f,'%s\n',ocrtxt.Words{:})
produces:
This
is
a
test
and no loop is necessary.
See if that works with your data.
Laura Elder
Laura Elder on 7 Jul 2019
f = fopen('C:\Users\e362609\Documents\MATLAB\Examples\vision\TextDetectionExample\trytext.txt', 'w+');
fprintf(f, '%s\r\n',ocrtxt.Words{:})
fclose('all');
Expected one output from a curly brace or dot indexing expression, but there were 544 results.
Error in REMOVEDallEXTRAbits (line 144)
fprintf(f, '%s\r\n',ocrtxt.Words{:})

Sign in to comment.

Accepted Answer

Laura Elder
Laura Elder on 7 Jul 2019
THE GOLDEN TICKET!!!!
THANK YOU ALL FOR ALL OF YOUR HELP :)
ocrtxt = ocr(BW2, textBBoxes, 'Language', 'C:\Users\e362609\Documents\MATLAB\Examples\vision\TextDetectionExample\try50\tessdata\try50.traineddata');
% ocrtxt(1,1).Text
f = fopen('C:\Users\e362609\Documents\MATLAB\Examples\vision\TextDetectionExample\trytext.txt', 'w+');
for i= 1 :numel(ocrtxt)
fprintf(f,'%s\r\n',ocrtxt(i).Text);
end
% fclose('all');

More Answers (2)

dpb
dpb on 6 Jul 2019
To write cell output with fprintf, you must dereference the cell content "use the curlies, Luke!".
fprintf(fileID, '%s\r\n', text{:});
or, use the new writecell if you have R2019a.
  6 Comments
dpb
dpb on 6 Jul 2019
whos orctext
Misspelled the variable...
whos orctxt
Laura Elder
Laura Elder on 6 Jul 2019
sorry
>> whos ocrtxt
Name Size Bytes Class Attributes
ocrtxt 544x1 1918916 ocrText
>>

Sign in to comment.


dpb
dpb on 6 Jul 2019
Edited: dpb on 7 Jul 2019
AHA! The one about "Expected one output from a curly brace or dot indexing expression, but there were 544 results." is helpful! ocrtxt must be a struct and the Text field a cellstr array.
When we dereferenced with {:}, that gets the whole array as a comma-separated list--there apparently are actually 544 lines in the text you got back.
You need to output each cellstr...
for i=numel(ocr.Text)
fprintf(fid,'%s\n',ocr.Text{i})
end
should provide a pathway to nirvana...presuming, of course, you've opened fid for write operations first...
ADDENDUM
The whos line showed an array of the object and so it isn't a single object with array of strings as the above presumes but an array with one string per element. So iterate over the array of objects instead of a (presumed) cellstr array..
for i=numel(ocrtxt)
fprintf(fid,'%s\n',ocrtxt(i).Text)
end
If the doc is correct, this should NOT cause any issues about cell element in fprintf
  9 Comments
Laura Elder
Laura Elder on 7 Jul 2019
Edited: Laura Elder on 7 Jul 2019
>> ocrtxt(1)
ans =
ocrText with properties:
Text: '1D10↵↵'
CharacterBoundingBoxes: [6×4 double]
CharacterConfidences: [6×1 single]
Words: {'1D10'}
WordBoundingBoxes: [6932 8805 154 49]
WordConfidences: 0.9324
>>
>> ocrtxt{1}
Brace indexing is not supported for variables of this type.
>>
>> ocrtxt(1,1).Text
ans =
'1D10
'
>>
Laura Elder
Laura Elder on 7 Jul 2019
Edited: Laura Elder on 7 Jul 2019
f = fopen('trytext2.txt');
for i=numel(ocrtxt.Text)
fprintf(f,'%s\n',ocrtxt(i).Text)
end
fclose(f);
Error
Array indices must be positive integers or logical values.
Error in REMOVEDallEXTRAbits (line 145)
fprintf(f,'%s\n',ocrtxt(i).Text)
I double checked and I do not use i anyehere else in the file
I went though the help docs and someone had said that you might get this if you are trying to start at 0 and not at 1. So I tried :
for i= 1 :numel(ocrtxt.Text)
fprintf(f,'%s\n',ocrtxt(i).Text)
end
Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in REMOVEDallEXTRAbits (line 147)
fclose(f);

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!