feat: 删除设置识别语言包路径接口

Description:  删除设置识别语言包接口,优化为内部根据系统语言设置

Log: 优化接口
Change-Id: I634d90297cd111a38139b2a710e1ea770faf1148
This commit is contained in:
wangcong 2021-06-15 13:22:21 +08:00 committed by liuminghang
parent d6b3771524
commit e73c254cb8
5 changed files with 148 additions and 94 deletions

Binary file not shown.

Binary file not shown.

View File

@ -62,7 +62,7 @@ dbus_service.files=./com.deepin.Ocr.service
#Tesseract-ocr识别语言包 #Tesseract-ocr识别语言包
tesslangs.path=/usr/share/deepin-ocr/tesslangs tesslangs.path=/usr/share/deepin-ocr/tesslangs
tesslangs.files=./assets/tesslangs/chi_sim.traineddata ./assets/tesslangs/chi_tra.traineddata ./assets/tesslangs/eng.traineddata tesslangs.files=./assets/tesslangs/chi_sim.traineddata ./assets/tesslangs/chi_tra.traineddata ./assets/tesslangs/eng.traineddata ./assets/tesslangs/chi_sim_vert.traineddata ./assets/tesslangs/chi_tra_vert.traineddata
INSTALLS += target dbus_service translations tesslangs INSTALLS += target dbus_service translations tesslangs

View File

