Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.BufferedImage;
  3. import java.awt.image.MemoryImageSource;
  4. import java.awt.image.PixelGrabber;
  5. import java.awt.image.WritableRaster;
  6. import java.awt.image.ImageObserver;
  7. import java.io.*;
  8. import java.net.URL;
  9. import java.util.*;
  10.  
  11. import javax.imageio.ImageIO;
  12. import javax.imageio.stream.ImageInputStream;
  13. import javax.swing.*;
  14. import java.awt.GraphicsConfiguration;
  15.  
  16. public class ocr
  17. {
  18.     public static BufferedImage bimg;
  19.     public static Image img;
  20.     public static Robot rob;
  21.     public static HashMap<int[],String> cmap;
  22.     public static HashMap<String, int[]> analysis;
  23.     public static boolean analyze;
  24.  
  25.     public static void main(String[] args) throws Exception
  26.     {
  27.         analyze=true;
  28.         cmap = new HashMap<int[], String>();
  29.  
  30.         try{
  31.             FileInputStream fis = new FileInputStream("crusade.cmap");
  32.             ObjectInputStream ois = new ObjectInputStream(fis);
  33.  
  34.             analysis = (HashMap<String, int[]>)ois.readObject();
  35.  
  36.         } catch(Exception e) {
  37.             analysis = new HashMap<String, int[]>();
  38.         }
  39.         for(int x = 0; x < 50;x++)
  40.         {
  41.             File sourceimage = new File("samples/ocr"+x+".jpg");
  42.             bimg = ImageIO.read(sourceimage);
  43.  
  44.             int[] rgbarray = new int[120*40];
  45.  
  46.             for(int i = 0; i < 120*40;i++)
  47.             {
  48.                 rgbarray[i] = bimg.getRGB(i%120, ((i/120)%40));
  49.             }
  50.  
  51.             for(int i = 0; i <rgbarray.length;i++)
  52.             {
  53.                 int alpha = (rgbarray[i] >> 24) & 0xff;
  54.                 int red   = (rgbarray[i] >> 16) & 0xff;
  55.                 int green = (rgbarray[i] >>  8) & 0xff;
  56.                 int blue  = (rgbarray[i]      ) & 0xff;
  57.  
  58.                 if(blue < 120 && red < 120 && green < 120)
  59.                 {
  60.                     bimg.setRGB(i%120, ((i/120)%40), 0x00000000);
  61.                     //newarray[i] = -99999999;
  62.                 } else {
  63.                     bimg.setRGB(i%120, ((i/120)%40), 0xFFFFFFFF);
  64.                     //newarray[i] = 0xffffffff;
  65.                 }
  66.  
  67.             }
  68.  
  69.             BufferedImage[] characters = isolatechars();
  70.  
  71.             for(int i = 1; i < characters.length;i++)
  72.             {
  73.                 analyse(characters[i]);
  74.                 saveMap("crusade");
  75.             }
  76.  
  77.         }
  78.         PrintWriter out = new PrintWriter(new File("analysis.txt"));
  79.  
  80.         for(String key : analysis.keySet())
  81.         {
  82.             for(int i = 0; i < analysis.get(key).length;i++)
  83.             {
  84.                 out.print(((analysis.get(key)[i])));
  85.                 if(((i%13) == 0) && !(i ==0))
  86.                     out.println();
  87.             }
  88.             out.println();
  89.             out.println();
  90.             out.println();
  91.             out.println();
  92.             out.println();
  93.         }
  94.         out.close();
  95.  
  96.  
  97.     }
  98.  
  99.     public static Image getImageFromArray(int[] pixels, int width, int height) {
  100.         MemoryImageSource mis = new MemoryImageSource(width, height, pixels, 0, width);
  101.         Toolkit tk = Toolkit.getDefaultToolkit();
  102.         return tk.createImage(mis);
  103.     }
  104.  
  105.  
  106.     public static BufferedImage[] isolatechars() throws Exception
  107.     {
  108.         BufferedImage[] sections = new BufferedImage[7];
  109.         int[] section = new int[13*26];
  110.  
  111.  
  112.         for(int x = 1; x < 7;x++)
  113.         {
  114.             //PixelGrabber pg = new PixelGrabber(bimg,0+(16*x+1),8,15,26,false);
  115.             //pg.grabPixels();
  116.             //section = (int[])pg.getPixels();
  117.             //bimg.getSubimage(x, y, w, h)
  118.             if(x==1)
  119.                 sections[x] = (BufferedImage)bimg.getSubimage(15,8,13,26);
  120.             else if (x == 2)
  121.                 sections[x] = (BufferedImage)bimg.getSubimage(31,8,13,26);
  122.             else if (x == 3)
  123.                 sections[x] = (BufferedImage)bimg.getSubimage(47,8,13,26);
  124.             else if (x == 4)
  125.                 sections[x] = (BufferedImage)bimg.getSubimage(64,8,13,26);
  126.             else if (x == 5)
  127.                 sections[x] = (BufferedImage)bimg.getSubimage(80,8,13,26);
  128.             else if (x == 6)
  129.                 sections[x] = (BufferedImage)bimg.getSubimage(95,8,13,26);
  130.         }
  131.  
  132.         return sections;
  133.     }
  134.  
  135.     public static String solve(BufferedImage img) throws Exception
  136.     {
  137.         int[] network = new int[13*26];
  138.         float[] probs = new float[analysis.keySet().size()];
  139.         int index = 0;
  140.         int temp = 0;
  141.         String[] keysAtIndex = new String[network.length];
  142.  
  143.         for(int i = 0; i < 13*26;i++)
  144.         {
  145.             network[i] = img.getRGB(i%13, ((i/13)%26));
  146.             if(network[i] == -16777216)
  147.             {
  148.                 network[i] = 1;
  149.             } else {
  150.                 network[i] = 0;
  151.             }
  152.         }
  153.         /* see our network...
  154.         PrintWriter out = new PrintWriter(new File("tempanalyse.txt"));
  155.         for(int i = 0; i < network.length;i++)
  156.         {
  157.             out.print(network[i]);
  158.             if(((i%13) == 0) && !(i ==0))
  159.                 out.println();
  160.         }
  161.         out.close();
  162.        
  163.         System.exit(1);
  164.          */
  165.         int numPixelsHit = 0;
  166.         for(int i = 0; i < network.length;i++)
  167.             if(network[i] == 1)
  168.                 numPixelsHit++;
  169.  
  170.         for(String key : analysis.keySet())
  171.         {
  172.             temp = 0;
  173.             for(int x = 0; x < network.length;x++)
  174.             {
  175.                 if(network[x] == 1)
  176.                 {
  177.                     temp += analysis.get(key)[x];
  178.                 }
  179.             }
  180.             probs[index] = temp / numPixelsHit;
  181.             keysAtIndex[index] = key;
  182.             index++;
  183.         }
  184.  
  185.         int highestIndex = 0;
  186.         float highest = 0;
  187.         for(int i = 0; i < probs.length;i++)
  188.         {
  189.             if(probs[i] > highest)
  190.             {
  191.                 highestIndex = i;
  192.                 highest = probs[i];
  193.             }
  194.         }
  195.  
  196.  
  197.         return keysAtIndex[highestIndex];
  198.     }
  199.  
  200.     public static void analyse(BufferedImage im) throws Exception
  201.     {
  202.         JFrame frame = new JFrame();
  203.         JLabel label = new JLabel(new ImageIcon(im));
  204.         frame.getContentPane().add(label, BorderLayout.CENTER);
  205.         frame.pack();
  206.         frame.setVisible(true);
  207.         System.out.println("best guess: " + solve(im)); // where the magic happens baby
  208.         int[] network = new int[13*26];
  209.  
  210.         for(int i = 0; i < 13*26;i++)
  211.         {
  212.             network[i] = im.getRGB(i%13, ((i/13)%26));
  213.             if(network[i] == -16777216)
  214.             {
  215.                 network[i] = 1;
  216.             } else {
  217.                 network[i] = 0;
  218.             }
  219.         }
  220.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  221.         String theletter = br.readLine();
  222.  
  223.         if(!analysis.containsKey(theletter))
  224.         {
  225.             analysis.put(theletter, network);
  226.         }
  227.         else
  228.         {
  229.             addToAnalysis(theletter, network);
  230.         }
  231.         frame.dispose();
  232.     }
  233.  
  234.     public static void addToAnalysis(String letter, int[] array)
  235.     {
  236.         for(int i = 0; i < analysis.get(letter).length;i++)
  237.         {
  238.             if(array[i] != 0)
  239.                 analysis.get(letter)[i]++;
  240.         }
  241.     }
  242.    
  243.     public static void saveMap(String name) throws Exception
  244.     {
  245.         FileOutputStream fos = new FileOutputStream(name+".cmap");
  246.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  247.         oos.writeObject(analysis);
  248.     }
  249.  
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement