SoFunction
Updated on 2024-11-15

Python realize the desktop for real-time capture screen method details

present (sb for a job etc)

Recently in the study of target detection of small things, the need to the desktop for real-time capture screen, get the screen and then detect, to achieve the purpose of real-time desktop target detection, so wrote a small code to achieve the function, the measured speed is very fast, in line with my needs. Here to record.

coding

import argparse
import time
import cv2
import keyboard
import mss
import numpy as np
import 
import win32con
import win32gui


class ScreenCapture:
    """
    parameters
    ----------
        screen_frame : Tuple[int, int]
            screen width and height,consist ofx,y
        region : Tuple[float, float]
            Actual screenshot range,consist ofx,y,(1.0, 1.0)Indicates full-screen detection,The lower the range, the smaller the detection range(Always keep the center of the screen as the center)
        window_name : str
            Display window name
        exit_key : int
            Exit key value for the end window,for each key of the keyboardASCIIcode value,The default isESClinchpin
    """

    def __init__(self, screen_frame=(1920, 1080), region=(0.5, 0.5), window_name='test', exit_key=0x1B):
         = ()
        .add_argument('--region', type=tuple, default=region,
                                 help='Scope of Screenshots;consist ofx,y,(1.0, 1.0)Indicates full-screen detection,The lower the range, the smaller the detection range(Always keep the center of the screen as the center)')
        self.parser_args = .parse_args()

         = (mon=-1, optimize=True)  # Instantiate mss and use efficient mode

        self.screen_width = screen_frame[0]  # The width of the screen
        self.screen_height = screen_frame[1]  # The screen's high
        self.mouse_x, self.mouse_y = self.screen_width // 2, self.screen_height // 2  # Screen center coordinates

        # Screenshot area
        self.GAME_WIDTH, self.GAME_HEIGHT = int(self.screen_width * self.parser_args.region[0]), int(
            self.screen_height * self.parser_args.region[1])  # Width and height
        self.GAME_LEFT, self.GAME_TOP = int(0 + self.screen_width // 2 * (1. - self.parser_args.region[0])), int(
            0 + 1080 // 2 * (1. - self.parser_args.region[1]))  # Origin

        self.RESZIE_WIN_WIDTH, self.RESIZE_WIN_HEIGHT = self.screen_width // 4, self.screen_height // 4  # Display window size
         = {
            'left': self.GAME_LEFT,
            'top': self.GAME_TOP,
            'width': self.GAME_WIDTH,
            'height': self.GAME_HEIGHT
        }

        self.window_name = window_name
        self.Exit_key = exit_key
         = None

    def grab_screen_mss(self, monitor):
        # capture the image, convert the image to an array, cvtColor convert BRGA to BRG, remove the transparent channel
        return (((monitor)), cv2.COLOR_BGRA2BGR)

    def update_img(self, img):
         = img

    def get_img(self):
        return 

    def run(self):
        SetForegroundWindow_f = 0  # Determine if a top window is needed
        while True:
            # Determine if ctrl+U is pressed and the window is always on top.
            if keyboard.is_pressed('ctrl+U'):
                while keyboard.is_pressed('ctrl+U'):
                    continue
                if SetForegroundWindow_f == 0:
                    SetForegroundWindow_f = 1
                    (1)
                    continue
                else:
                    SetForegroundWindow_f = 0

            if  is None:
                img = self.grab_screen_mss()

            (self.window_name, cv2.WINDOW_NORMAL)  # cv2.WINDOW_NORMAL Sets image size based on window size
            (self.window_name, self.RESZIE_WIN_WIDTH, self.RESIZE_WIN_HEIGHT)
            (self.window_name, img)

            if SetForegroundWindow_f == 1:
                shell = ("")
                ('%')
                ((None, self.window_name))
                ((None, self.window_name), win32con.SW_SHOW)

            if (1) & 0XFF == self.Exit_key:  # Default: ESC
                ()
                exit("The end.")

code talk

The main idea of the function implementation is to use mss library for screenshot and opencv library for image display and processing.

First, the argparse library is used to parse the incoming arguments and set the size of the detection range.

Then, use the mss library to instantiate a screenshot object, cap.

Next, set the width and height of the screen and calculate the coordinates of the center point of the screen.

After that, the width, height and origin coordinates of the in-game screenshot area are calculated based on the incoming parameters and saved in the variable mointor.

Defined a function grab_screen_mss that uses the capture image and converts the image to an array with cvtColor, then converts BRGA to BRG, removing the transparent channel.

A run function is defined that loops over and over, determining whether ctrl+U is pressed, and if it is, the window is always on top.

Then the grab_screen_mss function is called to get the screenshot, use the cv2 library for image display, and set the size of the display window.

If the window needs to be topped, use the win32com library and the win32gui library to top the window.

Finally, use the cv2 library's waitKey function to wait for the user to do something and press the ESC key to exit the program.

Recall Example

sc = ScreenCapture()
    ()

Parameter Explanation:

screen_frame : Tuple[int, int]

Screen width and height, respectively x, y

region : Tuple[float, float]

Actual screenshot range, respectively x, y, (1.0, 1.0) means full screen detection, the lower the detection range is smaller (always keep the center of the screen as the center)

window_name : str

Display window name

exit_key : int

The exit key value of the end window is the ASCII code value corresponding to each key of the keyboard, the default is the ESC key.

(sth. or sb) else

ASCII code value for each key of the keyboard (0x means hexadecimal, the ascii code value for the delete key is 0x2e, i.e. 46 in decimal)

  • 0x1 Left mouse button
  • 0x2 Right mouse button
  • 0x3 CANCEL key
  • 0x4 Middle mouse button
  • 0x8 BACKSPACE key
  • 0x9 TAB key
  • 0xC CLEAR key
  • 0xD ENTER key
  • 0x10 SHIFT key
  • 0x11 CTRL key
  • 0x12 MENU key
  • 0x13 PAUSE key
  • 0x14 CAPS LOCK key
  • 0x1B ESC key
  • 0x20 SPACEBAR key
  • 0x21 PAGE UP key
  • 0x22 PAGE DOWN key
  • 0x23 END key
  • 0x24 HOME key
  • 0x25 LEFT ARROW key
  • 0x26 UP ARROW key
  • 0x27 RIGHT ARROW key
  • 0x28 DOWN ARROW key
  • 0x29 SELECT key
  • 0x2A PRINT SCREEN key
  • 0x2B EXECUTE key
  • 0x2C SNAPSHOT key
  • 0x2D INSERT key
  • 0x2E DELETE key
  • 0x2F HELP key
  • 0x90 NUM LOCK key

The A to Z keys have the same ASCII code as the A - Z letters:

(be) worth descriptive
65 A key
66 B Key
67 C key
68 D Key
69 E key
70 F key
71 G-key
72 H key
73 I key
74 J key
75 K-key
76 L-key
77 M-key
78 N key
79 O key
80 P-key
81 Q key
82 R key
83 S-key
84 T-key
85 U key
86 V key
87 W key
88 X-key
89 Y key
90 Z key

The 0 through 9 keys have the same ASCII code as the numbers 0 - 9:

(be) worth descriptive
48 0 key
49 1 key
50 2 Keys
51 3 Keys
52 4 Keys
53 5 Keys
54 6 Keys
55 7 Keys
56 8 keys
57 9 Keys

The following constants represent keys on the numeric keypad:

(be) worth descriptive
0x60 0 key
0x61 1 key
0x62 2 Keys
0x63 3 Keys
0x64 4 Keys
0x65 5 Keys
0x66 6 Keys
0x67 7 Keys
0x68 8 keys
0x69 9 Keys
0x6A MULTIPLICATION SIGN (*) key
0x6B PLUS SIGN (+) key
0x6C ENTER
0x6D MINUS SIGN (-) key
0x6E DECIMAL POINT (.) KEYS
0x6F DIVISION SIGN (/) key

The following constants represent function keys:

(be) worth descriptive
0x70 F1 key
0x71 F2 key
0x72 F3 key
0x73 F4 key
0x74 F5 key
0x75 F6 key
0x76 F7 key
0x77 F8 key
0x78 F9 key
0x79 F10 key
0x7A F11 key
0x7B F12 key
0x7C F13 key
0x7D F14 key
0x7E F15 key
0x7F F16 key

To this point, this article on Python to achieve real-time capture of the desktop screen method details of the article is introduced to this, more related Python desktop real-time capture screen content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!