From 56b6f061cd89acc52a7422dc2da8e32958e17e69 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 8 Dec 2016 15:08:48 -0800 Subject: [PATCH] Revise after code review --- api/baseapi.cpp | 34 ++++++++++++++++++++++++---------- api/baseapi.h | 10 +++++----- api/capi.cpp | 28 ++++------------------------ api/capi.h | 5 ++--- 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/api/baseapi.cpp b/api/baseapi.cpp index 129406a22..d60749751 100644 --- a/api/baseapi.cpp +++ b/api/baseapi.cpp @@ -1891,7 +1891,15 @@ char* TessBaseAPI::GetUNLVText() { return result; } -bool TessBaseAPI::DetectOrientationScript(int& orient_deg, float& orient_conf, std::string& script, float& script_conf) { +/** + * Detect the orientation of the input image and apparent script (alphabet). + * orient_deg is the detected clockwise rotation of the input image in degrees (0, 90, 180, 270) + * orient_conf is the confidence (15.0 is reasonably confident) + * script_name is an ASCII string, the name of the script, e.g. "Latin" + * script_conf is confidence level in the script + * Returns true on success and writes values to each parameter as an output + */ +bool TessBaseAPI::DetectOrientationScript(int* orient_deg, float* orient_conf, const char** script_name, float* script_conf) { OSResults osr; bool osd = DetectOS(&osr); @@ -1901,15 +1909,21 @@ bool TessBaseAPI::DetectOrientationScript(int& orient_deg, float& orient_conf, s int orient_id = osr.best_result.orientation_id; int script_id = osr.get_best_script(orient_id); - orient_conf = osr.best_result.oconfidence; - script_conf = osr.best_result.sconfidence; - const char* script_name = + if (orient_conf) + *orient_conf = osr.best_result.oconfidence; + if (orient_deg) + *orient_deg = orient_id * 90; // convert quadrant to degrees + + if (script_name) { + const char* script = osr.unicharset->get_script_from_script_id(script_id); - // clockwise orientation of the input image, in degrees - orient_deg = orient_id * 90; + *script_name = script; + } - script = script_name; + if (script_conf) + *script_conf = osr.best_result.sconfidence; + return true; } @@ -1921,10 +1935,10 @@ bool TessBaseAPI::DetectOrientationScript(int& orient_deg, float& orient_conf, s char* TessBaseAPI::GetOsdText(int page_number) { int orient_deg; float orient_conf; - std::string script_name; + const char* script_name; float script_conf; - if (!DetectOrientationScript(orient_deg, orient_conf, script_name, script_conf)) + if (!DetectOrientationScript(&orient_deg, &orient_conf, &script_name, &script_conf)) return NULL; // clockwise rotation needed to make the page upright @@ -1939,7 +1953,7 @@ char* TessBaseAPI::GetOsdText(int page_number) { "Orientation confidence: %.2f\n" "Script: %s\n" "Script confidence: %.2f\n", - page_number, orient_deg, rotate, orient_conf, script_name.c_str(), + page_number, orient_deg, rotate, orient_conf, script_name, script_conf); return osd_buf; diff --git a/api/baseapi.h b/api/baseapi.h index 14161409c..b7e9ba136 100644 --- a/api/baseapi.h +++ b/api/baseapi.h @@ -26,7 +26,6 @@ (patch)) #include -#include // To avoid collision with other typenames include the ABSOLUTE MINIMUM // complexity of includes here. Use forward declarations wherever possible // and hide includes of complex types in baseapi.cpp. @@ -621,12 +620,13 @@ class TESS_API TessBaseAPI { /** * Detect the orientation of the input image and apparent script (alphabet). - * orient_deg is the detected clockwise rotation of the input image - * orient_conf is the confidence (15.0 is reasonable) - * script is an ASCII string, the name of the script, e.g. "Latin" + * orient_deg is the detected clockwise rotation of the input image in degrees (0, 90, 180, 270) + * orient_conf is the confidence (15.0 is reasonably confident) + * script_name is an ASCII string, the name of the script, e.g. "Latin" * script_conf is confidence level in the script + * Returns true on success and writes values to each parameter as an output */ - bool DetectOrientationScript(int& orient_deg, float& orient_conf, std::string& script, float& script_conf); + bool DetectOrientationScript(int* orient_deg, float* orient_conf, const char** script_name, float* script_conf); /** * The recognized text is returned as a char* which is coded diff --git a/api/capi.cpp b/api/capi.cpp index 7e1fe3665..57bed872d 100644 --- a/api/capi.cpp +++ b/api/capi.cpp @@ -541,32 +541,12 @@ TESS_API BOOL TESS_CALL TessBaseAPIDetectOS(TessBaseAPI* handle, OSResults* resu return FALSE; // Unsafe ABI, return FALSE always } -TESS_API BOOL TESS_CALL TessBaseAPIDetectOrientationScript(TessBaseAPI* handle, char** best_script_name, - int* best_orientation_deg, float* script_confidence, - float* orientation_confidence) +TESS_API BOOL TESS_CALL TessBaseAPIDetectOrientationScript(TessBaseAPI* handle, + int* orient_deg, float* orient_conf, const char** script_name, float* script_conf) { - int orient_deg; - float orient_conf; - std::string script_name; - float script_conf; - BOOL success; - + bool success; success = handle->DetectOrientationScript(orient_deg, orient_conf, script_name, script_conf); - if (!success) - return FALSE; - if (best_script_name) { - *best_script_name = new char [script_name.length() + 1]; - strcpy(*best_script_name, script_name.c_str()); - } - - if (best_orientation_deg) - *best_orientation_deg = orient_deg; - if (script_confidence) - *script_confidence = script_conf; - if (orientation_confidence) - *orientation_confidence = orient_conf; - - return TRUE; + return (BOOL)success; } diff --git a/api/capi.h b/api/capi.h index 95704ecba..9a8edd917 100644 --- a/api/capi.h +++ b/api/capi.h @@ -287,9 +287,8 @@ TESS_API void TESS_CALL TessBaseAPISetProbabilityInContextFunc(TessBaseAPI* han TESS_API void TESS_CALL TessBaseAPISetFillLatticeFunc(TessBaseAPI* handle, TessFillLatticeFunc f); // Call TessDeleteText(*best_script_name) to free memory allocated by this function -TESS_API BOOL TESS_CALL TessBaseAPIDetectOrientationScript(TessBaseAPI* handle, char** best_script_name, - int* best_orientation_deg, float* script_confidence, - float* orientation_confidence); +TESS_API BOOL TESS_CALL TessBaseAPIDetectOrientationScript(TessBaseAPI* handle, + int* orient_deg, float* orient_conf, const char **script_name, float* script_conf); TESS_API void TESS_CALL TessBaseAPIGetFeaturesForBlob(TessBaseAPI* handle, TBLOB* blob, INT_FEATURE_STRUCT* int_features, int* num_features, int* FeatureOutlineIndex);