从全屏获取捕获

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

我正在尝试制作屏幕的“视频”,以便opencv可以分析它,到目前为止它运行良好,但是当我进入全屏模式时,程序停止记录或分析,这停止工作。 你能帮我吗?

from mss import mss
import cv2
from PIL import Image
import numpy as np
from time import time
from pynput.mouse import Button, Controller

mon = {'top': 370, 'left':675, 'width':15, 'height':20}
mouse = Controller();
sct = mss()
upper_purple = np.array([[255, 81, 255]])
lower_purple = np.array([[96, 35, 84]])
while 1:
    
    
    begin_time = time()
    sct_img = sct.grab(mon)
    img = Image.frombytes('RGB', (sct_img.size.width, sct_img.size.height), sct_img.rgb)
    img_hsv = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
    height, widht, channel_color = img_hsv.shape
    cv2.imshow('test', np.array(img_hsv))
    for y in range (0, height):
        for x in range (0, widht):
            red = img_hsv.item(y, x, 0)
            green = img_hsv.item(y, x, 1)
            blue = img_hsv.item(y, x, 2)
            offset = red - blue
            if ((img_hsv[y][x] >= lower_purple).all() and (img_hsv[y][x] <= upper_purple).all()):
                mouse.click(Button.left, 1)
                print("SHOOT")
    print('This frame takes {} seconds.'.format(time()-begin_time))
    
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

python opencv capture python-mss
1个回答
0
投票

Python 的 mss 包在某些情况下似乎无法处理 GPU 输出。根据游戏和实现,无法处理全屏模式,mss 仅捕获“窗口”模式。 我在运行 NVIDIA GeForce RTX 3070 Ti 的 Win10 上捕获《我的世界》时遇到了同样的问题。
找到了一些名为 dxcam 的模块,它似乎运行良好并且可以识别 GPU 输出。
我在 python 3.11 中尝试了这段代码并且它有效。该文档有点不清楚,但它有效。

import cv2
import dxcam

print(dxcam.device_info())
camera = dxcam.create()

# Start capturing frames
camera.start()

while True:
    # Get the latest captured frame from the camera
    frame = camera.get_latest_frame()

    if frame is not None:
        # Convert the BGR frame to RGB
        corrected_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Display the corrected frame using OpenCV
        cv2.imshow('Corrected Frame', corrected_frame)

        # Exit the loop and close the window when 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

# Stop capturing when done
camera.stop()

# Close OpenCV windows
cv2.destroyAllWindows()

我希望这有帮助,因为问题不太清楚。

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