A
A
Andrii Programmer2021-06-26 00:02:35
Python
Andrii Programmer, 2021-06-26 00:02:35

Tesseract text color recognition?

I am writing a program that should sort text in real time by color

like this:

it is shown the text, if it is red, it writes it to a variable, if it is different (only green and red should beat there), then the program stupidly forgets about it ... I
use tesseract opencv

PS in this code a bunch of codes from the Internet)

in short, here is my code:

import pyautogui
import cv2
import mss
import numpy
import time
import pytesseract

def screen_r():
  mon = {"top": 500, "left": 550, "width": 500, "height": 300 }

  title = "lol"
  fps = 0
  sct = mss.mss()
  last_time = time.time()

  while True:
    img = numpy.asarray(sct.grab(mon))
    fps += 1
    balance = pytesseract.image_to_string(img, config='outputbase digits')
    print(balance)


    cv2.imshow(title, img)
    if cv2.waitKey(25) & 0xff == ord("q"):
      cv2.destroyAllWindows()
      break

  return fps

print("mss:", screen_r())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-06-27
@fox_12

Look at the predominant color in the RGB channel.
Here is an example on the knee:

import os
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
import cv2
import numpy
import pytesseract

os.environ['TESSDATA_PREFIX'] = r'C:/Program Files (x86)/Tesseract-OCR/tessdata'

TESS_PATH = r"C:/Program Files (x86)/Tesseract-OCR/tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = TESS_PATH

IMAGE1 = '.\img\Screenshot_9.png'
IMAGE2 = '.\img\Screenshot_10.png'
IMAGE3 = '.\img\Screenshot_11.png'
IMAGE4 = '.\img\Screenshot_12.png'

def detect_color(color):
    THRES = 10
    if bool(color[2] - color[1] > THRES and color[2] - color[0] > THRES):
        return 'red'
    if bool(color[1] - color[2] > THRES and color[1] - color[0] > THRES):
        return 'green'
    if bool(color[0] - color[1] > THRES and color[0] - color[2] > THRES):
        return 'blue'
    return 'unknown'

plt.rcParams['figure.figsize'] = [15, 7]
f1,ax = plt.subplots(4,2)

for i,im in enumerate([IMAGE1, IMAGE2, IMAGE3, IMAGE4]):
    image = cv2.imread(im)
    average = image.mean(axis=0).mean(axis=0)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    (thresh, bw) = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    print(pytesseract.image_to_string(bw).strip().ljust(60), detect_color(average))
    ax[i][0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    fillcolor = numpy.full((3, 3, 3), average, dtype=numpy.ubyte)
    ax[i][1].imshow(cv2.cvtColor(numpy.full((3, 3, 3), average, dtype=numpy.ubyte), cv2.COLOR_BGR2RGB))

60d8b054eeb1a092435690.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question