-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
125 lines (109 loc) · 3.91 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import subprocess
from ctypes_screenshots import screencapture_window
import cv2
import time
import pandas as pd
from a_cv2_easy_resize import add_easy_resize_to_cv2
from time import time
from ctypes_window_info import get_window_infos
add_easy_resize_to_cv2()
def cropimage(img, coords):
return img[coords[1] : coords[3], coords[0] : coords[2]].copy()
def get_screenwidth(adb_path, deviceserial):
screenwidth, screenheight = (
subprocess.run(
rf'{adb_path} -s {deviceserial} shell dumpsys window | grep cur= |tr -s " " | cut -d " " -f 4|cut -d "=" -f 2',
shell=True,
capture_output=True,
)
.stdout.decode("utf-8", "ignore")
.strip()
.split("x")
)
screenwidth, screenheight = int(screenwidth), int(screenheight)
return screenwidth, screenheight
def connect_to_adb(adb_path, deviceserial):
_ = subprocess.run(f"{adb_path} start-server", capture_output=True, shell=False)
_ = subprocess.run(
f"{adb_path} connect {deviceserial}", capture_output=True, shell=False
)
def get_bluestacks_screenshot(
adb_path,
deviceserial,
windowtitle,
hwnd=None,
interpolation=cv2.INTER_AREA,
ignore_exceptions=True,
show_fps=True,
):
connect_to_adb(adb_path, deviceserial)
screenadbx, screenadby = get_screenwidth(adb_path, deviceserial)
if hwnd is None:
from a_pandas_ex_automate_win32 import find_elements
df = pd.DataFrame(find_elements())
filter1 = df.loc[df.title.str.contains(windowtitle)]
if filter1.empty:
filter1 = df.loc[df.windowtext.str.contains(windowtitle)]
possible_proc = df.loc[df.pid.isin(filter1.pid)]
filter1 = possible_proc.loc[
possible_proc.title.str.contains(
"BlueStacks Keymap Overlay", na=False, regex=False
)
]
if filter1.empty:
filter1 = df.loc[
df.windowtext.str.contains(
"BlueStacks Keymap Overlay", na=False, regex=False
)
]
right_proc = df.loc[df.pid.isin(filter1.pid)]
lf = right_proc.loc[right_proc.title.str.contains(windowtitle)]
if lf.empty:
lf = right_proc.loc[right_proc.windowtext.str.contains(windowtitle)]
hwnd = int(lf.hwnd.iloc[0])
print(f"hWnd: {hwnd}")
ita = screencapture_window(hwnd)
loop_time = None
while True:
try:
if show_fps:
loop_time = time()
df = pd.DataFrame(get_window_infos())
ctypescreen = next(ita)
fi1x = df.loc[df.hwnd == hwnd]
pid = fi1x.pid.iloc[0]
alleb = df.loc[df.pid == pid]
locas = alleb.loc[
(
alleb.title.str.contains(
"BlueStacks Keymap Overlay", na=False, regex=False
)
)
| (
alleb.windowtext.str.contains(
"BlueStacks Keymap Overlay", na=False, regex=False
)
)
| (
alleb.class_name.str.contains(
"BlueStacks Keymap Overlay", na=False, regex=False
)
)
]
x, y = locas.dim_client.iloc[0]
croppedim = cropimage(
ctypescreen, (1, ctypescreen.shape[0] - y, x, ctypescreen.shape[0])
)
yield cv2.easy_resize_image(
croppedim,
width=screenadbx,
height=screenadby, # pic2.shape[0] + mincrop,
percent=None,
interpolation=interpolation,
)
if show_fps:
print("FPS {} ".format(1 / (time() - loop_time)), end="\r")
except Exception as fe:
if not ignore_exceptions:
raise fe
continue