-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSOULS_DC.py
347 lines (314 loc) · 13.8 KB
/
SOULS_DC.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# from PIL import ImageGrab
from time import sleep
import datetime
import tkinter
import threading
import cv2
import numpy as np
import mss
import os
# testing 67, 67
lower_red1 = (0, 67, 80)
upper_red1 = (5, 255, 255)
lower_red2 = (170, 67, 80)
upper_red2 = (180, 255, 255)
gif_img = "./ELDENRING_TITLE.gif"
gif_frame = 7
you_died = False
death_count = 0
key = 0
global frames
output_path = os.getcwd().replace('\\', '/') + '/YOU_DIED.txt'
try:
f = open(output_path, 'r', encoding='utf-8')
except:
with open(output_path, 'w', encoding='utf-8') as f:
f.write("YOU DIED : 0\n")
else:
died_str = f.readlines()
died_str = died_str[-1].split(' ')
death_count = int(died_str[-1])
finally:
f.close()
class UI(tkinter.Tk):
global death_count
program_quit = 0
__counting_status = 0
def __init__(self):
tkinter.Tk.__init__(self)
self.gif_after = 0
self.gif_frame = gif_frame
self.after(0, self.die_counter)
self.t_label = tkinter.Label(self, text="YOU DIED ", font=("times", "16"))
self.t_label.place(x = 50, y = 260)
self.d_label = tkinter.IntVar(self, death_count)
count_label = tkinter.Label(self, textvariable=self.d_label, font=("times", "16"))
count_label.place(x=200, y=260)
died_label = tkinter.Label(text="Prepare To ELDEN RING", font=("times"))
died_label.place(x=50, y=290)
end_label = tkinter.Label(text="2022.02.25", font=("times"))
end_label.place(x=93, y=417)
up_btn1 = tkinter.Button(self, text="up", width=2, command=self.dc_up)
up_btn1.place(x=202, y=323)
dn_btn1 = tkinter.Button(self, text="dn", width=2, command=self.dc_dn)
dn_btn1.place(x=229, y=323)
button1 = tkinter.Button(self, text="ELDEN RING", width=16, command = self.start_elden_cap)
button1.place(x=5, y=323)
button2 = tkinter.Button(self, text="SOULS", width=16, command = self.start_cap)
button2.place(x=5, y=353)
button3 = tkinter.Button(self, text="STOP", width=16, command = self.check_end1)
button3.place(x=131, y=353)
button4 = tkinter.Button(self, text="SEKIRO", width=16, command=self.start_skr_cap)
button4.place(x=5, y=383)
button5 = tkinter.Button(self, text="RESET", width=16, command=self.reset_data)
button5.place(x=131, y=383)
def die_counter(self):
self.d_label.set(death_count)
# print("die_counter fn : ", self.d_label.get())
self.after(1000, self.die_counter)
def dc_up(self):
global death_count
death_count += 1
self.d_label.set(death_count)
def dc_dn(self):
global death_count
if death_count > 0:
death_count -= 1
self.d_label.set(death_count)
def reset_data(self):
global death_count
death_count = 0
time_now = datetime.datetime.now()
time_str = time_now.strftime("%Y-%m-%d_%H%M%S_") + "YOU DIED : 0\n"
with open(output_path, 'a', encoding='utf-8') as f:
f.write(time_str)
def start_cap(self):
global key
global frames
global gif_img
global gif_frame
self.t_label.config(fg="red")
if self.__counting_status == 0:
self.__counting_status = 1
key = 1
gif_img = "./icon1_save3.gif"
self.gif_frame = 21
frames = [tkinter.PhotoImage(file=gif_img,
format="gif -index {}".format(i)).subsample(2) for i in range(self.gif_frame)]
self.after_cancel(self.gif_after)
self.after(100, self.draw_gif, 0, 1)
death_count_t = DeathCountStart("souls", self.program_quit)
death_count_t.daemon = True
death_count_t.start()
def start_elden_cap(self):
global key
global frames
global gif_img
global gif_frame
self.t_label.config(fg="red")
if self.__counting_status == 0:
self.__counting_status = 1
key = 1
gif_img = "./ELDENRING_TITLE.gif"
self.gif_frame = 7
frames = [tkinter.PhotoImage(file=gif_img,
format="gif -index {}".format(i)).subsample(2) for i in range(self.gif_frame)]
self.after_cancel(self.gif_after)
self.after(100, self.draw_gif, 0, 1)
death_count_t = DeathCountStart("ring", self.program_quit)
death_count_t.daemon = True
death_count_t.start()
def start_skr_cap(self):
global key
global frames
global gif_img
global gif_frame
self.t_label.config(fg="red")
if self.__counting_status == 0:
self.__counting_status = 1
key = 1
gif_img = "./icon1_save3.gif"
self.gif_frame = 21
frames = [tkinter.PhotoImage(file=gif_img,
format="gif -index {}".format(i)).subsample(2) for i in range(self.gif_frame)]
self.after_cancel(self.gif_after)
self.after(100, self.draw_gif, 0, 1)
death_count_t = DeathCountStart("sekiro", self.program_quit)
death_count_t.daemon = True
death_count_t.start()
def check_end1(self):
global death_count
global frames
global key
self.t_label.config(fg="black")
frames = [tkinter.PhotoImage(file=gif_img,
format="gif -index 0").subsample(2)]
self.after_cancel(self.gif_after)
self.after(100, self.draw_gif, 0, 0)
if key == 1:
key = 0
self.__counting_status = 0
time_now = datetime.datetime.now()
time_str = time_now.strftime("%Y-%m-%d_%H%M%S_") + "YOU DIED : " + str(death_count) + "\n"
with open(output_path, 'a', encoding='utf-8') as f:
f.write(time_str)
def draw_gif(self, idx, flag):
if idx == self.gif_frame:
idx = 0
frame = frames[idx]
gif_label.configure(image=frame)
if flag == 0:
return 0
idx += 1
self.after_cancel(self.gif_after)
self.gif_after = self.after(100, self.draw_gif, idx, 1)
class DeathCountStart(threading.Thread):
def __init__(self, game_type, program_quit):
threading.Thread.__init__(self)
self.game_type = game_type
self.program_quit = program_quit
def run(self):
global key
with mss.mss() as scr:
while key == 1:
# start = time.time()
# __red color mask__
# __DARK SOULS 1, 2, 3__ __DEMONS SOULS REMAKE__ __SEKIRO SHADOW DIE TWICE__
# img_ori = cv2.cvtColor(np.array(ImageGrab.grab()), cv2.COLOR_BGRA2RGB)
img_ori = np.array(scr.grab(scr.monitors[1]))
original_image = img_ori
img_ori = cv2.resize(img_ori, dsize=(960, 540), interpolation=cv2.INTER_AREA)
img_hsv = cv2.cvtColor(img_ori, cv2.COLOR_BGR2HSV)
img_mask = cv2.inRange(img_hsv, lower_red1, upper_red1)
img_mask2 = cv2.inRange(img_hsv, lower_red2, upper_red2)
img_mask3 = img_mask + img_mask2
# img_result = cv2.bitwise_and(img_ori, img_ori, mask=img_mask3)
contours, _ = cv2.findContours(img_mask3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
height, width, channels = img_ori.shape
w_rate = width / 1920
h_rate = height / 1080
# 사각형 박스
fX = []
D = []
contour_array = []
blue_box = []
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt) # 수직인 사각형 생성
if self.game_type == "souls" \
and 70 * h_rate < h < 140 * h_rate \
and 10 * w_rate < w < 110 * w_rate:
contour_array.append([x, y, w, h])
elif self.game_type == "sekiro" and 200 < h < 350 and 50 < w < 350:
contour_array.append([x, y, w, h])
elif self.game_type == "ring" and 50 * h_rate < h < 80 * h_rate and 10 * w_rate < w < 80 * w_rate:
contour_array.append([x, y, w, h])
# __ sort countours
for i in range(len(contour_array) - 1):
index = i
for j in range(i + 1, len(contour_array)):
# print(centers[j])
if contour_array[index][0] > contour_array[j][0]:
index = j
contour_array[i], contour_array[index] = contour_array[index], contour_array[i]
# __ y position average
contour_y_sum = 0
avg = 0
if self.game_type == "souls" or self.game_type == "ring":
delete_array = []
for i in range(len(contour_array)):
contour_y_sum = contour_y_sum + contour_array[i][1]
try:
avg = int(contour_y_sum / len(contour_array))
except ZeroDivisionError:
avg = 0
# print("avg : ", avg)
for i in range(len(contour_array)):
# __ contour_array[i][1] : contours y position
sub = avg - contour_array[i][1]
if sub < 0: # sub = int(np.sqrt(sub * sub))
sub *= -1
if sub > 80 * h_rate:
delete_array.append(i)
for i in reversed(delete_array):
del contour_array[i]
delete_array.clear()
contour_y_sum = 0
for i in range(len(contour_array)):
contour_y_sum = contour_y_sum + contour_array[i][1]
try:
avg = int(contour_y_sum / len(contour_array))
except ZeroDivisionError:
avg = 0
# print("len contour_array : ", len(contour_array))
# print("contour_array : ", contour_array)
for i in range(len(contour_array)):
# print("i : ", i)
# __ (average - contour_y position) < 50 ? bluebox.append : next
sub = avg - contour_array[i][1]
if sub < 0: # sub = int(np.sqrt(sub * sub))
sub *= -1
if sub < 50 * h_rate:
blue_box.append(contour_array[i])
# __debug : check bounding box
# cv2.rectangle(
# img_result,
# (contour_array[i][0], contour_array[i][1]),
# (contour_array[i][0] + contour_array[i][2], contour_array[i][1] + contour_array[i][3]),
# (255, 0, 0), 2)
# ### contour 사이의 거리[D]를 구함 & contour 들의 수직 비율이 일정해야 함.
if len(blue_box) >= 2: # ##if len(centers) >= 2: if len(contour_array) >= 2:
for idx in range(len(blue_box) - 1):
dx = blue_box[idx][0] - blue_box[idx + 1][0]
dy = blue_box[idx][1] - blue_box[idx + 1][1]
D.append(int(np.sqrt(dx * dx + dy * dy)))
# print("D : ", D)
for i in range(len(D)):
if self.game_type == "souls" and D[i] < 160 * w_rate: # __ souls
fX.append(D[i])
elif self.game_type == "sekiro" and 160 < D[i] < 165: # __ sekiro
fX.append(D[i])
elif self.game_type == "ring" and D[i] < 50 * w_rate * 2:
fX.append(D[i])
# print(fX)
if self.game_type == "souls" and 5 < len(fX) < 9: # __ souls
self.save_death_count(original_image)
elif self.game_type == "sekiro" and len(fX) == 1: # __ sekiro
self.save_death_count(original_image)
elif self.game_type == "ring" and 5 < len(fX) < 9: # __ elden ring
self.save_death_count(original_image)
contour_array.clear()
blue_box.clear()
D.clear()
fX.clear()
# print("FPS : {:.2f}".format(1 / (time.time() - start)))
# __OCR test__
# text_img = pytesseract.image_to_string(img_result, lang='eng')
# print(text_img)
# __debug only__
# img_resize = cv2.resize(img_result, dsize=(0, 0), fx=0.5, fy=0.5)
# cv2.imshow("capture", img_resize)
# cv2.waitKey(1)
# if key == 0:
# cv2.destroyAllWindows()
# break
def save_death_count(self, original_image):
global death_count
global output_path
death_count += 1
with open(output_path, 'w', encoding='utf-8') as f:
f.write("YOU DIED : " + str(death_count) + "\n")
time_now = datetime.datetime.now()
time_path = time_now.strftime("%Y-%m-%d_%H%M%S_")
save_img_path = "save_img/" + time_path + str(death_count) + ".png"
cv2.imwrite(save_img_path, original_image)
sleep(5)
window = UI()
window.title("유다희")
window.geometry("260x450+0+0")
frames = [tkinter.PhotoImage(file=gif_img,
format="gif -index 0").subsample(2)]
gif_label = tkinter.Label(window)
gif_label.place(x=0, y=0)
window.gif_after = window.after(0, window.draw_gif, 0, 0)
window.resizable(width = False, height = False)
window.mainloop()