-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSBUFrameAnalyzer.py
211 lines (153 loc) · 6.88 KB
/
SSBUFrameAnalyzer.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
from skimage.feature import hog
import cv2
import json
# import numpy as np
from SSBUDigitClassifier import SSBUDigitClassifier
from SSBUNameRecognizer import SSBUNameRecognizer
from SSBUCharaClassifier import SSBUCharaClassifier
from SSBUStockClassifier import SSBUStockClassifier
import SSBUBoundingBoxUtil
class SSBUFrameAnalyzer:
def __init__(self, digit_classifier, name_recognizer, chara_classifier, stock_classifier):
self.digit_classifier = digit_classifier
self.name_recognizer = name_recognizer
self.chara_classifier = chara_classifier
self.stock_classifier = stock_classifier
def __call__(self, frame, fighter_num=2):
dmgs = self.analyze_damage(frame, fighter_num=fighter_num)
names = self.analyze_name(frame, fighter_num=fighter_num)
charas = self.analyze_chara(frame, fighter_num=fighter_num)
stocks = self.analyze_stock(frame, fighter_num=fighter_num)
fighters = {}
for fighter_idx in range(fighter_num):
fighters[fighter_idx] = {
'chara_name': charas[fighter_idx],
'name': names[fighter_idx],
'damage': dmgs[fighter_idx],
'stocks': stocks[fighter_idx],
}
result = {
'fighters': fighters,
}
return result
def analyze_damage(self, frame, fighter_num):
fighters_dmg_bboxes = SSBUBoundingBoxUtil.fighters_damage_bboxes(fighter_num=fighter_num)
assert fighter_num == len(fighters_dmg_bboxes)
dc = self.digit_classifier
def predict_digit(img):
digits, dists = dc(img, k=3)
min_dist = dists[0]
thresh_dist = 2.
digit = digits[0] if min_dist < thresh_dist else None
return digit
result = {}
for fighter_idx in range(fighter_num):
bboxes = fighters_dmg_bboxes[fighter_idx]
dmg_str = ''
for bbox_idx, bbox in enumerate(bboxes):
dimg = frame[bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]] # RGB
dimg = cv2.cvtColor(dimg, cv2.COLOR_BGR2GRAY) # GRAY
dimg = cv2.resize(dimg, (35, 55)) # FIXME: magic number
# cv2.imwrite('fighter-dmg-%d-%d.png' % (fighter_idx, bbox_idx, ), dimg)
digit = predict_digit(dimg)
if digit is None:
digit = 0 # placeholder
end = bbox_idx == len(bboxes)-1
digit_str = str(digit)
if end:
digit_str = '.' + digit_str
dmg_str += digit_str
dmg = float(dmg_str)
result[fighter_idx] = dmg
return result
def analyze_name(self, frame, fighter_num):
fighters_name_bbox = SSBUBoundingBoxUtil.fighters_name_bbox(fighter_num=fighter_num)
assert fighter_num == len(fighters_name_bbox)
nr = self.name_recognizer
result = {}
for fighter_idx in range(fighter_num):
bbox = fighters_name_bbox[fighter_idx]
nimg = frame[bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]] # RGB
nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2GRAY) # GRAY
# cv2.imwrite('fighter-name-%d.png' % (fighter_idx, ), nimg)
name = nr(nimg)
result[fighter_idx] = name
return result
def analyze_chara(self, frame, fighter_num):
fighters_chara_bbox = SSBUBoundingBoxUtil.fighters_chara_bbox(fighter_num=fighter_num)
assert fighter_num == len(fighters_chara_bbox)
cc = self.chara_classifier
def predict_chara(img):
names, dists = cc(img, k=3)
min_dist = dists[0]
# print(names, dists)
thresh_dist = 10.
name = names[0] if min_dist < thresh_dist else None
return name
result = {}
for fighter_idx in range(fighter_num):
bbox = fighters_chara_bbox[fighter_idx]
cimg = frame[bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]] # RGB
cimg = cv2.cvtColor(cimg, cv2.COLOR_BGR2GRAY) # GRAY
# cv2.imwrite('fighter-face-%d.png' % (fighter_idx, ), cimg)
name = predict_chara(cimg)
result[fighter_idx] = name
return result
def analyze_stock(self, frame, fighter_num):
fighters_stock_bboxes = SSBUBoundingBoxUtil.fighters_stock_bboxes(fighter_num=fighter_num, stock_num=5)
assert fighter_num == len(fighters_stock_bboxes)
sc = self.stock_classifier
def predict_stock(img):
stocks, dists = sc(img, k=3)
min_dist = dists[0]
# print(stocks, dists)
thresh_dist = 0.6
stock = stocks[0] if min_dist < thresh_dist else None
return stock
result = {}
for fighter_idx in range(fighter_num):
bboxes = fighters_stock_bboxes[fighter_idx]
stocks = []
for bbox_idx, bbox in enumerate(bboxes):
simg = frame[bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]] # RGB
simg = cv2.cvtColor(simg, cv2.COLOR_BGR2GRAY) # GRAY
# cv2.imwrite('fighter-stock-%d-%d.png' % (fighter_idx, bbox_idx, ), simg)
stock = predict_stock(simg)
if stock is None:
break
stocks.append(stock)
result[fighter_idx] = stocks
return result
if __name__ == '__main__':
import argparse
import time
import os
parser = argparse.ArgumentParser()
parser.add_argument('digit_dictionary', type=str)
parser.add_argument('chara_dictionary', type=str)
parser.add_argument('stock_dictionary', type=str)
parser.add_argument('input', type=str)
parser.add_argument('fighter_num', type=int) # TODO: predict
args = parser.parse_args()
print('loading digit classifier...')
digit_classifier = SSBUDigitClassifier(feature_json=args.digit_dictionary)
print('loaded digit classifier')
print('loading name recognizer...')
name_recognizer = SSBUNameRecognizer()
print('loaded name recognizer')
print('loading chara classifier...')
chara_classifier = SSBUCharaClassifier(feature_json=args.chara_dictionary)
print('loaded chara classifier')
print('loading stock classifier...')
stock_classifier = SSBUStockClassifier(feature_json=args.stock_dictionary)
print('loaded stock classifier')
analyzer = SSBUFrameAnalyzer(digit_classifier=digit_classifier, name_recognizer=name_recognizer, chara_classifier=chara_classifier, stock_classifier=stock_classifier)
frame = cv2.imread(args.input, 1) # RGB
frame = cv2.resize(frame, (1280, 720))
# print(frame.shape)
assert frame.shape[1] == 1280 and frame.shape[0] == 720
t = time.time()
ret = analyzer(frame, fighter_num=args.fighter_num)
elapsed = time.time() - t
print(ret)
print('FPS: %f (%f s)' % (1/elapsed, elapsed, ))