@ -39,12 +39,29 @@
*/ */
#include "tessocrutils.h" #include "tessocrutils.h"
/**
* @brief
*/
const QString TESSDATA_PATH = "/usr/share/deepin-ocr/tesslangs";
/**
* @brief TessOcrUtils::m_tessOcrUtils
*/
TessOcrUtils *TessOcrUtils::m_tessOcrUtils = nullptr;
TessOcrUtils::TessOcrUtils() TessOcrUtils::TessOcrUtils()
{ {
//设置语言包路径
setLanguagesPath(TESSDATA_PATH);
//设置当前默认使用的识别语言系统语言+英语
m_sLangs = getLanguages();
}
TessOcrUtils::~TessOcrUtils()
{
} }
TessOcrUtils *TessOcrUtils::m_tessOcrUtils = nullptr;
TessOcrUtils *TessOcrUtils::instance() TessOcrUtils *TessOcrUtils::instance()
{ {
@ -54,11 +71,11 @@ TessOcrUtils *TessOcrUtils::instance()
return m_tessOcrUtils; return m_tessOcrUtils;
} }
//传入待识别图片的路径和想得到的返回结果类型,获取识别结果
RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath,const ResultType resultType) RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath,const ResultType resultType)
{ {
QString errorMessage = ""; QString errorMessage = "";
ErrorCode errorCode = ErrorCode::UKNOW; ErrorCode errorCode = ErrorCode::UNKNOWN;
RecognitionResult t_result; RecognitionResult t_result;
Pix *p_image; Pix *p_image;
@ -66,7 +83,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath,cons
{ {
//errorMesage = "不能传递空的图片路径!"; //errorMesage = "不能传递空的图片路径!";
errorMessage = "Can't pass an empty image path!"; errorMessage = "Can't pass an empty image path!";
errorCode = ErrorCode::OCRPNULL; errorCode = ErrorCode::OCR_P_NULL;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
@ -77,7 +94,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath,cons
{ {
//errorMesage = "图片不存在imagePath: " + imagePath; //errorMesage = "图片不存在imagePath: " + imagePath;
errorMessage = "Image does not exist! imagePath: " + imagePath; errorMessage = "Image does not exist! imagePath: " + imagePath;
errorCode = ErrorCode::OCRPNULL; errorCode = ErrorCode::OCR_P_NULL;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
@ -91,7 +108,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath,cons
} catch (const std::logic_error &e) { } catch (const std::logic_error &e) {
//errorMesage = "加载图片失败!" + QString(qExc.what()); //errorMesage = "加载图片失败!" + QString(qExc.what());
errorMessage = "Failed to load picture! " + QString(e.what()); errorMessage = "Failed to load picture! " + QString(e.what());
errorCode = ErrorCode::OCRLIF; errorCode = ErrorCode::OCR_LI_F;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
@ -99,22 +116,24 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath,cons
return getRecogitionResult(p_image,resultType); return getRecogitionResult(p_image,resultType);
} }
//传入待识别图片的路径,获取纯字符串的识别结果
RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath) RecognitionResult TessOcrUtils::getRecogitionResult(const QString imagePath)
{ {
return getRecogitionResult(imagePath,ResultType::ResultString); return getRecogitionResult(imagePath,ResultType::RESULT_STRING);
} }
//传入待识别图片和想得到的返回结果类型,获取识别结果
RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image, const ResultType resultType) RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image, const ResultType resultType)
{ {
QString errorMessage = ""; QString errorMessage = "";
ErrorCode errorCode = ErrorCode::UKNOW; ErrorCode errorCode = ErrorCode::UNKNOWN;
RecognitionResult t_result; RecognitionResult t_result;
if(image->isNull()) if(image->isNull())
{ {
//errorMesage = "不能传递空的图片路径!"; //errorMesage = "不能传递空的图片路径!";
errorMessage = "Can't pass an empty image!"; errorMessage = "Can't pass an empty image!";
errorCode = ErrorCode::OCRPNULL; errorCode = ErrorCode::OCR_P_NULL;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
@ -137,17 +156,51 @@ RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image, const ResultT
//p_image->colormap->nalloc; //p_image->colormap->nalloc;
//p_image->colormap->n; //p_image->colormap->n;
p_image->data = reinterpret_cast<l_uint32*>(image->bits()); p_image->data = reinterpret_cast<l_uint32*>(image->bits());
//获取识别结果 //获取识别结果
return getRecogitionResult(p_image,resultType); return getRecogitionResult(p_image,resultType);
} }
//传入待识别图片,获取纯字符串的识别结果
RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image) RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image)
{ {
return getRecogitionResult(image,ResultType::ResultString); return getRecogitionResult(image,ResultType::RESULT_STRING);
} }
//获取系统当前的语言
Languages TessOcrUtils::getSystemLang()
{
QLocale locale;
if(locale.language() == QLocale::Language::Chinese){
//查询当前国别代码
QLocale::Country t_countryId = locale.country();
if(t_countryId == QLocale::Country::China){
return Languages::CHI_SIM;
}else {
return Languages::CHI_TRA;
}
}else {
return Languages::ENG;
}
}
//获取识别需要的使用的语言包
QString TessOcrUtils::getLanguages()
{
QString t_langs = "";
//当前系统语言
Languages t_systemLang = getSystemLang();
if(t_systemLang == Languages::ENG)
{
t_langs=getLangStr(t_systemLang);
}else{
t_langs=getLangStr(t_systemLang)+"+"+getLangStr(Languages::ENG);
}
return t_langs;
}
//设置识别需要的使用的语言包的路径
bool TessOcrUtils::setLanguagesPath(const QString langsPath) bool TessOcrUtils::setLanguagesPath(const QString langsPath)
{ {
if(langsPath.isNull() || langsPath.isEmpty()) if(langsPath.isNull() || langsPath.isEmpty())
@ -158,24 +211,22 @@ bool TessOcrUtils::setLanguagesPath(const QString langsPath)
return true; return true;
} }
//获取识别结果
RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resultType) RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resultType)
{ {
QString errorMessage = ""; QString errorMessage = "";
ErrorCode errorCode = ErrorCode::UKNOW; ErrorCode errorCode = ErrorCode::UNKNOWN;
QString result = ""; QString result = "";
RecognitionResult t_result; RecognitionResult t_result;
if(!isExistsResultType(resultType)) if(!isExistsResultType(resultType))
{ {
//errorMesage = "结果类型不存在 resultType: " + QString::number(resultType); //errorMesage = "结果类型不存在 resultType: " + QString::number(resultType);
errorMessage = "The result type does not exist! resultType: " + QString::number(resultType); errorMessage = "The result type does not exist! resultType: " + QString::number(resultType);
errorCode = ErrorCode::OCRRTNULL; errorCode = ErrorCode::OCR_RT_NULL;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
tesseract::TessBaseAPI *t_Tesseract = new tesseract::TessBaseAPI(); tesseract::TessBaseAPI *t_Tesseract = new tesseract::TessBaseAPI();
try{ try{
//实例化tesseract //实例化tesseract
t_Tesseract = new tesseract::TessBaseAPI(); t_Tesseract = new tesseract::TessBaseAPI();
@ -184,45 +235,40 @@ RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resul
{ {
//errorMesage = "初始化 Tesseract 失败!" + QString::number(resultType); //errorMesage = "初始化 Tesseract 失败!" + QString::number(resultType);
errorMessage = "Could not initialize tesseract! Tesseract couldn't load any languages!"; errorMessage = "Could not initialize tesseract! Tesseract couldn't load any languages!";
errorCode = ErrorCode::OCRINIF; errorCode = ErrorCode::OCR_INI_F;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
}catch(const std::logic_error &e){ }catch(const std::logic_error &e){
//errorMesage = "初始化 Tesseract 失败Tesseract 内部错误!" + QString(qExc.what()); //errorMesage = "初始化 Tesseract 失败Tesseract 内部错误!" + QString(qExc.what());
errorMessage = "Could not initialize tesseract! Tesseract internal error! " + QString(e.what()); errorMessage = "Could not initialize tesseract! Tesseract internal error! " + QString(e.what());
errorCode = ErrorCode::OCRINIF; errorCode = ErrorCode::OCR_INI_F;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
//为Tesseract设置待识别图片 //为Tesseract设置待识别图片
t_Tesseract->SetImage(image); t_Tesseract->SetImage(image);
try { try {
//识别图片 //识别图片
switch (resultType) { switch (resultType) {
case ResultType::ResultXML: case ResultType::RESULT_HTML:
//result = QString(t_Tesseract->GetAltoText(0));
break;
case ResultType::ResultHTML:
result = QString(t_Tesseract->GetHOCRText(0)); result = QString(t_Tesseract->GetHOCRText(0));
break; break;
case ResultType::ResultString: case ResultType::RESULT_STRING:
result = QString(t_Tesseract->GetUTF8Text()); result = QString(t_Tesseract->GetUTF8Text());
break; break;
} }
} catch (const std::logic_error &e) { } catch (const std::logic_error &e) {
//errorMesage = "识别图片失败!" + QString(qExc.what()); //errorMesage = "识别图片失败!" + QString(qExc.what());
errorMessage = "Image recognition failed! " + QString(e.what()); errorMessage = "Image recognition failed! " + QString(e.what());
errorCode = ErrorCode::OCRLIF; errorCode = ErrorCode::OCR_LI_F;
setResult(errorCode,errorMessage,resultType,t_result); setResult(errorCode,errorMessage,resultType,t_result);
return t_result; return t_result;
} }
t_Tesseract->End(); t_Tesseract->End();
delete t_Tesseract; delete t_Tesseract;
pixDestroy(&image); pixDestroy(&image);
t_result.flag = true; t_result.flag = true;
t_result.message = errorMessage; t_result.message = errorMessage;
t_result.errorCode = ErrorCode::OK; t_result.errorCode = ErrorCode::OK;
@ -231,9 +277,9 @@ RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resul
return t_result; return t_result;
} }
//设置返回结果,内部使用
void TessOcrUtils::setResult(ErrorCode errCode, const QString errMessage,const ResultType resultType,RecognitionResult &result) void TessOcrUtils::setResult(ErrorCode errCode, const QString errMessage,const ResultType resultType,RecognitionResult &result)
{ {
result.flag = false; result.flag = false;
result.message = errMessage; result.message = errMessage;
result.errorCode = errCode; result.errorCode = errCode;
@ -241,34 +287,32 @@ void TessOcrUtils::setResult(ErrorCode errCode, const QString errMessage,const R
result.result = ""; result.result = "";
} }
bool TessOcrUtils::setLanguages(const QList<Languages> langs) //设置当前使用的语言包
{ //bool TessOcrUtils::setLanguages(const QList<Languages> langs)
QString t_langs = ""; //{
if(langs.isEmpty()) // QString t_langs = "";
{ // if(langs.isEmpty())
return false; // {
} // return false;
// }
int i; // int i;
for ( i = 0;i<langs.length();i++) { // for ( i = 0;i<langs.length();i++) {
Languages lang = langs[i]; // Languages lang = langs[i];
if(!isExistsLanguage(lang)) // if(!isExistsLanguage(lang))
{ // {
return false; // return false;
} // }
if((langs.length()-1) == i){ // if((langs.length()-1) == i){
t_langs+=getLangStr(lang); // t_langs+=getLangStr(lang);
// }else{
}else{ // t_langs+=getLangStr(lang) + "+";
t_langs+=getLangStr(lang) + "+"; // }
} // }
} // m_sLangs = t_langs;
// return true;
m_sLangs = t_langs; //}
return true;
}
//根据传入的枚举,获取相应的语言包字符串
QString TessOcrUtils::getLangStr(Languages lang) QString TessOcrUtils::getLangStr(Languages lang)
{ {
QString langStr = ""; QString langStr = "";
@ -287,24 +331,23 @@ QString TessOcrUtils::getLangStr(Languages lang)
return langStr; return langStr;
} }
//判断指定的结果类型是否存在
bool TessOcrUtils::isExistsResultType(ResultType resultType) bool TessOcrUtils::isExistsResultType(ResultType resultType)
{ {
bool flag = false; bool flag = false;
switch (resultType) switch (resultType)
{ {
case ResultType::ResultString: case ResultType::RESULT_STRING:
flag = true; flag = true;
break; break;
case ResultType::ResultHTML: case ResultType::RESULT_HTML:
flag = true;
break;
case ResultType::ResultXML:
flag = true; flag = true;
break; break;
} }
return flag; return flag;
} }
//判断指定的语言包类型是否存在
bool TessOcrUtils::isExistsLanguage(Languages lang) bool TessOcrUtils::isExistsLanguage(Languages lang)
{ {
bool flag = false; bool flag = false;

View File

@ -57,28 +57,29 @@
* @brief * @brief
*/ */
enum ResultType{ enum ResultType{
ResultString = 1, //纯字符串结果 RESULT_STRING = 1, //纯字符串结果
ResultHTML = 2, //HTML文本 RESULT_HTML = 2, //HTML文本
ResultXML = 3 //XML文本 UNKNOWN_TYPE = -1 //XML文本
}; };
/** /**
* @brief * @brief
*/ */
enum ErrorCode{ enum ErrorCode{
OK = 1, //成功 OK = 1, //成功
UKNOW = -1, //未知错误 UNKNOWN = -1, //未知错误
OCRPNULL = 101, //文件路径为空 OCR_P_NULL = 101, //文件路径为空
OCRRTNULL = 102, //结果字符串类型不存在 OCR_RT_NULL = 102, //结果字符串类型不存在
OCRINIF = 103, //ocr三方库初始化失败 OCR_INI_F = 103, //ocr三方库初始化失败
OCRLIF = 104, //OCR加载图片失败 OCR_LI_F = 104, //OCR加载图片失败
OCRRIF = 105 //OCR识别图片失败 OCR_RI_F = 105 //OCR识别图片失败
}; };
/** /**
* @brief * @brief
*/ */
enum Languages{ enum Languages{
UNKNOWN_LAN = -1, //未知语言
CHI_SIM=1, //简体中文 CHI_SIM=1, //简体中文
CHI_TRA=2, //繁体中文 CHI_TRA=2, //繁体中文
ENG=3 //英文 ENG=3 //英文
@ -117,23 +118,22 @@ struct RecognitionResult{
RecognitionResult(){ RecognitionResult(){
flag = false; flag = false;
message.clear(); message.clear();
errorCode = ErrorCode::UKNOW; errorCode = ErrorCode::UNKNOWN;
resultType = ResultType::ResultString; resultType = ResultType::RESULT_STRING;
result.clear(); result.clear();
} }
}; };
/** /**
* @brief ocr接口工具 * @brief ocr接口工具
* 使: * 使: *
* 1. TessOcrUtils::instance()->setLanguagesPath(langPath); * 1. TessOcrUtils::instance()->getRecogitionResult(t_image);
* 2. TessOcrUtils::instance()->setLanguages(langs);
* 3. TessOcrUtils::instance()->getRecogitionResult(t_image);
*/ */
class TessOcrUtils class TessOcrUtils
{ {
public: public:
TessOcrUtils(); TessOcrUtils();
~TessOcrUtils();
static TessOcrUtils *instance(); static TessOcrUtils *instance();
/** /**
@ -166,23 +166,22 @@ public:
*/ */
RecognitionResult getRecogitionResult(QImage *image); RecognitionResult getRecogitionResult(QImage *image);
/**
* @brief 使
* @param 使
* @return
*/
bool setLanguages(const QList<Languages> langs);
/**
* @brief 使
* @param
* @return
*/
bool setLanguagesPath(const QString langsPath);
private : private :
/**
* @brief 使
* @param 使
* @return
*/
//bool setLanguages(const QList<Languages> langs);
/**
* @brief 使
* @param
* @return
*/
bool setLanguagesPath(const QString langsPath);
/** /**
* @brief * @brief
* @param * @param
@ -204,6 +203,19 @@ private :
*/ */
QString getLangStr(Languages lang); QString getLangStr(Languages lang);
/**
* @brief
* @return
*/
Languages getSystemLang();
/**
* @brief 使
* @param 使
* @return
*/
QString getLanguages();
/** /**
* @brief * @brief
* @param * @param
@ -220,9 +232,8 @@ private :
*/ */
void setResult(ErrorCode errCode, const QString errMessage,const ResultType resultType,RecognitionResult &result); void setResult(ErrorCode errCode, const QString errMessage,const ResultType resultType,RecognitionResult &result);
/** /**
* @brief m_image * @brief
*/ */
QString m_sTessdataPath; QString m_sTessdataPath;