This commit is contained in:
mrbesen 2022-03-01 01:28:30 +01:00
commit d328bc9ce2
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
14 changed files with 463 additions and 0 deletions

18
.gitignore vendored Normal file
View File

@ -0,0 +1,18 @@
*.bin
*.out
build/
*.exe
.gdb_history
*.so
*.bmp
*.d
test
.vscode/settings.json
lolautoaccept
lolautoaccept_strip
log.txt
*.png

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "thirdparty/Log"]
path = thirdparty/Log
url = https://git.okaestne.de/okaestne/Log.git

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

@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/src/",
"${workspaceFolder}/thirdparty/Log/",
"${workspaceFolder}/include/**",
"/usr/include/opencv4/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}

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

@ -0,0 +1,50 @@
{
// 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": "test Debuggen (gdb)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make test",
"miDebuggerPath": "/usr/bin/gdb"
},
{
"name": "Debuggen (gdb)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/lolautoaccept",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make all",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

45
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make all",
"type": "shell",
"command": "make -j all",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
},
{
"label": "make clean",
"type": "shell",
"command": "make clean",
"problemMatcher": []
},
{
"label": "leak check",
"type": "shell",
"command": "/usr/bin/valgrind --leak-check=full ${workspaceFolder}/lolautoaccept -s",
"problemMatcher": [],
"dependsOn": "make all"
},
{
"label": "leak check ALL",
"type": "shell",
"command": "/usr/bin/valgrind --leak-check=full --show-leak-kinds=all ${workspaceFolder}/lolautoaccept -s",
"problemMatcher": [],
"dependsOn": "make all"
},
{
"label": "make test",
"type": "shell",
"command": "make -j test",
"problemMatcher": ["$gcc"],
}
]
}

74
Makefile Normal file
View File

@ -0,0 +1,74 @@
# Author Yannis Gerlach
# Hochschule Osnabrück
# 13.11.2020
# `make clean all` nicht mit -j verwenden! -> race condition im make file
# statdessen: `make clean; make all -j` verwenden
NAME = lolautoaccept
NAMETEST = test
CFLAGS = -std=c++17 -O2 -g -pipe -Wall -Wextra -Wno-unused-parameter -Wpedantic -rdynamic -mfpmath=both -march=native -m64 -funroll-loops -mavx2 `pkg-config opencv4 --cflags`
CXX = g++
SRCF = src/
BUILDDIR = build/
TESTF = tests/
DEPF = $(BUILDDIR)deps/
INCF = ./include/
INCFS = $(shell find $(INCF) -type d)
LOGF = ./thirdparty/Log/
LOGO = $(LOGF)Log.o
INCLUDES = -I$(LOGF) $(addprefix -I, $(INCFS))
LDFLAGS = -lX11 -lXext `pkg-config opencv4 --libs`
SRCFILES = $(shell find $(SRCF) -name "*.cpp")
OBJFILES = $(patsubst $(SRCF)%, $(BUILDDIR)%, $(patsubst %.cpp, %.o, $(SRCFILES))) $(LOGO)
DEPFILES = $(wildcard $(DEPF)*.d)
SOURCEDIRS = $(shell find $(SRCF) -type d -printf "%p/\n")
BUILDDIRS = $(patsubst $(SRCF)%, $(BUILDDIR)%, $(SOURCEDIRS))
OBJFILESTEST = $(filter-out $(BUILDDIR)main.o, $(OBJFILES))
INCLUDES += $(addprefix -I, $(SOURCEDIRS))
all: $(NAME) runtest
$(NAME): $(BUILDDIRS) $(DEPF) $(OBJFILES)
@echo "Linking $@"
@$(CXX) $(CFLAGS) -o $@ $(filter %.o, $^) $(LDFLAGS)
$(BUILDDIR)%.o: $(SRCF)%.cpp
@echo "Compiling: $@"
@$(CXX) $(CFLAGS) $(INCLUDES) $< -MM -MT $@ > $(DEPF)$(subst /,_,$*).d
@$(CXX) -c -o $@ $(CFLAGS) $(INCLUDES) $<
$(NAME)_strip: $(NAME)
@echo "Strip $<"
@strip -o $@ $<
%/:
mkdir -p $@
clean-depends:
$(RM) -r $(DEPF)
$(LOGO):
$(MAKE) -C $(LOGF) all
clean:
$(RM) -r $(NAME) $(BUILDDIR) $(NAMETEST) $(NAME)_strip
$(MAKE) -C $(LOGF) $@
$(NAMETEST): $(BUILDDIRS) $(DEPF) $(TESTF)*.cpp $(OBJFILESTEST)
@echo "Compiling tests"
@$(CXX) -o $@ $(filter %.o, $^) $(filter %.cpp, $^) $(CFLAGS) -I$(SRCF) $(INCLUDES) $(LDFLAGS)
runtest: $(NAMETEST)
@echo "Running tests"
./$<
.PHONY: clean all $(NAMETEST) clean-depends runtest
include $(DEPFILES)

