EasyOCR icon indicating copy to clipboard operation
EasyOCR copied to clipboard

EasyOCR (1.7.0: `-215:Assertion failed) !ssize.empty()` cv2 Error

Open Casyfill opened this issue 2 years ago • 15 comments

Platform:

  • MacOS Ventura 13.4 (22F66)

Versions:

  • EasyOCR 1.7.0
  • OpenCV 4.7.0

Code (jupyter):

READER = easyocr.Reader(['en'])

path = './1470646.jpg'
result = READER.readtext(path)

Results in this Error:

File ~/Library/Caches/pypoetry/virtualenvs/floorplans-ocr-TIa0fr6E-py3.8/lib/python3.8/site-packages/easyocr/easyocr.py:383, in Reader.recognize(self, img_cv_grey, horizontal_list, free_list, decoder, beamWidth, batch_size, workers, allowlist, blocklist, detail, rotation_info, paragraph, contrast_ths, adjust_contrast, filter_ths, y_ths, x_ths, reformat, output_format)
    381 h_list = [bbox]
    382 f_list = []
--> 383 image_list, max_width = get_image_list(h_list, f_list, img_cv_grey, model_height = imgH)
    384 result0 = get_text(self.character, imgH, int(max_width), self.recognizer, self.converter, image_list,\
    385               ignore_char, decoder, beamWidth, batch_size, contrast_ths, adjust_contrast, filter_ths,\
    386               workers, self.device)
    387 result += result0

File ~/Library/Caches/pypoetry/virtualenvs/floorplans-ocr-TIa0fr6E-py3.8/lib/python3.8/site-packages/easyocr/utils.py:613, in get_image_list(horizontal_list, free_list, img, model_height, sort_output)
    611     pass
    612 else:
--> 613     crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height)
    614     image_list.append( ( [[x_min,y_min],[x_max,y_min],[x_max,y_max],[x_min,y_max]] ,crop_img) )
    615     max_ratio_hori = max(ratio, max_ratio_hori)

File ~/Library/Caches/pypoetry/virtualenvs/floorplans-ocr-TIa0fr6E-py3.8/lib/python3.8/site-packages/easyocr/utils.py:574, in compute_ratio_and_resize(img, width, height, model_height)
    572 if ratio<1.0:
    573     ratio = calculate_ratio(width,height)
--> 574     img = cv2.resize(img,(model_height,int(model_height*ratio)), interpolation=Image.ANTIALIAS)
    575 else:
    576     img = cv2.resize(img,(int(model_height*ratio),model_height),interpolation=Image.ANTIALIAS)

error: OpenCV(4.7.0) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/resize.cpp:4062: error: (-215:Assertion failed) !ssize.empty() in function 'resize'

Image itself is fine and both PIL and cv2 can read it and shows correct img.shape. I copy-pasted the compute_ratio_and_resize function and ran it on the image as a whole and all went good, so I assume there is an issue with how it's been used

PS. I downgraded to 1.6.2 (which lead my system to downgrade opencv-python-headless from 4.7.0.72 to 4.5.4.60) and the same exact code works

Casyfill avatar May 26 '23 21:05 Casyfill

here is the image example (albeit I have similar issue with other images in the batch): link

Casyfill avatar May 26 '23 21:05 Casyfill

did your version of pytorch changed?

deadworldisee avatar Jun 08 '23 12:06 deadworldisee

I don't believe it did. Poetry only reported the two changes I noted above.

Casyfill avatar Jun 08 '23 15:06 Casyfill

