Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import pyautogui
- # Function to detect red touching purple within a circular region of interest (ROI)
- def detect_red_touching_purple():
- # Capture the screen
- screenshot = pyautogui.screenshot()
- img = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
- # Get the dimensions of the screen
- screen_width, screen_height = pyautogui.size()
- # Define the circular region of interest (ROI) parameters
- roi_radius = min(screen_width, screen_height) // 4 # Radius of the circular ROI
- roi_center_x = screen_width // 2 # X-coordinate of the center of the ROI
- roi_center_y = screen_height // 2 # Y-coordinate of the center of the ROI
- # Create a mask for the circular ROI
- mask = np.zeros((screen_height, screen_width), dtype=np.uint8)
- cv2.circle(mask, (roi_center_x, roi_center_y), roi_radius, 255, thickness=cv2.FILLED)
- # Apply the mask to the original image
- masked_img = cv2.bitwise_and(img, img, mask=mask)
- # Convert BGR to HSV (Hue, Saturation, Value)
- hsv = cv2.cvtColor(masked_img, cv2.COLOR_BGR2HSV)
- # Define range of red color in HSV
- lower_red = np.array([0, 100, 100])
- upper_red = np.array([10, 255, 255])
- # Define range of purple color in HSV
- lower_purple = np.array([125, 50, 50])
- upper_purple = np.array([150, 255, 255])
- # Threshold the HSV image to get only red and purple colors within the circular ROI
- mask_red = cv2.inRange(hsv, lower_red, upper_red)
- mask_purple = cv2.inRange(hsv, lower_purple, upper_purple)
- # Bitwise AND operation to find regions where both red and purple are present within the circular ROI
- red_and_purple_touching = cv2.bitwise_and(mask_red, mask_purple)
- # Check if any nonzero pixel values are present in the resulting image
- touching = np.any(red_and_purple_touching)
- return touching
- # Main loop
- while True:
- if detect_red_touching_purple():
- # Press the "E" key when red touches purple within the circular ROI
- pyautogui.press('e')
- print("Red touching purple within the circular ROI. 'E' key pressed.")
- else:
- print("Red not touching purple within the circular ROI.")
- # Delay to control the loop rate (adjust as needed)
- cv2.waitKey(100)
Advertisement
Add Comment
Please, Sign In to add comment