Skip to content

Commit

Permalink
大会の順位を表示する機能を追加 (#161)
Browse files Browse the repository at this point in the history
* Add tournament rank

* Update buildspec.json
  • Loading branch information
umireon authored Jan 13, 2024
1 parent 7f238ee commit 8d988b5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion buildspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}
},
"name": "obs-pokemon-sv-screen-builder",
"version": "0.6.5",
"version": "0.6.6",
"author": "Kaito Udagawa",
"website": "https://github.com/umireon/obs-pokemon-sv-screen-builder",
"email": "[email protected]",
Expand Down
10 changes: 8 additions & 2 deletions data-generator/MyRankExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@
from os import path
from cbor2 import dump

rect = [1206, 419, 409, 55]
rects = []
threshold = 200
cols = []
data = []
ratio = 0.05
matchTypes = []
for file in glob('./assets/screenshots/MyRankExtractor/*.png'):
name = path.splitext(path.basename(file))[0]
args = name.split(' ')
x0, x1, y0, y1 = [int(x) for x in args[0:4]]
rect = [int(x) for x in args[4:8]]
matchType = args[8]
srcImg = cv2.imread(file)
destImg = srcImg[y0:y1, x0:x1]
destImg = cv2.cvtColor(destImg, cv2.COLOR_BGR2GRAY)
retval, destImg = cv2.threshold(destImg, 200, 255, cv2.THRESH_BINARY)
cv2.imwrite('./assets/MyRankExtractor/' + name + '.png', destImg)

rects.append(rect)
cols.append(destImg.shape[1])
data.append(np.ravel(destImg).tolist())
matchTypes.append(matchType)

with open('data/preset/MyRankExtractor.cbor', 'wb') as fp:
dump({
"rect": rect,
"rects": rects,
"threshold": threshold,
"cols": cols,
"data": data,
"ratio": ratio,
"matchTypes": matchTypes,
}, fp)
Binary file modified data/preset/MyRankExtractor.cbor
Binary file not shown.
39 changes: 23 additions & 16 deletions src/Extractors/MyRankExtractor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,49 @@

class MyRankExtractor {
public:
MyRankExtractor(cv::Rect _rect, int _threshold,
const std::vector<int> _cols,
MyRankExtractor(const std::vector<cv::Rect> &_rects, int _threshold,
const std::vector<int> &_cols,
const std::vector<std::vector<uchar>> &_data,
double _ratio)
: rect(_rect),
double _ratio,
const std::vector<std::string> &_matchTypes)
: rects(_rects),
threshold(_threshold),
cols(_cols),
data(_data),
templates(generateMatVector(cols, data, CV_8U)),
ratio(_ratio)
ratio(_ratio),
matchTypes(_matchTypes)
{
}

cv::Rect operator()(const cv::Mat &gameplayGray) const
{
assert(gameplayGray.cols == 1920 && gameplayGray.rows == 1080);
cv::Mat lineGray = gameplayGray(rect), lineBinary;
cv::threshold(lineGray, lineBinary, threshold, 255,
cv::THRESH_BINARY);
cv::Mat icon = templates[0];
const int rankStart = matchIcon(lineBinary, icon);
if (rankStart < 0) {
return {};
} else {
return {rect.x + rankStart, rect.y,
lineBinary.cols - rankStart, rect.height};

for (size_t i = 0; i < templates.size(); i++) {
cv::Mat lineGray = gameplayGray(rects[i]), lineBinary;
cv::threshold(lineGray, lineBinary, threshold, 255,
cv::THRESH_BINARY);
const int rankStart =
matchIcon(lineBinary, templates[i]);
if (rankStart >= 0) {
return {rects[i].x + rankStart, rects[i].y,
lineBinary.cols - rankStart,
rects[i].height};
}
}

return {};
}

private:
const cv::Rect rect;
const std::vector<cv::Rect> rects;
const int threshold;
const std::vector<int> cols;
const std::vector<std::vector<uchar>> data;
const std::vector<cv::Mat> templates;
double ratio;
const std::vector<std::string> matchTypes;

static std::vector<cv::Mat>
generateMatVector(const std::vector<int> &cols,
Expand Down
3 changes: 2 additions & 1 deletion src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ MyRankExtractor newMyRankExtractor(const char *name)
nlohmann::json json = nlohmann::json::from_cbor(ifs);

return {
json["rect"].template get<cv::Rect>(),
json["rects"].template get<std::vector<cv::Rect>>(),
json["threshold"].template get<int>(),
json["cols"].template get<std::vector<int>>(),
json["data"].template get<std::vector<std::vector<uchar>>>(),
json["ratio"].template get<double>(),
json["matchTypes"].template get<std::vector<std::string>>(),
};
}

Expand Down

0 comments on commit 8d988b5

Please sign in to comment.