Skip to content

Commit

Permalink
Update plugin-support.h and plugin-support.c.in
Browse files Browse the repository at this point in the history
Update filter-data.h

Update CMakeLists.txt

Update obs-utils.h

Update FetchOpenCV.cmake

Update FetchTesseract.cmake

Update ocr-filter.cpp
  • Loading branch information
royshil committed Jan 5, 2024
1 parent 9184b08 commit 36a1dd9
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 55 deletions.
7 changes: 2 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ endif()
include(cmake/FetchTesseract.cmake)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Tesseract)

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

set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${_name})
8 changes: 4 additions & 4 deletions cmake/FetchOpenCV.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ if(CUSTOM_OPENCV_URL STREQUAL "")
else()
message(STATUS "Using custom OpenCV: ${CUSTOM_OPENCV_URL}")
if(CUSTOM_OPENCV_HASH STREQUAL "")
message(FATAL_ERROR "CUSTOM_OPENCV_HASH not found. Both of CUSTOM_OPENCV_URL and CUSTOM_OPENCV_HASH must be present!")
message(
FATAL_ERROR "CUSTOM_OPENCV_HASH not found. Both of CUSTOM_OPENCV_URL and CUSTOM_OPENCV_HASH must be present!")
else()
set(USE_PREDEFINED_OPENCV OFF)
endif()
Expand Down Expand Up @@ -74,8 +75,7 @@ if(MSVC)
${opencv_SOURCE_DIR}/x64/vc17/staticlib/zlib.lib)
target_include_directories(OpenCV SYSTEM INTERFACE ${opencv_SOURCE_DIR}/include)
else()
target_link_libraries(
OpenCV INTERFACE ${opencv_SOURCE_DIR}/lib/libopencv_imgproc.a
${opencv_SOURCE_DIR}/lib/libopencv_core.a)
target_link_libraries(OpenCV INTERFACE ${opencv_SOURCE_DIR}/lib/libopencv_imgproc.a
${opencv_SOURCE_DIR}/lib/libopencv_core.a)
target_include_directories(OpenCV SYSTEM INTERFACE ${opencv_SOURCE_DIR}/include/opencv4)
endif()
19 changes: 11 additions & 8 deletions cmake/FetchTesseract.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ if(CUSTOM_TESSERACT_URL STREQUAL "")
else()
message(STATUS "Using custom Tesseract: ${CUSTOM_TESSERACT_URL}")
if(CUSTOM_TESSERACT_HASH STREQUAL "")
message(FATAL_ERROR "CUSTOM_TESSERACT_HASH not found. Both of CUSTOM_TESSERACT_URL and CUSTOM_TESSERACT_HASH must be present!")
message(
FATAL_ERROR
"CUSTOM_TESSERACT_HASH not found. Both of CUSTOM_TESSERACT_URL and CUSTOM_TESSERACT_HASH must be present!")
else()
set(USE_PREDEFINED_TESSERACT OFF)
endif()
Expand Down Expand Up @@ -67,14 +69,15 @@ FetchContent_MakeAvailable(tesseract)

add_library(Tesseract INTERFACE)
if(MSVC)
target_link_libraries(
Tesseract
INTERFACE ${tesseract_SOURCE_DIR}/x64/vc17/staticlib/tesseract.lib)
target_link_libraries(Tesseract INTERFACE ${tesseract_SOURCE_DIR}/lib/tesseract53.lib
${tesseract_SOURCE_DIR}/lib/leptonica-1.84.1.lib)
target_include_directories(Tesseract SYSTEM INTERFACE ${tesseract_SOURCE_DIR}/include)
elseif(APPLE)
target_link_libraries(Tesseract INTERFACE ${tesseract_SOURCE_DIR}/lib/libtesseract.a
${tesseract_SOURCE_DIR}/lib/libleptonica.a)
target_include_directories(Tesseract SYSTEM INTERFACE ${tesseract_SOURCE_DIR}/include)
else()
target_link_libraries(
Tesseract INTERFACE ${tesseract_SOURCE_DIR}/lib/libtesseract.a
${tesseract_SOURCE_DIR}/lib/libleptonica.a
)
target_link_libraries(Tesseract INTERFACE ${tesseract_SOURCE_DIR}/lib/libtesseract.a
${tesseract_SOURCE_DIR}/lib/lib/libleptonica.a)
target_include_directories(Tesseract SYSTEM INTERFACE ${tesseract_SOURCE_DIR}/include)
endif()
10 changes: 1 addition & 9 deletions src/filter-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ struct filter_data {
char *output_source_name = nullptr;
std::mutex *output_source_mutex = nullptr;

#if _WIN32
const wchar_t *modelFilepath = nullptr;
const wchar_t *tesseractTraineddataFilepath = nullptr;
const wchar_t *vocabularyFilepath = nullptr;
#else
const char *modelFilepath = nullptr;
const char *tesseractTraineddataFilepath = nullptr;
const char *vocabularyFilepath = nullptr;
#endif
char *tesseractTraineddataFilepath = nullptr;
};

#endif /* FILTERDATA_H */
22 changes: 14 additions & 8 deletions src/obs-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ void acquire_weak_output_source_ref(struct filter_data *usd)
usd->output_source = obs_source_get_weak_source(source);
obs_source_release(source);
if (!usd->output_source) {
obs_log(LOG_ERROR, "failed to get weak source for text source %s",
obs_log(LOG_ERROR,
"failed to get weak source for text source %s",
usd->output_source_name);
}
} else {
obs_log(LOG_ERROR, "text source '%s' not found", usd->output_source_name);
obs_log(LOG_ERROR, "text source '%s' not found",
usd->output_source_name);
}
}

Expand Down Expand Up @@ -153,16 +155,19 @@ bool add_sources_to_list(void *list_property, obs_source_t *source)
return true;
}

void update_text_source_on_settings(struct filter_data *usd, obs_data_t *settings)
void update_text_source_on_settings(struct filter_data *usd,
obs_data_t *settings)
{
// update the text source
const char *new_text_source_name = obs_data_get_string(settings, "text_sources");
const char *new_text_source_name =
obs_data_get_string(settings, "text_sources");
obs_weak_source_t *old_weak_text_source = NULL;

if (!is_valid_output_source_name(new_text_source_name)) {
// new selected text source is not valid, release the old one
if (usd->output_source) {
std::lock_guard<std::mutex> lock(*usd->output_source_mutex);
std::lock_guard<std::mutex> lock(
*usd->output_source_mutex);
old_weak_text_source = usd->output_source;
usd->output_source = nullptr;
}
Expand All @@ -173,10 +178,12 @@ void update_text_source_on_settings(struct filter_data *usd, obs_data_t *setting
} else {
// new selected text source is valid, check if it's different from the old one
if (usd->output_source_name == nullptr ||
strcmp(new_text_source_name, usd->output_source_name) != 0) {
strcmp(new_text_source_name, usd->output_source_name) !=
0) {
// new text source is different from the old one, release the old one
if (usd->output_source) {
std::lock_guard<std::mutex> lock(*usd->output_source_mutex);
std::lock_guard<std::mutex> lock(
*usd->output_source_mutex);
old_weak_text_source = usd->output_source;
usd->output_source = nullptr;
}
Expand All @@ -188,4 +195,3 @@ void update_text_source_on_settings(struct filter_data *usd, obs_data_t *setting
obs_weak_source_release(old_weak_text_source);
}
}

10 changes: 6 additions & 4 deletions src/obs-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ bool getRGBAFromStageSurface(filter_data *tf, uint32_t &width,

inline bool is_valid_output_source_name(const char *output_source_name)
{
return output_source_name != nullptr && strcmp(output_source_name, "none") != 0 &&
strcmp(output_source_name, "(null)") != 0 && strcmp(output_source_name, "") != 0;
return output_source_name != nullptr &&
strcmp(output_source_name, "none") != 0 &&
strcmp(output_source_name, "(null)") != 0 &&
strcmp(output_source_name, "") != 0;
}

void acquire_weak_output_source_ref(struct filter_data *usd);
Expand All @@ -18,7 +20,7 @@ void setTextCallback(const std::string &str, struct filter_data *usd);

bool add_sources_to_list(void *list_property, obs_source_t *source);

void update_text_source_on_settings(struct filter_data *usd, obs_data_t *settings);

void update_text_source_on_settings(struct filter_data *usd,
obs_data_t *settings);

#endif /* OBS_UTILS_H */
27 changes: 11 additions & 16 deletions src/ocr-filter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#ifdef _WIN32
#include <wchar.h>
#endif // _WIN32

#include <opencv2/imgproc.hpp>

Expand All @@ -13,6 +10,8 @@

#include <QString>

#include <util/bmem.h>

#include <plugin-support.h>
#include "filter-data.h"
#include "obs-utils.h"
Expand Down Expand Up @@ -68,9 +67,9 @@ obs_properties_t *ocr_filter_properties(void *data)
"scoreboard");

// Add a property for the output text source
obs_property_t *sources = obs_properties_add_list(props, "text_sources",
"Output text source", OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
obs_property_t *sources = obs_properties_add_list(
props, "text_sources", "Output text source",
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
// Add a "none" option
obs_property_list_add_string(sources, "No output", "none");
// Add the sources
Expand Down Expand Up @@ -135,21 +134,14 @@ void *ocr_filter_create(obs_data_t *settings, obs_source_t *source)

tf->source = source;
tf->texrender = gs_texrender_create(GS_BGRA, GS_ZS_NONE);
tf->output_source_name = bstrdup(obs_data_get_string(settings, "text_sources"));
tf->output_source_name =
bstrdup(obs_data_get_string(settings, "text_sources"));
tf->output_source = nullptr;
// initialize the mutex
tf->output_source_mutex = new std::mutex();

// get the model file path from the module
#if _WIN32
tf->modelFilepath = obs_module_file("crnn_cs.onnx");
// get the models folder path from the module
tf->tesseractTraineddataFilepath = obs_module_file("tessdata");
tf->vocabularyFilepath = obs_module_file("alphabet_36.txt");
#else
tf->modelFilepath = obs_module_file("crnn_cs.onnx");
tf->tesseractTraineddataFilepath = obs_module_file("tessdata");
tf->vocabularyFilepath = obs_module_file("alphabet_94.txt");
#endif

tf->tesseract_model = nullptr;

Expand All @@ -170,6 +162,9 @@ void ocr_filter_destroy(void *data)
}
obs_leave_graphics();

if (tf->tesseractTraineddataFilepath != nullptr) {
bfree(tf->tesseractTraineddataFilepath);
}
if (tf->tesseract_model != nullptr) {
tf->tesseract_model->End();
delete tf->tesseract_model;
Expand Down
2 changes: 2 additions & 0 deletions src/plugin-support.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ with this program. If not, see <https://www.gnu.org/licenses/>

#include <plugin-support.h>

extern void blogva(int log_level, const char *format, va_list args);

const char *PLUGIN_NAME = "@CMAKE_PROJECT_NAME@";
const char *PLUGIN_VERSION = "@CMAKE_PROJECT_VERSION@";

Expand Down
1 change: 0 additions & 1 deletion src/plugin-support.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extern const char *PLUGIN_NAME;
extern const char *PLUGIN_VERSION;

void obs_log(int log_level, const char *format, ...);
extern void blogva(int log_level, const char *format, va_list args);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 36a1dd9

Please sign in to comment.