Skip to content

Commit

Permalink
Misc. Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Sep 30, 2023
1 parent 0700a1c commit f265994
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ void ScreenWatchDisplayWidget::paintEvent(QPaintEvent* event){
QWidget::paintEvent(event);

VideoSnapshot snapshot = m_last_frame;
if (!snapshot){
return;
}

double aspect_ratio = (double)snapshot.frame->width() / snapshot.frame->height();
if (aspect_ratio < 2.0){
Expand Down
8 changes: 5 additions & 3 deletions SerialPrograms/Source/PokemonSV/PokemonSV_Panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{
// ret.emplace_back("---- Sandwiches ----");
ret.emplace_back(make_single_switch_program<SandwichMaker_Descriptor, SandwichMaker>());

// ret.emplace_back("---- Boxes ----");
ret.emplace_back("---- Boxes ----");
ret.emplace_back(make_single_switch_program<MassRelease_Descriptor, MassRelease>());
ret.emplace_back(make_single_switch_program<MassAttachItems_Descriptor, MassAttachItems>());

ret.emplace_back("---- Farming ----");
ret.emplace_back(make_single_switch_program<LPFarmer_Descriptor, LPFarmer>());
Expand Down Expand Up @@ -122,11 +123,12 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{
ret.emplace_back(make_single_switch_program<RideCloner101_Descriptor, RideCloner101>());
ret.emplace_back(make_single_switch_program<CloneItems101_Descriptor, CloneItems101>());

if (PreloadSettings::instance().DEVELOPER_MODE || IS_BETA_VERSION){
if (PreloadSettings::instance().DEVELOPER_MODE
// || IS_BETA_VERSION
){
ret.emplace_back("---- Untested/Beta/WIP ----");
}
if (IS_BETA_VERSION){
ret.emplace_back(make_single_switch_program<MassAttachItems_Descriptor, MassAttachItems>());
}
if (PreloadSettings::instance().DEVELOPER_MODE){
ret.emplace_back(make_single_switch_program<ShinyHuntScatterbug_Descriptor, ShinyHuntScatterbug>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,99 @@ namespace PokemonSV{
using namespace Pokemon;


VideoFceSettings::VideoFceSettings()
: GroupOption("Join Settings (V-FCE)", LockWhileRunning::UNLOCKED)
, OCR_METHOD(
"<b>Text Recognition Method:</b><br>"
"Each text recognition method has its own strengths and weaknesses. This option lets you choose which method to use.",
{
{VideoFceOcrMethod::RAW_OCR, "raw-ocr", "Raw OCR. No image pre-processing."},
{VideoFceOcrMethod::BLACK_TEXT, "black-on-white", "Filter: Black Text on White Background"},
{VideoFceOcrMethod::WHITE_TEXT, "white-on-black", "Filter: White Text on Black Background"},
{VideoFceOcrMethod::TERA_CARD, "tera-card", "Tera Card"},
},
LockWhileRunning::UNLOCKED,
VideoFceOcrMethod::TERA_CARD
)
, SKIP_INITIAL_CODE(
"<b>Skip Initial Code:</b><br>"
"If there is already a code up when you start the program, skip it since it's from the last raid.",
LockWhileRunning::UNLOCKED,
true
)
{
PA_ADD_OPTION(OCR_METHOD);
PA_ADD_OPTION(SKIP_INITIAL_CODE);
}



void wait_for_video_code_and_join(
MultiSwitchProgramEnvironment& env, CancellableScope& scope,
ScreenWatchOption& screen_watcher,
VideoFceSettings& join_method,
FastCodeEntrySettingsOption& fce_settings
){
bool skip_initial = join_method.SKIP_INITIAL_CODE;
VideoSnapshot snapshot;
while (true){
scope.throw_if_cancelled();
VideoSnapshot current = screen_watcher.screenshot();

// env.log("start");

if (snapshot && current &&
snapshot->width() == current->width() &&
snapshot->height() == current->height()
){
double rmsd = ImageMatch::pixel_RMSD(snapshot, current);
if (rmsd < 2){
scope.wait_for(std::chrono::milliseconds(1));
continue;
}
}
snapshot = std::move(current);
// env.log("done new frame check");

if (skip_initial){
skip_initial = false;
continue;
}

std::string code;
switch (join_method.OCR_METHOD){
case VideoFceOcrMethod::RAW_OCR:
code = OCR::ocr_read(Language::English, snapshot);
env.log("OCR: " + code);
break;
case VideoFceOcrMethod::BLACK_TEXT:{
ImageRGB32 filtered = to_blackwhite_rgb32_range(snapshot, 0xff000000, 0xff7f7f7f, true);
code = OCR::ocr_read(Language::English, filtered);
env.log("OCR: " + code);
break;
}
case VideoFceOcrMethod::WHITE_TEXT:{
ImageRGB32 filtered = to_blackwhite_rgb32_range(snapshot, 0xffc0c0c0, 0xffffffff, true);
code = OCR::ocr_read(Language::English, filtered);
env.log("OCR: " + code);
break;
}
case VideoFceOcrMethod::TERA_CARD:
code = read_raid_code(env.logger(), env.realtime_dispatcher(), snapshot);
}
const char* error = enter_code(env, scope, fce_settings, code, false);
if (error == nullptr){
break;
}else{
env.log(std::string("Invalid Code: ") + error, COLOR_RED);
}
}
}





VideoFastCodeEntry_Descriptor::VideoFastCodeEntry_Descriptor()
: MultiSwitchProgramDescriptor(
"PokemonSV:VideoFastCodeEntry",
Expand Down Expand Up @@ -55,28 +148,16 @@ VideoFastCodeEntry::VideoFastCodeEntry()
LockWhileRunning::LOCKED,
false
)
, OCR_METHOD(
"<b>Text Recognition Method:</b><br>"
"Each text recognition method has its own strengths and weaknesses. This option lets you choose which method to use.",
{
{OcrMethod::RAW_OCR, "raw-ocr", "Raw OCR. No image pre-processing."},
{OcrMethod::BLACK_TEXT, "black-on-white", "Filter: Black Text on White Background"},
{OcrMethod::WHITE_TEXT, "white-on-black", "Filter: White Text on Black Background"},
{OcrMethod::TERA_CARD, "tera-card", "Tera Card"},
},
LockWhileRunning::LOCKED,
OcrMethod::TERA_CARD
)
, SETTINGS(LockWhileRunning::LOCKED)
, FCE_SETTINGS(LockWhileRunning::LOCKED)
, NOTIFICATIONS({
&NOTIFICATION_PROGRAM_FINISH,
})
{
PA_ADD_OPTION(SCREEN_WATCHER);
PA_ADD_OPTION(MODE);
PA_ADD_OPTION(SKIP_CONNECT_TO_CONTROLLER);
PA_ADD_OPTION(OCR_METHOD);
PA_ADD_OPTION(SETTINGS);
PA_ADD_OPTION(JOIN_METHOD);
PA_ADD_OPTION(FCE_SETTINGS);
PA_ADD_OPTION(NOTIFICATIONS);

// Preload the OCR data.
Expand All @@ -86,7 +167,7 @@ VideoFastCodeEntry::VideoFastCodeEntry()


void VideoFastCodeEntry::program(MultiSwitchProgramEnvironment& env, CancellableScope& scope){
FastCodeEntrySettings settings(SETTINGS);
FastCodeEntrySettings settings(FCE_SETTINGS);

if (MODE == Mode::MANUAL){
std::string code = read_raid_code(env.logger(), env.realtime_dispatcher(), SCREEN_WATCHER.screenshot());
Expand All @@ -105,55 +186,7 @@ void VideoFastCodeEntry::program(MultiSwitchProgramEnvironment& env, Cancellable
// Preload 6 threads to OCR the code.
env.realtime_dispatcher().ensure_threads(6);


VideoSnapshot snapshot;
while (true){
scope.throw_if_cancelled();
VideoSnapshot current = SCREEN_WATCHER.screenshot();

// env.log("start");

if (snapshot && current &&
snapshot->width() == current->width() &&
snapshot->height() == current->height()
){
double rmsd = ImageMatch::pixel_RMSD(snapshot, current);
if (rmsd < 2){
scope.wait_for(std::chrono::milliseconds(1));
continue;
}
}
snapshot = std::move(current);
// env.log("done new frame check");

std::string code;
switch (OCR_METHOD){
case OcrMethod::RAW_OCR:
code = OCR::ocr_read(Language::English, snapshot);
env.log("OCR: " + code);
break;
case OcrMethod::BLACK_TEXT:{
ImageRGB32 filtered = to_blackwhite_rgb32_range(snapshot, 0xff000000, 0xff7f7f7f, true);
code = OCR::ocr_read(Language::English, filtered);
env.log("OCR: " + code);
break;
}
case OcrMethod::WHITE_TEXT:{
ImageRGB32 filtered = to_blackwhite_rgb32_range(snapshot, 0xffc0c0c0, 0xffffffff, true);
code = OCR::ocr_read(Language::English, filtered);
env.log("OCR: " + code);
break;
}
case OcrMethod::TERA_CARD:
code = read_raid_code(env.logger(), env.realtime_dispatcher(), snapshot);
}
const char* error = enter_code(env, scope, settings, code, false);
if (error == nullptr){
break;
}else{
env.log(std::string("Invalid Code: ") + error, COLOR_RED);
}
}
wait_for_video_code_and_join(env, scope, SCREEN_WATCHER, JOIN_METHOD, FCE_SETTINGS);

send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,38 @@ namespace NintendoSwitch{
namespace PokemonSV{


enum VideoFceOcrMethod{
RAW_OCR,
BLACK_TEXT,
WHITE_TEXT,
TERA_CARD,
};
class VideoFceSettings : public GroupOption{
public:
VideoFceSettings();

class VideoFastCodeEntry_Descriptor : public MultiSwitchProgramDescriptor{
public:
VideoFastCodeEntry_Descriptor();
EnumDropdownOption<VideoFceOcrMethod> OCR_METHOD;
BooleanCheckBoxOption SKIP_INITIAL_CODE;
};

void wait_for_video_code_and_join(
MultiSwitchProgramEnvironment& env, CancellableScope& scope,
ScreenWatchOption& screen_watcher,
VideoFceSettings& join_method,
FastCodeEntrySettingsOption& fce_settings
);





class VideoFastCodeEntry_Descriptor : public MultiSwitchProgramDescriptor{
public:
VideoFastCodeEntry_Descriptor();
};


class VideoFastCodeEntry : public MultiSwitchProgramInstance{
public:
VideoFastCodeEntry();
Expand All @@ -44,15 +67,9 @@ class VideoFastCodeEntry : public MultiSwitchProgramInstance{

BooleanCheckBoxOption SKIP_CONNECT_TO_CONTROLLER;

enum OcrMethod{
RAW_OCR,
BLACK_TEXT,
WHITE_TEXT,
TERA_CARD,
};
EnumDropdownOption<OcrMethod> OCR_METHOD;
VideoFceSettings JOIN_METHOD;

FastCodeEntrySettingsOption SETTINGS;
FastCodeEntrySettingsOption FCE_SETTINGS;
EventNotificationsOption NOTIFICATIONS;
};

Expand Down

0 comments on commit f265994

Please sign in to comment.