diff --git a/resource/infrast.json b/resource/infrast.json index 9fe281afc8..e423d1b324 100644 --- a/resource/infrast.json +++ b/resource/infrast.json @@ -837,74 +837,6 @@ "endFlag_Doc": "识别到这里面的名字,就不继续往后识别了,一般用列表里的最后一个人名" } ], - "featureKey_Doc": "特征检测的关键字,如果OCR识别到了左边的内容,但是却没有右边的内容,则进行特征检测进一步确认", - "featureKey": [ - [ - "森", - "森蚺" - ], - [ - "白面", - "白面鸮" - ], - [ - "灰", - "灰烬" - ], - [ - "早", - "早露" - ], - [ - "石", - "燧石" - ], - [ - "深", - "深靛" - ], - [ - "影", - "傀影" - ], - [ - "峨", - "嵯峨" - ], - [ - "利安", - "卡涅利安" - ], - [ - "星", - "陨星" - ], - [ - "冬", - "凛冬" - ], - [ - "狮", - "狮蝎" - ] - ], - "featureWhatever_Doc": "这里面的无论如何都进行特征检测", - "featureWhatever": [ - "砾", - "煌", - "山", - "黑", - "W", - "夕", - "红", - "芬", - "梅", - "宴", - "孑", - "吽", - "空", - "阿" - ], "allNames": [ "桃金娘", "食铁兽", diff --git a/resource/operators/W.png b/resource/operators/W.png deleted file mode 100644 index e44f050b1d..0000000000 Binary files a/resource/operators/W.png and /dev/null differ diff --git a/resource/operators/傀影.png b/resource/operators/傀影.png deleted file mode 100644 index bfaf950bad..0000000000 Binary files a/resource/operators/傀影.png and /dev/null differ diff --git a/resource/operators/凛冬.png b/resource/operators/凛冬.png deleted file mode 100644 index b77853994f..0000000000 Binary files a/resource/operators/凛冬.png and /dev/null differ diff --git a/resource/operators/卡涅利安.png b/resource/operators/卡涅利安.png deleted file mode 100644 index b6005df18a..0000000000 Binary files a/resource/operators/卡涅利安.png and /dev/null differ diff --git a/resource/operators/古米.png b/resource/operators/古米.png deleted file mode 100644 index fb7f4fc4db..0000000000 Binary files a/resource/operators/古米.png and /dev/null differ diff --git a/resource/operators/吽.png b/resource/operators/吽.png deleted file mode 100644 index 1860eb1ed2..0000000000 Binary files a/resource/operators/吽.png and /dev/null differ diff --git a/resource/operators/夕.png b/resource/operators/夕.png deleted file mode 100644 index 954311f62d..0000000000 Binary files a/resource/operators/夕.png and /dev/null differ diff --git a/resource/operators/夜刀.png b/resource/operators/夜刀.png deleted file mode 100644 index 6217f54919..0000000000 Binary files a/resource/operators/夜刀.png and /dev/null differ diff --git a/resource/operators/孑.png b/resource/operators/孑.png deleted file mode 100644 index 98d6520eba..0000000000 Binary files a/resource/operators/孑.png and /dev/null differ diff --git a/resource/operators/宴.png b/resource/operators/宴.png deleted file mode 100644 index 85421cf18e..0000000000 Binary files a/resource/operators/宴.png and /dev/null differ diff --git a/resource/operators/山.png b/resource/operators/山.png deleted file mode 100644 index cee80f6ee5..0000000000 Binary files a/resource/operators/山.png and /dev/null differ diff --git a/resource/operators/嵯峨.png b/resource/operators/嵯峨.png deleted file mode 100644 index b8858e9cd7..0000000000 Binary files a/resource/operators/嵯峨.png and /dev/null differ diff --git a/resource/operators/早露.png b/resource/operators/早露.png deleted file mode 100644 index 97c52a982d..0000000000 Binary files a/resource/operators/早露.png and /dev/null differ diff --git a/resource/operators/梅.png b/resource/operators/梅.png deleted file mode 100644 index 1f14735db1..0000000000 Binary files a/resource/operators/梅.png and /dev/null differ diff --git a/resource/operators/梓兰.png b/resource/operators/梓兰.png deleted file mode 100644 index 1366acb172..0000000000 Binary files a/resource/operators/梓兰.png and /dev/null differ diff --git a/resource/operators/森蚺.png b/resource/operators/森蚺.png deleted file mode 100644 index 89266b8718..0000000000 Binary files a/resource/operators/森蚺.png and /dev/null differ diff --git a/resource/operators/深靛.png b/resource/operators/深靛.png deleted file mode 100644 index 56eaffec0e..0000000000 Binary files a/resource/operators/深靛.png and /dev/null differ diff --git a/resource/operators/深靛png.png b/resource/operators/深靛png.png deleted file mode 100644 index 56eaffec0e..0000000000 Binary files a/resource/operators/深靛png.png and /dev/null differ diff --git a/resource/operators/灰烬.png b/resource/operators/灰烬.png deleted file mode 100644 index bea5ee8c70..0000000000 Binary files a/resource/operators/灰烬.png and /dev/null differ diff --git a/resource/operators/煌.png b/resource/operators/煌.png deleted file mode 100644 index 36f82f8694..0000000000 Binary files a/resource/operators/煌.png and /dev/null differ diff --git a/resource/operators/燧石.png b/resource/operators/燧石.png deleted file mode 100644 index a0e1c31c7c..0000000000 Binary files a/resource/operators/燧石.png and /dev/null differ diff --git a/resource/operators/狮蝎.png b/resource/operators/狮蝎.png deleted file mode 100644 index adf3abd36f..0000000000 Binary files a/resource/operators/狮蝎.png and /dev/null differ diff --git a/resource/operators/白面鸮.png b/resource/operators/白面鸮.png deleted file mode 100644 index 2ef190a283..0000000000 Binary files a/resource/operators/白面鸮.png and /dev/null differ diff --git a/resource/operators/砾.png b/resource/operators/砾.png deleted file mode 100644 index ac88d84e12..0000000000 Binary files a/resource/operators/砾.png and /dev/null differ diff --git a/resource/operators/空.png b/resource/operators/空.png deleted file mode 100644 index d4f41bb5ea..0000000000 Binary files a/resource/operators/空.png and /dev/null differ diff --git a/resource/operators/红.png b/resource/operators/红.png deleted file mode 100644 index 5b37f24813..0000000000 Binary files a/resource/operators/红.png and /dev/null differ diff --git a/resource/operators/芬.png b/resource/operators/芬.png deleted file mode 100644 index 2390446cf8..0000000000 Binary files a/resource/operators/芬.png and /dev/null differ diff --git a/resource/operators/阿.png b/resource/operators/阿.png deleted file mode 100644 index 05973b37bb..0000000000 Binary files a/resource/operators/阿.png and /dev/null differ diff --git a/resource/operators/陨星.png b/resource/operators/陨星.png deleted file mode 100644 index 003fdd3aab..0000000000 Binary files a/resource/operators/陨星.png and /dev/null differ diff --git a/resource/operators/黑.png b/resource/operators/黑.png deleted file mode 100644 index 6e94175f80..0000000000 Binary files a/resource/operators/黑.png and /dev/null differ diff --git a/src/MeoAssistance/Assistance.cpp b/src/MeoAssistance/Assistance.cpp index 5af93de5e4..ff7b1f948a 100644 --- a/src/MeoAssistance/Assistance.cpp +++ b/src/MeoAssistance/Assistance.cpp @@ -84,20 +84,6 @@ Assistance::Assistance(AsstCallback callback, void* callback_arg) throw "resource broken"; } - for (const auto& [key, name] : InfrastConfiger::get_instance().m_oper_name_feat) { - ret = m_identify_ptr->add_text_image(name, GetResourceDir() + "operators\\" + Utf8ToGbk(name) + ".png"); - if (!ret) { - callback_error(name); - throw "resource broken"; - } - } - for (const auto& name : InfrastConfiger::get_instance().m_oper_name_feat_whatever) { - ret = m_identify_ptr->add_text_image(name, GetResourceDir() + "operators\\" + Utf8ToGbk(name) + ".png"); - if (!ret) { - callback_error(name); - throw "resource broken"; - } - } for (const auto& file : std::filesystem::directory_iterator(GetResourceDir() + "penguin-stats-recognize\\items")) { ret = m_identify_ptr->penguin_load_templ(file.path().stem().u8string(), file.path().u8string()); if (!ret) { diff --git a/src/MeoAssistance/Identify.cpp b/src/MeoAssistance/Identify.cpp index a181bb1aee..87f8d8fe9d 100644 --- a/src/MeoAssistance/Identify.cpp +++ b/src/MeoAssistance/Identify.cpp @@ -5,7 +5,6 @@ #include #include -#include #include namespace penguin { @@ -17,7 +16,6 @@ namespace penguin { using namespace asst; using namespace cv; -using namespace cv::xfeatures2d; bool Identify::add_image(const std::string& name, const std::string& path) { @@ -29,18 +27,6 @@ bool Identify::add_image(const std::string& name, const std::string& path) return true; } -bool asst::Identify::add_text_image(const std::string& text, const std::string& path) -{ - Mat image = imread(path); - if (image.empty()) { - return false; - } - - m_feature_map.emplace(text, surf_detect(image)); - - return true; -} - Mat Identify::image_2_hist(const cv::Mat& src) { Mat src_hsv; @@ -66,165 +52,6 @@ double Identify::image_hist_comp(const cv::Mat& src, const cv::MatND& hist) return 1 - compareHist(image_2_hist(src), hist, CV_COMP_BHATTACHARYYA); } -std::pair, cv::Mat> asst::Identify::surf_detect(const cv::Mat& image) -{ - // 灰度图转换 - cv::Mat mat_gray; - cv::cvtColor(image, mat_gray, cv::COLOR_RGB2GRAY); - - constexpr int min_hessian = 400; - // SURF特征点检测 - static cv::Ptr detector = SURF::create(min_hessian); - std::vector keypoints; - cv::Mat mat_vector; - // 找到特征点并计算特征描述子(向量) - detector->detectAndCompute(mat_gray, Mat(), keypoints, mat_vector); - - return std::make_pair(std::move(keypoints), std::move(mat_vector)); -} - -std::optional asst::Identify::feature_match( - const std::vector& query_keypoints, const cv::Mat& query_mat_vector, - const std::vector& train_keypoints, const cv::Mat& train_mat_vector -#ifdef LOG_TRACE - , const cv::Mat query_mat, const cv::Mat train_mat -#endif -) -{ - if (query_mat_vector.empty() || train_mat_vector.empty()) { - DebugTraceError("feature_match | input vector is empty"); - return std::nullopt; - } - std::vector matches; - static FlannBasedMatcher matcher; - matcher.match(query_mat_vector, train_mat_vector, matches); - -#ifdef LOG_TRACE - //std::cout << matches.size() << " / " << query_keypoints.size() << std::endl; - cv::Mat allmatch_mat; - cv::drawMatches(query_mat, query_keypoints, train_mat, train_keypoints, matches, allmatch_mat); -#endif - - // 最大的距离 - auto max_iter = std::max_element(matches.cbegin(), matches.cend(), - [](const cv::DMatch& lhs, const cv::DMatch& rhs) ->bool { - return lhs.distance < rhs.distance; - }); // 描述符欧式距离(knn) - if (max_iter == matches.cend()) { - return std::nullopt;; - } - float maxdist = max_iter->distance; - - std::vector approach_matches; - std::vector train_approach_keypoints; - std::vector query_approach_keypoints; - std::vector train_approach_points; - std::vector query_approach_points; - // 利用距离进行一次逼近 - constexpr static const double MatchRatio = 0.4; - int approach_index = 0; - for (const cv::DMatch dmatch : matches) { - if (dmatch.distance < maxdist * MatchRatio) { - // 按理说不会越界,以防万一还是检查一下 - if (dmatch.queryIdx >= 0 && dmatch.queryIdx < query_keypoints.size() - && dmatch.trainIdx >= 0 && dmatch.trainIdx < train_keypoints.size()) { - approach_matches.emplace_back(dmatch); - train_approach_points.emplace_back(train_keypoints.at(dmatch.trainIdx).pt); - query_approach_points.emplace_back(query_keypoints.at(dmatch.queryIdx).pt); - train_approach_keypoints.emplace_back(train_keypoints.at(dmatch.trainIdx)); - query_approach_keypoints.emplace_back(query_keypoints.at(dmatch.queryIdx)); - } - } - } - -#ifdef LOG_TRACE - cv::Mat approach_mat; - cv::drawMatches(query_mat, query_keypoints, train_mat, train_keypoints, approach_matches, approach_mat); -#endif - - if (query_approach_points.empty()) { - return std::nullopt; - } - - // 使用RANSAC剔除异常值 - std::vector ransac_status; - cv::Mat fundametal = cv::findFundamentalMat(query_approach_points, train_approach_points, ransac_status, cv::FM_RANSAC); - - std::vector ransac_matchs; - std::vector train_ransac_keypoints; - std::vector query_ransac_keypoints; - int index = 0; - for (size_t i = 0; i != ransac_status.size(); ++i) { - if (ransac_status.at(i) != 0) { - train_ransac_keypoints.emplace_back(train_approach_keypoints.at(i)); - query_ransac_keypoints.emplace_back(query_approach_keypoints.at(i)); - cv::DMatch dmatch = approach_matches.at(i); - ransac_matchs.emplace_back(std::move(dmatch)); - ++index; - } - } - - // 做一次算数均值滤波,过滤异常的点。这个算法有点蠢,TODO可以看下怎么改 - size_t point_size = train_ransac_keypoints.size(); - if (point_size == 0) { - return std::nullopt; - } - cv::Point sum_point = std::accumulate( - train_ransac_keypoints.cbegin(), train_ransac_keypoints.cend(), cv::Point(), - [](cv::Point sum, const cv::KeyPoint& rhs) -> cv::Point { - return cv::Point(sum.x + rhs.pt.x, sum.y + rhs.pt.y); - }); - cv::Point avg_point(sum_point.x / point_size, sum_point.y / point_size); - std::vector good_matchs; - std::vector good_points; - // TODO,这个阈值需要根据分辨率进行缩放,而且最好写到配置文件里 - constexpr static int DistanceThreshold = 100; - for (size_t i = 0; i != train_ransac_keypoints.size(); ++i) { - // 没必要算距离,x y各算一下就行了,省点CPU时间 - //int distance = std::sqrt(std::pow(avg_point.x - cur_x, 2) + std::pow(avg_point.y - cur_y, 2)); - cv::Point2f& pt = train_ransac_keypoints.at(i).pt; - int x_distance = std::abs(avg_point.x - pt.x); - int y_distance = std::abs(avg_point.y - pt.y); - if (x_distance < DistanceThreshold && y_distance < DistanceThreshold) { - good_matchs.emplace_back(ransac_matchs.at(i)); - good_points.emplace_back(pt); - } - } - -#ifdef LOG_TRACE - - cv::Mat ransac_mat; - cv::drawMatches(query_mat, query_keypoints, train_mat, train_keypoints, ransac_matchs, ransac_mat); - - cv::Mat good_mat; - cv::drawMatches(query_mat, query_keypoints, train_mat, train_keypoints, good_matchs, good_mat); - -#endif - - constexpr static const double MatchSizeRatioThreshold = 0.1; - if (good_points.size() >= query_keypoints.size() * MatchSizeRatioThreshold) { - Rect dst; - int left = 0, right = 0, top = 0, bottom = 0; - for (const cv::Point& pt : good_points) { - if (pt.x < left || left == 0) { - left = pt.x; - } - if (pt.x > right || right == 0) { - right = pt.x; - } - if (pt.y < top || top == 0) { - top = pt.y; - } - if (pt.y > bottom || bottom == 0) { - bottom = pt.y; - } - } - dst = { left, top, right - left, bottom - top }; - return dst; - } - return std::nullopt; -} - std::vector