文章目录
- 前言
- 编辑代码
- 打包项目构造镜像
- 发布镜像到dockerhub
- 发布镜像到阿里云
- 使用这个镜像
前言
有时候,我们需要识别图片中为内容。而java识别图片需要基于特定的环境。
代码已发布到Gitee:https://gitee.com/lengcz/springboot-ocr
编辑代码
-
新建springboot模块(选择web组件)
-
添加依赖
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.5.3</version>
</dependency>
- 编写ocrController
package com.lengcz.springbootocr.controller;
import com.lengcz.springbootocr.config.OcrProperties;
import com.lengcz.springbootocr.utils.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
@RestController
public class OcrController {
/**
* 第三种对象注入
*/
@Autowired
private OcrProperties ocrProperties;
@GetMapping("/hello")
public String hello() {
return "hello ocr";
}
@ApiOperation(value = "图片识别", notes = "图片识别")
@PostMapping(value = "/ocr", headers = "content-type=multipart/form-data")
@ResponseBody
public Object ocr(@ApiParam("文件") @RequestParam(value = "images") MultipartFile images,
@ApiParam(name = "language",
value = "语言包(默认:chi_sim 简体中文),支持数字中文简体(chi_sim),中文简体竖版(chi_sim_vert)," +
"繁体((chi_tra)),繁体((chi_tra_vert)),英文(eng)")
@RequestParam(name = "language", defaultValue = "chi_sim") String language) throws Exception {
if (null == images) {
return ResponseUtil.badArgument();
}
String fileName = images.getOriginalFilename(); // 文件名称
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 图片后缀
// 判断文件后缀是否为后端默认的后缀名
if (isImageFile(suffixName)) {
ITesseract instance = new Tesseract();
//设置训练库的位置
instance.setDatapath(ocrProperties.getTessdata());
// instance.setLanguage("chi_sim");
instance.setLanguage(language);
BufferedImage testImage = ImageIO.read(images.getInputStream());
String code = instance.doOCR(testImage);
return ResponseUtil.ok(code);
}
return ResponseUtil.unsupportedFormat();
}
// 判断后缀
private Boolean isImageFile(String fileName) {
// String[] img_type = new String[]{".jpg", ".jpeg", ".png", ".bmp"};
if (fileName == null) {
return false;
}
fileName = fileName.toLowerCase();
String[] img_type = ocrProperties.getFile_suffix();
for (String type : img_type) {
if (fileName.endsWith(type)) {
return true;
}
}
return false;
}
}
OcrProperties.java
package com.lengcz.springbootocr.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@ConfigurationProperties(prefix = "ocr")
public class OcrProperties {
private String tessdata;
private String[] file_suffix;
public String getTessdata() {
return tessdata;
}
public void setTessdata(String tessdata) {
this.tessdata = tessdata;
}
public String[] getFile_suffix() {
return file_suffix;
}
public void setFile_suffix(String[] file_suffix) {
this.file_suffix = file_suffix;
}
@Override
public String toString() {
return "OcrProperties{" +
"tessdata='" + tessdata + '\'' +
", file_suffix=" + Arrays.toString(file_suffix) +
'}';
}
}
- 配置文件application.yml
servlet:
multipart:
max-file-size: 128MB #上传文件总大值
max-request-size: 20MB #单个文件的最大值
ocr:
tessdata: /usr/local/share/tessdata/ #语言包路径
file_suffix: #可识别的文件后缀
- .jpg
- .jpeg
- .png
- .bmp
- 编写测试index页面(注意存放位置)
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<head>
<title>example</title>
</head>
<body>
<hr>
<h1>OCR example</h1>
<form action="/ocr"
enctype="multipart/form-data" method="post">
file <input type="file" name="images">
<br><br>
language:<input type="text" value="chi_sim">
<br><br>
<input type="submit" value="submit">
</form>
<hr>
This application is based on tesseract ocr,leptonica.<br>
swagger api:<a href="swagger-ui.html"> OCR API </a>
</body>
</html>
- 编辑Dockerfile文件
如何构建ocr基础镜像,请见:docker应用篇(2):构建tesseract-ocr运行环境
#构建好的用于ocr识别的镜像
#使用dockerhub上的ocr环境镜像(国内较慢)
#FROM lengcz/tesseract-ocr-environment:1.0
#使用阿里云上的ocr容器镜像(国内快)
FROM registry.cn-guangzhou.aliyuncs.com/lengcz/tesseract-ocr-environment:1.0
EXPOSE 8080
ADD target/*.jar /app.jar
ENTRYPOINT ["java","-Dfile.encoding=utf-8","-jar", "app.jar"]
打包项目构造镜像
由于ocr识别依赖于环境,所以如果在本地电脑运行,需要安装
tesseract和leptonica,这里不在本地运行,直接使用已经构建好的ocr环境镜像,不需要一堆的复制操作。
-
打包文件并上传,如图
-
构造镜像
docker build -t springboot-ocr .
- 运行镜像
#启动镜像(使用默认语言包:中文和英文)
docker run -d -p 8080:8080 --name myocr-application springboot-ocr
#启动镜像(需要挂载语言包,其它语言)
docker run -d -v /usr/tessdata:/usr/local/share/tessdata -p 8080:8080 --name myocr-application springboot-ocr
- 测试 http://106.13.2.249:8080/,选择一张图片上传。
测试用图片
选择一张英文中午和数字的图片进行识别。
大功告成,识别可用。
发布镜像到dockerhub
开放仓库:https://hub.docker.com/repository/docker/lengcz/tesseract-ocr-web
#标记镜像
docker tag 4c646d940edd lengcz/tesseract-ocr-web:1.0
#推送镜像到仓库
docker push lengcz/tesseract-ocr-web:1.0
发布镜像到阿里云
该镜像为开放镜像,可使用。下载速度很快。
发布操纵省略。
docker run -d -p 8080:8080 --name mytesseractweb01 registry.cn-guangzhou.aliyuncs.com/lengcz/tesseract-ocr-web:1.0
镜像地址:
https://hub.docker.com/repository/docker/lengcz/tesseract-ocr-web
使用这个镜像
前面发布了这个镜像,这里可以直接使用这个镜像,一次开发,随处运行。
- dockerhub镜像
#后台运行
docker run -d -p 8080:8080 --name mytesseract01 lengcz/tesseract-ocr-web:1.0
#(需要额外的语言包)以挂载的方式后台运行
docker run -d -p 8080:8080 -v /usr/tessdata:/usr/local/share/tessdata --name mytesseract01 lengcz/tesseract-ocr-web:1.0
- 阿里云容器镜像
#后台运行
docker run -d -p 8080:8080 --name mytesseractweb01 registry.cn-guangzhou.aliyuncs.com/lengcz/tesseract-ocr-web:1.0
#(需要额外的语言包)以挂载的方式后台运行
docker run -d -p 8080:8080 -v /usr/tessdata:/usr/local/share/tessdata --name mytesseractweb01 registry.cn-guangzhou.aliyuncs.com/lengcz/tesseract-ocr-web:1.0