Skip to content

Commit

Permalink
The Big Kahuna
Browse files Browse the repository at this point in the history
Things I did: switched input from using virtual keys to hardware scan
codes. Fixed formatting.  Trying BGR2GRAY for shits and giggles.

And the big problems: Lines

Things needed to do: Find out if GPUMat conversion is needed.  Figure
out what the heck we are doing with imageGPU.

Big Problems: Lines 144 and 149 are causing program to crash, presumably
because of buffer overflow.
  • Loading branch information
zappybiby committed Jan 20, 2017
1 parent c8d24d5 commit 6c96498
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 125 deletions.
Binary file modified ets2_auto_driving.sdf
Binary file not shown.
Binary file modified ets2_auto_driving.v12.suo
Binary file not shown.
247 changes: 122 additions & 125 deletions ets2_auto_driving/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
//#include <opencv2/imageproc/imageproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/photo/cuda.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/cudaarithm.hpp"
#include <opencv2/cudaarithm.hpp>
#include <opencv2/highgui/highgui_c.h>
//#include <opencv2/cudaimgproc.hpp>
//#include <opencv2/cudafilters.hpp>
//#include <opencv2/gpu/gpu.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/cudafilters.hpp>
#include <Windows.h>
#include <iostream>
#include <string>
Expand All @@ -35,17 +33,17 @@ int main() {
while (true) {
auto begin = chrono::high_resolution_clock::now();
// ETS2
HWND hWnd = FindWindow("prism3d", NULL);
HWND hWnd = FindWindow("prism3d", NULL); // This finds the game window and sets the source for hWnd
// NOTEPAD
//HWND hWnd = FindWindow("Photo_Light", NULL);
Mat image, outputImg;
Mat image, outputImg; // Creates two Mats, image and outputImg. outputImg is used for showing what the program sees to user in a new window.
hwnd2mat(hWnd).copyTo(image);

// Mat to GpuMat
//cuda::GpuMat imageGPU;
//imageGPU.upload(image);

medianBlur(image, image, 3);
medianBlur(image, image, 3);
//bilateralFilter(imageGPU, imageGPU, 15, 80, 80);

int width = 0, height = 0;
Expand All @@ -58,12 +56,12 @@ int main() {

// The 4-points at the input image
vector<Point2f> origPoints;
origPoints.push_back(Point2f(0, (height-50)));
origPoints.push_back(Point2f(width, height-50));
origPoints.push_back(Point2f(width/2+125, height/2+30));
origPoints.push_back(Point2f(width/2-125, height/2+30));

origPoints.push_back(Point2f(0, (height - 50)));
origPoints.push_back(Point2f(width, height - 50));
origPoints.push_back(Point2f(width / 2 + 125, height / 2 + 30));
origPoints.push_back(Point2f(width / 2 - 125, height / 2 + 30));


// The 4-points correspondences in the destination image
vector<Point2f> dstPoints;
Expand All @@ -84,28 +82,28 @@ int main() {
//ipm.drawPoints(origPoints, image);

//imageGPU.download(image);
//Mat row = outputImg.row[0];
cv::Mat gray;
cv::Mat blur;
cv::Mat sobel;
cv::Mat contours;
cv::resize(outputImg, outputImg, cv::Size(320, 240));
cv::cvtColor(outputImg, gray, COLOR_RGB2GRAY);
//cv::cvtColor(outputImg, gray, COLOR_RGB2GRAY);
cv::cvtColor(outputImg, gray, COLOR_BGR2GRAY); // testing using BGR instead of RGB (https://stackoverflow.com/questions/7461075)
cv::blur(gray, blur, cv::Size(10, 10));
cv::Sobel(blur, sobel, blur.depth(), 1, 0, 3, 0.5, 127);
cv::threshold(sobel, contours, 145, 255, CV_THRESH_BINARY);
//Thinning(contours, contours.rows, contours.cols);
//cv::Canny(gray, contours, 125, 350);

LineFinder ld; // 인스턴스 생성

// 확률적 허프변환 파라미터 설정하기
LineFinder ld;

ld.setLineLengthAndGap(20, 120);
ld.setMinVote(55);

std::vector<cv::Vec4i> li = ld.findLines(contours);
ld.drawDetectedLines(contours);

//cv::cvtColor(contours, contours, COLOR_GRAY2RGB);
imshow("Test", contours);
waitKey(1);
Expand All @@ -118,144 +116,143 @@ int main() {
cout << 1000 / ms << "fps avr:" << 1000 / (sum / (++i)) << endl;
*/
///////////////////////////////////////

unsigned char row_center = gray.at<unsigned char>(10, 160);
unsigned char row_left = 0;
unsigned char row_right = 0;
int left = 0;
int right = 0;
int i = 0;
int row_number = 5;
while (i < 150) {

unsigned char row_left = 0;
unsigned char row_right = 0;

int left = 0;
int right = 0;
int i = 0;
int row_number = 5;
while (i < 150) {
if (i == 149) {
i = 0;
row_left = 0;
row_right = 0;
left = 0;
right = 0;
row_number++;

}
if (row_left == 255 && row_right == 255) {
row_number = 5;
break;
}
if (row_left != 255) {
row_left = gray.at<unsigned char>(row_number, 159 + left);
row_left = gray.at<unsigned char>(row_number, 159 + left); // Access violation reading location - see results.txt
left--;

}
if (row_right != 255) {
row_right = gray.at<unsigned char>(row_number, 159 + right);
row_right = gray.at<unsigned char>(row_number, 159 + right); // Access violation reading location - see results.txt
right++;

}
i++;

}
SetActiveWindow(hWnd);
int average = (left == -150 || right == 150) ? 0 : left + right;
if (left + right < -50){
if (left + right < -50)
{
cout << "go left ";

//SendMessage(hWnd, WM_KEYUP, 0x44, 0);
//Sleep(100);
//SendMessage(hWnd, WM_KEYDOWN, 0x74, 0);
//Sleep(100);
//SendMessage(hWnd, WM_KEYUP, 0x74, 0);

HKL kbl = GetKeyboardLayout(0);

// Create a generic keyboard event structure
INPUT ip;
Sleep(1000);
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
while (1)
{
ip.ki.wVk = 0x74;
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(ip));

ip.ki.wVk = 0x74;
ip.ki.dwFlags = KEYEVENTF_KEYUP; // Releases 0x74
SendInput(1, &ip, sizeof(ip));

ip.ki.dwFlags = 0;
ip.ki.wVk = 0x44; // Presses 'D'
SendInput(1, &ip, sizeof(ip));
}


//SendMessage(hWnd, WM_KEYUP, 0x44, 0); // SendMessage not working for DirectX window, using SendInput instead
//Sleep(100);
//SendMessage(hWnd, WM_KEYDOWN, 0x74, 0);
//Sleep(100);
//SendMessage(hWnd, WM_KEYUP, 0x74, 0);

HKL kbl = GetKeyboardLayout(0);

// Create a generic keyboard event structure
INPUT ip; // Using SendInput to send input commands
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.wVk = 0; // We're doing scan codes instead (not using virtual keys)
ip.ki.dwExtraInfo = 0;
//while (1)
//{
ip.ki.wScan = 0x74;
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // Releases 0x74
SendInput(1, &ip, sizeof(ip));
Sleep(100);

ip.ki.dwFlags = KEYEVENTF_SCANCODE;
ip.ki.wScan = 0x41; // Presses 'A'
SendInput(1, &ip, sizeof(ip));
//}
}
else if (left + right > -50 && left + right < 50){
cout << "go straight ";
for (int x = 0, y = 0; x < 700 && y<700; x += 10, y += 10)
{
for (int x = 0, y = 0; x < 700 && y < 700; x += 10, y += 10)
{
SendMessage(hWnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
Sleep(10);
}
//SendMessage(hWnd, WM_KEYUP, 0x44, 0);
//Sleep(10);
//SendMessage(hWnd, WM_KEYUP, 0x41, 0);
INPUT ip;
Sleep(5000);
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
while (1)
ip.ki.wVk = 0x44;
ip.ki.dwFlags = KEYEVENTF_KEYUP; // Releases 0x44
SendInput(1, &ip, sizeof(ip));

ip.ki.wVk = 0x41;
ip.ki.dwFlags = KEYEVENTF_KEYUP; // Releases 0x41
SendInput(1, &ip, sizeof(ip));
Sleep(1000);
Sleep(100);

//SendMessage(hWnd, WM_KEYUP, 0x44, 0);
//Sleep(10);
//SendMessage(hWnd, WM_KEYUP, 0x41, 0);

INPUT ip; // Using SendInput to send input commands
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.wVk = 0; // We're doing scan codes instead
ip.ki.dwExtraInfo = 0;

ip.ki.wScan = 0x44;
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // Releases 'D'
SendInput(1, &ip, sizeof(ip));
Sleep(100);

ip.ki.wScan = 0x41;
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // Releases 'A'
SendInput(1, &ip, sizeof(ip));
Sleep(100);
}
}
else{
cout << "go right ";
//SendMessage(hWnd, WM_KEYUP, 0x41, 0);
//Sleep(100);
//SendMessage(hWnd, WM_KEYDOWN, 0x74, 0);
//Sleep(100);
//SendMessage(hWnd, WM_KEYUP, 0x74, 0);

INPUT ip;
Sleep(1000);
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
while (1)
{
ip.ki.wVk = 0x74;
ip.ki.dwFlags = 0; // Presses 0x41
SendInput(1, &ip, sizeof(ip));

ip.ki.wVk = 0x74;
ip.ki.dwFlags = KEYEVENTF_KEYUP; // Releases 0x41
SendInput(1, &ip, sizeof(ip));

ip.ki.dwFlags = 0;
ip.ki.wVk = 0x41; // Presses 'A'
SendInput(1, &ip, sizeof(ip));

Sleep(1000);
}
// keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
{
//SendMessage(hWnd, WM_KEYUP, 0x41, 0);
//Sleep(100);
//SendMessage(hWnd, WM_KEYDOWN, 0x74, 0);
//Sleep(100);
//SendMessage(hWnd, WM_KEYUP, 0x74, 0);

INPUT ip;
Sleep(100);
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.wVk = 0; // We're doing scan codes instead
ip.ki.dwExtraInfo = 0;
//while (1)
//{
ip.ki.wScan = 0x44;
ip.ki.dwFlags = KEYEVENTF_SCANCODE; // Presses 0x41
SendInput(1, &ip, sizeof(ip));
Sleep(100);

ip.ki.wScan = 0x44;
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // Releases 'A'
SendInput(1, &ip, sizeof(ip));
Sleep(100);

ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
ip.ki.wScan = 0x41; // Presses 'A'
SendInput(1, &ip, sizeof(ip));
Sleep(100);
// keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
}
}
cout << "left: " << left << ", right: " << right << ", average: " << average << endl;
///////////////////////////////////////


imshow("Test", gray);
///////////////////////////////////////


imshow("Test", gray);
waitKey(1);
}
return 0;
}
Binary file modified ets2_auto_driving/vc120.pdb
Binary file not shown.
32 changes: 32 additions & 0 deletions results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Dr. Memory version 1.11.0 build 2 built on Aug 29 2016 02:41:18
Dr. Memory results for pid 6088: "ets2_auto_driving.exe"
Application cmdline: "C:\Users\brian\Desktop\ChosunTruck\x64\Release\ets2_auto_driving.exe"
Recorded 115 suppression(s) from default C:\Program Files (x86)\Dr. Memory\bin64\suppress-default.txt

Error #1: UNADDRESSABLE ACCESS beyond heap bounds: reading 1 byte(s)
# 0 main [c:\users\brian\desktop\chosuntruck\ets2_auto_driving\main.cpp:144]
Note: refers to 2 byte(s) before next malloc

Error #2: UNADDRESSABLE ACCESS beyond heap bounds: reading 1 byte(s)
# 0 main [c:\users\brian\desktop\chosuntruck\ets2_auto_driving\main.cpp:149]

Error #3: UNADDRESSABLE ACCESS beyond heap bounds: reading 1 byte(s)
# 0 main [c:\users\brian\desktop\chosuntruck\ets2_auto_driving\main.cpp:144]
Note: refers to 3 byte(s) before next malloc

Error #4: UNADDRESSABLE ACCESS beyond heap bounds: reading 1 byte(s)
# 0 main [c:\users\brian\desktop\chosuntruck\ets2_auto_driving\main.cpp:149]

Error #5: UNADDRESSABLE ACCESS beyond heap bounds: reading 1 byte(s)
# 0 main [c:\users\brian\desktop\chosuntruck\ets2_auto_driving\main.cpp:144]
Note: refers to 3 byte(s) before next malloc

Error #6: UNADDRESSABLE ACCESS beyond heap bounds: reading 1 byte(s)
# 0 main [c:\users\brian\desktop\chosuntruck\ets2_auto_driving\main.cpp:149]
Note: refers to 16 byte(s) before memory that was freed here:
Note: # 0 replace_operator_delete_nothrow_nomatch [d:\drmemory_package\common\alloc_replace.c:3052]
Note: # 1 opencv_imgproc310.dll!cv::ocl::Device::isAMD +0x322e (0x00007ffd0d81084f <opencv_imgproc310.dll+0x132084f>)
Note: # 2 opencv_imgproc310.dll!cv::_InputArray::_InputArray +0x2bde (0x00007ffd0d7ebb2f <opencv_imgproc310.dll+0x12fbb2f>)
Note: # 3 opencv_imgproc310.dll!cv::String::operator+= +0x29b4 (0x00007ffd0d805fb5 <opencv_imgproc310.dll+0x1315fb5>)
Note: # 4 opencv_imgproc310.dll!cv::String::operator+= +0x1e63 (0x00007ffd0d805464 <opencv_imgproc310.dll+0x1315464>)
Note: # 5 opencv_imgproc310.dll!cv::Mat::create +0x188b (0x00007ffd0d80791c <opencv_imgproc310.dll+0x131791c>)

0 comments on commit 6c96498

Please sign in to comment.