手把手教你开发图片识别(QT篇)
生活随笔
收集整理的這篇文章主要介紹了
手把手教你开发图片识别(QT篇)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
tesseract是一個開源的OCR庫在linux系統中我們可以很方便的開發一個簡單的圖片識別工具。
開發環境:QT 5.11.3
操作系統:UOS 64位(debian) 支持國產操作系統!
安裝開發環境
sudo apt-get install tesseract-ocr # tesseract sudo apt-get install libtesseract-dev # 安裝開發包 sudo apt-get install tesseract-ocr-eng # 安裝英語 sudo apt-get install tesseract-ocr-chi-sim # 安裝中文語言包dpkg -L tesseract-ocr-eng # 查看語言包的安裝目錄,開發的時候需要配置 dpkg -L tesseract-ocr-chi-sim # 查看語言包的安裝目錄,開發的時候需要配置
獲取語言包的路徑
開發
1. 新建工程
新建QT工程。
我的項目中一般不使用界面文件,因此去掉勾選。
2. 開發配置
有在識別文字的時候需要使用到語言包因此需要配置語言包的環境變量。
3. 界面開發
布局結果大致如下:
以下代碼僅是布局代碼。
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow>class QWidget; class QVBoxLayout; class QHBoxLayout; class QLineEdit; class QPushButton; class QTextEdit; class MainWindow : public QMainWindow {Q_OBJECT private:QWidget *m_Widgetmain;QHBoxLayout *m_layoutFileSelect;QVBoxLayout *m_layoutMain;QLineEdit *m_lineEditPath; // 顯示圖片路徑QPushButton *m_btnSelectFile; // 文件選擇按鈕QTextEdit *m_textEditRes; // 結果展示 public:MainWindow(QWidget *parent = 0);~MainWindow(); public slots:// 控件按鈕選擇文件的事件處理void slotSelectFile(); };#endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h" #include <QHBoxLayout> #include <QVBoxLayout> #include <QLineEdit> #include <QTextEdit> #include <QPushButton> #include <QFileDialog> #include <tesseract/baseapi.h> #include <leptonica/allheaders.h> #include <QDebug> MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),m_Widgetmain(new QWidget),m_layoutFileSelect(new QHBoxLayout()),m_layoutMain(new QVBoxLayout()),m_lineEditPath(new QLineEdit),m_btnSelectFile(new QPushButton),m_btnOrc(new QPushButton),m_textEditRes(new QTextEdit) {m_btnSelectFile->setText("選擇文件");m_btnOrc->setText("識別");m_layoutFileSelect->addWidget(m_lineEditPath);m_layoutFileSelect->addWidget(m_btnSelectFile);m_layoutFileSelect->addWidget(m_btnOrc);m_layoutMain->addLayout(m_layoutFileSelect);m_layoutMain->addWidget(m_textEditRes);m_Widgetmain->setLayout(m_layoutMain);// 設置按鈕監聽事件connect(m_btnSelectFile,&QPushButton::clicked,this,&MainWindow::slotSelectFile);connect(m_btnOrc,&QPushButton::clicked,this,&MainWindow::slotGetORCRes);setCentralWidget(m_Widgetmain);resize(800,600); }MainWindow::~MainWindow() {}void MainWindow::slotSelectFile() {QFileDialog fileDlg;QString file_name = QFileDialog::getOpenFileName(NULL,"標題",".","*.*");m_lineEditPath->setText(file_name); }void MainWindow::slotGetORCRes() {// todo }4. 功能開發
void MainWindow::slotGetORCRes() {char *outText;tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();// Initialize tesseract-ocr with English, without specifying tessdata pathif (api->Init(NULL, "chi_sim")) {fprintf(stderr, "Could not initialize tesseract.\n");exit(1);}// Open input image with leptonica libraryQString strPath = m_lineEditPath->text(); // 獲取圖片路徑Pix *image = pixRead(strPath.toUtf8());api->SetImage(image);// Get OCR resultoutText = api->GetUTF8Text();qInfo()<<outText;printf("OCR output:\n%s", outText);// 顯示結果m_textEditRes->setText(outText);// Destroy used object and release memoryapi->End();delete api;delete [] outText;pixDestroy(&image); }5.效果
測試圖片
結果運行:
源代碼地址:https://gitee.com/arv000/qt-prince/tree/master/TestOCR
源代碼地址:https://gitcode.net/arv002/qt/-/tree/master/TestOCR
總結
以上是生活随笔為你收集整理的手把手教你开发图片识别(QT篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenHarmony AI图像识别开发
- 下一篇: Windows C++界面库