Search code examples
pythonopencvmachine-learningcomputer-visioneasyocr

cv2.error: Unknown C++ exception from OpenCV code in easyOCR reader.readtext() function


I have tried to read the text using easyOCR in the number plate detection model.but some times there is a error called

  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\dash\dash.py", line 1344, in dispatch
    response.set_data(func(*args, outputs_list=outputs_list))
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\dash\_callback.py", line 151, in add_context
    output_value = func(*func_args, **func_kwargs)  # %% callback invoked %%
  File "E:\Git Projects\simple-flask-webApp\index.py", line 305, in update_output
    return [[parse_contents(list_of_contents, list_of_names)], [parse_image(list_of_contents)]]
  File "E:\Git Projects\simple-flask-webApp\index.py", line 122, in parse_image
    numberplate = number_plate(original_img)
  File "E:\Git Projects\simple-flask-webApp\index.py", line 289, in number_plate
    ocr_result = reader.readtext(region)
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\easyocr\easyocr.py", line 385, in readtext
    horizontal_list, free_list = self.detect(img, min_size, text_threshold,\
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\easyocr\easyocr.py", line 275, in detect
    text_box_list = get_textbox(self.detector, img, canvas_size, mag_ratio,
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\easyocr\detection.py", line 95, in get_textbox
    bboxes_list, polys_list = test_net(canvas_size, mag_ratio, detector,
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\easyocr\detection.py", line 55, in test_net
    boxes, polys, mapper = getDetBoxes(
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\easyocr\craft_utils.py", line 236, in getDetBoxes
    boxes, labels, mapper = getDetBoxes_core(textmap, linkmap, text_threshold, link_threshold, low_text, estimate_num_chars)
  File "C:\Users\MY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\easyocr\craft_utils.py", line 31, in getDetBoxes_core
    nLabels, labels, stats, centroids = cv2.connectedComponentsWithStats(text_score_comb.astype(np.uint8), connectivity=4)
cv2.error: Unknown C++ exception from OpenCV code

but sometimes it works properly. after running two or three times this error occurred.
This is the code that gives the error.

   for idx, box in enumerate(boxes):
        print(box)
        roi = box * [height, width, height, width]
        print(roi)
        region = image[int(roi[0]):int(roi[2]), int(roi[1]):int(roi[3])]
        reader = easyocr.Reader(['en'])
        ocr_result = reader.readtext(region)
        print(ocr_result)
        plt.imshow(cv2.cvtColor(region, cv2.COLOR_BGR2RGB))
        for result in ocr_result:
            print(np.sum(np.subtract(result[0][2],result[0][1])))
            print(result[1])

Looking for a solution to fix this issue.


Solution

  • Try an earlier version of opencv-python. I saw it elsewhere and it solved my constant error like yours. I think 4.5.4.60 worked.