Skip to content

Commit

Permalink
Merge pull request #3 from occ-ai/roy.add_output_formatting
Browse files Browse the repository at this point in the history
Add vendor/inja and output formatting support
  • Loading branch information
royshil authored Jan 10, 2024
2 parents 0c1c551 + 6f663ce commit e554661
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
!CMakePresets.json
!LICENSE
!README.md
!/vendor
!/vendor/inja

# Exclude lock files
*.lock.json
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/inja"]
path = vendor/inja
url = [email protected]:pantor/inja.git
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ endif()
include(cmake/FetchTesseract.cmake)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Tesseract)

include(cmake/BuildInja.cmake)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE inja)

target_sources(${CMAKE_PROJECT_NAME} PRIVATE src/plugin-main.c src/obs-utils.cpp src/tesseract-ocr-utils.cpp
src/ocr-filter.cpp src/ocr-filter-info.c)
src/ocr-filter.cpp src/ocr-filter-info.c)

set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${_name})
4 changes: 2 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"description": "Build for Linux x86_64 on CI",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_COMPILE_WARNING_AS_ERROR": true
"CMAKE_COMPILE_WARNING_AS_ERROR": false
}
},
{
Expand Down Expand Up @@ -121,7 +121,7 @@
"description": "Build for Linux aarch64 on CI",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_COMPILE_WARNING_AS_ERROR": true
"CMAKE_COMPILE_WARNING_AS_ERROR": false
}
}
],
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ Available now:
- Choose the segmentation mode: Word, Line, Page, etc.
- "Semantic Smoothing": getting more consistent outputs with higher accuracy and confidence by "averaging" several text outputs
- Timing/Running modes: per X-milliseconds
- Output formatting (with inja): e.g. "Score: {{score}}"

Coming soon:
- More languages built-in (pretrained Tesseract models)
- Allowing external model files
- More output capabilities: Parsing, formatting
- More output capabilities e.g. Parsing
- Output to more built-in OBS sources (Image, etc.)
- Extracting text from complex image layouts
- Different timing/run modes: per X-frames, source change, etc.
- Different timing/run modes: per X-frames, image change, etc.
- Image stabilization

Check out our other plugins:
- [Background Removal](https://github.com/occ-ai/obs-backgroundremoval) removes background from webcam without a green screen.
Expand All @@ -65,7 +67,7 @@ Check out our other plugins:
- [Polyglot](https://github.com/occ-ai/obs-polyglot) translation AI plugin for real-time, local translation to hunderds of languages
- [URL/API Source](https://github.com/occ-ai/obs-urlsource) will connect to any URL/API HTTP and get the data/image/audio to your scene.

If you like this work, which is given to you completely free of charge, please consider supporting it on GitHub: https://github.com/sponsors/royshil
If you like this work, which is given to you completely free of charge, please consider supporting it https://github.com/sponsors/royshil or https://www.patreon.com/RoyShilkrot

## Download
Check out the [latest releases](https://github.com/occ-ai/obs-ocr/releases) for downloads and install instructions.
Expand Down
10 changes: 10 additions & 0 deletions cmake/BuildInja.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set(INJA_USE_EMBEDDED_JSON UNIX AND NOT APPLE)
set(INJA_INSTALL OFF)
set(INJA_EXPORT OFF)
set(BUILD_TESTING OFF)
set(INJA_BUILD_TESTS OFF)
set(BUILD_BENCHMARK OFF)
set(COVERALLS OFF)
# inja uses a lot of shadowing
add_compile_options(-Wno-shadow)
add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/inja ${CMAKE_BINARY_DIR}/inja EXCLUDE_FROM_ALL)
Binary file modified data/tessdata/daktronics.traineddata
Binary file not shown.
1 change: 1 addition & 0 deletions src/filter-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct filter_data {
size_t word_length;
size_t window_size;
uint32_t update_timer_ms;
std::string output_format_template;

bool isDisabled;

Expand Down
6 changes: 6 additions & 0 deletions src/ocr-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ obs_properties_t *ocr_filter_properties(void *data)

obs_property_set_modified_callback(enable_smoothing_property, enable_smoothing_modified);

// Output formatting
obs_properties_add_text(props, "output_formatting", obs_module_text("OutputFormatting"),
OBS_TEXT_MULTILINE);

// Add a property for the output text source
obs_property_t *sources =
obs_properties_add_list(props, "text_sources", obs_module_text("OutputTextSource"),
Expand Down Expand Up @@ -162,6 +166,7 @@ void ocr_filter_defaults(obs_data_t *settings)
obs_data_set_default_bool(settings, "enable_smoothing", false);
obs_data_set_default_int(settings, "word_length", 5);
obs_data_set_default_int(settings, "window_size", 10);
obs_data_set_default_string(settings, "output_formatting", "{{output}}");
}

void ocr_filter_update(void *data, obs_data_t *settings)
Expand All @@ -180,6 +185,7 @@ void ocr_filter_update(void *data, obs_data_t *settings)
tf->word_length = obs_data_get_int(settings, "word_length");
tf->window_size = obs_data_get_int(settings, "window_size");
tf->update_timer_ms = (uint32_t)obs_data_get_int(settings, "update_timer");
tf->output_format_template = obs_data_get_string(settings, "output_formatting");

// Initialize the Tesseract OCR model
initialize_tesseract_ocr(tf);
Expand Down
14 changes: 14 additions & 0 deletions src/tesseract-ocr-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <tesseract/baseapi.h>

#include <inja/inja.hpp>

#include <string>
#include <fstream>
#include <deque>
Expand Down Expand Up @@ -181,6 +183,15 @@ std::string CharacterBasedSmoothingFilter::add_reading(const std::string &inWord
return smoothed_word;
}

std::string format_text_with_template(inja::Environment &env, const std::string &text,
struct filter_data *tf)
{
// Replace the {{output}} placeholder with the source text using inja
nlohmann::json data;
data["output"] = text;
return env.render(tf->output_format_template, data);
}

void stop_and_join_tesseract_thread(struct filter_data *tf)
{
{
Expand Down Expand Up @@ -209,6 +220,8 @@ void tesseract_thread(void *data)

obs_log(LOG_INFO, "Starting Tesseract thread, update timer: %d", tf->update_timer_ms);

inja::Environment env;

while (true) {
{
std::lock_guard<std::mutex> lock(tf->tesseract_mutex);
Expand Down Expand Up @@ -238,6 +251,7 @@ void tesseract_thread(void *data)
if (!ocr_result.empty() &&
is_valid_output_source_name(tf->output_source_name)) {
// If an output source is selected - send the results there
ocr_result = format_text_with_template(env, ocr_result, tf);
setTextCallback(ocr_result, tf);
}
} catch (const std::exception &e) {
Expand Down
1 change: 1 addition & 0 deletions vendor/inja
Submodule inja added at 0066e6

0 comments on commit e554661

Please sign in to comment.