1. 程式人生 > >Tesseract-OCR 3.04簡單使用舉例(讀入影象輸出識別結果)

Tesseract-OCR 3.04簡單使用舉例(讀入影象輸出識別結果)

        下面code是對Tesseract-OCR 3.04版本進行簡單使用的舉例:包括兩段,一個是讀入帶有中文字元的影象,一個是讀入僅有英文字元的影象:

#include "funset.hpp"

#include <iostream>
#include <string>
#include <vector>

#include <allheaders.h>

#include <baseapi.h>
#include <basedir.h>
#include <renderer.h>
#include <strngs.h>
#include <tprintf.h>
#include <openclwrapper.h>

namespace {

#ifdef _MSC_VER
void utf8_to_gbk(const char* utf8, char* gbk)
{
	const int maxlen = 128;
	wchar_t unicode_str[maxlen];
	int outlen = MultiByteToWideChar(CP_UTF8, 0, utf8, strlen(utf8), unicode_str, maxlen);
	outlen = WideCharToMultiByte(CP_ACP, 0, unicode_str, outlen, gbk, 128, nullptr, nullptr);
	gbk[outlen] = '\0';
}
#endif

} // namespace

int test_recognize_image_content_1()
{
	// reference: https://github.com/tesseract-ocr/tesseract/wiki/APIExample
{ // chinese
	// Initialize tesseract-ocr
	tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
	const char* datapath{ "E:/GitCode/OCR_Test/test_data/tessdata/" };
	if (api->Init(datapath, "chi_sim")) {
		fprintf(stderr, "Could not initialize tesseract.\n");
		return -1;
	}

	// Open input image with leptonica library
	Pix* image = pixRead("E:/GitCode/OCR_Test/test_data/chi_sim_1.png");
	api->SetImage(image);
	// Get OCR result
	char* outText = api->GetUTF8Text();
	char gbk[256];
	utf8_to_gbk(outText, gbk);
	fprintf(stdout, "OCR output: %s\n", gbk);

	// Destroy used object and release memory
	api->End();
	delete api;
	delete[] outText;
	pixDestroy(&image);
}

{ // english
	// Initialize tesseract-ocr
	tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
	const char* datapath{ "E:/GitCode/OCR_Test/test_data/tessdata/" };
	if (api->Init(datapath, "eng")) {
		fprintf(stderr, "Could not initialize tesseract.\n");
		return -1;
	}

	// Open input image with leptonica library
	Pix* image = pixRead("E:/GitCode/OCR_Test/test_data/eng_1.png");
	api->SetImage(image);
	// Get OCR result
	char* outText = api->GetUTF8Text();
	char gbk[256];
	utf8_to_gbk(outText, gbk);
	fprintf(stdout, "OCR output: %s\n", gbk);

	// Destroy used object and release memory
	api->End();
	delete api;
	delete[] outText;
	pixDestroy(&image);
}

	return 0;
}

        測試影象如下:



        執行結果如下:


相關推薦

Tesseract-OCR 3.04簡單使用舉例(影象輸出識別結果)

        下面code是對Tesseract-OCR 3.04版本進行簡單使用的舉例:包括兩段,一個是讀入帶有中文字元的影象,一個是讀入僅有英文字元的影象:#include "funset.hpp" #include <iostream> #include

Tesseract-OCR 3 04在Windows7 vs2013上編譯過程

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Tesseract-OCR 3.04在Windows7 vs2013上編譯過程

static const STRING kCharsToEx[] = {"'", "`", "\"", "\\", ",", ".", "〈", "〉", "《", "》", "」", "「", ""}; 修改為 static const

Tesseract-OCR 3.0+ 安裝和使用

