0% found this document useful (0 votes)
5 views3 pages

New Text Document (2) 22

Uploaded by

pranashu Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

New Text Document (2) 22

Uploaded by

pranashu Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pyautogui

import cv2
import numpy as np
import pytesseract

# Take a screenshot of the game window


screenshot = pyautogui.screenshot()

# Convert the screenshot to a NumPy array


frame = np.array(screenshot)

# Convert the frame to BGR (OpenCV's default color space)


frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

# Define the yellow color range (adjust these values as needed)


yellow_lower = np.array([20, 100, 100])
yellow_upper = np.array([40, 255, 255])

# Convert the frame to HSV and threshold the yellow color range
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, yellow_lower, yellow_upper)

# Find contours of the yellow boxes


contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Set a minimum size for the yellow boxes (adjust these values as needed)
min_box_area = 1000 # minimum area of the box (in pixels)
min_box_width = 50 # minimum width of the box (in pixels)
min_box_height = 50 # minimum height of the box (in pixels)

# Define the grid dimensions


grid_x = 41
grid_y = 451
cell_width = 404
cell_height = 254

# Create a 4x3 grid to store the car levels


grid = []
for i in range(4):
row = []
for j in range(3):
x = grid_x + j * cell_width
y = grid_y - i * cell_height
row.append((x, y, cell_width, cell_height, None)) # add a fifth element to
store the car level
grid.append(row)

# Iterate through the contours and draw rectangles around the yellow boxes
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
area = w * h
if area > min_box_area and w > min_box_width and h > min_box_height:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Click on the yellow box to make the car appear


pyautogui.click(x+w/2, y+h/2)

# Wait for the car to appear


pyautogui.sleep(0.1)
# Take a new screenshot to capture the car
car_screenshot = pyautogui.screenshot(region=(x, y, w, h))

# Convert the screenshot to a NumPy array


car_frame = np.array(car_screenshot)

# Convert the frame to BGR (OpenCV's default color space)


car_frame = cv2.cvtColor(car_frame, cv2.COLOR_RGB2BGR)

# Iterate through the grid and detect the car levels


for row in grid:
for box in row:
bx, by, bw, bh, _ = box
# Check if the yellow box is within the grid box
if x >= bx and x <= bx + bw and y >= by and y <= by + bh:
# Extract the region of interest (ROI) at the bottom right
corner of the box
roi_x = max(0, bx + bw - 20)
roi_y = max(0, by + bh - 20)
roi_w = min(20, bw)
roi_h = min(20, bh)
roi = car_frame[roi_y:roi_y+roi_h, roi_x:roi_x+roi_w]

# Check if the ROI is not empty


if roi.size > 0:
# Convert the ROI to grayscale and apply thresholding to
improve OCR accuracy
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV
+ cv2.THRESH_OTSU)[1]

# Use OCR to extract the car level from the ROI


car_level = pytesseract.image_to_string(thresh, config='--
psm 11')

# If a car level is detected, store it in the grid


if car_level.strip() != "":
box[4] = int(car_level)

# Find duplicate car levels in the grid


car_levels = [box[4] for row in grid for box in row if box[4] is not None]
duplicates = [level for level in car_levels if car_levels.count(level) > 1]

# If duplicates are found, drag one car to another


for duplicate in duplicates:
# Find the boxes with the duplicate car level
boxes = [(i, j) for i, row in enumerate(grid) for j, box in enumerate(row) if
box[4] == duplicate]

# Drag one car to another


if len(boxes) > 1:
# Get the coordinates of the two boxes with the same level
x1, y1, _, _, _ = grid[boxes[0][0]][boxes[0][1]]
x2, y2, _, _, _ = grid[boxes[1][0]][boxes[1][1]]

# Move to the first car


pyautogui.moveTo(x1 + cell_width / 2, y1 + cell_height / 2)
# Drag to the second car
pyautogui.dragTo(x2 + cell_width / 2, y2 + cell_height / 2, duration=0.5)

# Optionally, you can add more logic to confirm successful merging, or repeat the
process as needed

You might also like