Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
kuro337 committed Apr 21, 2024
1 parent 6ea283c commit 7444dab
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 92 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ endif()

enable_testing()

set(TEST_EXECUTABLES fs_test ocr_test
set(TEST_EXECUTABLES fs_test ocr_test imgclass_test
tesseractparallel_test bulk_test pdf_test
textractapi_tests atomic_test similarity_test
threadlocal_test)
Expand Down
50 changes: 50 additions & 0 deletions docs/debug/lldb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# lldb

```bash



lldb ./imgclass_test
run
bt

(lldb) run # run debugger
(lldb) quit # quit debugger

# Examine Once Crash Happens

(lldb) frame variable # or fv : Displays local variables in the current stack frame.
(lldb) print *this # non static contexts
(lldb) print variable_name

# frame 2 calls fr1 , f1 calls f0 , etc.
(lldb) bt # get backtrace of execution
frame select 0 # shows top stack frame
register read x8 # from stack frame - check memory addr read



(lldb) bt

# print <variable_name> or p <variable_name>: Prints the value of a variable.

(lldb) print variable_name

## Example Output for Debugging

- Shows us the top frame of the stack

frame select 0

# output : -> 0x10000ab10 <+120>: ldr x8, [x8, #0x8]

- Then view the register at address x8

register read x8

# output x8 = 0x0000000000000000 => NULL MEMORY ADDRESS

quit


```
43 changes: 42 additions & 1 deletion src/tesseract.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,45 @@ void createPDF(const std::string &input_path,
api->End();
delete api;
delete renderer;
}
}

auto extractTextFromImageFileLeptonica(const std::string &file_path,
const std::string &lang = "eng") -> std::string {
auto *api = new tesseract::TessBaseAPI();
if (api->Init(nullptr, "eng") != 0) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
Pix *image = pixRead(file_path.c_str());

// fully automatic - suitable for single columns of text

api->SetPageSegMode(tesseract::PSM_AUTO);

api->SetImage(image);
std::string outText(api->GetUTF8Text());
outText = api->GetUTF8Text();

api->End();
delete api;
pixDestroy(&image);
return outText;
}

auto extractTextLSTM(const std::string &file_path, const std::string &lang = "eng") -> std::string {
auto *api = new tesseract::TessBaseAPI();
if (api->Init(nullptr, "eng", tesseract::OEM_LSTM_ONLY) != 0) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
Pix *image = pixRead(file_path.c_str());

api->SetImage(image);
std::string outText(api->GetUTF8Text());
outText = api->GetUTF8Text();

api->End();
delete api;
pixDestroy(&image);
return outText;
}
21 changes: 21 additions & 0 deletions src/tesseract.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#include <leptonica/allheaders.h>
#include <llvm/Support/raw_ostream.h>
#include <tesseract/baseapi.h>
#include <tesseract/renderer.h>

// tesseract.h
#ifndef TESSERACT_H
#define TESSERACT_H

void createPDF(const std::string &input_path,
const std::string &output_path,
const char *tessdata_path,
bool text_only = false);

auto extractTextFromImageFileLeptonica(const std::string &file_path,
const std::string &lang = "eng") -> std::string;

auto extractTextLSTM(const std::string &file_path, const std::string &lang = "eng") -> std::string;

#endif // TESSERACT_H
60 changes: 60 additions & 0 deletions tests/imgclass_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

#include <gtest/gtest.h>
#include <src/fs.h>
#include <textract.h>

namespace imgclass_test_constants {
static constexpr auto tempDirectory = "temporary_dir";
constexpr auto *const imgFolder = IMAGE_FOLDER_PATH;
constexpr auto *const inputOpenTest = INPUT_OPEN_TEST_PATH;
} // namespace imgclass_test_constants

using namespace imgclass_test_constants;

class Imgclasstest: public ::testing::Test {
protected:
// static std::vector<std::string> static_memb;
static std::vector<std::string> fpaths;
static std::unique_ptr<imgstr::ImgProcessor> app;

void SetUp() override {
// runs per test
}
void TearDown() override {
// runs per test
}

static void SetUpTestSuite() {
// Once for whole Fixture

app = std::make_unique<imgstr::ImgProcessor>(1000); // Adjust parameters as needed

// static_member = initializeStaticMember();
fpaths = getFilePaths(imgFolder).get();
}

static void TearDownTestSuite() {
// Once for Whole Fixture
}

public:
std::string tempDir = "tempImgClass";
};

// Definition of static members
std::vector<std::string> Imgclasstest::fpaths;
std::unique_ptr<imgstr::ImgProcessor> Imgclasstest::app;

TEST_F(Imgclasstest, BasicAssertions) {
// implement test

ASSERT_FALSE(fpaths.empty()); // Ensure there is at least one path to work with
if (!fpaths.empty()) {
ASSERT_NO_THROW(app->convertImageToTextFile(fpaths[0], tempDir));
}
}

