I'm a complete novice at all of this (including Python), but I need it for a problem I'm working on. I've annotated my code so hopefully it's easy to see what I've done. I'm trying to read a section of my screen in realtime, with whatever text is detected by the OCR being compared to all of the if/else statements. When it matches, I want it to print a string. Currently, the realtime OCR detection is working, but it doesn't go through the if/else statements to print the correct strings. I know this as print(tesstr) gives correct outputs but the current code doesn't produce any output.
# cv2.cvtColor takes a numpy ndarray as an argument
import numpy as nm
import pytesseract
# importing OpenCV
import cv2
from PIL import ImageGrab
def imToString():
while(True):
# ImageGrab-To capture the screen image in a loop.
# Bbox used to capture a specific area.
cap = ImageGrab.grab(bbox=(267, 225, 344, 257))
# Inverted the image for it to be easily
# read by the OCR and obtained the output String.
tesstr = pytesseract.image_to_string(
cv2.bitwise_not(nm.array(cap)),
lang='eng',
config = '--psm 7')
if (tesstr == '1'):
print('Ace')
elif (tesstr == '2'):
print('Queen')
elif (tesstr == '3'):
print('King')
...
# Calling the function
imToString()
Aucun commentaire:
Enregistrer un commentaire