Skip to content

Commit

Permalink
・v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
amate committed Apr 16, 2021
0 parents commit e0f77ed
Show file tree
Hide file tree
Showing 49 changed files with 22,669 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#Windowsによって作られるサムネイルを無視
Thumbs.db
#Visual Studioによって作られるファイルを無視
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.sbr
*.opensdf
*.sdf
obj/
[Rr]elease*/
_ReSharper*/

/EventGetAnalyzeBuild/
/#Release/
/.vs/
/simstring-1.0/

/opencv/
/tesseract/





31 changes: 31 additions & 0 deletions UmaCruise.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UmaCruise", "UmaCruise\UmaCruise.vcxproj", "{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Debug|x64.ActiveCfg = Debug|x64
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Debug|x64.Build.0 = Debug|x64
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Debug|x86.ActiveCfg = Debug|Win32
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Debug|x86.Build.0 = Debug|Win32
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Release|x64.ActiveCfg = Release|x64
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Release|x64.Build.0 = Release|x64
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Release|x86.ActiveCfg = Release|Win32
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3DEB6B7A-9C4E-46A6-8801-AEAE539DB3FC}
EndGlobalSection
EndGlobal
193 changes: 193 additions & 0 deletions UmaCruise/AboutDlg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@

#include "stdafx.h"
#include "AboutDlg.h"
#include "PreviewWindow.h"

#include <tesseract\baseapi.h>
#include <leptonica\allheaders.h>

#include <opencv2\opencv.hpp>

#include "Utility\CodeConvert.h"
#include "Utility\CommonUtility.h"
#include "Utility\json.hpp"
#include "Utility\timer.h"
#include "TesseractWrapper.h"
using namespace TesseractWrapper;

using json = nlohmann::json;
using namespace CodeConvert;
using namespace cv;

cv::Mat GdiPlusBitmapToOpenCvMat(Gdiplus::Bitmap* bmp);

void LoadPointSizeFromJson(const json& json, const std::string& key, CPoint& pt, CSize& size);

CRect AdjustBounds(const cv::Mat& srcImage, CRect bounds)
{
const CSize baseSize = { 588, 1045 }; // デバッグ用なので決め打ちで
//CSize imageSize(static_cast<int>(image->GetWidth()), static_cast<int>(image->GetHeight()));
CSize imageSize(srcImage.size().width, srcImage.size().height);
const double Xratio = static_cast<double>(imageSize.cx) / baseSize.cx;
const double Yratio = static_cast<double>(imageSize.cy) / baseSize.cy;

CRect adjustRect = bounds;
adjustRect.top *= Yratio;
adjustRect.left *= Xratio;
adjustRect.right *= Xratio;
adjustRect.bottom *= Yratio;

return adjustRect;
}

CRect GetTextBounds(cv::Mat cutImage, const CRect& rcBounds); // UmaTextRecognizer.cpp

/////////////////////////////////////////////////////////////////////////////////////

CAboutDlg::CAboutDlg(PreviewWindow& previewWindow): m_previewWindow(previewWindow)
{
}

LRESULT CAboutDlg::OnInitDialog(UINT, WPARAM, LPARAM, BOOL&)
{
CenterWindow(GetParent());

m_cmbTestBounds = GetDlgItem(IDC_COMBO_TESTBOUNDS);
m_editResult = GetDlgItem(IDC_EDIT_RESULT);
m_sliderThreshold = GetDlgItem(IDC_SLIDER_THRESHOLD);
m_sliderThreshold.SetRange(0, 255);

for (LPCWSTR name : kTestBoundsName) {
m_cmbTestBounds.AddString(name);
}
m_cmbTestBounds.SetCurSel(0);

return TRUE;
}

LRESULT CAboutDlg::OnCloseCmd(WORD, WORD wID, HWND, BOOL&)
{
cv::destroyAllWindows();
EndDialog(wID);
return 0;
}

