home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / microcrn / issue_51.arc / OCR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-11-29  |  8.0 KB  |  304 lines

  1. program OCR;
  2.  
  3. (* (c) Acquired Intelligence; po box 2091; davis,CA 95617
  4.        for Tidbits #51, Micro Cornucopia
  5.        Questions -- call 916-753-0360
  6.  
  7. Program --
  8. (1) reads PCX file into a viewport (full screen in this example);
  9. (2) converts viewport to a 2 dimensional picture (Xs and .s);
  10. (3) execs to BrainMaker (a neural network);
  11. (4) BrainMaker evaluates the pictures;
  12. (4) converts BrainMaker output evaluations to text;
  13.  
  14. requires BrainMaker neural net (from California Scientific Software)
  15. and PCX Tools (from Genus Programming).
  16.  
  17. *)
  18.  
  19. uses
  20.   Crt, Dos, Graph, pcx_tp;
  21.  
  22. var
  23.   F, F2 : text;
  24.  
  25. const
  26.   { BGI fonts }
  27.   Fonts : array[0..4] of string[13] =
  28.   ('DefaultFont', 'TriplexFont', 'SmallFont',
  29.    'SansSerifFont', 'GothicFont');
  30.  
  31.   { BGI text directions }
  32.   TextDirect : array[0..1] of string[8] =
  33.   ('HorizDir', 'VertDir');
  34.  
  35.   Num_of_patterns            = 10;
  36.   Num_of_characters          = 2000;  { for 80 x 25 Viewport }
  37.   Input_file_from_neural_net = 'C:\TP\EXE\BrainRTS.Out';
  38.   Output_file_for_neural_net = 'C:\TP\EXE\BrainRTS.In';
  39.   OCR_Output_file            = 'C:\TP\EXE\OCR.Out';
  40.   PCX_file                   = 'C:\TP\EXE\a.PCX';
  41.   Line_length                = 79;
  42.   Threshold                  = 0.60;
  43.   PCX_type                   = pcxCGA_6;
  44.  
  45. type
  46.  
  47.   Weights = array[1..Num_of_characters] of string[4];
  48.   Patterns = array[1..Num_of_characters] of string[1];
  49.  
  50. { objects }
  51.  
  52. NNIptr = ^neural_net_interpreter;
  53. neural_net_interpreter = object
  54.   Array_index    : integer;
  55.   First_char, S  : string;
  56.   Weight         : Weights;
  57.   Output_pattern : Patterns;
  58.   constructor Init;
  59.   destructor Done; virtual;
  60.   procedure Get_weights;
  61.   procedure Output_characters;
  62. end;
  63.  
  64. Screenptr = ^screen;
  65. screen = object
  66.   GraphDriver : integer;  { Graphics device driver }
  67.   GraphMode   : integer;  { Graphics mode value }
  68.   MaxX, MaxY  : word;     { Maximum screen resolution }
  69.   ErrorCode   : integer;  { Reports any graphics errors }
  70.   MaxColor    : word;     { Maximum color value available }
  71.   pcxReturn   : integer;
  72.   PixelStatus : integer;
  73.   ViewInfo    : ViewPortType;
  74.   constructor init;
  75.   destructor done; virtual;
  76.   procedure Initialize;
  77. end;
  78.  
  79. var
  80.  
  81.   OldExitProc : Pointer;  { Saves exit procedure address }
  82.  
  83. {$F+}
  84. procedure MyExitProc;
  85. begin
  86.   ExitProc := OldExitProc; { Restore exit procedure address }
  87.   CloseGraph;              { Shut down the graphics system }
  88. end; { MyExitProc }
  89. {$F-}
  90.  
  91. procedure screen.Initialize;
  92. { Initialize graphics and report errors}
  93. var
  94.   InGraphicsMode: boolean; { Flags graphics initialization}
  95.   PathToDriver  : string;  { Stores DOS path to *.BGI & *.CHR }
  96. begin
  97.                            { When using Crt & graphics, turn }
  98.                            { off Crt's memory-mapped writes }
  99.   DirectVideo := False;
  100.   OldExitProc := ExitProc; { Save previous exit proc }
  101.   ExitProc := @MyExitProc; { Insert our exit proc in chain }
  102.   PathToDriver := '';
  103.   repeat
  104.     GraphDriver := Detect;       { Autodetect graphics adapter }
  105.     InitGraph(GraphDriver, GraphMode, PathToDriver);
  106.     ErrorCode := GraphResult;    { Preserve error return }
  107.     if ErrorCode <> grOK then    { Error? }
  108.     begin
  109.       Writeln('Graphics error: ', GraphErrorMsg(ErrorCode));
  110.       if ErrorCode = grFileNotFound then
  111.       begin
  112.         Writeln('Enter full path to BGI driver');
  113.         Writeln('or type <Ctrl-Break> to quit.');
  114.         Readln(PathToDriver);
  115.         Writeln;
  116.       end
  117.       else
  118.         Halt(1);             { Some other error: terminate }
  119.     end;
  120.   until ErrorCode = grOK;
  121. end; { Initialize }
  122.  
  123. { object constructors & destructors }
  124.  
  125. constructor screen.init;
  126. begin
  127. end;
  128.  
  129. destructor screen.done;
  130. begin
  131. end;
  132.  
  133. constructor neural_net_interpreter.init;
  134. begin
  135. end;
  136.  
  137. destructor neural_net_interpreter.done;
  138. begin
  139. end;
  140.  
  141. { object methods }
  142.  
  143. procedure neural_net_interpreter.Get_weights;
  144.  
  145. var
  146.    This_weight  : string[4];
  147.    This_pattern : string[1];
  148.    Count        : integer;
  149.    W            : word;
  150.    Char_Ptr     : integer;
  151. begin
  152.   FOR Count := 1 TO Num_of_characters DO  { Initialize arrays }
  153.     begin
  154.       Weight[Count]         := ' ';
  155.       Output_pattern[Count] := ' ';
  156.     end;
  157.  
  158.   Assign(F,Input_file_from_neural_net);
  159.   Reset(F);
  160.   Array_index := 1;
  161.   WHILE Array_index <= Num_of_characters DO
  162.     begin
  163.       Readln(F,S);
  164.       First_char := Copy(S,1,1);
  165.          IF First_char = ' ' THEN
  166.            begin
  167.              Char_Ptr  := 2;
  168.              FOR Count := 1 TO Num_of_patterns DO
  169.                begin
  170.                  This_weight := Copy(S,Char_Ptr,4);
  171.                  Weight[Array_index] := This_weight;
  172.                  Char_Ptr := Char_Ptr + 5;
  173.                  This_pattern := Copy(S,Char_Ptr,1);
  174.                  Output_pattern[Array_index] := This_pattern;
  175.                  Char_Ptr := Char_Ptr + 2;
  176.                  Inc(Array_index);
  177.                end;
  178.            end;
  179.     end;
  180.   Close(F);
  181. end;
  182.  
  183. procedure neural_net_interpreter.output_characters;
  184.  
  185. var
  186.   Output_char : string;
  187.   Pattern_count, Char_count, ReturnCode : integer;
  188.   Wt, New_weight : real;
  189.  
  190. begin
  191.   Assign(F2,OCR_Output_file);
  192.   Rewrite(F2);
  193.   Array_index := 1;
  194.   Char_count  := 1;
  195.  
  196.   WHILE Array_index <= Num_of_characters DO
  197.   begin
  198.    Pattern_count := 1;
  199.    Wt := 0;
  200.    Output_char   := ' ';
  201.    WHILE Pattern_count <= Num_of_patterns DO
  202.     begin
  203.       Val(Weight[Array_index],New_weight,ReturnCode);
  204.       IF New_weight > Wt THEN
  205.         begin
  206.           Wt := New_weight;
  207.           Output_char := Output_pattern[Array_index];
  208.         end;
  209.       Inc(Pattern_count);
  210.       Inc(Array_index);
  211.     end;
  212.    IF Wt >= Threshold THEN
  213.      Write(F2,Output_char)
  214.    ELSE
  215.      Write(F2,' ');
  216.    IF Char_count > Line_length THEN
  217.    begin
  218.      Writeln(F2);
  219.      Char_count := 0;
  220.    end;
  221.    Inc(Char_count);
  222.   end;
  223.   Close(F2);
  224. end;
  225.  
  226. var
  227.   NNI : NNIptr;
  228.  
  229. procedure pcx_to_neural_net;
  230.  
  231. { get a.PCX; display it; & convert it to txt for neural net. }
  232.  
  233. var
  234.   SPort           : Screenptr;
  235.   X, Y            : integer;
  236.   XPt, YPt, RowPt : integer;
  237.   S               : string;
  238.  
  239. begin
  240.   New(SPort,init);
  241.   WITH SPort^ DO
  242.    begin
  243.     Initialize;
  244.     Maxx := GetMaxx;
  245.     Maxy := GetMaxy;
  246.     SetViewPort(0,0,Maxx,Maxy,ClipOn);
  247.     SetTextStyle(DefaultFont, HorizDir, 1);
  248.     pcxReturn := pcxSetDisplay(PCX_type);
  249.     pcxReturn := pcxFileDisplay(PCX_file,0,0,0);
  250.     IF (pcxReturn = pcxSuccess) THEN
  251.     begin
  252.       Assign(F,Output_file_for_neural_net);
  253.       Rewrite(F);
  254.       GetViewSettings(ViewInfo); { coordinates of Viewport }
  255.       XPt   := 0;
  256.       YPt   := 0;
  257.       RowPt := 0;
  258.       WHILE RowPt <= ViewInfo.y2 DO
  259.       begin
  260.        WHILE XPt <= ViewInfo.x2 DO
  261.         begin
  262.           FOR Y := YPt to (YPt + 7) DO
  263.             begin
  264.               FOR X := XPt to (XPt + 7) DO
  265.                 begin
  266.                   PixelStatus := GetPixel(X,Y);
  267.                   IF PixelStatus = 0 THEN
  268.                     write(F,'.')
  269.                   ELSE
  270.                     write(F,'X');
  271.                 end;
  272.                writeln(F);
  273.             end;
  274.           YPt := RowPt;
  275.           XPt := XPt + 8;
  276.         end;
  277.        XPt   := 0;
  278.        RowPt := RowPt + 8;
  279.        YPt   := RowPt;
  280.       end;
  281.     end;
  282.     Close(F);
  283.    end;
  284.   Dispose(SPort,done);
  285. end;  { pcx_to_neural_net}
  286.  
  287.  
  288. begin { program body }
  289.   pcx_to_neural_net;
  290.   New(NNI, init);
  291.   WITH NNI^ DO
  292.   begin
  293.     SwapVectors;
  294.     exec('C:\COMMAND.COM','/C C:\BATCH\net');
  295.     SwapVectors;
  296.     IF DosError <> 0 THEN
  297.      Writeln('Dos error # ',DosError)
  298.     ELSE
  299.      Get_weights;
  300.      Output_characters;
  301.      Dispose(NNI, done);
  302.    end;
  303. end.
  304.