adding img and opencv

This commit is contained in:
Andy Pack 2023-06-17 13:11:42 +01:00
parent 13aee5d6f5
commit 70855d50e7
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
12 changed files with 174 additions and 17 deletions

View File

@ -9,5 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Install Deps
run: sudo apt -y install libopencv-dev
- name: Build
run: mkdir build && cd build && ../cbuild

20
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "dbg img",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/kc",
"args": ["img"],
"environment": [{ "name": "config", "value": "Debug" }],
"cwd": "${workspaceFolder}/build",
"osx": {
"MIMode": "lldb"
}
}
]
}

View File

@ -1,5 +1,6 @@
add_executable(kc
main.cpp
appcontext.cpp
fs/fs.cpp
fs/FileEntry.cpp
parse/Link.cpp
@ -8,6 +9,7 @@
valid/link.cpp
logging.cpp
config.cpp
image/img.cpp
)
FetchContent_Declare(
@ -17,4 +19,7 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(Boost)
target_link_libraries(kc PRIVATE Boost::program_options Boost::log Boost::date_time Boost::filesystem Boost::system Boost::thread Boost::log_setup Boost::chrono Boost::atomic)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries(kc PRIVATE Boost::program_options Boost::log Boost::date_time Boost::filesystem Boost::system Boost::thread Boost::log_setup Boost::chrono Boost::atomic ${OpenCV_LIBS})

41
src/appcontext.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "appcontext.hpp"
#include "config.hpp"
#include "logging.hpp"
namespace kc {
AppContext::AppContext()
{
}
void AppContext::load_config(int argc, const char *argv[])
{
config = init_config(argc, argv);
config_loaded = true;
}
void AppContext::load_and_parse_cache()
{
auto env_path = (*config)["path"].as<std::string>();
print_and_log("> Loading knowledge base from " + env_path);
file_cache = std::make_shared<kc::FileContextCache>();
file_cache->load(env_path);
file_cache->parse_all();
}
std::string AppContext::command()
{
if (config->count("command") == 1)
{
return (*config)["command"].as<std::string>();
}
else
{
return "";
}
}
}

31
src/appcontext.hpp Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <memory>
#include <string>
#include <boost/program_options.hpp>
#include "parse/FileContextCache.hpp"
namespace kc {
class AppContext {
public:
std::shared_ptr<boost::program_options::variables_map> config;
std::shared_ptr<kc::FileContextCache> file_cache;
AppContext();
void load_config(int argc, const char *argv[]);
void load_and_parse_cache();
std::string command();
private:
bool config_loaded;
};
}

View File

@ -14,6 +14,7 @@ std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
("help", "produce help message")
("path,p", po::value<std::string>()->default_value("."), "set root path of knowledge base")
("config", po::value<std::string>()->default_value("kc.ini"), "config file location")
("out,o", po::value<std::string>()->default_value("."), "output file location")
("command", po::value<std::string>(), "command to execute")
("subargs", po::value<std::vector<std::string> >(), "Arguments for command")
("index", po::value<int>()->default_value(1), "index")

25
src/image/img.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "img.hpp"
namespace kc {
void image_proc(const std::vector<std::shared_ptr<kc::FileContext>> &contexts)
{
for (auto const context: contexts)
{
if (context->images.size() > 0)
{
auto img = context->images[0];
auto composed_path = context->abs_path(img);
cv::Mat image; // variable image of datatype Matrix
image = cv::imread(composed_path);
cv::imshow(img.link, image);
cv::waitKey(0);
break;
}
}
}
}

14
src/image/img.hpp Normal file
View File

@ -0,0 +1,14 @@
# pragma once
#include <memory>
#include <vector>
#include <opencv2/opencv.hpp>
#include "../parse/FileContext.hpp"
namespace kc {
void image_proc(const std::vector<std::shared_ptr<kc::FileContext>> &contexts);
}

View File

@ -8,11 +8,14 @@
#include "const.hpp"
#include "logging.hpp"
#include "config.hpp"
#include "appcontext.hpp"
#include "fs/fs.hpp"
#include "parse/FileContextCache.hpp"
#include "valid/link.hpp"
#include "image/img.hpp"
void run_validate(std::shared_ptr<boost::program_options::variables_map> config);
void run_validate(kc::AppContext app_context);
void run_img(kc::AppContext app_context);
int main(int argc, const char *argv[]) {
@ -24,17 +27,23 @@ int main(int argc, const char *argv[]) {
BOOST_LOG_TRIVIAL(info) << "================================";
BOOST_LOG_TRIVIAL(info) << "Starting up....";
auto config = init_config(argc, argv);
kc::AppContext app_context;
app_context.load_config(argc, argv);
if(config)
if(app_context.config)
{
if (config->count("command") == 1)
auto command = app_context.command();
if (!command.empty())
{
auto command = (*config)["command"].as<std::string>();
if (command == "validate")
{
run_validate(config);
app_context.load_and_parse_cache();
run_validate(app_context);
}
else if (command == "img")
{
app_context.load_and_parse_cache();
run_img(app_context);
}
}
else
@ -49,14 +58,12 @@ int main(int argc, const char *argv[]) {
return 1;
}
void run_validate(std::shared_ptr<boost::program_options::variables_map> config)
void run_validate(kc::AppContext app_context)
{
auto env_path = (*config)["path"].as<std::string>();
print_and_log("> Loading knowledge base from " + env_path);
kc::validate_links(app_context.file_cache->get());
}
auto file_cache = kc::FileContextCache();
file_cache.load(env_path);
file_cache.parse_all();
kc::validate_links(file_cache.get());
void run_img(kc::AppContext app_context)
{
kc::image_proc(app_context.file_cache->get());
}

View File

@ -37,7 +37,11 @@ void FileContext::parse()
file_content = image_match.suffix();
}
#if __APPLE__
std::regex tag_regex(MD_TAG_REGEX, std::regex::multiline);
#else
std::regex tag_regex(MD_TAG_REGEX);
#endif
file_content = file_entry->get_content();
std::smatch tag_match;
while(std::regex_search(file_content, tag_match, tag_regex)) {
@ -48,4 +52,9 @@ void FileContext::parse()
}
std::filesystem::path FileContext::abs_path(kc::Link link)
{
return file_entry->file_entry.path().parent_path() / fs::path(link.link);
}
}

View File

@ -3,6 +3,7 @@
#include <vector>
#include <memory>
#include <regex>
#include <filesystem>
#include "../fs/FileEntry.hpp"
#include "Link.hpp"
@ -21,6 +22,7 @@ class FileContext {
std::vector<std::string> tags;
void parse();
std::filesystem::path abs_path(kc::Link link);
private:

View File

@ -23,7 +23,7 @@ std::vector<kc::FileLinkStateResult> validate_links(const std::vector<std::share
{
if(!link.is_external()) {
auto composed = context->file_entry->file_entry.path().parent_path() / fs::path(link.link);
auto composed = context->abs_path(link);
auto entry = fs::directory_entry(composed);