LRESULT CAboutDlg::OnOCR(WORD, WORD, HWND, BOOL&)
{
const int index = m_cmbTestBounds.GetCurSel();
if (index == -1) {
return 0;
}
auto image = m_previewWindow.GetImage();
if (!image) {
return 0;
}

CRect rcBounds;
if (index != kDirect) {
json jsonCommon;
std::ifstream ifs((GetExeDirectory() / L"Common.json").string());
ATLASSERT(ifs);
if (!ifs) {
return 0;
}
ifs >> jsonCommon;

json jsonOCR = jsonCommon["Common"]["TestBounds"];

CPoint pt;
CSize size;
LoadPointSizeFromJson(jsonOCR, UTF8fromUTF16(kTestBoundsName[index]), pt, size);
rcBounds = CRect(pt, size);
m_previewWindow.ChangeDragdropBounds(rcBounds);
} else {
rcBounds = m_previewWindow.GetDragdropActualBounds();
}

if (rcBounds.Size() == CSize()) {
return 0;
}

const bool bManualThresOnly = ::GetKeyState(VK_CONTROL) < 0; // ctrl でスライドバーOCRのみ
const bool bNoAdjustBounds = ::GetKeyState(VK_SHIFT) < 0; // shift でテキストを囲わない
const bool bScale4 = ::GetKeyState(VK_MENU) < 0; // atl で4倍拡大

// 切り抜き&変換
Utility::timer timer;

Gdiplus::Bitmap bmp(image->GetWidth(), image->GetHeight(), PixelFormat24bppRGB);
Gdiplus::Graphics graphics(&bmp);
graphics.DrawImage(image, 0, 0);
auto srcImage = GdiPlusBitmapToOpenCvMat(&bmp);//cv::imread(ssPath.string());
rcBounds = AdjustBounds(srcImage, rcBounds);

cv::Rect rcTrim(rcBounds.left, rcBounds.top, rcBounds.Width(), rcBounds.Height());
cv::Mat cutImage(srcImage, rcTrim);

if (index == kEventNameBounds) {
if (!bNoAdjustBounds) { // テキストを正確囲む
CRect rcAdjustTextBounds = GetTextBounds(cutImage, rcBounds);
rcBounds = rcAdjustTextBounds;
}
}
m_previewWindow.ChangeDragdropBounds(rcBounds);

{
cv::Mat resizedImage;
//constexpr double scale = 4.0;
double scale = 1.0;//4.0;
if (bScale4) {
scale = 4.0;
}
cv::resize(cutImage, resizedImage, cv::Size(), scale, scale, cv::INTER_CUBIC);
auto saveImagePath = GetExeDirectory() / L"debug1.png";
cv::imwrite(saveImagePath.string(), resizedImage);

cv::Mat grayImage;
cv::cvtColor(resizedImage, grayImage, cv::COLOR_RGB2GRAY);

cv::Mat invertedImage;
cv::bitwise_not(grayImage, invertedImage);

cv::Mat thresImage;
//cv::threshold(grayImage, thresImage, 190.0, 255.0, cv::THRESH_BINARY_INV);
cv::threshold(grayImage, thresImage, 0.0, 255.0, cv::THRESH_OTSU);

cv::Mat thresImage2;
//cv::threshold(grayImage, thresImage, 190.0, 255.0, cv::THRESH_BINARY_INV);
cv::threshold(invertedImage, thresImage2, 0.0, 255.0, cv::THRESH_OTSU);

// ==================================================

const int threshold = m_sliderThreshold.GetPos();
cv::Mat manualThresholdImage;
cv::threshold(grayImage, manualThresholdImage, threshold, 255.0, cv::THRESH_BINARY);


ATLTRACE(L"切り抜き変換 %s\n", UTF16fromUTF8(timer.format()).c_str());

cv::imshow("1", cutImage);
cv::imshow("2", resizedImage);
cv::imshow("3", grayImage);
cv::imshow("4", invertedImage);
cv::imshow("5", thresImage);
cv::imshow("6", thresImage2);
cv::imshow("7", manualThresholdImage);

cv::Mat targetImage = cutImage;//thresImage;
CString result;
if (!bManualThresOnly) {
result += L"1: " + CString(TextFromImage(cutImage).c_str()) + L"\r\n";
result += L"2: " + CString(TextFromImage(resizedImage).c_str()) + L"\r\n";
result += L"3: " + CString(TextFromImage(grayImage).c_str()) + L"\r\n";
result += L"4: " + CString(TextFromImage(invertedImage).c_str()) + L"\r\n";
result += L"5: " + CString(TextFromImage(thresImage).c_str()) + L"\r\n";
result += L"6: " + CString(TextFromImage(thresImage2).c_str()) + L"\r\n";
}
result += L"7: " + CString(TextFromImage(manualThresholdImage).c_str()) + L" thres:" + std::to_wstring(threshold).c_str() + L"\r\n";
m_editResult.SetWindowTextW(result);

}

return LRESULT();
}
49 changes: 49 additions & 0 deletions UmaCruise/AboutDlg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// aboutdlg.h : interface of the CAboutDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