終端輸入命令:tesseract --help Usage: tesseract --help | --help-psm | --version tesseract --list-langs [--tessdata-dir PATH] tesseract --print-parameters [

python 3.5 解決csv 中的'utf-8' codec can't decode辦法

------------------------------------------------------------------------------------------------------------------------------------------

tesseract-ocr 3.02 信心值 字元座標 學習筆記

在使用字元識別的過程中,需要辨別一些字元本身是否殘缺或與標準字元是否有形變又或者想知道其在截圖上面的座標是什麼,這樣就需要用到tesseract-ocr 輸出的HTML檔案的功能了. 命令列為 tesseract -l eng outtext hocr 其中hocr 引數是

Tesseract-OCR 3.02命令列程式的使用

  OCR(Optical Character Recognition):光學字元識別,是指對圖片檔案中的文字進行分析識別,獲取的過程。最近因為需要看了看OCR的相關知識,這裡我看的是谷歌的Tesseract-OCR檢測引擎。Tesseract:開源的OCR識別

『c++輸出的探索』

對於讀入與輸出的探索和優化 ·cin和cout ·scanf和printf ·關閉同步流 ·freopen和fclose ·整型read ·浮點型與字串read ·快輸 ·fread <更新提示> <第一次更新> <正

PCL系列1——輸出pcd格式點雲檔案

1.從檔案讀取點雲 寫法1: //1.loadPCDFile讀取點雲 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>); if (pcl::i

模板庫(一) - 輸出優化

寫在前面 “模板庫”這一系列文章用來複習 O I OI

優化&&輸出優化

#include<cstdio> int isdigit[300]; void read(int &x) { int f = 1; x = 0; char s = getchar(); while(!isdigit[s]) {if(x

輸出shp檔案——Matlab

Matlab自帶有mapping box工具箱,該工具箱具有很多與地圖相關的功能,如下圖,詳情請訪問Matlab官網幫助. 讀寫shp檔案也是非常容易的事情。shp檔案在Matlab中被視為一個struct,每個欄位的屬性值則是struct的一個元胞。 讀入s

double 型別的輸出

2018年11月17日 18:36:39 weixin_43687233 閱讀數:8 標籤: C語言

關於__int 128 的輸出

git while pan clas print 關於 getchar() div getc inline __int128 read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch==‘-‘;ch

Python-openCV影象

文章參考於張平編著《OpenCV演算法精解-基於Python與C++》 #coding=utf-8 import cv2 import sys if __name__=='__main__': if len(sys.argv)>1: image

優化&輸出優化

注意了注意了注意了,重要的事情說3遍,這個東西是騙分神器,騙分神器,騙分神器!!! 眾所周知:scanf比cin快得多,printf比cout快得多,如果你不知道就……就現在知道了 那有沒有更快的呢?當然。 請看: 我懵逼了,至於慢近100ms嗎? 好

OpenCV影象,設定大小,按鍵移動影象位置。

歡迎使用Markdown編輯器寫部落格 在OpenCV編寫程式實現以下功能: 讀入影象 設定影象大小 按鍵移動影象位置 實現程式碼 #include "opencv2/core/core.hpp" #include "opencv2/im

C++實現大整數類及其輸出、加法、乘法運算

C/C++並沒有內建高精度整數類,就算使用long long,也無法滿足我們的需求。要實現高精度正整數,我們需要使用特別的方法。 下面的C++程式碼實現了高精度正整數結構體。它把一個長的整數分割成許多部分,在內部倒序儲存以便於運算。由於過載了”<<

【模板】優化&輸出優化

先列一份摘自LOJ的一份讀入測試資料(資料說明一切) 以下是若干讀入整數速度測試的結果(單位:毫秒)。 輸入:3×1063×106 ​ 個在區間中隨機生成的十進位制整數。 # Lanuage [0,2) [0,8) [0,215

C#基礎視頻教程6.3 如何簡單寫數據庫

一點 犯錯 .html ack ble 數字 擴展 你知道 定義 在繼續往下做之前,我們需要把之前的代碼盡可能的精簡(會對後面很有好處,而且讀者也應該仔細比對這一部分的代碼和上一部分哪裏真正得到了優化,從而提高編程水平)。 首先數據庫的操作類有哪些是可以做的更加普遍,變量