|
|
|
@@ -0,0 +1,216 @@ |
|
|
|
package com.iformall.ocr; |
|
|
|
|
|
|
|
import java.io.BufferedWriter; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileWriter; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.LineNumberReader; |
|
|
|
|
|
|
|
/** |
|
|
|
* 训练 |
|
|
|
* @author iformall |
|
|
|
*/ |
|
|
|
public class FormallTess4jTrain { |
|
|
|
|
|
|
|
private static final String newFileFolder = "C:\\Users\\iformall\\Desktop\\newfile"; |
|
|
|
|
|
|
|
/** |
|
|
|
* 第一步,用jTessBoxEditor --> Tools --> Merge 多选图片,保存为tif格式 |
|
|
|
* 命名: [lang].[fontname].exp[num].tif lang是语言 fontname是字体 |
|
|
|
*/ |
|
|
|
|
|
|
|
/** |
|
|
|
* 第二部,用tif生成box |
|
|
|
* @param args |
|
|
|
*/ |
|
|
|
public static void createBoxFile(String tifFileName,String lang,String fontName) { |
|
|
|
String prefix = getprefix(lang,fontName); |
|
|
|
String realFileName = prefix+".tif"; |
|
|
|
if (tifFileName.equals(realFileName)) { |
|
|
|
String cmdstr = "tesseract "+tifFileName+" "+prefix+" -l chi_sim batch.nochop makebox"; |
|
|
|
System.out.println(cmdstr); |
|
|
|
//String result = executeLocalCmd(cmdstr, null); |
|
|
|
|
|
|
|
//System.out.println("Done createBoxFile"+result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 第三步,用jTessBoxEditor 打开.box文件进行矫正。 |
|
|
|
*/ |
|
|
|
|
|
|
|
/** |
|
|
|
* 第四步,新建font_properties,内容 [fontName] 0 0 0 0 0 |
|
|
|
* <italic> 、<bold> 、<fixed> 、<serif>、 <fraktur>的取值为1或0,表示字体是否具有这些属性 |
|
|
|
*/ |
|
|
|
public static void createFontPorperties(String fontName,int italic, int bold, int fixed,int serif,int fraktur) { |
|
|
|
try { |
|
|
|
|
|
|
|
String content = fontName+" "+italic+" "+bold+" "+fixed+" "+serif+" "+fraktur; |
|
|
|
System.out.println(content); |
|
|
|
File file = new File(newFileFolder+"\\font_properties"); |
|
|
|
if (file.exists()) { |
|
|
|
file.delete(); |
|
|
|
} |
|
|
|
file.createNewFile(); |
|
|
|
|
|
|
|
FileWriter fw = new FileWriter(file.getAbsoluteFile()); |
|
|
|
BufferedWriter bw = new BufferedWriter(fw); |
|
|
|
bw.write(content); |
|
|
|
bw.close(); |
|
|
|
System.out.println("Done createFontPorperties"); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*** |
|
|
|
* 第五步:执行以下命令 |
|
|
|
* @param lang |
|
|
|
* @param fontName |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static void createTrainFiles(String lang,String fontName) { |
|
|
|
String prefix = getprefix(lang,fontName); |
|
|
|
String realFileName = prefix+".tif"; |
|
|
|
|
|
|
|
String trainCmdstr = "tesseract "+realFileName+" "+prefix+" nobatch box.train"; |
|
|
|
System.out.println(trainCmdstr); |
|
|
|
//executeLocalCmd(trainCmdstr, null); |
|
|
|
|
|
|
|
String extractorCmdstr = "unicharset_extractor "+prefix+".box"; |
|
|
|
System.out.println(extractorCmdstr); |
|
|
|
//executeLocalCmd(extractorCmdstr, null); |
|
|
|
|
|
|
|
String shapeCmdStr = "shapeclustering -F font_properties -U unicharset "+prefix+".tr"; |
|
|
|
System.out.println(shapeCmdStr); |
|
|
|
//executeLocalCmd(shapeCmdStr, null); |
|
|
|
|
|
|
|
String mftraingCmdstr = "mftraining -F font_properties -U unicharset -O unicharset "+prefix+".tr"; |
|
|
|
System.out.println(mftraingCmdstr); |
|
|
|
//executeLocalCmd(mftraingCmdstr, null); |
|
|
|
|
|
|
|
String cntrainCmdstr = "cntraining "+prefix+".tr"; |
|
|
|
System.out.println(cntrainCmdstr); |
|
|
|
//executeLocalCmd(cntrainCmdstr, null); |
|
|
|
|
|
|
|
//System.out.println("DONE createTrainFiles !"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 第六步,执行完成后生成以下几个文件加前缀 |
|
|
|
* unicharset、inttemp、pffmtable、shapetable、normproto 添加这几个文件的前缀为fontName |
|
|
|
* @param lang |
|
|
|
* @param fontName |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static void renameTrainFiles(String fontName) { |
|
|
|
String renCmdstr = "REN unicharset "+fontName+".unicharset"; |
|
|
|
System.out.println(renCmdstr); |
|
|
|
//executeLocalCmd(renCmdstr, null); |
|
|
|
|
|
|
|
renCmdstr = "REN inttemp "+fontName+".inttemp"; |
|
|
|
System.out.println(renCmdstr); |
|
|
|
//executeLocalCmd(renCmdstr, null); |
|
|
|
|
|
|
|
renCmdstr = "REN pffmtable "+fontName+".pffmtable"; |
|
|
|
System.out.println(renCmdstr); |
|
|
|
//executeLocalCmd(renCmdstr, null); |
|
|
|
|
|
|
|
renCmdstr = "REN shapetable "+fontName+".shapetable"; |
|
|
|
System.out.println(renCmdstr); |
|
|
|
//executeLocalCmd(renCmdstr, null); |
|
|
|
|
|
|
|
renCmdstr = "REN normproto "+fontName+".normproto"; |
|
|
|
System.out.println(renCmdstr); |
|
|
|
//executeLocalCmd(renCmdstr, null); |
|
|
|
|
|
|
|
//System.out.println("DONE renameTrainFiles !"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 第七步:合并5个文件 |
|
|
|
* @param lang |
|
|
|
* @param fontName |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static void mergeTrainFiles(String fontName) { |
|
|
|
String mergeCmdstr = "combine_tessdata "+fontName+"."; |
|
|
|
System.out.println(mergeCmdstr); |
|
|
|
//executeLocalCmd(mergeCmdstr, null); |
|
|
|
|
|
|
|
//System.out.println("DONE mergeTrainFiles !"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String getprefix(String lang,String fontName) { |
|
|
|
String realFileName = lang+"."+fontName+".exp0"; |
|
|
|
return realFileName; |
|
|
|
} |
|
|
|
|
|
|
|
private static String executeLocalCmd(String cmd, File workpath) { |
|
|
|
try { |
|
|
|
String[] cmdA = { "cmd.exe", "/c", cmd }; |
|
|
|
Process process = null; |
|
|
|
if(workpath==null){ |
|
|
|
process = Runtime.getRuntime().exec(cmdA); |
|
|
|
}else{ |
|
|
|
process = Runtime.getRuntime().exec(cmdA, null, workpath); |
|
|
|
} |
|
|
|
// LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream())); |
|
|
|
LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream(),"GBK")); |
|
|
|
StringBuffer sb = new StringBuffer(); |
|
|
|
String line; |
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
sb.append(line).append("\n"); |
|
|
|
} |
|
|
|
return sb.toString(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
// String cmdStr = "mspaint";// 打开画板 |
|
|
|
// //cmdStr = "ren C:/server/data/ccgavr/3.png C:/server/data/ccgavr/4.png";// 重命名,DOS中不可 |
|
|
|
// //cmdStr = "ren 3.png 4.png"; |
|
|
|
// cmdStr = "ipconfig"; |
|
|
|
//// executeCMD(cmdStr); |
|
|
|
// System.out.println(executeLocalCmd(cmdStr, null)); |
|
|
|
|
|
|
|
/**第一步,用jTessBoxEditor --> Tools --> Merge 多选图片,保存为tif格式*/ |
|
|
|
System.out.println("第一步,用jTessBoxEditor --> Tools --> Merge 多选图片,保存为tif格式 . 命名格式:lang.fontName.exp0.tif"); |
|
|
|
|
|
|
|
/**第二部,用tif生成box*/ |
|
|
|
System.out.println("第二步,用tif生成box"); |
|
|
|
createBoxFile("mylang.test1.exp0.tif", "mylang", "test1"); |
|
|
|
|
|
|
|
/**第三步,用jTessBoxEditor 打开.box文件进行矫正。*/ |
|
|
|
System.out.println("第三步,用jTessBoxEditor 打开.box文件进行矫"); |
|
|
|
|
|
|
|
/**第四步,新建font_properties,内容 [fontName] 0 0 0 0 0*/ |
|
|
|
System.out.println("第四步:新建font_properties,内容 [fontName] 0 0 0 0 0"); |
|
|
|
createFontPorperties("test1", 0, 0, 0, 0, 0); |
|
|
|
|
|
|
|
/** 第五步:执行以下命令*/ |
|
|
|
System.out.println("第五步:执行以下命令"); |
|
|
|
createTrainFiles("mylang", "test1"); |
|
|
|
|
|
|
|
/**第六步,执行完成后生成以下几个文件加前缀*/ |
|
|
|
System.out.println("第六步,执行完成后生成以下几个文件加前缀"); |
|
|
|
renameTrainFiles("test1"); |
|
|
|
|
|
|
|
/**第七步:合并5个文件*/ |
|
|
|
System.out.println("第七步:合并5个文件"); |
|
|
|
mergeTrainFiles("test1"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |