r/pythonhelp • u/salbee2 • Feb 21 '20
Error with cv2 and OCR
ok so I've been trying to take an image, edit it in open cv, create a temporary file to apply ocr, then print the text outputted. I keep getting
Expected Ptr<cv::UMat> for argument '%s at the line with cv2.write.
This is the code
import pytesseract
import cv2
import os
from PIL import Image
# load the example image and convert it to grayscale
gray = cv2.imread("download.jpeg", cv2.COLOR_BGR2GRAY)
gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY)
#gray = cv2.medianBlur(gray, 3)
# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "{}.png".format(os.getpid())
dirname= "test"
os.mkdir(dirname)
cv2.imwrite(os.path.join(dirname, filename), gray)
# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
os.remove(dirname)
print(text)
# show the output images
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
2
u/truh Feb 21 '20
There is a good chance that an IDE like Pycharm would show you the problem immediately.
My guess is you need to imwrite gray[1] instead of gray.