Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OCRBeamSearchDecoder bug: a risk of assigning value to NULL. #3790

Merged
merged 1 commit into from
Sep 18, 2024
Merged

Fix OCRBeamSearchDecoder bug: a risk of assigning value to NULL. #3790

merged 1 commit into from
Sep 18, 2024

Conversation

masterAllen
Copy link
Contributor

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Details

In text module, class OCRBeamSearchDecoder has some run methods:
image

These methods will execute OCRBeamSearchDecoderImpl::run:

    void run( Mat& src,
              string& out_sequence,
              vector<Rect>* component_rects,
              vector<string>* component_texts,
              vector<float>* component_confidences,
              int component_level) CV_OVERRIDE

However, some parameters can be NULL, but this method does not check for this when assigning values to them at the end of the method.

        // check NULL
        if (component_rects != NULL)
            component_rects->clear();
        if (component_texts != NULL)
            component_texts->clear();
        if (component_confidences != NULL)
            component_confidences->clear();

        // ....

        // not check, RISK
        component_rects->push_back(Rect(0,0,src.cols,src.rows));
        component_texts->push_back(out_sequence);
        component_confidences->push_back((float)exp(lp));

Actually, some OCRBeamSearchDecoder::run methods must fail because they indeed pass one parameter as NULL when executing the upper method:

CV_WRAP String OCRBeamSearchDecoder::run(InputArray image, int min_confidence, int component_level)
{
    std::string output1;
    std::string output2;
    vector<string> component_texts;
    vector<float> component_confidences;
    Mat image_m = image.getMat();
    // NULL as a parameter
    run(image_m, output1, NULL, &component_texts, &component_confidences, component_level);
    // ...
}

What's worse, python can only some warp methods which execute upper methods . Using OCRBeamSearch in python must fail.
image
image

The following code must throw error: Unknown C++ exception from OpenCV code.

import cv2
chartables = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

classifier = cv2.text.loadOCRBeamSearchClassifierCNN('OCRBeamSearch_CNN_model_data.xml.gz')

fs = cv2.FileStorage('OCRHMM_transitions_table.xml',cv2.FILE_STORAGE_READ)
transition_p = fs.getNode('transition_probabilities').mat()
emission_p = np.eye(len(chartables), len(chartables)).astype(float)

decoder = cv2.text.OCRBeamSearchDecoder.create(
    classifier, 
    vocabulary=chartables, 
    transition_probabilities_table=transition_p,
    emission_probabilities_table=emission_p
)
src = cv2.imread(f'scenetext_word01.jpg', -1)
src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# Error: Unknown C++ exception from OpenCV code
text = decoder.run(src, min_confidence=0)

Sorry, something went wrong.

@asmorkalov asmorkalov self-assigned this Sep 17, 2024
@asmorkalov asmorkalov merged commit 80f1ca2 into opencv:4.x Sep 18, 2024
10 of 11 checks passed
@asmorkalov asmorkalov mentioned this pull request Sep 20, 2024
@masterAllen masterAllen deleted the fix_OCRBeamSearchDecoder branch September 21, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants