-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarknet_video1.py
229 lines (196 loc) · 6.11 KB
/
darknet_video1.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import random
import os
import cv2
import numpy as np
import time
import darknet
import win32api
from math import sqrt
import mss
import aimbot
import threading
monitor = {}
netMain = None
metaMain = None
altNames = None
me = 't'
shotflag = False
Yolowidth = 288
def init(configPath="cfg/yolov4-custom.cfg", weightPath="backup_3/yolov4-custom_7000.weights", metaPath="cfg/voc.data"):
global metaMain, netMain, altNames # pylint: disable=W0603
if netMain is None:
netMain = darknet.load_net_custom(
configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1
) # batch size = 1
if metaMain is None:
metaMain = darknet.load_meta(metaPath.encode("ascii"))
if altNames is None:
# In Python 3, the metafile default access craps out on Windows (but not Linux)
# Read the names file and create a list to feed to detect
try:
with open(metaPath) as metaFH:
metaContents = metaFH.read()
import re
match = re.search(
"names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE
)
if match:
result = match.group(1)
else:
result = None
try:
if os.path.exists(result):
with open(result) as namesFH:
namesList = namesFH.read().strip().split("\n")
altNames = [x.strip() for x in namesList]
except TypeError:
pass
except Exception:
pass
def convertBack(x, y, w, h):
xmin = int(round(x - (w / 2)))
xmax = int(round(x + (w / 2)))
ymin = int(round(y - (h / 2)))
ymax = int(round(y + (h / 2)))
return xmin, ymin, xmax, ymax
def cvDrawBoxes(detections, img, fps):
for detection in detections:
if float(detection[1]) < 0.5:
continue
x, y, w, h = detection[2][0],\
detection[2][1],\
detection[2][2],\
detection[2][3]
xmin, ymin, xmax, ymax = convertBack(
float(x), float(y), float(w), float(h))
pt1 = (xmin, ymin)
pt2 = (xmax, ymax)
cl = (255, 236, 139)if detection[0].decode() == 't' else(65, 105, 225)
cv2.rectangle(img, pt1, pt2, cl, 1)
cv2.putText(img,
detection[0].decode() +
" [" + str(round(detection[1] * 100, 2)) + "]",
(pt1[0], pt1[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
cl, 2)
cv2.putText(img, str(fps), (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
[0, 255, 0], 2)
return img
class Arec:
x = None
y = None
tp = None
def __init__(self, x, y, tp):
self.x = x
self.y = y
self.tp = tp
self.dis = int(sqrt((x - Yolowidth / 2)**2 + (y - Yolowidth / 2)**2))
def __lt__(self, other):
s = self.dis * (1 if 'h' in self.tp else 10000)
o = other.dis * (1 if 'h' in self.tp else 10000)
return s < o
def disok(self):
if self.dis < 40:
return True
return False
def pdetections(detections):
for detection in detections:
if float(detection[1]) < 0.5:
continue
x, y, w, h = detection[2][0],\
detection[2][1],\
detection[2][2],\
detection[2][3]
xmin, ymin, xmax, ymax = convertBack(
float(x), float(y), float(w), float(h))
if 'h' in detection[0].decode():
sxmin = xmin
sxmax = xmax
symin = ymin
symax = ymax
else:
sxmin = int(0.1 * w) + xmin
sxmax = int(0.9 * w) + xmin
symin = ymin
symax = int(0.6 * h) + ymin
if sxmin < Yolowidth / 2 and sxmax > Yolowidth / 2 and symin < Yolowidth / 2 and symax > Yolowidth / 2:
print("START SHOT", sxmin, symin, sxmax, symax)
aimbot.shot(0, 0)
return
def head(detections):
ar = []
for detection in detections:
if float(detection[1]) < 0.5:
continue
x, y, w, h = detection[2][0],\
detection[2][1],\
detection[2][2],\
detection[2][3]
xmin, ymin, xmax, ymax = convertBack(
float(x), float(y), float(w), float(h))
tp = detection[0].decode()
if 'h' in tp:
ar.append(Arec(random.randint(
xmin, xmax
), random.randint(ymin, ymax), tp))
else:
sx = int(random.randint(2, 6) * 0.1 * w) + xmin
sy = int(random.randint(1, 5) * 0.1 * h) + ymin
ar.append(Arec(sx, sy, tp))
if len(ar) > 0 and aimbot.BoolShot(600):
ar.sort()
for i in ar:
print(i.x, i.y, i.dis, i.tp, i.disok())
if ar[0].disok():
aimbot.shot(ar[0].x, ar[0].y)
# exit()
def Detect():
global detections
darknet_image = darknet.make_image(darknet.network_width(netMain),
darknet.network_height(netMain), 3)
i = 0
while True:
prev_time = time.time()
i += 1
if i % 1000 == 0:
i = 0
print('averge {}ms'.format(int(1000 * (time.time() - prev_time)) / 1000))
prev_time = time.time()
st = np.array(sct.grab(monitor))
stime = time.time()
frame_rgb = cv2.cvtColor(st, cv2.COLOR_BGR2RGB)
frame_resized = cv2.resize(frame_rgb,
(darknet.network_width(netMain),
darknet.network_height(netMain)),
interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())
detections = darknet.detect_image(
netMain, metaMain, darknet_image, thresh=0.25)
image = cvDrawBoxes(detections, frame_resized,
1 / (time.time() - prev_time))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imshow('Demo', image)
cv2.waitKey(3)
if win32api.GetKeyState(0x14) == 1 and len(detections) > 0:
pdetections(detections)
# send.send(detections)
sct = mss.mss()
if __name__ == "__main__":
# thread_monitorkeyboard = threading.Thread(target=pdetections)
# thread_monitorkeyboard.daemon = True
# thread_monitorkeyboard.start()
# recv, send = Pipe(duplex=False)
# shot = Process(target=pdetections, args=(recv,))
# shot.daemon = True
# shot.start()
# zx = int(input())
# zy = int(input())
zx = 960
zy = 540
monitor['left'] = zx - int(Yolowidth / 2)
monitor['top'] = zy - int(Yolowidth / 2)
monitor['width'] = Yolowidth
monitor['height'] = Yolowidth
print(monitor)
init()
Detect()
print("sb")