用户:想要成为NuclearBomb查看:0 回复:2 评论:0 创建时间:2023-08-06T18:26:18
【作品展示】

【作品介绍】
AI瞄
【作品源代码】
class ScreenUtils():
@staticmethod
def get_real_resolution():
"""获取真实的分辨率"""
hDC = win32gui.GetDC(0)
# 横向分辨率
w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
# 纵向分辨率
h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)
return w, h
@staticmethod
def get_screen_size():
"""获取缩放后的分辨率"""
w = GetSystemMetrics(0)
h = GetSystemMetrics(1)
return w, h
@staticmethod
def scale_rate():
real_resolution = ScreenUtils.get_real_resolution()
screen_size = ScreenUtils.get_screen_size()
screen_scale_rate = round(real_resolution[0] / screen_size[0], 2)
return screen_scale_rate
class RectangleDrawer:
def __init__(self, size=100, pool_size=6):
self.draw_helper_pool = ThreadPoolManager(max_workers=pool_size)
self.size = size
self.screen_scale_rate = ScreenUtils.scale_rate()
self.dc = win32gui.GetDC(0)
self.dcObj = win32ui.CreateDCFromHandle(self.dc)
self.hwnd = win32gui.WindowFromPoint((0, 0))
self.monitor = (0, 0, GetSystemMetrics(0), GetSystemMetrics(1))
self.red = win32api.RGB(255, 0, 0) # Red
self.drawing = False
# rgbs = np.random.rand(32, 3) * 255
rgbs = [(255, 0, 0), (255, 255, 0), (0, 0, 204), (0, 255, 0)]
self.screen_width = win32api.GetSystemMetrics(0)
self.screen_height = win32api.GetSystemMetrics(1)
self.colours = [win32api.RGB(
int(c[0]), int(c[1]), int(c[2])) for c in rgbs]
self.going_draw = True
self.items = []
def drawRectanglesBySelf(self, fps=60):
def go():
t = 1/fps
while(self.going_draw):
time.sleep(t)
self.drawRectangles()
self.draw_helper_pool.execute(go)
def setItems(self, items):
self.items = items
def drawRectangles(self):
"""
绘制多个目标框
:param items:
:return:
"""
for item in self.items:
text = item['cls']+":"+"conf:"+"{:.2f}".format(item['conf'])
left, top, width, height = item['box']
color = self.colours[int(item['id'] % len(self.colours))]
# 进行坐标边界检查
left = max(0, min(left, self.screen_width - 1))
top = max(0, min(top, self.screen_height - 1))
right = max(0, min(left + width, self.screen_width - 1))
bottom = max(0, min(top + height, self.screen_height - 1))
# 绘制矩形框
self.new_items = True
if(item['cls'] == 'person'):
# self.draw_helper_pool.execute(self.drawSingle,text, left, top, right - left, bottom - top, color)
self.drawSingle(text, left, top, right -
left, bottom - top, color)
def drawSingle(self, text, left, top, width, height, color, h=5):
start_x = int(left)
start_y = int(top)
# past_coordinates = self.monitor
past_coordinates = (start_x - 2 * width, start_y - 2 * height,
start_x + 2 * width, start_y + 2 * height
)
rect = win32gui.CreateRoundRectRgn(*past_coordinates, 2, 2)
win32gui.RedrawWindow(self.hwnd, past_coordinates,
rect, win32con.RDW_INVALIDATE)
try:
for k in range(h):
#绘制多重框
for x in range(width-k):
#绘制两条横线
win32gui.SetPixel(self.dc, start_x + x, start_y+k, color)
win32gui.SetPixel(self.dc, start_x + x,
start_y + height-k, color)
for y in range(height-k):
#绘制两条竖线
win32gui.SetPixel(self.dc, start_x + k,
start_y + y + k, color)
win32gui.SetPixel(self.dc, start_x +
width-k, start_y + y + k, color)
text_coordinates = (
start_x - width, start_y - height,
start_x + 2 * width, start_y + height
)
# 在矩形框中显示文字
win32gui.DrawText(self.dc, text, -1, text_coordinates,
win32con.DT_CENTER | win32con.DT_VCENTER | win32con.DT_SINGLELINE)
except Exception as e:
pass
def draw(self, text="你好"):
past_coordinates = self.monitor
while(self.drawing):
m = win32gui.GetCursorPos()
rect = win32gui.CreateRoundRectRgn(*past_coordinates, 2, 2)
win32gui.RedrawWindow(
self.hwnd, past_coordinates, rect, win32con.RDW_INVALIDATE)
start_x = int(m[0]*self.screen_scale_rate)
start_y = int(m[1]*self.screen_scale_rate)
for x in range(self.size):
win32gui.SetPixel(self.dc, start_x + x, start_y, self.red)
win32gui.SetPixel(self.dc, start_x + x,
start_y + self.size, self.red)
win32gui.SetPixel(self.dc, start_x, start_y + x, self.red)
win32gui.SetPixel(self.dc, start_x +
self.size, start_y + x, self.red)
past_coordinates = (start_x - 2*self.size, start_y - 2 *
self.size, start_x + 2*self.size, start_y + 2*self.size)
text_coordinates = (
start_x - self.size, start_y - self.size, start_x + 2 * self.size, start_y + self.size)
# 在矩形框中显示文字
win32gui.DrawText(self.dc, text, -1, text_coordinates,
win32con.DT_CENTER | win32con.DT_VCENTER | win32con.DT_SINGLELINE)
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn