0

Previously I was using pytesseract and it was too slow for me so I switched to paddleOCR. Then I find out that PaddleOCR can't seem to recognize PIL Image in RAM Memory. Then I tried to turn it into lists, ndarrays and so on, everything that paddleOCR supports and it turns out that paddleOCR can't seem to recognize it. (Extensive research was done, but none solves the problem)

from paddleocr import PaddleOCR
from PIL import ImageGrab
import numpy as np

ocr_model = PaddleOCR(lang="en")
pic = np.array(ImageGrab.grab(bbox=[0, 0, 1000, 1000]))
result = ocr_model.ocr(pic)

print(result)

I tried the code above(expecting it to recognize the words on the screen) but it presented a ValueError(Every type of solution(failed) I tried shows basically the same error):

Traceback (most recent call last):
  File "/Users/apple/Desktop/Testing Grounds.py", line 379, in <module>
    result = ocr_model.ocr(pic)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paddleocr/paddleocr.py", line 555, in ocr
    dt_boxes, rec_res, _ = self.__call__(img, cls)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paddleocr/tools/infer/predict_system.py", line 71, in __call__
    dt_boxes, elapse = self.text_detector(img)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paddleocr/tools/infer/predict_det.py", line 227, in __call__
    data = transform(data, self.preprocess_op)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paddleocr/ppocr/data/imaug/__init__.py", line 56, in transform
    data = op(data)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paddleocr/ppocr/data/imaug/operators.py", line 94, in __call__
    img.astype('float32') * self.scale - self.mean) / self.std
ValueError: operands could not be broadcast together with shapes (960,960,4) (1,1,3) 
pockspocky
  • 38
  • 3

1 Answers1

0

from PIL import Image
from numpy import asarray
from paddleocr import PaddleOCR

ocr_model = PaddleOCR(lang="en")

img = Image.open(imagepath)

cropped_img = img.crop((xmin,ymin,xmax,ymax))

numpydata = asarray(cropped_img)

result = ocr.ocr(numpydata, cls=True)