class PreviewWindow;

#include "resource.h"

class CAboutDlg : public CDialogImpl<CAboutDlg>
{
public:
enum { IDD = IDD_ABOUTBOX };

CAboutDlg(PreviewWindow& previewWindow);

BEGIN_MSG_MAP(CAboutDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(IDOK, OnCloseCmd)
COMMAND_ID_HANDLER(IDCANCEL, OnCloseCmd)
COMMAND_ID_HANDLER(IDC_BUTTON_OCR, OnOCR)
END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

LRESULT OnOCR(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

private:
PreviewWindow& m_previewWindow;

enum TestBounds {
kDirect, kUmaMusumeSubNameBounds, kUmaMusumeNameBounds, kCurrentTurnBounds, kEventCategoryBounds, kEventNameBounds, kEventNameIconBounds, kMaxCount
};
static constexpr LPCWSTR kTestBoundsName[kMaxCount] = {
L"Direct", L"UmaMusumeSubNameBounds", L"UmaMusumeNameBounds", L"CurrentTurnBounds", L"EventCategoryBounds", L"EventNameBounds", L"EventNameIconBounds",
};


CComboBox m_cmbTestBounds;
CEdit m_editResult;
CTrackBarCtrl m_sliderThreshold;
};
50 changes: 50 additions & 0 deletions UmaCruise/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "stdafx.h"
#include "Config.h"

#include <fstream>

#include "Utility\CodeConvert.h"
#include "Utility\CommonUtility.h"
#include "Utility\json.hpp"

using json = nlohmann::json;
using namespace CodeConvert;


bool Config::LoadConfig()
{
std::ifstream fs((GetExeDirectory() / "setting.json").string());
if (!fs) {
return true;
}
json jsonSetting;
fs >> jsonSetting;

if (jsonSetting["Config"].is_null()) {
return true; // default
}

refreshInterval = jsonSetting["Config"].value("RefreshInterval", refreshInterval);
autoStart = jsonSetting["Config"].value("AutoStart", autoStart);

return true;
}

void Config::SaveConfig()
{
std::ifstream ifs((GetExeDirectory() / "setting.json").string());
if (!ifs) {
ATLASSERT(FALSE);
ERROR_LOG << L"SaveConfig failed: !fs";
return ;
}
json jsonSetting;
ifs >> jsonSetting;
ifs.close();

jsonSetting["Config"]["RefreshInterval"] = refreshInterval;
jsonSetting["Config"]["AutoStart"] = autoStart;

std::ofstream ofs((GetExeDirectory() / "setting.json").string());
ofs << jsonSetting;
}
11 changes: 11 additions & 0 deletions UmaCruise/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

struct Config
{
int refreshInterval = 1;
bool autoStart = false;

bool LoadConfig();
void SaveConfig();
};

Loading

0 comments on commit e0f77ed

Please sign in to comment.