Pyautogui 返回导致我的脚本崩溃的屏幕截图

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

我正在尝试使用 pyautogui 在 while 循环内收集屏幕截图,以模仿我的计算机屏幕的视频源。我的问题是,有时这个脚本工作得很好,其他时候 pyautogui.screenshot() 返回一个灰色的 opencv 图像,最终导致程序崩溃。

# Manually enter the four points of the bounded box (x, y) coordinates
points = [
    (100, 50),  # Top-left corner
    (500, 50),  # Top-right corner
    (2500, 1400),  # Bottom-right corner
    (100, 400)   # Bottom-left corner
]

scaling_factor = 0.5
while True:
    time.sleep(.2)
    threshold = 2300
    frame = pyautogui.screenshot(region=(points[0][0], points[0][1], points[2][0] - points[0][0], points[2][1] - points[0][1]))
    frame1 = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
    IsSearching = find_image_match_in_rectangle(frame1, template_image_path, rectangle_top_left, rectangle_width, rectangle_height)
    if (Lockin)==True:
        #reset the filter
        print("NO SNIPES to reset")
    if (IsSearching) == True:
        Lockin = False
        print("Searching")
    if (IsSearching)== False:
        GetValues = draw_rectangles(frame1, num_rectangles, initial_top_left, rectangle_size, x_shift)
        if (GetValues[1])<threshold:
            print("Buy pos 1 at {}".format(GetValues[1]))
            buy1()
        if (GetValues[2])<threshold:
            print("Buy pos 2 at {}".format(GetValues[2]))
            #buy2()
        if (GetValues[3])<threshold:
            print("Buy pos 3 at {}".format(GetValues[3]))
            #buy3()
        if (GetValues[4])<threshold:
            print("Buy pos 4 at {}".format(GetValues[4]))
            #buy4()
        if (GetValues[5])<threshold:
            print("Buy pos 5 at {}".format(GetValues[5]))
            #buy5()
        if (GetValues[6])<threshold:
            print("Buy pos 6 at {}".format(GetValues[6]))
            #buy6()
        Lockin = True
        continue
    
    key = cv2.waitKey(25)
    if key == ord('q'):
        break


我不知道为什么它有时有效,有时无效。

python-3.x opencv pyautogui
1个回答
0
投票

您可能遇到的问题是因为 pyautogui 的无限屏幕截图,这可能会导致崩溃。 相反,使用 pyautogui 使用 opencv 预构建的帧捕获功能

cv2.VideoCapture(camera number)

如果您想捕获应用程序,请使用OBS捕获屏幕并将其设置为虚拟相机并将视频捕获编号设置为1

cv2.VideoCapture(0)
或其他

下面的代码逐帧读取并捕获您之前编码的特定区域

import cv2

# Bounded box coordinates
top_left = (100, 50)
bottom_right = (2500, 1400)

cap = cv2.VideoCapture(0)
# Capture the screen within the bounded box
while True:

    ret, frame = cap.read()
    frame = frame[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0]]

    # Process the captured frame here
    # ...

    cv2.imshow("Captured Frame", frame)

    key = cv2.waitKey(25)
    if key == ord('q'):
        break

cv2.destroyAllWindows()

希望对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.