Pages

2023年5月4日 星期四

Java使用Tesseract進行OCR辨識的一些心得

最近有個專案想抓取PDF的檔案內容,進行檔案rename的動作,因此才開始瞭解Tesseract相關的用法,以下是使用的一些心得。

1. Windows上安裝Tesseract 5.0後,如果想轉移到其他台Windows PC,該PC要先安裝 Visual Studio 2015 的 Visual C++ 可轉散發套件,再將Tesseract安裝的目錄整個zip起來後,在其他台Windows PC上解壓縮後,設定Tesseract相關windows環境變數路徑後,Java程式就可以直接使用。


2. Tesseract程式中辨識資料的路徑與繁體中文語言設定如下:

  1. tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\");
  2. tesseract.setLanguage("chi_tra");


3. 不要透過抓取PDF文字後再辨識,因為會抓不到PDF內容文字,要先轉成圖檔(TIFF)後,在進行圖片辨識文字,會比較不會出錯,下面是範例的Code。

  1. private boolean pdfToTiffConverter(String inputPdf, String outputTiff) {
  2. int dpi = 300; // the resolution of the output image in DPI
  3. boolean result = false;
  4. PDDocument document = null;
  5. PDFRenderer pdfRenderer = null;
  6. BufferedImage image = null;
  7. try {
  8. File pdfFile = new File(inputPdf);
  9. Thread.sleep(100); // wait 100ms for loading pdf to memory to avoid NullPointException
  10. document = PDDocument.load(pdfFile);
  11. pdfRenderer = new PDFRenderer(document);
  12. image = pdfRenderer.renderImageWithDPI(0, dpi); // render the first page of the PDF document
  13. ImageIO.write(image, "TIFF", new File(outputTiff)); // write the image to a TIFF file
  14. result = true;
  15. document.close();
  16. } catch (IOException e) {
  17. logger.error(e.getStackTrace());
  18. result = false;
  19. } catch (InterruptedException e) {
  20. logger.error(e.getStackTrace());
  21. } finally {
  22. }
  23. return result;
  24. }
  25.  
  26. private String tiffOcr(String outputTiff) {
  27. String tiffPath = outputTiff;
  28. Tesseract tesseract = new Tesseract();
  29. String result = "";
  30. try {
  31. tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\"); // set the path to the Tesseract data directory
  32. tesseract.setLanguage("chi_tra");
  33. result = tesseract.doOCR(new File(tiffPath));
  34. } catch (TesseractException e) {
  35. logger.error(e.getStackTrace());
  36. } finally {
  37. File imageFile = new File(outputTiff);
  38. if(imageFile.exists()) imageFile.delete();
  39. }
  40. return result;
  41. }

4. import的套件很多,可以參考下面圖片的套件版本。


5. 在Eclipse上運行正常,但匯出成可運行的JAR後,辨識時會出現以下錯誤。

javax.imageio.spi.ImageOutputStreamSpi: Provider com.sun.media.imageioimpl.stream.ChannelImageOutputStreamSpi could not be instantiated

解決方式:Eclipse匯出方式需要變更為【copy required libraries into sub-folder....】。


可運行的JAR在執行前,要先把sub-folder的目錄跟可運行的JAR放在同一層執行,這樣才能順利運行。

這個問題花了我5個小時才解決掉,希望對大家有助。




Related Posts:

  • Java判斷作業系統與檔案路徑如何在java程式中判斷幕前作業系統是何種作業系統 以下位範例程式: public class OSValidator { public static void main(String[] args) { if (isWindows()) { System.out.println("This is Windows"); } else if (isMac()) { … Read More
  • Java Connection.setAutoCommit使用注意事項setAutoCommit總的來說就是保持資料的完整性,一個系統的更新操作可能要涉及多張表,需多個SQL語句進行操作 迴圈裡連續的進行插入操作,如果你在開始時設置了:conn.setAutoCommit(false); 最後才進行conn.commit(),這樣你即使插入的時候報錯,修改的內容也不會提交到資料庫, 而如果你沒有手動的進行setAutoCommit(false); 出錯時就會造成,前幾條插入,後幾條沒有 會形成髒資料~~ … Read More
  • Microsoft JDBC for MS SQL Server 系統需求 JDBC for SQL Server 2000 JAR 描 述 msbase.jar mssqlserver.jar msutil.jar jar 類別庫會提供 MSSQL2000 JDBC DRIVER 的支援。 JDBC Driver支援 JRE 1.4,但連到SQL 2005 或 2008會出現異常。 JDBC 2.0 for SQL Server 2005 … Read More
  • Microsoft JDBC 連接 URL 範例 在下列範例中,範例程式碼會在連接 URL 中設定各種連接屬性,然後呼叫 DriverManager 類別的 getConnection 方法,以傳回SQLServerConnection 物件。 接著,範例程式碼會使用 SQLServerConnection 物件的 createStatement 方法建立 SQLServerStatemen… Read More
  • Java String 依照單字出現次數/頻率高的依序印出功能: 依照每個單字出現的次數,由大到小,排列印出。 假設: String oriString = "This is a book. That is a pencil" 輸出: is 出現 2 次 a 出現 2 次 This 出現 1 次 That 出現 1 次 book 出現 1 次 pencil 出現 1 次 程式碼: import java.util.regex.*; import java.util.*; public class… Read More

0 意見: