Tesseract在Tomcat上运行缓慢

时间:2019-07-16 07:51:08

标签: java performance tomcat servlets tesseract

我编写了一个使用Tesseract进行OCR的servlet。我全部在Tomcat WebServer上运行
如果我通过Eclipse测试运行Tomcat的servlet,一切正常。
如果我通过控制台启动Tomcat,则Tesseract的速度要比我通过Eclipse启动Tomcat慢得多。

怎么可能?有什么解决方案可以提高执行速度吗?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{   
    int numeroTotalaAree = 0;


    Tesseract tesseract = new Tesseract();
    tesseract.setDatapath(getServletContext().getRealPath("./"));
    tesseract.setLanguage("ita+eng");

    ArrayList<Area> aree = new ArrayList<Area>();

    try 
    {
        String content = "";
        int numeroPagine = Integer.valueOf(request.getParameter("numeroPagine"));
        for (int i = 0; i < numeroPagine; i++) 
        {
            if(request.getParameter("numeroAree" + i) != null)
            {
                int numeroAree = Integer.valueOf(request.getParameter("numeroAree" + i));
                if(request.getParameter("x_" + i + "_0") != null)
                {
                    for (int j = 0; j < numeroAree; j++) 
                    {
                        numeroTotalaAree ++;


                        if((Integer.parseInt(request.getParameter("originalHieght")) + Integer.valueOf(request.getParameter("x_" + i + "_" + j))) < Integer.valueOf(request.getParameter("height_" + i + "_" + j)))
                        {
                            Rectangle r = new Rectangle(Integer.valueOf(request.getParameter("x_" + i + "_" + j)), Integer.valueOf(request.getParameter("y_" + i + "_" + j)), Integer.valueOf(request.getParameter("width_" + i + "_" + j)), Integer.valueOf(request.getParameter("height_" + i + "_" + j)) - 2);
                            BufferedImage bufferedImage = ImageIO.read(new File(getServletContext().getRealPath("temp/temp-" + i + ".png")));


                            ImageIOUtil.writeImage(bufferedImage.getSubimage((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight()), getServletContext().getRealPath("temp/temp.png"), 300);
                            File temp = new File(getServletContext().getRealPath("./temp/temp.png"));


                            content = tesseract.doOCR(ImageIO.read(temp)) + "\n";
                            aree.add(new Area(i, j, "Area_" + i + "_" + j, content));

                            temp.delete();
                        }
                        else
                        {
                            Rectangle r = new Rectangle(Integer.valueOf(request.getParameter("x_" + i + "_" + j)), Integer.valueOf(request.getParameter("y_" + i + "_" + j)), Integer.valueOf(request.getParameter("width_" + i + "_" + j)), Integer.valueOf(request.getParameter("height_" + i + "_" + j)));
                            BufferedImage bufferedImage = ImageIO.read(new File(getServletContext().getRealPath("temp/temp-" + i + ".png")));

                            ImageIOUtil.writeImage(bufferedImage.getSubimage((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight()), getServletContext().getRealPath("temp/temp.png"), 300);
                            File temp = new File(getServletContext().getRealPath("./temp/temp.png"));


                            content = tesseract.doOCR(ImageIO.read(temp)) + "\n";
                            aree.add(new Area(i, j, "Area_" + i + "_" + j, content));

                            temp.delete();
                        }
                    }
                }
            }
        }

        request.setAttribute("numeroTotaleAree", numeroTotalaAree);
        request.setAttribute("aree", aree);
        request.getRequestDispatcher("result.jsp").forward(request, response);
    } 
    catch (Exception e) 
    {
        //e.printStackTrace();
    }       
}

0 个答案:

没有答案