I want to pre-process this image for OCR. I want it to be able to read all the text from it, as you can see, the text has different colors. I'm using HSV ranges to detect the colors I want and I just turn everything into white so it's easier for the OCR software to translate the image into text. With my current settings it's reading all the text correctly except the one that it's surrounded by the light green background, I believe the issue is my HSV values, how could I adjust it so it reads all the text?
This is my preprocessing code:
def pre_process_img(img):
"""
Prepares image for OCR
:param img: PIL.Image
Image object
:return: output_img
"""
open_cv_image = np.array(img)
# Convert image to HSV in order to be able to extract what we care about using masks
img_hsv = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2HSV)
# Init mask
mask = 0
for color_name, data in ocr.COLORS.items():
if color_name not in ['white', 'dark_red', 'yellow']:
continue
lower = np.array(data['lower'])
upper = np.array(data['upper'])
# Join masks
mask += cv2.inRange(img_hsv, lower, upper)
# Set my output img to zero everywhere except my mask
# output_img = open_cv_image.copy()
img_hsv[np.where(mask == 0)] = 0
# Change image to white where we found the color we want
img_hsv[mask > 0] = (255, 255, 255)
# cv2.imshow("out", img_hsv)
# cv2.waitKey(0)
return img_hsv
My HSV ranges
COLORS = {
'dark_red': {
'lower': [0, 180, 100],
'upper': [5, 240, 200]
},
'red': {
'lower': [0, 150, 100],
'upper': [10, 255, 255]
},
'white': {
'lower': [0, 0, 168],
'upper': [172, 111, 255]
},
'blue': {
'lower': [100, 150, 0],
'upper': [140, 255, 255]
},
'grey': {
'lower': [36, 50, 70],
'upper': [89, 255, 255]
},
'purple': {
'lower': [129, 50, 70],
'upper': [158, 255, 255]
},
'green': {
'lower': [36, 50, 70],
'upper': [89, 255, 255]
},
'yellow': {
'lower': [25, 50, 70],
'upper': [60, 255, 255]
},
'orange': {
'lower': [5, 50, 50],
'upper': [15, 180, 230]
}
}
I know it looks like I don't need all the colors but there are specific examples that use all the colors.
This is the image
My image to text function in case it's needed
def image_to_text(img):
"""
:param img: PIL.Image
Image object
:return: text: str
Generated text
"""
# Tesseract path
pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
img = pre_process_img(img)
config = '--oem 3 --psm %d' % 6
text = image_to_string(img, config=config, lang='eng')
# Dirty text
# print(text)
return text
My current output is
You have decided to bug § DaddyEredin's house tonight.
10 Pagond has left the game.
9 viagra has left the game.
Amember of the mafia visited 8 Blesser last night.
Amember of the mafia visited 14 jesus last night.
Amember of the mafia visited 7 Someone Especial last night.
My desired output is
You have decided to bug § DaddyEredin's house tonight.
10 Pagond has left the game.
9 viagra has left the game.
You were attacked but someone nursed you back to health
Amember of the mafia visited 8 Blesser last night.
Amember of the mafia visited 14 jesus last night.
Amember of the mafia visited 7 Someone Especial last night.
from Which HSV range should I use to get rid of the light green color in this picture?

No comments:
Post a Comment