基於百度OCR的圖片文字識別

  先上圖,有圖有真相html

      

  首先在百度開通ORC服務,目前是免費的,普通識別天天50000次免費,很是棒!ios

       百度文檔:http://ai.baidu.com/docs#/OCR-API/topgit

  下載百度SDK神馬的就很少說了,須要包含CURL和JSON庫,注意版本要求json

  windows下的openssl 32位和64位一鍵安裝包順便分享下,本身安裝太麻煩windows

  連接:https://pan.baidu.com/s/1HAuplB3deQGFk2eO8zC13A
  提取碼:mh34 api

  CURL和JSON庫就不貼出來了,網上隨便都能找到,須要的朋友能夠找我,我私發給你。curl

  接下來進入正題,貼代碼:函數

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.cppurl

#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

相關文章
相關標籤/搜索