先上图,有图有真相
首先在百度开通ORC服务,目前是免费的,普通识别每天50000次免费,非常棒!
百度文档:http://ai.baidu.com/docs#/OCR-API/top
下载百度SDK神马的就不多说了,需要包含CURL和JSON库,注意版本要求
windows下的openssl 32位和64位一键安装包顺便分享下,自己安装太麻烦
链接:https://pan.baidu.com/s/1HAuplB3deQGFk2eO8zC13A
提取码:mh34
CURL和JSON库就不贴出来了,网上随便都能找到,需要的朋友可以找我,我私发给你。
接下来进入正题,贴代码:
ImageRecogition.h
1 #pragma once 2 3 #include "json/json.h" 4 5 class CImageRecogition 6 { 7 public: 8 CImageRecogition(); 9 ~CImageRecogition(); 10 11 public: 12 /*accurate_basic*/ 13 Json::Value static accurate_basic(std::string szFile); 14 /*general_basic*/ 15 Json::Value static general_basic(std::string szFile); 16 /*general_enhanced*/ 17 Json::Value static general_enhanced(std::string szFile); 18 /*receipt*/ 19 Json::Value static receipt(std::string szFile); 20 /*custom*/ 21 Json::Value static custom(std::string szFile);24 /*save result to file*/ 25 void static SaveResultToFile(Json::Value & result, std::string szFile); 26 private: 27 };
ImageRecogition.cpp
#include "stdafx.h" #include "ImageRecogition.h" #include "baiduapi/ocr.h" #define APP_ID "xxxxxxx" #define API_KEY "xxxxxxx" #define SECRET_KEY "xxxxx" CImageRecogition::CImageRecogition() { } CImageRecogition::~CImageRecogition() { } Json::Value CImageRecogition::accurate_basic(std::string szFile) { aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image; aip::get_file_content(szFile.c_str(), &image); std::map<std::string, std::string> options; options["detect_direction"] = "true"; options["probability"] = "true"; std::cout << "高精识别开始:"; return client.accurate_basic(image, options); } Json::Value CImageRecogition::general_basic(std::string szFile) { aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image; aip::get_file_content(szFile.c_str(), &image); std::map<std::string, std::string> options; //options["language_type"] = "KOR"; options["detect_direction"] = "true"; options["detect_language"] = "true"; options["probability"] = "true"; std::cout << "普通识别开始:"; return client.general_basic(image, options); } Json::Value CImageRecogition::general_enhanced(std::string szFile) { aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image; aip::get_file_content(szFile.c_str(), &image); std::map<std::string, std::string> options; //options["language_type"] = "KOR"; options["detect_direction"] = "true"; options["detect_language"] = "true"; options["probability"] = "true"; std::cout << "生僻字识别开始:"; return client.general_enhanced(image, options); } Json::Value CImageRecogition::receipt(std::string szFile) { aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image; aip::get_file_content(szFile.c_str(), &image); std::map<std::string, std::string> options; //options["recognize_granularity"] = "small"; options["probability"] = "true"; //options["accuracy"] = "normal"; options["detect_direction"] = "true"; std::cout << "通用票据识别开始:"; return client.receipt(image, options); } Json::Value CImageRecogition::custom(std::string szFile) { aip::Ocr client(APP_ID, API_KEY, SECRET_KEY); std::string image; aip::get_file_content(szFile.c_str(), &image); std::map<std::string, std::string> options; std::string templateSign = "354b9b4fd9b0e4b38aedb8096260c6de"; std::cout << "自定义模板识别开始:"; return client.custom(image, templateSign, options); } void CImageRecogition::SaveResultToFile(Json::Value & result, std::string szFile) { FILE* fp = nullptr; auto error_no = fopen_s(&fp, szFile.c_str(), "a+"); if (fp != nullptr) { fprintf_s(fp, "==============================================================================\n"); SYSTEMTIME st; ::GetLocalTime(&st); fprintf_s(fp, "%04d-%02d-%02d %02d:%02d:%02d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); fprintf_s(fp, "log_id : %d\n", result["log_id"].asInt64()); auto result_num = result["words_result"].size(); if (result_num > 0) std::cout << "识别成功,识别行数:" << result_num << std::endl; else std::cout << "识别失败,未识别数据" << std::endl; for (int i = 0; i < result_num; ++i) fprintf_s(fp, "%s\n", result["words_result"][i]["words"].asString().c_str()); fprintf_s(fp, "==============================================================================\n"); fprintf_s(fp, "\n"); fclose(fp); } }
然后就是main函数的调用了
#include "stdafx.h" #include <iostream> #include "ServiceLogic/ImageRecogition.h" #include "curl/curl.h" #include "DirFile/DirFile.h" int _tmain(int argc, char* argv[]) { std::vector<std::string> vecFiles; DirFile::ListFiles(".", vecFiles, FILETYPE_JPG | FILETYPE_PNG | FILETYPE_JPEG); for (auto it : vecFiles) { std::cout << it << std::endl; std::string szFileName = it.substr(0, it.rfind(".")); std::string szPath = "./Log_" + szFileName + "/"; DirFile::CreatePath(szPath); //CImageRecogition::SaveResultToFile(CImageRecogition::accurate_basic(it), szPath + "accurate_basic.log"); CImageRecogition::SaveResultToFile(CImageRecogition::general_basic(it), szPath + "general_basic.log"); //CImageRecogition::SaveResultToFile(CImageRecogition::general_enhanced(it), szPath + "general_enhanced.log"); //CImageRecogition::SaveResultToFile(CImageRecogition::receipt(it), szPath + "receipt.log"); //CImageRecogition::SaveResultToFile(CImageRecogition::custom(it), szPath + "custom.log"); } system("pause"); return 0; }
DirFile.h是遍历当前目录下对应格式的文件,以及Log目录的创建的,这里就不贴出来了,毕竟不是这次的主题。
百度ORC的识别率,对图片要求还是比较高的,本人给朝鲜族的朋友测试韩文的发票,效果还是挺差强人意的。看大家的需求吧。
来源:https://www.cnblogs.com/yzhuang/p/10981609.html