您的位置:首页 > 其它

如何提高ocr的识别度

2018-03-19 16:43 99 查看
我的开发环境
ubuntu16.04 idea
采用test4j
ocr 总结

今天orc 终于有点进步了
总结一下,开心

之前我尝试来一下在客户端事来识别图片,效果不是很好

于是我将我所需要的图片信息,截取下来将它更加优化(将png 转化未jpg),还有一个就是用最新的数据包
需要下载的最新数据包(一个的下载好,再去下载其他,否则有点慢) https://github.com/tesseract-ocr/tessdata
这样做识别更快,更加准确

还有一点就是test4j,对于不同图片语言用不同的语言格式,这样也可以提高识别的效果和速度

包含所有代码(没有优化)
包含的功能 图片切割,图片背景色调色,图片识别

/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: sfdg
* Author: if
* Date: 18-3-16 下午2:37
* Description: fg
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.example.demoorc.test;

import net.coobird.thumbnailator.Thumbnails;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* 〈一句话功能简述〉<br>
* 〈fg〉
*
* @author if
* @create 18-3-16
* @since 1.0.0
*/
public class CutImage {
/**
* 初始化test 值
*/
static int test = 0;
/**
* 用来计数的个数
*/
static int num_sum = 0;
/**
* num1 采用的数据  num2 正确个数
*/
static Map<Integer, Integer> nums = new ConcurrentHashMap<>();
/**
* 开始的文件
*/

/**
* 生成的文件num
*/
static String toPicnum;

static String toPicworld;

/**
* 源文件目录
*/
static String srcpath = "/home/if/ioi/you";
/**
* 生成的编码文件目录
*/
static String numpath = "/home/if/ioi/num";
/**
* 文字目录
*/
static String worldpath = "/home/if/ioi/world";

public static boolean judge(String str) {
boolean flag = !str.contains("…");
if (!flag)
num_sum++;
return false;
}

public static void cutImage() throws IOException {

File file = new File(srcpath);

File[] files = file.listFiles();

for (File tempFile : files) {

format(tempFile.toString());

toPicnum = numpath + "/" + tempFile.getName();
toPicworld = worldpath + "/" + tempFile.getName();

//名称
// Thumbnails.of(tempFile).sourceRegion(120, 40, 430, 40).size(430, 40).toFile(toPicnum);
// 编号
Thumbnails.of(tempFile).sourceRegion(145, 0, test, 45).size(455, 42).toFile(toPicworld);

// readImage(toPicnum);
readImage(toPicworld);

}

}

public static void format(String path) throws IOException {

Color upu = new Color(229, 229, 229);
File file = new File(path);
// 把图片读到
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
// 获得底片效果
// 获取像素
int[] rgbArr = new int[bufferedImage.getWidth() * bufferedImage.getHeight()];
// 参数依次是 从哪一个像素点开始获取 x y 获取多宽 多高 这个数组里面充满了像素点的值 从数组的哪一个角标开始 一行有多少
bufferedImage.getRGB(0, 0, bufferedImage.getWidth(),
bufferedImage.getHeight(), rgbArr, 0, bufferedImage.getWidth());

for (int i = 0; i < rgbArr.length; i++) {
Color color = new Color(rgbArr[i]);
// 获取颜色的各个部分的值
if (color.getRed() == 0) {
rgbArr[i] = upu.getRGB();
// rgbArr[i] = Color.WHITE.getRGB();
// } else if (color.getRGB() == 229) {
// rgbArr[i] = Color.WHITE.getRGB();
} else {
}
}
bufferedImage.setRGB(0, 0, bufferedImage.getWidth(),
bufferedImage.getHeight(), rgbArr, 0, bufferedImage.getWidth());

FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(new File(path));

ImageIO.write(bufferedImage, "png", fileImageOutputStream);
fileImageOutputStream.flush();
fileImageOutputStream.close();

}

public static void readImage(String path) {
/**
*训练数据
*/
String trainPath = "/home/if/桌面/tessdata";
File imageFile = new File(path);
ITesseract instance = new Tesseract();
instance.setDatapath(trainPath);
// 默认是英文(识别字母和数字),如果要识别中文(数字 + 中文),需要制定语言包
instance.setLanguage("chi_sim");
try {
String result = instance.doOCR(imageFile).trim();
System.out.println(path + " : " + result + "---" + judge(result));
} catch (TesseractException e) {
System.out.println(e.getMessage());
}
}

public static void main(String[] args) throws IOException, InterruptedException {
test = 300;
//////
long startTime = System.currentTimeMillis();
cutImage();
System.out.println((System.currentTimeMillis() - startTime) / 1000);

//
// for (int i = 300; i < 400; i = i + 5) {
// test = i;
//
//
// cutImage();
//
// nums.put(i, num_sum);
// num_sum = 0;
// }
//
//
// for (Map.Entry<Integer, Integer> me : nums.entrySet()) {
// System.out.println(me.getKey() + " --> " + me.getValue());
// }
//
//
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息