-
Notifications
You must be signed in to change notification settings - Fork 0
/
ikalivecontrol3.py
597 lines (527 loc) · 24.9 KB
/
ikalivecontrol3.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# IkaLiveControl3
# Copyright (c) 2022-2023 UMEZAWA Takeshi
# Licensed under GNU GPL version 2 or later
# キャプボあるいは動画ファイルを開いて観戦動画配信支援
import cv2
import numpy as np
import argparse
from time import sleep
import yaml
import pyautogui
import os
from ctypes import windll, create_unicode_buffer, WINFUNCTYPE, c_bool, c_int, POINTER
OBS_SCENE_LOBBY_HOTKEY = ["scrolllock"]
OBS_SCENE_BATTLE_HOTKEY = ["pause"]
OBS_SCENE_CHANGE_DELAY = 1
RULES = ["nawabari", "area", "yagura", "hoko", "asari"]
# ステージリストは さんぽ や プラベ のステージ選択画面と同じ並びにしてある
STAGES = [
"yunohana", "gonzui", "yagara", "mategai",
"namerou", "masaba", "kinmedai", "mahimahi",
"ama", "chouzame", "zatou", "sumeshi",
"kusaya", "hirame"]
PHASE_WAIT_JOIN = 0 # 「メンバーの合流を待っています」を検出した後の状態
# - 「親がチームを決めています」になった後も PHASE_WAIT_JOIN のままだが気にしてはいけない
# - 「メンバーの準備を待っています」を検出したら PHASE_WAIT_READY へ
PHASE_WAIT_READY = 1 # 「メンバーの準備を待っています」を検出した後の状態
# - ルールとステージの検出処理が走る
# - 試合のオープニングを検出したら PHASE_OPENING へ
PHASE_OPENING = 2 # 試合のオープニング(ルールとステージが表示されるやつ)を検出した後の状態
# - ルールとステージの検出がまだだったら検出処理が走る
# - 試合の開始を検出したら PHASE_GAME へ
# 正確には試合が開始して1秒経過したことを検出したら。
# テンプレートマッチングとしては残り時間の分の部分が 4 または 2 になっていることを検出する
PHASE_GAME = 3 # 試合の開始を検出した後の状態
# - Finish! を検出したら PHASE_FINISH へ
PHASE_FINISHED = 4 # Finish! を検出した後の状態
# - 勝利/敗北 を検出したら PHASE_RESULT へ
PHASE_RESULT = 5 # 勝利/敗北 を検出した後の状態
# どの phase でも、「メンバーの合流を待っています」を検出したら PHASE_WAIT_JOIN へ
# - 試合結果を検出する前に「メンバーの合流を待っています」を検出したら無効試合扱い
SCENE_LOBBY = 0
SCENE_BATTLE = 1
current_phase = PHASE_WAIT_JOIN
current_scene = SCENE_LOBBY
# 画像のテンプレートマッチングを行うクラス
# 640x360 にリサイズして指定した閾値で白黒二値化した状態で、指定した範囲についてマッチングを行う
class Matcher:
BLACK = 4
DARK = 32
LIGHT = 224
WHITE = 252
THRESHOLDS = [BLACK, DARK, LIGHT, WHITE]
@staticmethod
def pre_filter(src, threshold):
tmp = src
# BLACK と WHITE は完全な黒および白を検出する
#if threshold == Matcher.BLACK or threshold == Matcher.WHITE:
# _, tmp = cv2.threshold(tmp, threshold, 255, cv2.THRESH_BINARY)
tmp = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)
_, tmp = cv2.threshold(tmp, threshold, 255, cv2.THRESH_BINARY)
return tmp
def __init__(self, filename, top, left, bottom, right, threshold):
self.top = top
self.left = left
self.bottom = bottom
self.right = right
self.pixels = (bottom - top) * (right - left)
self.threshold = threshold
tmp = cv2.imread(filename)
tmp = Matcher.pre_filter(tmp, threshold)
tmp = cv2.resize(tmp, (640, 360), cv2.INTER_CUBIC)
tmp = self.crop(tmp)
self.img = tmp
def crop(self, src):
return src[self.top:self.bottom, self.left:self.right]
# マッチしたかどうかを返す
def match(self, src, return_diff=False):
tgt = self.crop(src[self.threshold])
diff = cv2.absdiff(tgt, self.img).sum() * 1.0 / self.pixels
matched = diff < args.matcher_threshold
if return_diff:
return (matched, diff)
else:
return matched
# Matcher のリストを与えて、どれかとマッチしたかどうかを返す
@staticmethod
def list_match(matchers, src):
for matcher in matchers:
if matcher.match(src):
return True
return False
def cv2putText(img, text, color, org):
cv2.putText(img, text, org, cv2.FONT_HERSHEY_PLAIN, 1, color, 5)
cv2.putText(img, text, org, cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
lobby_wait_join_matcher = Matcher("templates/lobby_wait_join.png", 13, 240, 33, 400, Matcher.LIGHT)
lobby_wait_ready_matchers = []
lobby_wait_ready_matchers.append(Matcher("templates/lobby_wait_ready_0.png", 13, 240, 33, 400, Matcher.LIGHT))
lobby_wait_ready_matchers.append(Matcher("templates/lobby_wait_ready_1.png", 24, 348, 37, 406, Matcher.LIGHT))
result_win_matcher = Matcher("templates/result_win.png", 17, 10, 45, 100, Matcher.LIGHT)
result_lose_matcher = Matcher("templates/result_lose.png", 17, 10, 45, 100, Matcher.LIGHT)
result_nogame_matcher = Matcher("templates/result_nogame.png", 81, 402, 93, 475, Matcher.LIGHT)
result_matchers = {}
result_matchers["alpha"] = result_win_matcher
result_matchers["bravo"] = result_lose_matcher
result_matchers["nogame"] = result_nogame_matcher
started_matchers = []
started_matchers.append(Matcher("templates/started_0a.png", 18, 304, 31, 318, Matcher.LIGHT))
started_matchers.append(Matcher("templates/started_0b.png", 18, 304, 31, 318, Matcher.LIGHT))
started_matchers.append(Matcher("templates/started_1a.png", 18, 304, 31, 318, Matcher.LIGHT))
started_matchers.append(Matcher("templates/started_1b.png", 18, 304, 31, 318, Matcher.LIGHT))
opening_matchers = []
rule_matchers = {}
for r in RULES:
rule_matchers[r] = []
rule_matchers[r].append(Matcher("templates/lobby_rule_{0}.png".format(r), 137, 78, 153, 161, Matcher.DARK))
matcher = Matcher("templates/opening_rule_{0}.png".format(r), 223, 243, 242, 396, Matcher.LIGHT)
rule_matchers[r].append(matcher)
opening_matchers.append(matcher)
stage_matchers = {}
for s in STAGES:
stage_matchers[s] = []
stage_matchers[s].append(Matcher("templates/lobby_stage_{0}.png".format(s), 195, 140, 210, 210, Matcher.LIGHT))
# スメーシーは水面のきらめきが邪魔で Matcher.WHITE でも検出できない(いい感じに閾値を下回らない)ので、 Matcher.BLACK を併用する。
# 同じく水面がきらめいているマヒマヒはセーフだったのだが。
for t in [Matcher.WHITE, Matcher.BLACK]:
stage_matchers[s].append(Matcher("templates/opening_stage_{0}.png".format(s), 327, 505, 347, 627, t))
blank_matcher = Matcher("templates/blank.png", 90, 160, 270, 480, Matcher.BLACK)
finish_matchers = []
finish_matchers.append(Matcher("templates/finish_0a.png", 215, 339, 249, 410, Matcher.DARK))
finish_matchers.append(Matcher("templates/finish_0b.png", 215, 339, 249, 410, Matcher.DARK))
rule_images = {}
for r in RULES:
rule_images[r] = cv2.imread("images/rule_{0}.png".format(r), cv2.IMREAD_UNCHANGED)
stage_images = {}
for s in STAGES:
stage_images[s] = cv2.imread("images/stage_{0}.png".format(s), cv2.IMREAD_UNCHANGED)
stage_unknown_image = cv2.imread("images/stage_unknown.png", cv2.IMREAD_UNCHANGED)
number_images = {}
for i in range(10):
number_images["{}".format(i)] = cv2.imread("images/number_{0}.png".format(i), cv2.IMREAD_UNCHANGED)
ika_images = {}
for i in ["alpha", "bravo"]:
ika_images[i] = cv2.imread("images/ika_{0}.png".format(i), cv2.IMREAD_UNCHANGED)
banner_lobby_image = cv2.imread("images/banner_lobby.png", cv2.IMREAD_UNCHANGED)
banner_battle_image = cv2.imread("images/banner_battle.png", cv2.IMREAD_UNCHANGED)
argparser = argparse.ArgumentParser()
argparser.add_argument('--video-file', dest='video_file_name', help='specify input video file')
argparser.add_argument('--video-capture', dest='video_capture_id', type=int, help='specify input video capture device id')
argparser.add_argument('--capture-size', dest='capture_size', type=str, help='video capture size in width,height')
argparser.add_argument('--capture-fps', dest='capture_fps', type=float, help='video capture fps')
argparser.add_argument('--progress-file', dest='progress_file_name', help='specify progress file')
argparser.add_argument('--control-obs', dest='control_obs', action='store_true', help='control OBS')
argparser.add_argument('--control-obs-windows', dest='control_obs_windows', action='store_true', help='control OBS with Windows manner')
argparser.add_argument('--output-dir', dest='output_dir_name', help='specify image output directory')
argparser.add_argument('--test-matchers', dest='test_matchers', action='store_true', help='test matchers')
argparser.add_argument('--decimate-frames', dest='decimate_frames', action='store_true', help='decimate frames to decrease cpu load')
argparser.add_argument('--matcher-threshold', dest='matcher_threshold', type=float, default=2.0, help='template matching threshold')
args = argparser.parse_args()
#print(args)
if not args.video_file_name is None:
cap = cv2.VideoCapture(args.video_file_name)
elif not args.video_capture_id is None:
cap = cv2.VideoCapture(args.video_capture_id)
else:
cap = cv2.VideoCapture(0)
if not args.capture_size is None:
size = args.capture_size.split(",")
if len(size) != 2:
raise ValueError("'{}' is not size".format(args.capture_size))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, float(size[0]))
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, float(size[1]))
if not args.capture_fps is None:
cap.set(cv2.CAP_PROP_FPS, args.capture_fps)
print("capture size = {}x{}".format(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
fps = cap.get(cv2.CAP_PROP_FPS)
frame_interval = 1
print("frame rate = {:5.2f}fps".format(fps))
if args.decimate_frames and fps > 15.0:
frame_interval = int(fps / 12.5) # 29.97fps 系や 25fps 系だった場合に備えて 15 ではなく 12.5 で割る
print("check every {} frames".format(frame_interval))
if args.progress_file_name is None:
progress = {}
else:
with open(args.progress_file_name) as file:
progress = yaml.safe_load(file)
if not "teams" in progress or not isinstance(progress["teams"], dict):
progress["teams"] = {}
teams = progress["teams"]
if not "alpha" in teams or not isinstance(teams["alpha"], str):
teams["alpha"] = ""
if not "bravo" in teams or not isinstance(teams["bravo"], str):
teams["bravo"] = ""
if not "alpha_short_image" in teams or not isinstance(teams["alpha_short_image"], str):
teams["alpha_short_image"] = "images/team_alpha_short.png"
if not "bravo_short_image" in teams or not isinstance(teams["bravo_short_image"], str):
teams["bravo_short_image"] = "images/team_bravo_short.png"
if not "alpha_long_image" in teams or not isinstance(teams["alpha_long_image"], str):
teams["alpha_long_image"] = "images/team_alpha_long.png"
if not "bravo_long_image" in teams or not isinstance(teams["bravo_long_image"], str):
teams["bravo_long_image"] = "images/team_bravo_long.png"
if not "games" in progress or not isinstance(progress["games"], list):
progress["games"] = []
games = progress["games"]
if not "options" in progress or not isinstance(progress["options"], dict):
progress["options"] = {}
options = progress["options"]
if not "keep_nogame" in options or not isinstance(options["keep_nogame"], bool):
options["keep_nogame"] = False
if not "advantages" in progress or not isinstance(progress["advantages"], dict):
progress["advantages"] = {}
advantages = progress["advantages"]
if not "alpha" in advantages or not isinstance(advantages["alpha"], int):
advantages["alpha"] = 0
if not "bravo" in advantages or not isinstance(advantages["bravo"], int):
advantages["bravo"] = 0
alpha_long_image = cv2.imread(teams["alpha_long_image"], cv2.IMREAD_UNCHANGED)
bravo_long_image = cv2.imread(teams["bravo_long_image"], cv2.IMREAD_UNCHANGED)
alpha_short_image = cv2.imread(teams["alpha_short_image"], cv2.IMREAD_UNCHANGED)
bravo_short_image = cv2.imread(teams["bravo_short_image"], cv2.IMREAD_UNCHANGED)
if advantages["alpha"] > 0:
alpha_advantage_image = cv2.imread("images/advantage_{}.png".format(advantages["alpha"]), cv2.IMREAD_UNCHANGED)
if advantages["bravo"] > 0:
bravo_advantage_image = cv2.imread("images/advantage_{}.png".format(advantages["bravo"]), cv2.IMREAD_UNCHANGED)
EnumWindows = windll.user32.EnumWindows
PostMessage = windll.user32.PostMessageW
GetWindowText = windll.user32.GetWindowTextW
GetClassName = windll.user32.GetClassNameW
WM_KEYDOWN = 0x100
WM_KEYUP = 0x101
VK_PAUSE = 0x13
VK_SCROLL = 0x91
EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int))
hwnd_obs = None
if args.control_obs_windows:
def enum_windows_callback(hwnd, lparam):
global hwnd_obs
bufWindowText = create_unicode_buffer(256)
bufClassName = create_unicode_buffer(256)
GetWindowText(hwnd, bufWindowText, 256)
GetClassName(hwnd, bufClassName, 256)
if bufWindowText.value.find("OBS ") == 0 and bufClassName.value.find("QWindow") != -1:
hwnd_obs = hwnd
return False
return True
EnumWindows(EnumWindowsProc(enum_windows_callback), 0)
if hwnd_obs is None:
print("OBS window not found: falling back to pyautogui manner.")
args.control_obs_windows = False
args.control_obs = True
else:
print("OBS window found: using Windows manner.")
# 画像を貼り付ける
# 貼り付ける画像が RGBA 画像である場合は、アルファブレンディングを行う。
# 制限事項: はみ出すような貼り付けを行おうとするとコケる。
def paste_image(oimg, iimg, y, x):
h = iimg.shape[0]
w = iimg.shape[1]
if iimg.shape[2] == 3:
oimg[y:y+h, x:x+w] = iimg
elif oimg.shape[2] == 3:
oimg[y:y+h, x:x+w] = oimg[y:y+h, x:x+w] * (1 - iimg[:, :, 3:] / 255) + iimg[:, :, :3] * (iimg[:, :, 3:] / 255)
else:
ialpha = iimg[:, :, 3:].astype(float)
itransparency_scaled = 1 - ialpha / 255
oalpha = oimg[y:y+h, x:x+w, 3:].astype(float)
new_oalpha = iimg[:, :, 3:] + oalpha * itransparency_scaled
new_orgb = (iimg[:, :, :3] * ialpha + oimg[y:y+h, x:x+w, :3] * itransparency_scaled * new_oalpha)
new_orgb = np.divide(new_orgb, new_oalpha, out=np.zeros_like(new_orgb), where=new_oalpha!=0)
oimg[y:y+h, x:x+w] = np.block([new_orgb, new_oalpha])
# 画像をグレースケール化する
# RGB 画像を与えた場合、それをグレースケール化した RGB 画像を返す。3つのチャンネルは同じ値になる。
# RGBA 画像を与えた場合、RGB 部分をグレースケール化した RGB 画像に元のアルファを結合した画像を返す。
def cvt_grayscale(img):
if img.shape[2] == 3:
return cv2.cvtColor(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
else:
return np.block([cv2.cvtColor(cv2.cvtColor(img[:, :, :3], cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR), img[:, :, 3:]])
def draw_progress():
lobby_frame[:] = 0
battle_banner[:] = 0
paste_image(lobby_frame, banner_lobby_image, 0, 0)
paste_image(battle_banner, banner_battle_image, 0, 0)
wins = {"alpha": advantages["alpha"], "bravo": advantages["bravo"]}
for game in games:
if "result" in game and game["result"] != "nogame":
wins[game["result"]] += 1
s = "{}".format(wins["alpha"])
for k in range(len(s)):
paste_image(lobby_frame, number_images[s[k]], 72-48//2, 720+48-len(s)*16+32*k)
paste_image(battle_banner, number_images[s[k]], 72-48//2, 240+48-len(s)*16+32*k)
if advantages["alpha"] > 0:
paste_image(lobby_frame, alpha_advantage_image, 72+48//2, 720+48-16)
paste_image(battle_banner, alpha_advantage_image, 72+48//2, 240+48-16)
s = "{}".format(wins["bravo"])
for k in range(len(s)):
paste_image(lobby_frame, number_images[s[k]], 72-48//2, 1920-(720+48)-len(s)*16+32*k)
paste_image(battle_banner, number_images[s[k]], 72-48//2, 1920-(240+48)-len(s)*16+32*k)
if advantages["bravo"] > 0:
paste_image(lobby_frame, bravo_advantage_image, 72+48//2, 1920-(720+48)-16)
paste_image(battle_banner, bravo_advantage_image, 72+48//2, 1920-(240+48)-16)
paste_image(lobby_frame, alpha_long_image, 24, 0)
paste_image(lobby_frame, bravo_long_image, 24, 1920-720)
paste_image(battle_banner, alpha_short_image, 24, 0)
paste_image(battle_banner, bravo_short_image, 24, 1920-240)
numlist = 8
base = max(0, len(games) - numlist)
for i in range(numlist):
j = i + base
if j >= len(games):
break
top = 120+i*120
game = games[j]
nogame = "result" in game and game["result"] == "nogame"
if "stage" in game:
tmp = stage_images[game["stage"]]
if nogame:
tmp = cvt_grayscale(tmp)
else:
tmp = stage_unknown_image
paste_image(lobby_frame, tmp, top, 8)
if "rule" in game:
tmp = rule_images[game["rule"]]
if nogame:
tmp = cvt_grayscale(tmp)
paste_image(lobby_frame, tmp, top+8, 160)
if "result" in game and game["result"] != "nogame":
paste_image(lobby_frame, ika_images[game["result"]], top+60, 160)
s = "{}".format(j+1)
for k in range(len(s)):
paste_image(lobby_frame, number_images[s[k]], top+8, 16+32*k)
if not args.output_dir_name is None:
cv2.imwrite(os.path.join(args.output_dir_name, "lobby_frame.tmp.png"), lobby_frame)
cv2.imwrite(os.path.join(args.output_dir_name, "battle_banner.tmp.png"), battle_banner)
# PermissionError: [WinError 5] アクセスが拒否されました。 が出ることがある(たぶん OBS が読み込みのみ共有で開いてる)のでリトライする。
def replace_with_retry(src_base, dst_base):
num_try = 10
for i in range(num_try):
try:
os.replace(os.path.join(args.output_dir_name, src_base), os.path.join(args.output_dir_name, dst_base))
break
except PermissionError as e:
#print(e)
if i == num_try - 1:
throw
sleep(0.02)
replace_with_retry("lobby_frame.tmp.png", "lobby_frame.png")
replace_with_retry("battle_banner.tmp.png", "battle_banner.png")
outframe[:] = 128
paste_image(outframe, lobby_frame, 0, 0)
paste_image(outframe, battle_banner, 1080, 0)
def write_progress():
if not args.progress_file_name is None:
with open(args.progress_file_name, "w") as file:
yaml.dump(progress, file, sort_keys=False)
draw_progress()
#print(yaml.dump(progress, sort_keys=False))
lobby_frame = np.zeros((1080, 1920, 4), np.uint8) # RGBA, 1920x1080, 透明
battle_banner = np.zeros((120, 1920, 4), np.uint8) # RGBA, 1920x120, 透明
outframe = np.zeros((1200, 1920, 3), np.uint8) # RGB24, 1920x1200, 真っ黒
img = {}
draw_progress()
while True:
for i in range(frame_interval):
ret, captured = cap.read()
if not ret:
print("cannot read from capture")
break
if not ret:
break
#for i in Matcher.THRESHOLDS:
# img[i] = cv2.resize(Matcher.pre_filter(captured, i), (640, 360), cv2.INTER_CUBIC)
captured = cv2.resize(captured, (640, 360), cv2.INTER_CUBIC)
for i in Matcher.THRESHOLDS:
img[i] = Matcher.pre_filter(captured, i)
if current_phase == PHASE_WAIT_JOIN:
if Matcher.list_match(lobby_wait_ready_matchers, img):
print("detected wait_ready")
current_phase = PHASE_WAIT_READY
if len(games) > 0:
last_game = games[len(games)-1]
if "result" in last_game and last_game["result"] == "nogame" and not options["keep_nogame"]:
del games[-1]
current_game = {}
games.append(current_game)
write_progress()
elif current_phase == PHASE_WAIT_READY or current_phase == PHASE_OPENING:
if current_phase == PHASE_WAIT_READY:
if Matcher.list_match(opening_matchers, img):
print("detected opening")
current_phase = PHASE_OPENING
else: # PHASE_OPENING
if Matcher.list_match(started_matchers, img):
print("detected started")
current_phase = PHASE_GAME
# stage_matchers と rule_matchers はロビー用とオープニング用で分けた方が処理負荷が下がるが、めんどくさいのでそうしていない。
if not "stage" in current_game:
for s in STAGES:
if Matcher.list_match(stage_matchers[s], img):
print("detected stage {}".format(s))
current_game["stage"] = s
if "rule" in current_game:
write_progress()
break
if not "rule" in current_game:
for r in RULES:
if Matcher.list_match(rule_matchers[r], img):
print("detected rule {}".format(r))
current_game["rule"] = r
if "stage" in current_game:
write_progress()
break
elif current_phase == PHASE_GAME:
if Matcher.list_match(finish_matchers, img):
print("detected finish")
current_phase = PHASE_FINISHED
elif current_phase == PHASE_FINISHED:
if not "result" in current_game:
for i in list(result_matchers):
if result_matchers[i].match(img):
print("detected result {}".format(i))
current_phase = PHASE_RESULT
current_game["result"] = i
write_progress()
break
else:
pass # XXX
if current_phase == PHASE_WAIT_READY:
if current_scene != SCENE_BATTLE and blank_matcher.match(img):
print("detected blank to battle")
current_scene = SCENE_BATTLE
if args.control_obs or args.control_obs_windows:
sleep(OBS_SCENE_CHANGE_DELAY)
if args.control_obs:
pyautogui.hotkey(*OBS_SCENE_BATTLE_HOTKEY, interval=0.01)
if args.control_obs_windows:
PostMessage(hwnd_obs, WM_KEYDOWN, VK_PAUSE, 1);
PostMessage(hwnd_obs, WM_KEYUP, VK_PAUSE, 0xc0000001);
elif current_phase == PHASE_GAME or current_phase == PHASE_RESULT:
if current_scene != SCENE_LOBBY and blank_matcher.match(img):
print("detected blank to lobby")
current_scene = SCENE_LOBBY
if args.control_obs or args.control_obs_windows:
sleep(OBS_SCENE_CHANGE_DELAY)
if args.control_obs:
pyautogui.hotkey(*OBS_SCENE_LOBBY_HOTKEY, interval=0.01)
if args.control_obs_windows:
PostMessage(hwnd_obs, WM_KEYDOWN, VK_SCROLL, 1);
PostMessage(hwnd_obs, WM_KEYUP, VK_SCROLL, 0xc0000001);
# 念のため、どの状態からも PHASE_WAIT_JOIN に戻れるようにしておく
if current_phase != PHASE_WAIT_JOIN:
if lobby_wait_join_matcher.match(img):
print("detected wait_join")
current_phase = PHASE_WAIT_JOIN
if current_scene != SCENE_LOBBY:
current_scene = SCENE_LOBBY
if args.control_obs:
pyautogui.hotkey(*OBS_SCENE_LOBBY_HOTKEY, interval=0.01)
if args.control_obs_windows:
PostMessage(hwnd_obs, WM_KEYDOWN, VK_SCROLL, 1);
PostMessage(hwnd_obs, WM_KEYUP, VK_SCROLL, 0xc0000001);
# ロビーに戻ってきたのに結果が検出されていない場合は無効試合扱いにしておく
if not "result" in current_game:
current_game["result"] = "nogame"
write_progress()
# 出力バッファに貼り付け
outframe[180:540, 320:960] = captured
for i in range(3):
outframe[540:900, 320:960, i] = img[Matcher.DARK]
outframe[540:900, 960:1600, i] = img[Matcher.BLACK]
outframe[180:540, 960:1600, i] = img[Matcher.WHITE]
# Matcher の動作確認
if args.test_matchers:
def match_color(b):
if b:
return (255, 0, 0)
else:
return (0, 0, 0)
for i in range(len(STAGES)):
s = ""
mm = False
for matcher in stage_matchers[STAGES[i]]:
m, d = matcher.match(img, return_diff=True)
mm = mm or m
s = s + "{:6.2f} ".format(d)
cv2putText(captured, s + STAGES[i], match_color(mm), (10, 15+i*15))
for i in range(len(RULES)):
s = ""
mm = False
for matcher in rule_matchers[RULES[i]]:
m, d = matcher.match(img, return_diff=True)
mm = mm or m
s = s + "{:6.2f} ".format(d)
cv2putText(captured, s + RULES[i], match_color(mm), (320, 15+i*15))
m, d = lobby_wait_join_matcher.match(img, return_diff=True)
cv2putText(captured, "{:6.2f} wait_join".format(d), match_color(m), (320, 120))
for i in range(len(lobby_wait_ready_matchers)):
m, d = lobby_wait_ready_matchers[i].match(img, return_diff=True)
cv2putText(captured, "{:6.2f} wait_ready_{}".format(d, i), match_color(m), (320, 135+i*15))
m, d = result_win_matcher.match(img, return_diff=True)
cv2putText(captured, "{:6.2f} result_win".format(d), match_color(m), (320, 180))
m, d = result_lose_matcher.match(img, return_diff=True)
cv2putText(captured, "{:6.2f} result_lose".format(d), match_color(m), (320, 195))
m, d = result_nogame_matcher.match(img, return_diff=True)
cv2putText(captured, "{:6.2f} result_nogame".format(d), match_color(m), (320, 210))
ma, da = started_matchers[0].match(img, return_diff=True)
mb, db = started_matchers[1].match(img, return_diff=True)
cv2putText(captured, "{:6.2f} {:6.2f} started_{}".format(da, db, 0), match_color(ma or mb), (320, 240))
ma, da = started_matchers[2].match(img, return_diff=True)
mb, db = started_matchers[3].match(img, return_diff=True)
cv2putText(captured, "{:6.2f} {:6.2f} started_{}".format(da, db, 1), match_color(ma or mb), (320, 255))
ma, da = finish_matchers[0].match(img, return_diff=True)
mb, db = finish_matchers[1].match(img, return_diff=True)
cv2putText(captured, "{:6.2f} {:6.2f} finish_0".format(da, db, 0), match_color(ma or mb), (320, 270))
m, d = blank_matcher.match(img, return_diff=True)
cv2putText(captured, "{:6.2f} blank".format(d), match_color(m), (320, 300))
outframe[180:540, 320:960] = captured
# ウィンドウに描画
cv2.imshow('IkaLiveControl3', outframe)
# q または ESC を押したら終了
key = cv2.waitKey(1)
if key & 0xff == ord('q') or key & 0xff == 0x1b:
break
# r を押したら画像を再出力(OBS がたまに置き換えられたファイルの読み込みに失敗するので)
if key & 0xff == ord('r'):
draw_progress()
cap.release()
cv2.destroyAllWindows()