Python 无限窗口循环

问题描述 投票:0回答:0

所以基本上这个脚本自动农场。但重要的是定义 bbox 或区域的代码。当您打开脚本时,您会看到您要单击名为 Define bbox 的屏幕上的两个点来定义 bbox。之后脚本的主要功能起作用。但是当 Define bbox 窗口打开时,它会显示自己正在创建一个无限循环。 Infinite window loop。脚本

import random
import time
import cv2
import numpy as np
import pyautogui
from pynput import mouse, keyboard

class Rod:
    def __init__(self):
        print('Define bbox')
        self.mouse = mouse.Controller()

    def catch(self, *args):
        time.sleep(0.29)
        self.mouse.press(mouse.Button.left)
        time.sleep(0.1)
        self.mouse.release(mouse.Button.left)
        time.sleep(1)

def stop(key):
    if key == 'q':
        cv2.destroyAllWindows()
        exit()

keyboard.Listener(on_press=stop)

rod = Rod()

cv2.namedWindow("Define bbox", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Define bbox", 800, 600)
bbox = []
def on_mouse_click(event, x, y, flags, param):
    global bbox
    if event == cv2.EVENT_LBUTTONDOWN:
        bbox.append((x, y))
        if len(bbox) == 2:
            cv2.destroyAllWindows()
            return
cv2.setMouseCallback("Define bbox", on_mouse_click)

while True:
    screen = np.array(pyautogui.screenshot())
    screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
    cv2.imshow("Define bbox", screen)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break
    elif len(bbox) == 2:
        break

if len(bbox) == 2:
    x1, y1 = bbox[0]
    x2, y2 = bbox[1]
    bbox = {"top": min(y1, y2), "left": min(x1, x2), "width": abs(x2 - x1), "height": abs(y2 - y1)}
    print(f"Bbox defined as: {bbox}")
else:
    print("Error: Bbox not defined")
    exit()

cv2.imshow('programs vision', np.zeros([480, 640, 1]))
cv2.setWindowProperty('programs vision', cv2.WND_PROP_TOPMOST, 1)
cv2.imshow('binary', np.zeros([480, 640, 1]))
cv2.setWindowProperty('binary', cv2.WND_PROP_TOPMOST, 1)

kernel_size = (3, 3)
kernel_el = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)

while True:
    screen = np.array(pyautogui.screenshot(region=(bbox["left"], bbox["top"], bbox["width"], bbox["height"])))
    screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
    cv2.imshow('programs vision', screen)

    binary = np.zeros((screen.shape[:-1]))
    a = screen[:, :, 0] == screen[:, :, 1]
    b = screen[:, :, 1] == screen[:, :, 2]
    mask = np.logical_and(a, b)
    binary[mask] = 1
    binary = cv2.erode(binary, kernel_el, (-1, -1))
    cv2.imshow('binary', binary)

    average = np.average(binary)
    if average == 0:
        rod.catch()

    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

我把窗户变小了,但这还不够。

python window screen
© www.soinside.com 2019 - 2024. All rights reserved.