自瞄
自瞄框框可新开个线程,然后启动这个即可,有点耗内存,录像不知为啥显示不出来。
def draw():
global data
while True:
for i in range(xlen):
win32gui.SetPixel(dc, x+i, y, 255)
win32gui.SetPixel(dc, x+i, y+ylen, 255)
for i in range(ylen):
win32gui.SetPixel(dc, x, y+i, 255)
win32gui.SetPixel(dc, x+xlen, y+i, 255)
for man in data:
_x = int(x+man[0].item())
_y = int(y+man[1].item())
_xlen = int((man[2].item()-man[0].item())/2)
_ylen = int((man[3].item()-man[1].item())/2)
_x = int(_x+_xlen/2)
_y = int(_y+_ylen/2)
for i in range(_xlen):
win32gui.SetPixel(dc, _x+i, _y, 255)
win32gui.SetPixel(dc, _x+i, _y+_ylen, 255)
for i in range(_ylen):
win32gui.SetPixel(dc, _x, _y+i, 255)
win32gui.SetPixel(dc, _x+_xlen, _y+i, 255)
自瞄代码
import torch
import pyautogui
import win32gui
import sys
import threading
import time
from pynput import keyboard
dc = win32gui.GetDC(0)
x=300 #起点x位置
y=274 #起点y位置
xlen = 424#边框长度
ylen = 268
star_x =x + int(xlen/2)#准星位置
star_y= y + int(ylen/2)
data = torch.Tensor([])
device = torch.device('cuda')
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
model.classes=[0]
def shot():
a = time.time()
img = pyautogui.screenshot(region=[x,y,xlen,ylen])
results = model(img)
data = (results.xyxy[0]).clone().type(torch.int)
if len(data) !=0:
item_x = 0
item_y = 0
min = sys.maxsize
for man in data:
_x = x+man[0].item()
_y = y+man[1].item()
_xlen = man[2].item()-man[0].item()
_ylen = man[3].item()-man[1].item()
_x = _x+_xlen/2
_y = _y+_ylen/4
if ((_x-star_x)*(_x-star_x) + (_y-star_y)*(_y-star_y))<min:
min = (_x-star_x)*(_x-star_x) + (_y-star_y)*(_y-star_y)
item_x =_x
item_y=_y
threading.Thread(target=click,args=(item_x ,item_y)).start()
b = time.time()
print("检测消耗时间:{0}s".format(b-a))
def on_press(key):
try:
if(key.char == 't'):
print('alphanumeric key {0} pressed:开始检测'.format(
key.char))
shot()
except AttributeError:
key
def on_release(key):
if key == keyboard.Key.esc:
return False
def click(_x,_y):
print('在X:{},Y:{},方向发现敌人!锁定并射击!'.format(_x,_y))
pyautogui.moveTo(_x ,_y)
pyautogui.click()
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
listener.start()
微信搜索桔子科研或者扫描二维码,第一时间获取编程有趣的知识和最新科研学术成果。