While playing the game, a bot control appears at certain intervals as follows.
I created 5 different regions using the OpenCV and Python-tesseract libraries in Python. It scans the mathematical operation or text written in yellow in the main region and prints it to the console. I got this part done. But it doesn't see numbers or texts in 4 different regions found for answers.
I also tried to find it with different color settings, but I didn't succeed. I tried scanning the color codes in different ways. I tried to find the Regions by editing them.
My aim is to understand the question correctly in the verification processes that I come across in this way, then to click on the part with the correct answer with the mouse.
What do you suggest? Below is my code sample.
import cv2
import pytesseract
import pyautogui
import numpy as np
import re
def find_text_in_region(region, color_range):
# Ekran görüntüsünü alın
screen = pyautogui.screenshot()
image = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2BGR)
# Bölgeyi sınırlayıcı dikdörtgeni çizin
x, y, w, h = region
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Bölgedeki metni alın
region_image = image[y:y + h, x:x + w]
region_hsv = cv2.cvtColor(region_image, cv2.COLOR_BGR2HSV)
lower_color = np.array([color_range[0], 100, 100])
upper_color = np.array([color_range[1], 255, 255])
mask = cv2.inRange(region_hsv, lower_color, upper_color)
text = pytesseract.image_to_string(mask)
return text
def process_math_expression(expression):
# İfadedeki boşlukları kaldırın
expression = expression.replace(" ", "")
try:
result = eval(expression)
return result
except Exception as e:
print("Matematiksel ifade hesaplanamadı:", expression)
print("Hata:", str(e))
return None
def search_answers_in_regions(regions):
answers = []
for region_index, region in enumerate(regions, start=1):
# Ekran görüntüsünü alın
screen = pyautogui.screenshot()
image = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2BGR)
# Bölgeyi sınırlayıcı dikdörtgeni çizin
x, y, w, h = region
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Bölgedeki metni alın
region_image = image[y:y + h, x:x + w]
region_hsv = cv2.cvtColor(region_image, cv2.COLOR_BGR2HSV)
lower_color = np.array([20, 100, 100])
upper_color = np.array([30, 255, 255])
mask = cv2.inRange(region_hsv, lower_color, upper_color)
text = pytesseract.image_to_string(mask)
# Metni konsola yazdır
print(f"Region {region_index} metni: {text}")
# Metni cevaplar listesine ekleyin
answers.append(text)
return answers
def main():
# Ana bölge ve cevap bölgelerini tanımlayın
main_region = (1007, 473, 150, 40)
answer_regions = [
(957, 511, 246, 18), # Region 1
(957, 543, 246, 18), # Region 2
(957, 575, 246, 18), # Region 3
(957, 607, 246, 18) # Region 4
]
# Ana bölgedeki matematik ifadesini veya metni bulun
text = find_text_in_region(main_region, (20, 30))
print("Matematiksel ifade veya metin:", text)
# Matematik ifadesini hesaplayın
result = process_math_expression(text)
if result is not None:
print("Sonuç:", result)
# Cevap bölgelerindeki metinleri veya sayıları bulun
answers = search_answers_in_regions(answer_regions)
# Cevapları konsola yazdırın
for region_index, answer in enumerate(answers, start=1):
print(f"Cevap Region {region_index}: {answer}")
if __name__ == '__main__':
main()