I'm also experiencing this issue, both on 1.6.2 and 1.7.0.

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
File [~/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py:456](https://file+.vscode-resource.vscode-cdn.net/home/ian/dev/personal/~/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py:456), in Reader.readtext(self, image, decoder, beamWidth, batch_size, workers, allowlist, blocklist, detail, rotation_info, paragraph, min_size, contrast_ths, adjust_contrast, filter_ths, text_threshold, low_text, link_threshold, canvas_size, mag_ratio, slope_ths, ycenter_ths, height_ths, width_ths, y_ths, x_ths, add_margin, threshold, bbox_min_score, bbox_min_size, max_candidates, output_format)
    [454](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=453) # get the 1st result from hor & free list as self.detect returns a list of depth 3
    [455](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=454) horizontal_list, free_list = horizontal_list[0], free_list[0]
--> [456](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=455) result = self.recognize(img_cv_grey, horizontal_list, free_list,\
    [457](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=456)                         decoder, beamWidth, batch_size,\
    [458](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=457)                         workers, allowlist, blocklist, detail, rotation_info,\
    [459](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=458)                         paragraph, contrast_ths, adjust_contrast,\
    [460](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=459)                         filter_ths, y_ths, x_ths, False, output_format)
    [462](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=461) return result

File [~/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py:379](https://file+.vscode-resource.vscode-cdn.net/home/ian/dev/personal/~/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py:379), in Reader.recognize(self, img_cv_grey, horizontal_list, free_list, decoder, beamWidth, batch_size, workers, allowlist, blocklist, detail, rotation_info, paragraph, contrast_ths, adjust_contrast, filter_ths, y_ths, x_ths, reformat, output_format)
    [377](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=376) h_list = [bbox]
    [378](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=377) f_list = []
--> [379](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=378) image_list, max_width = get_image_list(h_list, f_list, img_cv_grey, model_height = imgH)
    [380](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=379) result0 = get_text(self.character, imgH, int(max_width), self.recognizer, self.converter, image_list,\
    [381](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=380)               ignore_char, decoder, beamWidth, batch_size, contrast_ths, adjust_contrast, filter_ths,\
    [382](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=381)               workers, self.device)
    [383](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/easyocr.py?line=382) result += result0

File [~/miniconda3/lib/python3.10/site-packages/easyocr/utils.py:571](https://file+.vscode-resource.vscode-cdn.net/home/ian/dev/personal/~/miniconda3/lib/python3.10/site-packages/easyocr/utils.py:571), in get_image_list(horizontal_list, free_list, img, model_height, sort_output)
    [569](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=568)     pass
    [570](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=569) else:
--> [571](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=570)     crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height)
    [572](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=571)     image_list.append( ( [[x_min,y_min],[x_max,y_min],[x_max,y_max],[x_min,y_max]] ,crop_img) )
    [573](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=572)     max_ratio_hori = max(ratio, max_ratio_hori)

File [~/miniconda3/lib/python3.10/site-packages/easyocr/utils.py:532](https://file+.vscode-resource.vscode-cdn.net/home/ian/dev/personal/~/miniconda3/lib/python3.10/site-packages/easyocr/utils.py:532), in compute_ratio_and_resize(img, width, height, model_height)
    [530](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=529) if ratio<1.0:
    [531](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=530)     ratio = calculate_ratio(width,height)
--> [532](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=531)     img = cv2.resize(img,(model_height,int(model_height*ratio)), interpolation=Image.ANTIALIAS)
    [533](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=532) else:
    [534](file:///home/ian/miniconda3/lib/python3.10/site-packages/easyocr/utils.py?line=533)     img = cv2.resize(img,(int(model_height*ratio),model_height),interpolation=Image.ANTIALIAS)

error: OpenCV(4.5.4) [/tmp/pip-req-build-4x5kub8r/opencv/modules/imgproc/src/resize.cpp:4051](https://file+.vscode-resource.vscode-cdn.net/tmp/pip-req-build-4x5kub8r/opencv/modules/imgproc/src/resize.cpp:4051): error: (-215:Assertion failed) !ssize.empty() in function 'resize'

specialprocedures avatar Jun 16 '23 13:06 specialprocedures

same here. I just installed pip install easyocr and then tried this simple example:

import easyocr

def recognize(path):
    reader = easyocr.Reader(['en']);
    result = reader.readtext('example.jpg')
    print(result)

Would be nice to have some help here.

BeastLe9enD avatar Jul 09 '23 11:07 BeastLe9enD

EasyOCR version : 1.6.2

We are calling easyocr library from python file(where we try to perform text masking), passing image ndarray as argument to readtext() of easyocr library.

And, its failing with below error,

detection_result = self.easyocr_reader.readtext(I)<EOL> File "C:\Python38\lib\site-packages\easyocr\easyocr.py", line 458, in readtext<EOL> result = self.recognize(img_cv_grey, horizontal_list, free_list,<EOL> File "C:\Python38\lib\site-packages\easyocr\easyocr.py", line 379, in recognize<EOL> image_list, max_width = get_image_list(h_list, f_list, img_cv_grey, model_height = imgH)<EOL> File "C:\Python38\lib\site-packages\easyocr\utils.py", line 571, in get_image_list<EOL> crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height)<EOL> File "C:\Python38\lib\site-packages\easyocr\utils.py", line 532, in compute_ratio_and_resize<EOL> img = cv2.resize(img,(model_height,int(model_height*ratio)), interpolation=Image.ANTIALIAS)<EOL>cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'<EOL><EOL>"

During debug, could see that crop_img is prepared based on horizontal list which is sent as arg to get_image_list(), and this crop_img is sent as empty array to cv2.resize() method of opencv python package, which is actually causing this error. [Attached the screenshot of how crop_img is populated]

crop_img

Dicom Image Specifications:

Transfer Syntax ID : 1.2.840.10008.1.2 (Implicit VR Endian) (0028,0004) Photometric Interpretation MONOCHROME2

Please help on this.

DivyaPalanisamy001 avatar Aug 22 '23 09:08 DivyaPalanisamy001

I noticed that in utils.py, the 'calculate_ratio' function sometimes delivers a negative ratio when the 'img' parameter is an empty ndarray (like '[]'). I think I can just set the ratio to 1 or something when it dips below 0, but I'm not sure the intended behavior here.

ozyhuboi avatar Sep 20 '23 20:09 ozyhuboi

And this is possibly related to this error from openCV: https://stackoverflow.com/questions/31996367/opencv-resize-fails-on-large-image-with-error-215-ssize-area-0-in-funct

ozyhuboi avatar Sep 20 '23 21:09 ozyhuboi

Or you could actually be processing a null img, which you shouldn't do

ozyhuboi avatar Sep 20 '23 21:09 ozyhuboi

Hi @ozyhuboi , Thank you for the response.

In my case, my input dicom image is 16-bit signed integer(Pixel Representation = 1 and bits Allocated = 16), when it is directly passed to easyocr, it is failing to handle signed int pixel data.

Hence, crop_img is prepared based on horizontal list which is sent as arg to get_image_list(), and this crop_img is sent as empty array to cv2.resize() method of opencv python package, which is actually causing this error. [Attached the screenshot of how crop_img is populated]

So, I tried to convert the 16bit signed int to 8bit unsigned int and passed the image to easyocr. Now able to get the detection results.

Is there any limitation that easyocr cannot process 16bit signed int type pixel data?

Please clarify

DivyaPalanisamy001 avatar Sep 21 '23 05:09 DivyaPalanisamy001

So these "images" are ndarray objects and are treated as such. Are you having an issue with the conversion? My biggest issue might be the fact that I'm not sure the intended behavior of this ratio is. A change that appears to be working for me is to check if the "image" is coming in as empty and skip, which seems to me is intended behavior.

ozyhuboi avatar Sep 21 '23 12:09 ozyhuboi

Right now, no issues with the conversion,

We are trying to do something like below,

if(bits_allocated == 16): I = (I - np.min(I)) / (np.max(I) - np.min(I)) * 255 I = I.astype(np.uint8)

After conversion, we are passing it to easyocr, easyocr_reader.readtext(I) to get the detection results.

In this way, its working fine for 16bit dicom images as well.

DivyaPalanisamy001 avatar Sep 21 '23 15:09 DivyaPalanisamy001

HI everyone,

I also received the same error, and for me the following was the cause:

  • The image was taken with a mobile phone, held in hand vertically, but when the phone saved the JPG, it only indicated the correct orientation in the EXIF metadata.. the image itself was not actually rotated. So if I simply loaded the file into Gimp (the free image editor) then it automatically offered to rotate the image based on the EXIF data. After saving it, the EasyOCR worked as expected.

I hope it helps.

bszollosinagy avatar Dec 09 '23 15:12 bszollosinagy