软件杯-题目和插件

在此次的软件杯的比赛中,我们的题目是:网店工商信息图片文字提取。

基于这次的题目,我认为最为关键就是图片文字的提取功能,在进行资料查阅的工程中,我了解到现有的文字提取的插件是tesseract在进行了此插件的安装和配置后。

1.第一次实验:只对一张图片进行识别

代码:

//实现中文识别
package main;

import java.io.IOException;

public class test2 {
    public static void main(String[] args) throws InterruptedException {
        try {  
            Process  pro = Runtime.getRuntime()  
                           .exec(new String[]{"E:/Tesseract-OCR/tesseract.exe", //插件的位置 
                                              "E:/test.png",   //图片位置
                                              "E:/test",       //结果输出位置 .txt
                                              "-l",
                                              "chi_sim"});  //自带的中文识别字库
            pro.waitFor();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
        System.out.println("识别结束");
        }

结果截图:

结论:可以识别,但是正确率很低,而且他的识别速度很慢,对于识别速度,我能想到的解决办法是对图片进行切割,关于正确率,我觉得需要对图片进行处理来使得这个图片更加清晰,便于识别。

2.题目还要求对于文件中的图片进行识别,为此我做了第二次实验:识别一个文件夹,为了节省时间,我在文件中放入两张图片。

代码:

Test3.java
//识别整个文件中的图片
package main;
import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStreamReader;  
import java.util.ArrayList;  
import java.util.List;  
import org.jdesktop.swingx.util.OS;  
public class test3  
{  
    private final String LANG_OPTION = "-l";  
    private final String EOL = System.getProperty("line.separator");  
    /** 
     * 文件位置我放置在,项目同一路径 
     */  
    private String tessPath = new File("E:/Tesseract-OCR").getAbsolutePath();  
    /** 
     * @param imageFile 
     *            传入的图像文件 
     * @param imageFormat 
     *            传入的图像格式 
     * @return 识别后的字符串 
     */  
    public String recognizeText(File imageFile) throws Exception  
    {  
        /** 
         * 设置输出文件的保存的文件目录 
         */  
        File outputFile = new File(imageFile.getParentFile(), "output");  
  
        StringBuffer strB = new StringBuffer();  
        List<String> cmd = new ArrayList<String>();  
        if (OS.isWindowsXP())  
        {  
            cmd.add(tessPath + "\tesseract");  
        } else if (OS.isLinux())  
        {  
            cmd.add("tesseract");  
        } else  
        {  
            cmd.add(tessPath + "\tesseract");  
        }  
        cmd.add("");  
        cmd.add(outputFile.getName());  
        cmd.add(LANG_OPTION);  
        cmd.add("chi_sim");  
        cmd.add("eng");  
  
        ProcessBuilder pb = new ProcessBuilder();  
        /** 
         *Sets this process builder's working directory. 
         */  
        pb.directory(imageFile.getParentFile());  
        cmd.set(1, imageFile.getName());  
        pb.command(cmd);  
        pb.redirectErrorStream(true);  
        Process process = pb.start();  
        // tesseract.exe 1.jpg 1 -l chi_sim  
        // Runtime.getRuntime().exec("tesseract.exe 1.jpg 1 -l chi_sim");  
        /** 
         * the exit value of the process. By convention, 0 indicates normal 
         * termination. 
         */  
//      System.out.println(cmd.toString());  
        int w = process.waitFor();  
        if (w == 0)// 0代表正常退出  
        {  
            BufferedReader in = new BufferedReader(new InputStreamReader(  
                    new FileInputStream(outputFile.getAbsolutePath() + ".txt"),  
                    "UTF-8"));  
            String str;  
  
            while ((str = in.readLine()) != null)  
            {  
                strB.append(str).append(EOL);  
            }  
            in.close();  
        } else  
        {  
            String msg;  
            switch (w)  
            {  
            case 1:  
                msg = "Errors accessing files. There may be spaces in your image's filename.";  
                break;  
            case 29:  
                msg = "Cannot recognize the image or its selected region.";  
                break;  
            case 31:  
                msg = "Unsupported image format.";  
                break;  
            default:  
                msg = "Errors occurred.";  
            }  
            throw new RuntimeException(msg);  
        }  
        new File(outputFile.getAbsolutePath() + ".txt").delete();  
        return strB.toString().replaceAll("\s*", "");  
    }  
}  
//测试类
package main;
import java.io.File;
public class ceshi {
    public static void main(String[] args) 
    {  
        try  
        {  
              
            File testDataDir = new File("E:\test");  
            System.out.println(testDataDir.listFiles().length);  
            int i = 0 ;   
            for(File file :testDataDir.listFiles())  
            {  
                i++ ;  
                String recognizeText = new test3().recognizeText(file);  
                System.out.print(recognizeText+"	");  
  
                if( i % 1  == 0 )  
                {  
                    System.out.println();  
                }  
            }  
        } catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        System.out.print("识别结束");
    }  
}

结果截图;

结论:在此次的两次实验中,我大致能够了解tesseract的使用过程,但是也发现了他的问题,他自带的字库中中文字数太少,且他的识别速度和他的识别正确率都没有很高,所以我接下来的任务就是 进行图片的进一步的处理

原文地址:https://www.cnblogs.com/baiyue/p/9306748.html