基于 Spring Boot3.3 + OCR 實現(xiàn)圖片轉(zhuǎn)文字功能,你學(xué)會了嗎?
在當(dāng)今數(shù)字化信息時代,圖像中的文字信息越來越重要,無論是文檔掃描、名片識別,還是車輛牌照的識別,OCR(Optical Character Recognition,光學(xué)字符識別)技術(shù)都在各個領(lǐng)域發(fā)揮著關(guān)鍵作用。本文將介紹如何基于 Spring Boot 框架集成 EasyOCR,實現(xiàn)圖片轉(zhuǎn)文字的功能。我們將通過實際的代碼示例,詳細(xì)講解從前端上傳圖片、后端處理到最終文字識別的全過程。
運行效果:
圖片
識別效果:
圖片
若想獲取項目完整代碼以及其他文章的項目源碼,且在代碼編寫時遇到問題需要咨詢交流,歡迎加入下方的知識星球。
什么是 OCR?
OCR 是一種將圖片中的印刷體或手寫文本轉(zhuǎn)換為可編輯文本的技術(shù)。它廣泛應(yīng)用于文檔管理系統(tǒng)、車牌識別、票據(jù)處理、身份證識別等領(lǐng)域。傳統(tǒng)的 OCR 解決方案通?;趶?fù)雜的機(jī)器學(xué)習(xí)算法,需進(jìn)行大量的數(shù)據(jù)訓(xùn)練。然而,隨著深度學(xué)習(xí)的快速發(fā)展,出現(xiàn)了一些更加靈活且易于使用的 OCR 框架,其中 EasyOCR 就是一個突出的代表。
EasyOCR 框架簡介
EasyOCR 簡介
EasyOCR 是一個由 Jaided AI 開發(fā)的開源 OCR 解決方案。它基于 PyTorch 深度學(xué)習(xí)框架,具有開箱即用、易于集成、支持多語言等特點。與傳統(tǒng)的 OCR 工具相比,EasyOCR 不僅識別速度快,還能處理各種復(fù)雜的文本圖像,如彎曲的文本、不同字體、各種語言混合的文本等。
EasyOCR 的特性
- 多語言支持:EasyOCR 支持 80 多種語言,包括中英雙語、日語、韓語、阿拉伯語等,特別適合需要處理多語言文本的場景。
- 開源免費:EasyOCR 完全開源,并且在 GitHub 上持續(xù)維護(hù)和更新,開發(fā)者可以免費使用并進(jìn)行二次開發(fā)。
- 易于集成:只需簡單幾行代碼,即可將 EasyOCR 集成到現(xiàn)有項目中。其 API 設(shè)計簡單明了,非常適合快速開發(fā)和部署。
- 高準(zhǔn)確率:基于深度學(xué)習(xí)的模型,EasyOCR 在復(fù)雜場景下的文本識別準(zhǔn)確率較高,能夠應(yīng)對彎曲文本、復(fù)雜背景等難題。
- 輕量級:與其他基于深度學(xué)習(xí)的 OCR 解決方案相比,EasyOCR 更加輕量級,占用資源少,適合嵌入式設(shè)備和服務(wù)器應(yīng)用。
環(huán)境準(zhǔn)備
- Python 環(huán)境:EasyOCR 是基于 Python 的,因此需要在系統(tǒng)中安裝 Python。
- EasyOCR 安裝:使用 pip 安裝 EasyOCR。
pip install easyocr
Spring Boot 項目:我們將創(chuàng)建一個 Spring Boot 項目,并通過 HTTP 請求將圖片傳遞給 Python 腳本進(jìn)行 OCR 處理。
項目結(jié)構(gòu)
easyocr
|-- src
| |-- main
| |-- java
| |-- com
| |-- icoderoad
| |-- easyocr
| |-- EasyOcrApplication.java
| |-- controller
| |-- OcrController.java
|-- resources
| |-- application.yml
|-- pom.xml
|-- ocr_script.py
Python OCR 腳本
首先,我們編寫一個 Python 腳本 ocr_script.py,用于接收圖像文件并使用 EasyOCR 進(jìn)行文字識別。
import easyocr
import sys
def extract_text_from_image(image_path):
# 初始化 EasyOCR Reader,支持中文和英文
reader = easyocr.Reader(['ch_sim', 'en']) # 'ch_sim' 用于簡體中文,'ch_tra' 用于繁體中文
results = reader.readtext(image_path)
text = ""
for result in results:
text += result[1] + "\n"
return text
if __name__ == "__main__":
image_path = sys.argv[1] # 從命令行參數(shù)獲取圖片路徑
text = extract_text_from_image(image_path)
print(text) # 輸出識別結(jié)果
Spring Boot 配置
pom.xml 配置
添加 spring-boot-starter-web 和 commons-io 依賴,用于創(chuàng)建 REST API 和處理文件操作。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
application.yml 配置
配置文件上傳的臨時存儲路徑。
server:
port: 8080
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
ocr:
python-path: /path/python/bin/python
script-path: /path/to/ocr_script.py
upload-dir: /tmp/uploads/
EasyOcrApplication.java
Spring Boot 啟動類。
package com.icoderoad.easyocr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EasyocrApplication {
public static void main(String[] args) {
SpringApplication.run(EasyocrApplication.class, args);
}
}
創(chuàng)建配置類
使用 @ConfigurationProperties 注解創(chuàng)建一個配置類,以便將 YAML 文件中的配置注入到 Spring Boot 應(yīng)用中。
OcrProperties.java
package com.icoderoad.easyocr.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "ocr")
public class OcrProperties {
private String pythonPath;
private String scriptPath;
private String uploadDir;
public String getPythonPath() {
return pythonPath;
}
public void setPythonPath(String pythonPath) {
this.pythonPath = pythonPath;
}
public String getScriptPath() {
return scriptPath;
}
public void setScriptPath(String scriptPath) {
this.scriptPath = scriptPath;
}
public String getUploadDir() {
return uploadDir;
}
public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
}
OcrController.java
控制器用于處理文件上傳和調(diào)用 Python 腳本。
package com.icoderoad.easyocr.controller;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.icoderoad.easyocr.config.OcrProperties;
@RestController
@RequestMapping("/api/ocr")
public class OcrController {
@Autowired
private OcrProperties ocrProperties;
@PostMapping("/extract")
public String extractText(@RequestParam("file") MultipartFile file) {
try {
// 保存上傳的文件
File tempFile = new File(ocrProperties.getUploadDir() + file.getOriginalFilename());
FileUtils.writeByteArrayToFile(tempFile, file.getBytes());
// 調(diào)用 Python 腳本
ProcessBuilder processBuilder = new ProcessBuilder(ocrProperties.getPythonPath(), ocrProperties.getScriptPath(), tempFile.getAbsolutePath());
Process process = processBuilder.start();
process.waitFor();
// 讀取輸出
String output = new String(process.getInputStream().readAllBytes());
return output;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return "OCR 識別失敗";
}
}
}
前端示例
使用 Thymeleaf 模板、Bootstrap 和 JavaScript 創(chuàng)建一個簡單的前端頁面,允許用戶上傳圖片并查看 OCR 結(jié)果。
src/main/resources/templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OCR 圖片識別</title>
<link rel="stylesheet" >
</head>
<body>
<div class="container">
<h1 class="mt-5">OCR 圖片識別</h1>
<form id="uploadForm">
<div class="form-group">
<label for="fileInput">選擇圖片文件:</label>
<input type="file" class="form-control" id="fileInput" name="file" required>
</div>
<button type="submit" class="btn btn-primary">上傳并識別</button>
</form>
<div class="mt-3">
<h2>識別結(jié)果:</h2>
<pre id="result">上傳圖片以查看識別結(jié)果</pre>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="/js/app.js"></script>
</body>
</html>
src/main/resources/static/js/app.js
$(document).ready(function() {
$('#uploadForm').on('submit', function(event) {
event.preventDefault();
// 獲取文件輸入
var fileInput = $('#fileInput')[0].files[0];
// 檢查是否選擇了文件
if (!fileInput) {
alert("請選擇一個文件");
return;
}
// 創(chuàng)建 FormData 對象
var formData = new FormData();
formData.append('file', fileInput);
// 使用 jQuery 的 AJAX 發(fā)送 POST 請求
$.ajax({
url: '/api/ocr/extract',
type: 'POST',
data: formData,
contentType: false, // 不設(shè)置內(nèi)容類型,讓瀏覽器自動處理
processData: false, // 不處理數(shù)據(jù),讓它保持原樣
success: function(result) {
// 在頁面上顯示識別結(jié)果
$('#result').text(result);
},
error: function(xhr, status, error) {
console.error('Error:', error);
alert('識別失敗,請稍后重試。');
}
});
});
});
總結(jié)
在這篇文章中,我們展示了如何使用 EasyOCR 與 Spring Boot 集成實現(xiàn)圖片文字識別。通過 Python 腳本處理 OCR 任務(wù),并在 Spring Boot 應(yīng)用中處理文件上傳和調(diào)用 OCR 腳本,最終將識別結(jié)果返回給前端頁面。這種方法結(jié)合了 EasyOCR 強(qiáng)大的文字識別能力與 Spring Boot 靈活的 Web 開發(fā)特性,為大家提供了一個完整的解決方案。