tesseract下載鏈接:
github:https://github.com/tesseract-ocr/?
db:https://digi.bib.uni-mannheim.de/tesseract/
文字識別技術在許多領域都有廣泛的應用,例如文檔處理、自動化辦公、移動設備上的文本輸入等。而Tesseract-OCR作為一款開源的OCR引擎,以其高效、準確的文字識別能力,受到了廣泛的關注和應用。本文將詳細介紹Tesseract-OCR的原理、優勢、使用方法以及應用案例,幫助讀者更好地理解和使用這款工具。
一、Tesseract-OCR簡介
Tesseract-OCR是由HP實驗室開發,后由Google維護的一款開源OCR引擎。OCR是Optical Character Recognition的縮寫,意為光學字符識別,是一種通過計算機軟件識別印刷或手寫文本的技術。Tesseract-OCR采用深度學習的方法進行文字識別,可以識別多種語言,包括英文、中文、德文、法文等。
二、Tesseract-OCR的優勢
- 準確性高:Tesseract-OCR的準確性在同類產品中處于領先地位,對于印刷體文本的識別率高達95%以上。
- 支持多種語言:Tesseract-OCR支持多種語言的識別,包括英文、中文、德文、法文等,并可以通過訓練來擴展識別其他語言。
- 靈活的API接口:Tesseract-OCR提供了靈活的API接口,可以輕松集成到各種應用中,方便開發者進行二次開發。
- 跨平臺性:Tesseract-OCR可以在多種操作系統上運行,如Windows、Linux和Mac OS等。
三、Tesseract-OCR的使用方法 - 安裝:首先需要下載并安裝Tesseract-OCR軟件。可以從官網下載最新版本的安裝包進行安裝。對于不同的操作系統,需要選擇相應的安裝包進行下載和安裝。
- 訓練數據:為了提高識別的準確性,需要對特定的字體或文本進行訓練。訓練數據可以是自己的數據集,也可以使用公開的數據集進行訓練。訓練完成后,保存為.traineddata文件供Tesseract-OCR使用。
- API接口:Tesseract-OCR提供了多種語言的API接口,包括C++、Java、Python等。開發者可以根據自己的需求選擇相應的接口進行集成和使用。使用API接口可以方便地進行文本識別的各種操作,例如識別圖片中的文本、進行文本轉換等。
四、Tesseract-OCR的應用案例 - PDF文字識別:將PDF文件中的文字識別出來,方便用戶進行編輯和使用。
- 圖形驗證碼識別:將圖形驗證碼中的文字識別出來,用于登錄驗證等場景。
- 移動設備上的文本輸入:通過拍照或掃描文檔,將圖片中的文字識別出來,方便用戶進行文本輸入。
- 自動化辦公:將紙質文檔或圖片中的文字識別出來,進行后續的處理和分析。
三、(源碼)Tesseract-OCR在Java中的應用實例
1、maven配置
<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j --><dependency><groupId>net.sourceforge.tess4j</groupId><artifactId>tess4j</artifactId><version>4.5.5</version></dependency>
2、源碼
/*** 提取圖片文字接口 V1.1*/
@RestController
@RequestMapping(value="/ts/api/extract")
public class TpExtractionApiController {// Windows圖片地址public final static String globalVariableImageUrl = "D:\\tesseract";// Windows tesseract 訓練地址public final static String globalVariableTesseractUrl = "D:\\tesseract";/*** main*/@RequestMapping(value="/image", method=RequestMethod.POST) public TpExtraction reg(@ApiParam(value = "文件") @RequestPart(value = "multipartFile") MultipartFile multipartFile) {//TpExtraction tpExtraction = new TpExtraction();//String filename = multipartFile.getOriginalFilename();//創建tess對象ITesseract tesseract = new Tesseract();//windows 設置訓練文件目錄tesseract.setDatapath(globalVariableUrl+"\\tessdata");//設置訓練語言tesseract.setLanguage("chi_sim");//String result = "";//try {File file = null;try {// MultipartFile 轉 Filefile = convert(multipartFile,filename);// 判斷是否為PNG類型boolean bl_png = isImagePng(file);// true/falseif(bl_png) {// true 不處理}else {// 判斷文件格式boolean bl = isImage(file);// if(bl) {// 轉換為pngfile = convertToPng(file);}else {tpExtraction.setMsg("文件格式異常,請上傳.png圖片格式。");return tpExtraction;}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}result = tesseract.doOCR(file);tpExtraction.setMsg(result);} catch (TesseractException e) {// TODO Auto-generated catch blocke.printStackTrace();}return tpExtraction;}/*** MultipartFile 轉 File**/public static File convert(MultipartFile file,String filename) throws IOException {// windows 圖片路徑File convFile = new File(globalVariableUrl+"\\"+filename);// 創建文件convFile.createNewFile();try (InputStream in = file.getInputStream();OutputStream out = new FileOutputStream(convFile)) {byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = in.read(buffer)) != -1) {out.write(buffer, 0, bytesRead);}}return convFile;}/*** 判斷文件類型是否為圖片類型*/public static boolean isImage(File file) {String name = file.getName().toLowerCase();return name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png") ||name.endsWith(".gif") || name.endsWith(".bmp") || name.endsWith(".wbmp");}/*** 判斷文件類型是否為PNG圖片類型*/public static boolean isImagePng(File file) {String name = file.getName().toLowerCase();return name.endsWith(".png");}/*** 將圖片文件轉換為PNG格式*/public static File convertToPng(File file) {//File writeFile = null;//try {BufferedImage image = ImageIO.read(file);String name = file.getName().toLowerCase();String format = "png";if (name.endsWith(".jpg") || name.endsWith(".jpeg")) {format = "png";} else if (name.endsWith(".gif")) {format = "gif";} else if (name.endsWith(".bmp") || name.endsWith(".wbmp")) {format = "bmp";}writeFile = new File(file.getParent(), name.substring(0, name.lastIndexOf('.')) + ".png");ImageIO.write(image, format, writeFile);} catch (IOException e) {e.printStackTrace();}return writeFile;}}
3、應用效果
四、 總結
如果想要提高tesseract識別率對圖片分塊是一個非常好的方法,識別率提高巨大。