-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
219 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
def fighters_info_bbox(fighter_num): | ||
assert fighter_num in (2, 3, 4), 'Not implemented' | ||
|
||
# FIXME: magic number | ||
if fighter_num == 2: | ||
return [ | ||
# Fighter 1 | ||
[ 245 ,560, 240,160 ], | ||
# Fighter 2 | ||
[ 245+495,560, 240,160 ], | ||
] | ||
elif fighter_num == 3: | ||
return [ | ||
# Fighter 1 | ||
[ 75 ,560, 240,160 ], | ||
# Fighter 2 | ||
[ 75+416,560, 240,160 ], | ||
# Fighter 3 | ||
[ 75+832,560, 240,160 ], | ||
] | ||
elif fighter_num == 4: | ||
return [ | ||
# Fighter 1 | ||
[ 98 ,560, 240,160 ], | ||
# Fighter 2 | ||
[ 98+272,560, 240,160 ], | ||
# Fighter 3 | ||
[ 98+544,560, 240,160 ], | ||
# Fighter 4 | ||
[ 98+816,560, 240,160 ], | ||
] | ||
|
||
|
||
def fighters_damage_bboxes(fighter_num): | ||
info_bboxes = fighters_info_bbox(fighter_num=fighter_num) | ||
|
||
ret = [] | ||
for fighter_idx, bbox in enumerate(info_bboxes): | ||
left = bbox[0] + 85 | ||
top = bbox[1] + 50 | ||
|
||
ret.append([ | ||
[ left ,top, 35,55 ], | ||
[ left+30,top, 35,55 ], | ||
[ left+60,top, 35,55 ], | ||
[ left+97,top+28, 18,25 ], | ||
]) | ||
return ret | ||
|
||
def fighters_name_bbox(fighter_num): | ||
info_bboxes = fighters_info_bbox(fighter_num=fighter_num) | ||
|
||
ret = [] | ||
for fighter_idx, bbox in enumerate(info_bboxes): | ||
ret.append([ bbox[0]+105,bbox[1]+110, 120,16 ]) | ||
return ret | ||
|
||
def fighters_chara_bbox(fighter_num): | ||
info_bboxes = fighters_info_bbox(fighter_num=fighter_num) | ||
|
||
ret = [] | ||
for fighter_idx, bbox in enumerate(info_bboxes): | ||
ret.append([ bbox[0]+10,bbox[1]+28, 110,110 ]) | ||
return ret | ||
|
||
def fighters_stock_bboxes(fighter_num, stock_num=3): | ||
info_bboxes = fighters_info_bbox(fighter_num=fighter_num) | ||
|
||
ret = [] | ||
for fighter_idx, bbox in enumerate(info_bboxes): | ||
left = bbox[0] + 73 | ||
top = bbox[1] + 131 | ||
|
||
ret.append( | ||
[ [ left + 17*k, top, 16, 16, ] for k in range(stock_num) ] | ||
) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from skimage.feature import hog | ||
import numpy as np | ||
import json | ||
import cv2 | ||
|
||
class SSBUStockClassifier: | ||
def __init__(self, feature_json): | ||
self.feature_json = feature_json | ||
|
||
with open(feature_json, 'r') as fp: | ||
data = json.load(fp) | ||
|
||
chara_id2name = sorted(list(data.keys())) | ||
|
||
charas = [] | ||
features = [] | ||
for chara_id, chara_name in enumerate(chara_id2name): | ||
fts = data[chara_name] | ||
|
||
# n = 0 | ||
for feature in fts: | ||
charas.append(int(chara_id)) | ||
features.append(np.asarray(feature, dtype=np.float32)) | ||
# n += 1 | ||
# if n == 4: | ||
# break | ||
|
||
self.categories = set(charas) | ||
self.datacount = len(charas) | ||
|
||
self.chara_id2name = chara_id2name | ||
self.charas = np.asarray(charas, dtype=np.int32) | ||
self.features = np.asarray(features, dtype=np.float32) | ||
|
||
|
||
def __call__(self, img, k=3): | ||
# img isinstance of np.ndarray | ||
# print(img.shape) | ||
assert len(img.shape) == 2 # gray | ||
assert img.shape[1] == 16 and img.shape[0] == 16 | ||
img = cv2.resize(img, (30, 30)) # hog requirement | ||
h0 = hog(img) | ||
|
||
dists = np.linalg.norm(self.features - h0, axis=1) | ||
|
||
sarg = np.argsort(dists) # sorted-arg | ||
topKarg = sarg[:k] | ||
|
||
charas = self.charas[topKarg].tolist() | ||
names = [] | ||
for i in range(len(charas)): | ||
chara_id = int(charas[i]) | ||
name = self.chara_id2name[chara_id] | ||
names.append(name) | ||
|
||
return names, dists[topKarg].tolist() | ||
|
||
if __name__ == '__main__': | ||
import argparse | ||
import time | ||
import cv2 | ||
import SSBUBoundingBoxUtil | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('dictionary', type=str) | ||
parser.add_argument('input', type=str) | ||
parser.add_argument('fighter_num', type=int) | ||
args = parser.parse_args() | ||
|
||
print('loading...') | ||
classifier = SSBUStockClassifier(feature_json=args.dictionary) | ||
print('loaded') | ||
|
||
img = cv2.imread(args.input, 0) | ||
stock_bboxes = SSBUBoundingBoxUtil.fighters_stock_bboxes(fighter_num=args.fighter_num, stock_num=3) | ||
|
||
t = time.time() | ||
# img = cv2.resize(img, (110, 110)) | ||
|
||
for fighter_idx, stock_bboxes in enumerate(stock_bboxes): | ||
for stock_idx, bbox in enumerate(stock_bboxes): | ||
simg = img[bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]] | ||
|
||
ret = classifier(simg) | ||
print(ret) | ||
|
||
elapsed = time.time() - t | ||
|
||
print('FPS: %f (%f s)' % (1/elapsed, elapsed, )) |