Home > Back-end >  how to compare live screen text in cv2 python
how to compare live screen text in cv2 python

Time:04-19

I want to do a program that ask to the user for a topic, then the program captures the screen, and if the topic appers in the screen do a condition.

import cv2
import pytesseract
import numpy as np
from PIL import ImageGrab
import time

pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
# img = cv2.imread('1.png')
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
def captureScreen(bbox=(300,300,1500,1000)):
    capScr = np.array(ImageGrab.grab(bbox))
    capScr = cv2.cvtColor(capScr, cv2.COLOR_RGB2BGR)
    return capScr
while True:
    timer = cv2.getTickCount()
    _,img = cap.read()
    img = captureScreen()
    #DETECTING CHARACTERES
    hImg, wImg,_ = img.shape
            
    boxes = pytesseract.image_to_data(img)
    for a,b in enumerate(boxes.splitlines()):
        print(b)
        if a!=0:
            b = b.split()
            if len(b)==12:
                x,y,w,h = int(b[6]),int(b[7]),int(b[8]),int(b[9])
                cv2.putText(img,b[11],(x,y-5),cv2.FONT_HERSHEY_SIMPLEX,1,(50,50,255),2)
                cv2.rectangle(img, (x,y), (x w, y h), (50, 50, 255), 2)
        cv2.imshow("Result",img)
        cv2.waitKey(1)

All that code search for text in the screen and can detect words well, but i don't know how I could compare that text to the user text.

CodePudding user response:

You can use input("input:") to get the user input and then you can simply compare those two texts using an if statement like so:

if input("input:") == b[11]:
   print("both texts are the same")
else: print("both texts are not equal")
  • Related