Skip to content

Commit 3a85f60

Browse files
Matt SamMatt Sam
authored andcommitted
...
1 parent 7166b48 commit 3a85f60

File tree

1 file changed

+125
-12
lines changed

1 file changed

+125
-12
lines changed

AI_M_BOT(opencv+yolo)/AI_M_BOT_X0.py

Lines changed: 125 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'''
2-
Detection code modified from project AIMBOT-YOLO
3-
Detection code Author: monokim
4-
Detection project website: https://github.com/monokim/AIMBOT-YOLO
5-
Detection project video: https://www.youtube.com/watch?v=vQlb0tK1DH0
2+
New Detection method(onnxruntime) modified from Project YOLOX
3+
YOLOX Project Authors: Zheng Ge, Songtao Liu, Feng Wang, Zeming Li, Jian Sun
4+
YOLOX Project website: https://github.com/Megvii-BaseDetection/YOLOX
5+
New Detection method(onnxruntime) cooperator: Barry
66
Screenshot method from: https://www.youtube.com/watch?v=WymCpVUPWQ4
77
Screenshot method code modified from project: opencv_tutorials
88
Screenshot method code Author: Ben Johnson (learncodebygaming)
@@ -14,8 +14,6 @@
1414
from multiprocessing import Process, Array, Pipe, freeze_support, JoinableQueue
1515
from win32api import GetAsyncKeyState, GetCurrentProcessId, OpenProcess
1616
from win32process import SetPriorityClass, ABOVE_NORMAL_PRIORITY_CLASS
17-
from yolox.data.data_augment import preproc as preprocess
18-
from yolox.utils import multiclass_nms, demo_postprocess
1917
from sys import exit, executable, platform
2018
from math import sqrt, pow, ceil
2119
from collections import deque
@@ -133,8 +131,9 @@ def update_window_info(self):
133131
# 确认截图相关数据
134132
self.total_w = client_rect[2] - client_rect[0]
135133
self.total_h = client_rect[3] - client_rect[1]
136-
self.cut_h = self.total_h // 2
137-
self.cut_w = self.cut_h
134+
cut_factor = int(self.total_h / 2 / 192)
135+
self.cut_h = 192 * cut_factor
136+
self.cut_w = 224 * cut_factor
138137
if self.windows_class == 'CrossFire': # 画面实际4:3简单拉平
139138
self.cut_w = self.cut_w * (self.total_w / self.total_h) * 3 // 4
140139
self.offset_x = (self.total_w - self.cut_w) // 2 + self.left_corner[0] - window_rect[0]
@@ -334,6 +333,117 @@ def analyze(predictions, ratio):
334333
return boxes_xyxy, scores
335334

336335

336+
# 从yolox复制的预处理函数
337+
def preprocess(image, input_size, mean, std, swap=(2, 0, 1)):
338+
if len(image.shape) == 3:
339+
padded_img = np.ones((input_size[0], input_size[1], 3)) * 114.0
340+
else:
341+
padded_img = np.ones(input_size) * 114.0
342+
img = image
343+
r = min(input_size[0] / img.shape[0], input_size[1] / img.shape[1])
344+
resized_img = cv2.resize(
345+
img,
346+
(int(img.shape[1] * r), int(img.shape[0] * r)),
347+
interpolation=cv2.INTER_LINEAR,
348+
).astype(np.float32)
349+
padded_img[: int(img.shape[0] * r), : int(img.shape[1] * r)] = resized_img
350+
351+
padded_img = padded_img[:, :, ::-1]
352+
padded_img /= 255.0
353+
if mean is not None:
354+
padded_img -= mean
355+
if std is not None:
356+
padded_img /= std
357+
padded_img = padded_img.transpose(swap)
358+
padded_img = np.ascontiguousarray(padded_img, dtype=np.float32)
359+
return padded_img, r
360+
361+
362+
# 从yolox复制的单类非极大值抑制函数
363+
@jit(nopython=True)
364+
def nms(boxes, scores, nms_thr):
365+
"""Single class NMS implemented in Numpy."""
366+
x1 = boxes[:, 0]
367+
y1 = boxes[:, 1]
368+
x2 = boxes[:, 2]
369+
y2 = boxes[:, 3]
370+
371+
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
372+
order = scores.argsort()[::-1]
373+
374+
keep = []
375+
while order.size > 0:
376+
i = order[0]
377+
keep.append(i)
378+
xx1 = np.maximum(x1[i], x1[order[1:]])
379+
yy1 = np.maximum(y1[i], y1[order[1:]])
380+
xx2 = np.minimum(x2[i], x2[order[1:]])
381+
yy2 = np.minimum(y2[i], y2[order[1:]])
382+
383+
w = np.maximum(0.0, xx2 - xx1 + 1)
384+
h = np.maximum(0.0, yy2 - yy1 + 1)
385+
inter = w * h
386+
ovr = inter / (areas[i] + areas[order[1:]] - inter)
387+
388+
inds = np.where(ovr <= nms_thr)[0]
389+
order = order[inds + 1]
390+
391+
return keep
392+
393+
394+
# 从yolox复制的多类非极大值抑制函数
395+
def multiclass_nms(boxes, scores, nms_thr, score_thr):
396+
"""Multiclass NMS implemented in Numpy"""
397+
final_dets = []
398+
num_classes = scores.shape[1]
399+
for cls_ind in range(num_classes):
400+
cls_scores = scores[:, cls_ind]
401+
valid_score_mask = cls_scores > score_thr
402+
if valid_score_mask.sum() == 0:
403+
continue
404+
else:
405+
valid_scores = cls_scores[valid_score_mask]
406+
valid_boxes = boxes[valid_score_mask]
407+
keep = nms(valid_boxes, valid_scores, nms_thr)
408+
if len(keep) > 0:
409+
cls_inds = np.ones((len(keep), 1)) * cls_ind
410+
dets = np.concatenate(
411+
[valid_boxes[keep], valid_scores[keep, None], cls_inds], 1
412+
)
413+
final_dets.append(dets)
414+
if len(final_dets) == 0:
415+
return None
416+
return np.concatenate(final_dets, 0)
417+
418+
419+
# 从yolox复制的后置处理函数
420+
def demo_postprocess(outputs, img_size, p6=False):
421+
grids = []
422+
expanded_strides = []
423+
424+
if not p6:
425+
strides = [8, 16, 32]
426+
else:
427+
strides = [8, 16, 32, 64]
428+
429+
hsizes = [img_size[0] // stride for stride in strides]
430+
wsizes = [img_size[1] // stride for stride in strides]
431+
432+
for hsize, wsize, stride in zip(hsizes, wsizes, strides):
433+
xv, yv = np.meshgrid(np.arange(wsize), np.arange(hsize))
434+
grid = np.stack((xv, yv), 2).reshape(1, -1, 2)
435+
grids.append(grid)
436+
shape = grid.shape[:2]
437+
expanded_strides.append(np.full((*shape, 1), stride))
438+
439+
grids = np.concatenate(grids, 1)
440+
expanded_strides = np.concatenate(expanded_strides, 1)
441+
outputs[..., :2] = (outputs[..., :2] + grids) * expanded_strides
442+
outputs[..., 2:4] = np.exp(outputs[..., 2:4]) * expanded_strides
443+
444+
return outputs
445+
446+
337447
# 简单检查gpu是否够格
338448
def check_gpu(level):
339449
pynvml.nvmlInit()
@@ -401,7 +511,7 @@ def close():
401511

402512
# 检测是否存在配置与权重文件
403513
def check_file(file):
404-
file_name = file + '.oonx'
514+
file_name = file + '.onnx'
405515
if not os.path.isfile(file_name):
406516
print(f'请下载{file_name}相关文件!!!')
407517
sleep(3)
@@ -483,7 +593,7 @@ def control_mouse(a, b, fps_var, ranges, rate, go_fire, win_class, move_rx, move
483593
# 追踪优化
484594
def track_opt(record_list, range_m, move):
485595
if len(record_list):
486-
if abs(median(record_list) - range_m) <= 15 and range_m <= 80:
596+
if abs(median(record_list) - range_m) <= 12 and range_m <= 60:
487597
record_list.append(range_m)
488598
else:
489599
record_list.clear()
@@ -545,9 +655,9 @@ def show_frames(output_pipe, array):
545655
show_str3 = 'Fire rate is at ' + str('{:02.0f}'.format((10000 / (array[13] + 306)))) + ' RPS'
546656
show_str4 = 'Please enjoy coding ^_^' if array[17] else 'Please enjoy coding @_@'
547657
if show_img.any():
548-
show_img = cv2.resize(show_img, (array[5], array[5]))
658+
show_img = cv2.resize(show_img, (array[5], int(array[5] * 6 / 7)))
549659
img_ex = cv2.resize(img_ex, (array[5], int(array[5] / 2)))
550-
cv2.putText(show_img, show_str0, (int(array[5] / 25), int(array[5] / 12)), font, array[5] / 600, (127, 255, 0), 2, cv2.LINE_AA)
660+
cv2.putText(show_img, show_str0, (int(array[5] / 25), int(array[5] * 6 / 7 / 12)), font, array[5] / 600, (127, 255, 0), 2, cv2.LINE_AA)
551661
cv2.putText(img_ex, show_str1, (10, int(array[5] / 9)), font, array[5] / 450, show_color, 1, cv2.LINE_AA)
552662
cv2.putText(img_ex, show_str2, (10, int(array[5] / 9) * 2), font, array[5] / 450, show_color, 1, cv2.LINE_AA)
553663
cv2.putText(img_ex, show_str3, (10, int(array[5] / 9) * 3), font, array[5] / 450, show_color, 1, cv2.LINE_AA)
@@ -632,6 +742,9 @@ def detection2(que, array):
632742
move_record_x = []
633743
move_record_y = []
634744

745+
# 如果文件不存在则退出
746+
check_file('yolox_nano')
747+
635748
# 分享数据以及展示新进程
636749
arr = Array('i', range(21))
637750
'''

0 commit comments

Comments
 (0)