1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-22 01:02:17 -05:00

OCR inner api changes. Added ``suppport'' for shadows.

Changed OCRBench a bit more.



git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@308 3f818213-9676-44b0-a9b4-5e4c4e03d09d
This commit is contained in:
Wizzup? 2009-12-21 19:48:57 +00:00
parent f4c25beb0e
commit 64ba5b72ec
7 changed files with 189 additions and 95 deletions

View File

@ -7,7 +7,7 @@
<TargetFileExt Value=""/>
<Icon Value="0"/>
<UseXPManifest Value="True"/>
<ActiveEditorIndexAtStart Value="2"/>
<ActiveEditorIndexAtStart Value="0"/>
</General>
<VersionInfo>
<ProjectVersion Value=""/>
@ -31,12 +31,12 @@
<PackageName Value="LCL"/>
</Item1>
</RequiredPackages>
<Units Count="7">
<Units Count="9">
<Unit0>
<Filename Value="project1.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="project1"/>
<UsageCount Value="21"/>
<UsageCount Value="24"/>
</Unit0>
<Unit1>
<Filename Value="unit1.pas"/>
@ -44,10 +44,10 @@
<ComponentName Value="Form1"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
<CursorPos X="75" Y="77"/>
<TopLine Value="55"/>
<CursorPos X="95" Y="72"/>
<TopLine Value="44"/>
<EditorIndex Value="0"/>
<UsageCount Value="21"/>
<UsageCount Value="24"/>
<Loaded Value="True"/>
</Unit1>
<Unit2>
@ -58,8 +58,8 @@
<UnitName Value="colourhistory"/>
<CursorPos X="10" Y="457"/>
<TopLine Value="437"/>
<EditorIndex Value="4"/>
<UsageCount Value="11"/>
<EditorIndex Value="5"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit2>
<Unit3>
@ -77,110 +77,165 @@
<UnitName Value="Client"/>
<CursorPos X="21" Y="52"/>
<TopLine Value="32"/>
<EditorIndex Value="1"/>
<UsageCount Value="10"/>
<EditorIndex Value="2"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit4>
<Unit5>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<UnitName Value="ocr"/>
<CursorPos X="23" Y="100"/>
<TopLine Value="90"/>
<EditorIndex Value="2"/>
<UsageCount Value="10"/>
<CursorPos X="10" Y="290"/>
<TopLine Value="313"/>
<EditorIndex Value="3"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit5>
<Unit6>
<Filename Value="../../Units/MMLCore/bitmaps.pas"/>
<UnitName Value="bitmaps"/>
<CursorPos X="3" Y="35"/>
<TopLine Value="27"/>
<EditorIndex Value="3"/>
<UsageCount Value="10"/>
<CursorPos X="49" Y="368"/>
<TopLine Value="337"/>
<EditorIndex Value="4"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
</Unit6>
<Unit7>
<Filename Value="../../Units/MMLCore/ocrutil.pas"/>
<UnitName Value="ocrutil"/>
<CursorPos X="58" Y="184"/>
<TopLine Value="160"/>
<EditorIndex Value="1"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
</Unit7>
<Unit8>
<Filename Value="../../../../Documents/lazarus/lcl/interfaces/gtk/gtkwidgetset.inc"/>
<CursorPos X="41" Y="1226"/>
<TopLine Value="1205"/>
<UsageCount Value="10"/>
</Unit8>
</Units>
<JumpHistory Count="20" HistoryIndex="19">
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="unit1.pas"/>
<Caret Line="9" Column="19" TopLine="1"/>
<Caret Line="65" Column="43" TopLine="33"/>
</Position1>
<Position2>
<Filename Value="unit1.pas"/>
<Caret Line="25" Column="36" TopLine="1"/>
<Caret Line="55" Column="70" TopLine="22"/>
</Position2>
<Position3>
<Filename Value="../SAMufasaGUI/colourhistory.pas"/>
<Caret Line="30" Column="67" TopLine="181"/>
<Filename Value="unit1.pas"/>
<Caret Line="54" Column="70" TopLine="22"/>
</Position3>
<Position4>
<Filename Value="unit1.pas"/>
<Caret Line="47" Column="87" TopLine="19"/>
<Caret Line="30" Column="60" TopLine="22"/>
</Position4>
<Position5>
<Filename Value="unit1.pas"/>
<Caret Line="57" Column="34" TopLine="33"/>
<Caret Line="31" Column="60" TopLine="22"/>
</Position5>
<Position6>
<Filename Value="unit1.pas"/>
<Caret Line="64" Column="7" TopLine="33"/>
<Caret Line="32" Column="60" TopLine="22"/>
</Position6>
<Position7>
<Filename Value="../../Units/MMLCore/client.pas"/>
<Caret Line="52" Column="21" TopLine="32"/>
<Filename Value="unit1.pas"/>
<Caret Line="31" Column="60" TopLine="22"/>
</Position7>
<Position8>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="40" Column="31" TopLine="17"/>
<Filename Value="unit1.pas"/>
<Caret Line="33" Column="60" TopLine="22"/>
</Position8>
<Position9>
<Filename Value="unit1.pas"/>
<Caret Line="65" Column="43" TopLine="33"/>
<Caret Line="84" Column="50" TopLine="44"/>
</Position9>
<Position10>
<Filename Value="unit1.pas"/>
<Caret Line="55" Column="70" TopLine="22"/>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="81" Column="22" TopLine="60"/>
</Position10>
<Position11>
<Filename Value="unit1.pas"/>
<Caret Line="54" Column="70" TopLine="22"/>
<Caret Line="75" Column="33" TopLine="49"/>
</Position11>
<Position12>
<Filename Value="unit1.pas"/>
<Caret Line="30" Column="60" TopLine="22"/>
<Caret Line="79" Column="15" TopLine="42"/>
</Position12>
<Position13>
<Filename Value="unit1.pas"/>
<Caret Line="31" Column="60" TopLine="22"/>
<Filename Value="../../Units/MMLCore/bitmaps.pas"/>
<Caret Line="98" Column="21" TopLine="75"/>
</Position13>
<Position14>
<Filename Value="unit1.pas"/>
<Caret Line="32" Column="60" TopLine="22"/>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="31" Column="54" TopLine="25"/>
</Position14>
<Position15>
<Filename Value="unit1.pas"/>
<Caret Line="31" Column="60" TopLine="22"/>
<Filename Value="../../Units/MMLCore/ocrutil.pas"/>
<Caret Line="180" Column="111" TopLine="157"/>
</Position15>
<Position16>
<Filename Value="unit1.pas"/>
<Caret Line="33" Column="60" TopLine="22"/>
<Filename Value="../../Units/MMLCore/ocrutil.pas"/>
<Caret Line="181" Column="13" TopLine="160"/>
</Position16>
<Position17>
<Filename Value="unit1.pas"/>
<Caret Line="84" Column="50" TopLine="44"/>
<Filename Value="../../Units/MMLCore/ocrutil.pas"/>
<Caret Line="57" Column="51" TopLine="36"/>
</Position17>
<Position18>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="81" Column="22" TopLine="60"/>
<Filename Value="unit1.pas"/>
<Caret Line="72" Column="83" TopLine="57"/>
</Position18>
<Position19>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="49" Column="22" TopLine="74"/>
<Filename Value="unit1.pas"/>
<Caret Line="78" Column="38" TopLine="65"/>
</Position19>
<Position20>
<Filename Value="unit1.pas"/>
<Caret Line="75" Column="33" TopLine="49"/>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="42" Column="34" TopLine="25"/>
</Position20>
<Position21>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="78" Column="70" TopLine="62"/>
</Position21>
<Position22>
<Filename Value="../../Units/MMLCore/ocr.pas"/>
<Caret Line="290" Column="10" TopLine="265"/>
</Position22>
<Position23>
<Filename Value="unit1.pas"/>
<Caret Line="75" Column="17" TopLine="46"/>
</Position23>
<Position24>
<Filename Value="unit1.pas"/>
<Caret Line="76" Column="21" TopLine="56"/>
</Position24>
<Position25>
<Filename Value="unit1.pas"/>
<Caret Line="87" Column="27" TopLine="56"/>
</Position25>
<Position26>
<Filename Value="unit1.pas"/>
<Caret Line="72" Column="4" TopLine="56"/>
</Position26>
<Position27>
<Filename Value="unit1.pas"/>
<Caret Line="70" Column="70" TopLine="56"/>
</Position27>
<Position28>
<Filename Value="../../Units/MMLCore/ocrutil.pas"/>
<Caret Line="169" Column="23" TopLine="156"/>
</Position28>
<Position29>
<Filename Value="unit1.pas"/>
<Caret Line="71" Column="29" TopLine="44"/>
</Position29>
<Position30>
<Filename Value="../../Units/MMLCore/ocrutil.pas"/>
<Caret Line="184" Column="58" TopLine="160"/>
</Position30>
</JumpHistory>
</ProjectOptions>
<CompilerOptions>

View File

@ -1,8 +1,9 @@
object Form1: TForm1
Left = 492
Left = 493
Height = 305
Top = 266
Top = 293
Width = 609
ActiveControl = BitmapButton
Caption = 'Form1'
ClientHeight = 305
ClientWidth = 609
@ -40,6 +41,14 @@ object Form1: TForm1
OnClick = PathButtonClick
TabOrder = 2
end
object FShadow: TCheckBox
Left = 507
Height = 20
Top = 112
Width = 79
Caption = 'Shadow?'
TabOrder = 3
end
object OCRFileOpen: TOpenDialog
Filter = '.bmp'
left = 528

View File

@ -14,6 +14,7 @@ type
TForm1 = class(TForm)
BitmapButton: TButton;
FShadow: TCheckBox;
PathButton: TButton;
OCRButton: TButton;
Image1: TImage;
@ -45,9 +46,10 @@ Var
C: TClient;
bmp: TMufasaBitmap;
x,y: integer;
s: string;
Shadow: boolean;
begin
writeln(BitmapPath);
if not FileExists(BitmapPath) then
begin
MessageBox(0,pchar('You did not set a valid bitmap'), Pchar('Bitmap Error'),
@ -65,16 +67,24 @@ begin
Exit;
end;
Form1.Image1.Canvas.Brush.Color := 0;
Form1.Image1.Canvas.Rectangle(0, 0, Form1.Image1.Canvas.Width, Form1.Image1.Canvas.Height);
C := TClient.Create;
bmp := TMufasaBitmap.Create;
bmp.LoadFromFile(BitmapPath);
C.MWindow.SetTarget(bmp);
C.MOCR.InitTOCR(UpTextPath + DS + '..' + DS);
writeln(C.MOCR.GetUpTextAt(7,7));
Shadow :=FShadow.Checked;
C.MOCR.InitTOCR(UpTextPath + DS + '..' + DS, Shadow);
s := C.MOCR.GetUpTextAt(7,7, Shadow);
for y := 0 to C.MOCR.debugbmp.Height - 1 do
for x := 0 to C.MOCR.debugbmp.Width -1 do
Form1.Image1.Canvas.Pixels[x,y] := C.MOCR.debugbmp.FastGetPixel(x,y);
Form1.Image1.Canvas.Font.Color:=clRed;
Form1.Image1.Canvas.TextOut(0, C.MOCR.debugbmp.Height, s);
C.Free;
end;

View File

@ -1,7 +1,7 @@
object ColourHistoryForm: TColourHistoryForm
Left = 373
Left = 374
Height = 250
Top = 308
Top = 335
Width = 490
ActiveControl = SelectionName
Caption = 'Colour Picker History'

View File

@ -1,8 +1,8 @@
{ This is an automatically generated lazarus resource file }
LazarusResources.Add('TColourHistoryForm','FORMDATA',[
'TPF0'#18'TColourHistoryForm'#17'ColourHistoryForm'#4'Left'#3'u'#1#6'Height'#3
+#250#0#3'Top'#3'4'#1#5'Width'#3#234#1#13'ActiveControl'#7#13'SelectionName'#7
'TPF0'#18'TColourHistoryForm'#17'ColourHistoryForm'#4'Left'#3'v'#1#6'Height'#3
+#250#0#3'Top'#3'O'#1#5'Width'#3#234#1#13'ActiveControl'#7#13'SelectionName'#7
+'Caption'#6#21'Colour Picker History'#12'ClientHeight'#3#227#0#11'ClientWidt'
+'h'#3#234#1#21'Constraints.MinHeight'#3#200#0#20'Constraints.MinWidth'#3#234
+#1#4'Menu'#7#10'CHMainMenu'#8'OnCreate'#7#10'FormCreate'#6'OnHide'#7#15'UnSe'

View File

@ -37,9 +37,9 @@ uses
TMOCR = class(TObject)
constructor Create(Owner: TObject);
destructor Destroy; override;
function InitTOCR(path: string): boolean;
function getTextPointsIn(sx, sy, w, h: Integer): TNormArray;
function GetUpTextAt(atX, atY: integer): string;
function InitTOCR(path: string; shadow: Boolean): boolean;
function getTextPointsIn(sx, sy, w, h: Integer; shadow: boolean): TNormArray;
function GetUpTextAt(atX, atY: integer; shadow: boolean): string;
private
Client: TObject;
OCRData: TocrDataArray;
@ -75,7 +75,7 @@ const
Non optimised. ;-)
}
function TMOCR.getTextPointsIn(sx, sy, w, h: Integer): TNormArray;
function TMOCR.getTextPointsIn(sx, sy, w, h: Integer; shadow: boolean): TNormArray;
var
bmp: TMufasaBitmap;
x,y: integer;
@ -250,6 +250,19 @@ begin
bmp.SaveToFile('/tmp/ocrdebug.bmp');
{$ENDIF}
if shadow then
begin
for y := 0 to bmp.Height - 1 do
for x := 0 to bmp.Width - 1 do
begin
if bmp.fastgetpixel(x,y) <> clPurple then
begin
bmp.FastSetPixel(x,y,0);
continue;
end;
end;
end else
begin
for y := 0 to bmp.Height - 1 do
for x := 0 to bmp.Width - 1 do
begin
@ -274,6 +287,7 @@ begin
continue;
end;
end;
end;
for y := 0 to bmp.Height -1 do
bmp.fastsetpixel(0, y, 0);
@ -337,7 +351,7 @@ begin
inherited Destroy;
end;
function TMOCR.InitTOCR(path: string): boolean;
function TMOCR.InitTOCR(path: string; shadow: boolean): boolean;
begin
{ This must be dynamic }
@ -345,17 +359,17 @@ begin
result := true;
OCRPath := path + DS;
if DirectoryExists(path + DS + 'UpChars' + DS) then
OCRData[0] := ocrutil.InitOCR(path + DS + 'UpChars' + DS)
OCRData[0] := ocrutil.InitOCR(path + DS + 'UpChars' + DS, shadow)
else
result := false;
if DirectoryExists(path + DS + 'StatChars' + DS) then
OCRData[1] := ocrutil.InitOCR(path + DS + 'StatChars' + DS)
OCRData[1] := ocrutil.InitOCR(path + DS + 'StatChars' + DS, shadow)
else
result := false;
end;
function TMOCR.GetUpTextAt(atX, atY: integer): string;
function TMOCR.GetUpTextAt(atX, atY: integer; shadow: boolean): string;
var
n:Tnormarray;
@ -365,7 +379,7 @@ begin
ww := 400;
hh := 20;
n := getTextPointsIn(atX, atY, ww, hh);
n := getTextPointsIn(atX, atY, ww, hh, shadow);
Result := ocrDetect(n, ww, hh, OCRData[0]);
//writeln(result);
end;

View File

@ -53,8 +53,8 @@ type
end;
procedure findBounds(glyphs: TocrGlyphMaskArray; out width,height: integer);
function LoadGlyphMasks(path: string): TocrGlyphMaskArray;
function InitOCR(path: string): TocrData;
function LoadGlyphMasks(path: string; shadow: boolean): TocrGlyphMaskArray;
function InitOCR(path: string; shadow: boolean): TocrData;
function GuessGlyph(glyph: TNormArray; ocrdata: TocrData): char;
function PointsToNorm(points: TpointArray; out w,h: integer): TNormArray;
function ImageToNorm(src: tRGBArray; w,h: integer): TNormArray;
@ -153,17 +153,22 @@ begin
end;
{This Loads the actual data from the .bmp, but does not init all fields}
function LoadGlyphMasks(path: string): TocrGlyphMaskArray;
function LoadGlyphMasks(path: string; shadow: boolean): TocrGlyphMaskArray;
var
strs: array of string;
bmp: array of Tbmp; {-> TMufasaBitmap, and why use an array? }
len,size,i,j: integer;
color: tRGB;
shadow_i: byte;
begin
strs:= GetFiles(path,'bmp');
len:= length(strs);
SetLength(result,len);
SetLength(bmp,len);
if shadow then
shadow_i := 0
else
shadow_i := 255;
for i:= 0 to len-1 do
begin
bmp[i]:= ReadBmp(path + strs[i]);
@ -172,8 +177,9 @@ begin
for j:= 0 to size-1 do
begin
color:= bmp[i].data[j];
if (color.r = 255) and (color.g = 255) and (color.b = 255) then
//if (color.r = 255) and (color.g = 0) and (color.b = 0) then
{ if (color.r = 255) and (color.g = 255 and not shadow_i) and
(color.b = 255 and not shadow_i) then}
if (color.r = 255) and (color.g = shadow_i) and (color.b = shadow_i) then
result[i].mask[j]:= 1
else
result[i].mask[j]:= 0;
@ -186,7 +192,7 @@ begin
end;
{Fully initalizes a TocrData structure, this is LoadFont or whatever, call it first}
function InitOCR(path: string): TocrData;
function InitOCR(path: string; shadow: boolean): TocrData;
var
masks: TocrGlyphMaskArray;
t,b,l,r,w,h,mw: integer;
@ -195,7 +201,7 @@ var
pos: integer;
ascii: char;
begin
masks:= LoadGlyphMasks(path);
masks:= LoadGlyphMasks(path, shadow);
w:= 0;
h:= 0;
findBounds(masks,w,h);