31
include/screen.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <opencv2/opencv.hpp>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
class ScreenShot {
private:
Display* display = nullptr;
Window root;
XWindowAttributes window_attributes;
XImage* ximg = nullptr;
XShmSegmentInfo shminfo;
int x, y, width, height;
bool init;
public:
ScreenShot();
~ScreenShot();
void operator() (cv::Mat& cvimg);
};

82
src/main.cpp Normal file
View File

@ -0,0 +1,82 @@
#include "Log.h"
#include "screen.h"
std::string type2str(int type) {
std::string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch ( depth ) {
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
case CV_16S: r = "16S"; break;
case CV_32S: r = "32S"; break;
case CV_32F: r = "32F"; break;
case CV_64F: r = "64F"; break;
default: r = "User"; break;
}
r += "C";
r += (chans+'0');
return r;
}
int main(int argc, const char** argv) {
Log::init();
Log::setConsoleLogLevel(Log::Level::TRACE);
Log::addLogfile("log.txt", Log::Level::TRACE);
#if __unix__
Log::setColoredOutput(true);
#endif
Log::info << "Hello, World!";
// load template
cv::Mat tem = cv::imread("imgs/Accept.png", cv::IMREAD_COLOR);
// cv::imshow("template", tem);
// cv::waitKey(0);
ScreenShot screen;
cv::Mat img;
screen(img);
// cv::imshow("img", img);
// cv::waitKey(0);
Log::info << "img " << img.type();
cv::Mat out;
int result_cols = img.cols - tem.cols + 1;
int result_rows = img.rows - tem.rows + 1;
out.create(result_rows, result_cols, CV_32FC1);
Log::info << "tem " << tem.type() << " " << type2str(tem.type());
// img.convertTo(img, tem.type());
cv::cvtColor(img, img, cv::COLOR_RGBA2RGB);
Log::info << "converted img: " << img.type() << " " << type2str(img.type());
cv::matchTemplate(img, tem, out, cv::TM_SQDIFF);
cv::normalize(out, out, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
double minVal; double maxVal;
cv::Point minLoc; cv::Point maxLoc; cv::Point matchLoc;
cv::minMaxLoc( out, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() );
matchLoc = minLoc;
cv::rectangle(img, matchLoc, cv::Point( matchLoc.x + tem.cols , matchLoc.y + tem.rows ), cv::Scalar(255, 0, 0, 0), 2);
cv::imshow("result", img);
cv::waitKey(0);
time_t t = time(0);
cv::imwrite("debugimages/" + std::to_string(t) + ".png", img);
Log::stop();
return 0;
}

59
src/screen.cpp Normal file
View File

@ -0,0 +1,59 @@
// g++ screena.cpp -o screena -lX11 -lXext -Ofast -mfpmath=both -march=native -m64 -funroll-loops -mavx2 `pkg-config opencv --cflags --libs` && ./screena
// This includes most headers!
#include "screen.h"
// https://stackoverflow.com/a/39781697/4399859
ScreenShot::ScreenShot() {
display = XOpenDisplay(nullptr);
root = DefaultRootWindow(display);
int count_screens = ScreenCount(display);
if(count_screens == 0) {
puts("no screen info!");
}
for (int i = 0; i < count_screens; ++i) {
Screen* screen = ScreenOfDisplay(display, i);
printf("\tScreen %d: %dX%d\n", i + 1, screen->width, screen->height);
}
XGetWindowAttributes(display, root, &window_attributes);
Screen* screen = window_attributes.screen;
width = screen->width;
height = screen->height;
x = 0;
y = 0;
ximg = XShmCreateImage(display, DefaultVisualOfScreen(screen), DefaultDepthOfScreen(screen), ZPixmap, NULL, &shminfo, width, height);
shminfo.shmid = shmget(IPC_PRIVATE, ximg->bytes_per_line * ximg->height, IPC_CREAT|0777);
shminfo.shmaddr = ximg->data = (char*)shmat(shminfo.shmid, 0, 0);
shminfo.readOnly = false;
if(shminfo.shmid < 0)
puts("Fatal shminfo error!");
Status s1 = XShmAttach(display, &shminfo);
printf("XShmAttach() %s\n", s1 ? "success!" : "failure!");
init = true;
}
void ScreenShot::operator() (cv::Mat& cv_img){
if(init)
init = false;
XShmGetImage(display, root, ximg, 0, 0, 0x00ffffff);
cv_img = cv::Mat(height, width, CV_8UC4, ximg->data);
}
ScreenShot::~ScreenShot(){
if(!init)
XDestroyImage(ximg);
XShmDetach(display, &shminfo);
shmdt(shminfo.shmaddr);
XCloseDisplay(display);
}

43
tests/main.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include "test.h"
#include <map>
#include <string>
#include <chrono>
#define RED "\033[1;91m"
#define GREEN "\033[1;92m"
#define AQUA "\033[1;36m"
#define RESET "\033[;1m"
extern std::map<std::string, test_t> tests;
void loadTests();
int main(int argc, char** argv) {
auto start = std::chrono::high_resolution_clock::now();
loadTests();
int failcount = 0;
int testcount = tests.size();
int testnumber = 0;
for(std::map<std::string, test_t>::iterator current = tests.begin(); current != tests.end(); ++current) {
printf("\033[1mRunning test: %d/%d " AQUA "%s " RESET, ++testnumber, testcount, current->first.c_str());
if((current->second)()) {
printf(GREEN "succeeded" RESET "!\n");
} else {
printf(RED "failed" RESET "\n");
failcount++;
}
}
const char* color = (failcount > 0 ? RED : GREEN); // red or green
printf("%s%d" RESET "/%d failed\n", color, failcount, testcount);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> t = end - start;
printf("Testing took: %fms\n", (t.count() * 1000));
return failcount > 0;
}

6
tests/sampletest.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "test.h"
TEST(ABC) {
CMPASSERT(1, true);
} TESTEND

17
tests/test.h Normal file
View File

@ -0,0 +1,17 @@
#define TESTFAILED 0
#define TESTGOOD 1
#include <iostream>
#define TESTDATA "./tests/data/"
#define TESTNAME(NAME) test_##NAME
#define TESTFUNC(NAME) bool TESTNAME(NAME)()
#define TEST(NAME) TESTFUNC(NAME) {
#define TESTEND return TESTGOOD; }
#define ASSERT(BED, ERR) if(!(BED)) { std::cout << __FILE__ << ":" << __LINE__ << " " << ERR << std::endl; return TESTFAILED; }
#define CMPASSERTE(A, B, ERR) if( !((A) == (B))) { std::cout << __FILE__ << ":" << __LINE__ << " is: \"" << (A) << "\" should: \"" << (B) << "\""<< std::endl; return TESTFAILED; }
#define CMPASSERT(A, B) CMPASSERTE(A, B, "")
typedef bool (*test_t)();

14
tests/tests.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "test.h"
#include <map>
#include <string>
#define REGISTERTEST(NAME) tests.insert({#NAME, TESTNAME(NAME)})
TESTFUNC(ABC);
std::map<std::string, test_t> tests;
void loadTests() {
REGISTERTEST(ABC);
}

1
thirdparty/Log vendored Submodule

@ -0,0 +1 @@
Subproject commit 96d10d6e72ddaffd8b688db5ef2705e38aff3a75