auto main(int argc, char **argv) -> int {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
163 changes: 94 additions & 69 deletions tests/ocr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#include <gtest/internal/gtest-port.h>
#include <iostream>
#include <leptonica/allheaders.h>
#include <llvm/ADT/STLExtras.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/FileSystem.h>
#include <ranges>
#include <src/fs.h>
#include <tesseract/baseapi.h>
#include <textract.h>
Expand Down Expand Up @@ -59,13 +63,6 @@ TEST_F(ImageProcessingTests, ConvertSingleImageToTextFile) {
}

TEST_F(ImageProcessingTests, WriteFileTest) {
// std::vector<std::string> paths;
// std::transform(images.begin(), images.end(),

// std::back_inserter(paths), [&](const std::string &img) {
// return path + img;
// });

imgstr::ImgProcessor imageTranslator;

EXPECT_NO_THROW(imageTranslator.addFiles(fpaths));
Expand All @@ -89,9 +86,6 @@ TEST_F(ImageProcessingTests, WriteFileTest) {

auto outputPath = tempDir + "/" + p.filename().string();

llvm::outs() << "Expected Output Path: " << expectedOutputPath << '\n';
llvm::outs() << "Computed Output Path: " << outputPath << '\n';

ASSERT_NO_THROW(imageTranslator.convertImageToTextFile(fpath, outputPath));

std::ifstream outputFile(expectedOutputPath);
Expand All @@ -105,9 +99,41 @@ TEST_F(ImageProcessingTests, WriteFileTest) {

outputFile.close();
}

if (deleteDirectory(tempDir)) {
std::cout << "error deleting" << '\n';
}
}

TEST_F(ImageProcessingTests, CheckForUniqueTextFile) {
const auto *equal_file_1 = "dupescreenshot.png";
const auto *equal_file_2 = "screenshot.png";

auto filteredFiles = fpaths | std::views::filter([&](const std::string &file) {
return file.find(equal_file_1) != std::string::npos ||
file.find(equal_file_2) != std::string::npos;
});

auto rit = std::ranges::find_if(filteredFiles, [&](const std::string &file) {
return file.ends_with(".png");
});

if (rit != filteredFiles.end()) {
std::cout << "Found PNG file: " << *rit << std::endl;
}

auto count = std::ranges::distance(filteredFiles);

std::vector<std::string> fullVector(filteredFiles.begin(), filteredFiles.end());

ASSERT_EQ(count, 2);

imgstr::ImgProcessor imageTranslator;

for (const auto &file: filteredFiles) {
imageTranslator.convertImageToTextFile(file, tempDir);
};

llvm::SmallString<128> dupePath(tempDir);
llvm::sys::path::append(dupePath, "dupescreenshot.txt");

Expand Down Expand Up @@ -150,65 +176,64 @@ TEST_F(ImageProcessingTests, BasicAssertions) {
EXPECT_EQ(7 * 6, 42);
}

// auto extractTextFromImageFileLeptonica(const std::string &file_path, const
// std::string &lang = "eng") -> std::string {
// auto *api = new tesseract::TessBaseAPI();
// if (api->Init(nullptr, "eng") != 0) {
// fprintf(stderr, "Could not initialize tesseract.\n");
// exit(1);
// }
// Pix *image = pixRead(file_path.c_str());

// // fully automatic - suitable for single columns of text

// api->SetPageSegMode(tesseract::PSM_AUTO);

// api->SetImage(image);
// std::string outText(api->GetUTF8Text());
// outText = api->GetUTF8Text();

// api->End();
// delete api;
// pixDestroy(&image);
// return outText;
// }

// auto extractTextLSTM(const std::string &file_path, const std::string &lang =
// "eng") -> std::string {
// auto *api = new tesseract::TessBaseAPI();
// if (api->Init(nullptr, "eng", tesseract::OEM_LSTM_ONLY) != 0) {
// fprintf(stderr, "Could not initialize tesseract.\n");
// exit(1);
// }
// Pix *image = pixRead(file_path.c_str());

// api->SetImage(image);
// std::string outText(api->GetUTF8Text());
// outText = api->GetUTF8Text();

// api->End();
// delete api;
// pixDestroy(&image);
// return outText;
// }

// TEST_F(MyTestSuite, OEMvsLSTMAnalysis) {
// auto start = imgstr::getStartTime();
// auto res1 = extractTextFromImageFileLeptonica(fpaths[1]);

// std::cout << res1 << '\n';

// auto time1 = imgstr::getDuration(start);
// std::cout << "Time Leptonica : " << time1 << '\n';

// auto start2 = imgstr::getStartTime();
// auto res2 = extractTextLSTM(fpaths[1]);

// std::cout << res2 << '\n';

// auto time2 = imgstr::getDuration(start);
// std::cout << "Time LSTM: " << time2 << '\n';
// }
auto extractTextFromImageFileLeptonica(const std::string &file_path,
const std::string &lang = "eng") -> std::string {
auto *api = new tesseract::TessBaseAPI();
if (api->Init(nullptr, "eng") != 0) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
Pix *image = pixRead(file_path.c_str());

// fully automatic - suitable for single columns of text

api->SetPageSegMode(tesseract::PSM_AUTO);

api->SetImage(image);
std::string outText(api->GetUTF8Text());
outText = api->GetUTF8Text();

api->End();
delete api;
pixDestroy(&image);
return outText;
}

auto extractTextLSTM(const std::string &file_path, const std::string &lang = "eng") -> std::string {
auto *api = new tesseract::TessBaseAPI();
if (api->Init(nullptr, "eng", tesseract::OEM_LSTM_ONLY) != 0) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
Pix *image = pixRead(file_path.c_str());

api->SetImage(image);
std::string outText(api->GetUTF8Text());
outText = api->GetUTF8Text();

api->End();
delete api;
pixDestroy(&image);
return outText;
}

TEST_F(ImageProcessingTests, OEMvsLSTMAnalysis) {
auto start = imgstr::getStartTime();
auto res1 = extractTextFromImageFileLeptonica(fpaths[1]);

std::cout << res1 << '\n';

auto time1 = imgstr::getDuration(start);
std::cout << "Time Leptonica : " << time1 << '\n';

auto start2 = imgstr::getStartTime();
auto res2 = extractTextLSTM(fpaths[1]);

std::cout << res2 << '\n';

auto time2 = imgstr::getDuration(start);
std::cout << "Time LSTM: " << time2 << '\n';
}

#ifdef _USE_OPENCV

Expand Down
Loading

0 comments on commit 7444dab

Please sign in to comment.