PythonBoy
(PythonBoy)
May 11, 2022, 10:11am
#1
Hello everyone, Just was creating a program where it identifies the number and print’s it. First the program takes the screenshot then crops it to the size of the number then resizes the number and is then made to identify. But this does not work. I don’t know but maybe the image is not clear.
Image:
The code:
import pyautogui
import cv2
import pytesseract
import time
import pyttsx3
H = pyttsx3.init()
pytesseract.pytesseract.tesseract_cmd = 'C:\\Users\\----\\AppData\\Local\\Tesseract-OCR\\tesseract.exe'
ReadScreenshot = cv2.imread('Screenshot.png')
Number1 = ReadScreenshot[563:583,52:71]
ResizedImage = cv2.resize(Number1, (50,50))
cv2.imshow('Test',ResizedImage)
cv2.waitKey()
cv2.destroyAllWindows()
Text = pytesseract.image_to_string(ResizedImage)
print(Text)
The code just shows the image but it does not print out the number (7)
You have print(Text)
so it prints something. What does it print?
PythonBoy
(PythonBoy)
May 11, 2022, 11:52am
#3
Yeah so let me show you that.
I don’t why but it leaves some weird space in between. It does print any number or it does not even give an error
PythonBoy
(PythonBoy)
May 11, 2022, 11:56am
#4
I guess it something to do with the image. Maybe pytesseract is not able to indetify it at 7
rob42
(Rob)
May 11, 2022, 12:07pm
#5
Should this:
ReadScreenshot = cv2.imread('Screenshot.png')
… not be:
ReadScreenshot = cv2.imread(r'/<path>/Screenshot.png')
PythonBoy
(PythonBoy)
May 11, 2022, 12:12pm
#6
Nope, Actually I forgot to show you the location of the screenshot
Here is the location:
It is along with other python project files
rob42
(Rob)
May 11, 2022, 12:24pm
#7
Fair enough.
I was just looking at this: pytesseract · PyPI
… and every example I can see, uses the r
flag which I don’t see in your post.
You’ve tried it, right?
PythonBoy
(PythonBoy)
May 11, 2022, 12:29pm
#8
Ohh yeah…, that might be the problem. Sorry since I am still a beginner. Thanks for your help.
rob42
(Rob)
May 11, 2022, 2:22pm
#9
No worries. It’s all too easy to forget, which is why I have this in my file access notes:
import os.path
#---------------------------------------------------#
def process_file(filename, path=None):
if path is not None:
filename = os.path.join(path, filename)
return(filename)
#---------------------------------------------------#
path = '/home/rob/python'
file = 'some_file_name'
f = open((process_file(file, path)), 'r')
output = f.read()
print(output)
f.close()
Simply change the file =
and / or path =
to something sane.
The flag can be changed to ‘w’ or ‘a’ or ‘a+’ or whatever is needed to match the method.
Note: that function may have come from this Forum. My thanks to the OP if that’s the case.
PythonBoy
(PythonBoy)
May 12, 2022, 5:26am
#10
Hey I don’t know if what I did was correct but its returning an error
This is the code:
import pyautogui
import cv2
import pytesseract
import time
import pyttsx3
H = pyttsx3.init()
pytesseract.pytesseract.tesseract_cmd = 'C:\\Users\\bhuvan_bhoomika\\AppData\\Local\\Tesseract-OCR\\tesseract.exe'
ReadScreenshot = cv2.imread(r'/Pyautoimages/Screenshot.png')
Number1 = ReadScreenshot[563:583,52:71]
ResizedImage = cv2.resize(Number1, (50,50))
cv2.imshow('Test',ResizedImage)
cv2.waitKey()
cv2.destroyAllWindows()
Text = pytesseract.image_to_string(ResizedImage)
print(Text)
It waits and gives the error
rob42
(Rob)
May 12, 2022, 9:24am
#11
Clearly it’s the Number1
variable that’s the issue (but you know that) so the list [563:583,52:71]
can’t be correct.
I’ve never used pyautogui
but again, from that link:
By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,
we need to convert from BGR to RGB format/mode:
So, I’d be looking into that if I were you.
Sorry I can’t be of any more help than that.
1 Like
PythonBoy
(PythonBoy)
May 12, 2022, 11:57am
#12
Actually really thanks for this documentation. Seems like this was the error. Thank you very much for your help!
1 Like