본문 바로가기
개발/C++

[C++] Tesseract OCR, OpenCV 다운로드 방법

by 보안매크로 2024. 4. 21.
728x90

vckpg를 설치해줍니다.

Step 1: Clone the vcpkg repo

git clone https://github.com/Microsoft/vcpkg.git

Make sure you are in the directory you want the tool installed to before doing this.

Step 2: Run the bootstrap script to build vcpkg

.\vcpkg\bootstrap-vcpkg.bat

 아래 방법으로 tesseract OCR, OpenCV이외에 다른 라이브러리들을 설치 가능합니다.

Install libraries for your project

vcpkg install [packages to install]

Using vcpkg with MSBuild / Visual Studio (may require elevation)

vcpkg integrate install


vcpkg가 깔린 곳으로 가서 cmd를 엽니다.

1. ./vcpkg install tesseract (안된다면, tesseract가 아닌 tesseract:x64-windows 해주세요)

2. ./vcpkg install leptonica:x64-windows 

3. vcpkg integrate install 로 설치된 걸 vs에 통합시켜줍니다.

4. 저는 python에서 사용하던 한국어, 영어 데이터를 가져와서 읽게하겠습니다.
(인터넷에서 tessoract다운받고, 다운 받은 파일에서 영어, 한국어 (eng.traineddata, kor.traineddata))를
옮겨주시고, 환경 변수 세팅 해주시면 정상적으로 동작합니다.

5. vs를 열어 테스트코드 작성 후 테스트.

#include <opencv2/opencv.hpp>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

int main() {
    tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();

    // Tesseract 초기화
    if (api->Init(NULL, "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }

    // 이미지 불러오기 (OpenCV 사용)
    cv::Mat image = cv::imread("/path/to/image.png");
    api->SetImage(image.data, image.cols, image.rows, 3, image.step);

    // 텍스트 인식
    api->Recognize(0);
    tesseract::ResultIterator* ri = api->GetIterator();
    tesseract::PageIteratorLevel level = tesseract::RIL_WORD;

    // 바운딩 박스 그리기 (OpenCV 사용)
    if (ri != 0) {
        do {
            int x1, y1, x2, y2;
            ri->BoundingBox(level, &x1, &y1, &x2, &y2);
            cv::rectangle(image, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 0, 255), 2);
        } while (ri->Next(level));
    }

    // 이미지 화면에 표시
    cv::imshow("Image with Bounding Boxes", image);
    cv::waitKey(0);

    // 정리
    delete ri;
    api->End();

    return 0;
}

 

완성!

728x90

'개발 > C++' 카테고리의 다른 글

C++, Visual Studio에서 MFC 설치하기  (0) 2024.03.12