Skip to content

Commit 9ff27b5

Browse files
committed
remake
1 parent b9db058 commit 9ff27b5

File tree

339 files changed

+74325
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

339 files changed

+74325
-0
lines changed

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.h eol=crlf
2+
*.c eol=crlf
3+
*.cpp eol=crlf
4+
*.hpp eol=crlf
5+
*.py eol=crlf
6+
*.txt eol=crlf
7+
*.md eol=crlf
8+
*.bat eol=crlf
9+
*.txt eol=crlf
10+
CNTK/* linguist-vendored
Binary file not shown.
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*
2+
* FasterRCNN_SE.h
3+
* FasterRCNN_SpringEdition
4+
*
5+
* Created by kimbom on 2017. 10. 2...
6+
* Copyright 2017 Sogang Univ. All rights reserved.
7+
*
8+
*/
9+
#if !defined(FASTERRCNN_SE_7E1_A_2_FASTERRCNN_SE_HPP_INCLUDED)
10+
#define FASTERRCNN_SE_7E1_A_2_FASTERRCNN_SE_HPP_INCLUDED
11+
#include<iostream>
12+
#include<vector>
13+
#include<string>
14+
#include<sstream>
15+
#include<Windows.h>
16+
#include<opencv2/opencv.hpp>
17+
#ifndef SPRING_EDITION_BOX
18+
#define SPRING_EDITION_BOX
19+
/**
20+
* @brief 이 클래스는 cv::Rect를 확장한 것으로 클래스값과 스코어값이 추가되었습니다.
21+
* @author kimbomm
22+
* @date 2017-10-05
23+
*
24+
* @see https://github.com/springkim/FasterRCNN_SpringEdition
25+
* @see https://github.com/springkim/YOLOv2_SpringEdition
26+
*/
27+
class BoxSE : public cv::Rect {
28+
public:
29+
int m_class = -1;
30+
float m_score = 0.0F;
31+
std::string m_class_name;
32+
BoxSE() {
33+
m_class_name = "Unknown";
34+
}
35+
BoxSE(int c, float s, int _x, int _y, int _w, int _h, std::string name = "")
36+
:m_class(c), m_score(s) {
37+
this->x = _x;
38+
this->y = _y;
39+
this->width = _w;
40+
this->height = _h;
41+
char* lb[5] = { "th","st","nd","rd","th" };
42+
if (name.length() == 0) {
43+
m_class_name = std::to_string(m_class) + lb[m_class < 4 ? m_class : 4] + " class";
44+
}
45+
}
46+
};
47+
#endif
48+
class FasterRCNN {
49+
public:
50+
static const int AlexNet = 0;
51+
static const int VGG16 = 1;
52+
static const int VGG19 = 2;
53+
private:
54+
int m_base_model;
55+
std::string m_key_mutex = "";
56+
HANDLE m_mutex = INVALID_HANDLE_VALUE;
57+
std::string m_key_shmem = "";
58+
HANDLE m_shmem = INVALID_HANDLE_VALUE;
59+
DWORD m_size = 0;
60+
char* m_buffer = nullptr;
61+
//0(48) = load model
62+
//1(49) = send image path
63+
//2(50) = detect
64+
//3(51) = receive
65+
//9(57) = terminate signal
66+
std::vector<std::string> m_class_map;
67+
public:
68+
void Create(int base_model, std::string model_path,std::string classfile,DWORD size=10000,float filter_threshold=0.4F) {
69+
m_base_model = base_model;
70+
m_key_shmem = this->GetKey() + "_shmem";
71+
m_key_mutex = this->GetKey() + "_mutex";
72+
m_size = size;
73+
74+
m_mutex = ::CreateMutexA(nullptr, FALSE, m_key_mutex.c_str());
75+
m_shmem = ::CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr,PAGE_READWRITE, 0, m_size, m_key_shmem.c_str());
76+
m_buffer = (char*)::MapViewOfFile(m_shmem, FILE_MAP_ALL_ACCESS, 0, 0, m_size);
77+
m_buffer[0] = 0;
78+
//std::string exec = "python35 \"C:/Users/sprin/Downloads/FasterRCNN_SpringEdition_final/FasterRCNN_SE_Train/src/FasterRCNN_Detect_SE.py\"";
79+
std::string exec = "FasterRCNN_Detect_SE.exe";
80+
HWND hwnd = GetForegroundWindow();
81+
std::vector<std::string> models = { "AlexNet","VGG16","VGG19" };
82+
std::ostringstream oss;
83+
//oss << exec << " " << m_key_shmem << " " << m_key_mutex << " " << m_size << " " << "\"" << model_path << "\"" << "\t" << filter_threshold << " " << hwnd << " " << models[m_base_model];
84+
//UINT ret=WinExec(oss.str().c_str(), SW_HIDE);
85+
oss << m_key_shmem << " " << m_key_mutex << " " << m_size << " " << "\"" << model_path << "\"" << "\t" << filter_threshold << " " << hwnd << " " << models[m_base_model];
86+
HINSTANCE ret=ShellExecuteA(NULL, "open", exec.c_str(), oss.str().c_str(), NULL, SW_HIDE);
87+
if ((size_t)ret < 31) {
88+
::MessageBoxA(nullptr, "FasterRCNN Detector execute failed\nIt needs \"FasterRCNN_Detect_SE.exe\".", "Error", MB_OK);
89+
exit(1);
90+
}
91+
bool load = false;
92+
while (load==false) {
93+
WaitForSingleObject(m_mutex, INFINITE);
94+
if (m_buffer[0] == '1') {
95+
load = true;
96+
}
97+
::ReleaseMutex(m_mutex);
98+
}
99+
std::fstream fin;
100+
fin.open(classfile, std::ios::in);
101+
if (fin.is_open() == false) {
102+
::MessageBoxA(nullptr, "Can't read class map file", "Error", MB_OK);
103+
exit(1);
104+
}
105+
std::string line;
106+
while (!fin.eof()) {
107+
std::getline(fin, line);
108+
if (line.length() == 0) {
109+
break;
110+
}
111+
std::istringstream iss(line);
112+
std::string _class;
113+
iss >> _class;
114+
m_class_map.push_back(_class);
115+
}
116+
fin.close();
117+
}
118+
void Release() {
119+
bool pass = false;
120+
while (pass == false) {
121+
WaitForSingleObject(m_mutex, INFINITE);
122+
if (m_buffer[0] == '1') {
123+
m_buffer[0] = '9';
124+
pass = true;
125+
}
126+
::ReleaseMutex(m_mutex);
127+
}
128+
::UnmapViewOfFile(m_buffer);
129+
::CloseHandle(m_shmem);
130+
::ReleaseMutex(m_mutex);
131+
m_shmem = INVALID_HANDLE_VALUE;
132+
m_mutex = INVALID_HANDLE_VALUE;
133+
}
134+
~FasterRCNN() {
135+
if (m_shmem != INVALID_HANDLE_VALUE) {
136+
this->Release();
137+
}
138+
}
139+
std::vector<BoxSE> Detect(std::string img_path, float threshold) {
140+
::ReleaseMutex(m_mutex);
141+
bool pass = false;
142+
while (pass == false) {
143+
WaitForSingleObject(m_mutex, INFINITE);
144+
if (m_buffer[0] == '1') {
145+
memset(m_buffer + 1, 0,m_size - 1);
146+
char* p = m_buffer + 1;
147+
const char* q = img_path.data();
148+
while (*q != '\0') {
149+
*p++ = *q++;
150+
}
151+
*p = '\0';
152+
//strcpy(m_buffer + 1, img_path.c_str()); //strcpy is not working on SDL.
153+
//strcpy_s(m_buffer + 1, m_size-1,img_path.c_str()); //strcpy_s is not working on debug mode.
154+
m_buffer[0] = '2'; //running
155+
pass = true;
156+
}
157+
::ReleaseMutex(m_mutex);
158+
}
159+
std::string receive;
160+
bool finish = false;
161+
while (finish == false) {
162+
WaitForSingleObject(m_mutex, INFINITE);
163+
if (m_buffer[0] == '3') {
164+
receive = m_buffer + 1;
165+
m_buffer[0] = '1'; //restore
166+
finish = true;
167+
}
168+
::ReleaseMutex(m_mutex);
169+
}
170+
std::string token = ";";
171+
std::string::size_type offset = 0;
172+
std::vector<int> result;
173+
while (offset<receive.length()) {
174+
std::string str = receive.substr(offset, receive.find(token, offset) - offset);
175+
result.push_back(atoi(str.c_str()));
176+
offset += str.length() + 1;
177+
}
178+
std::vector<BoxSE> boxes;
179+
for (int i = 0; i < result[0]; i++) {
180+
BoxSE box;
181+
box.m_class = result[i * 6 + 1];
182+
box.m_class_name = m_class_map[box.m_class];
183+
box.m_score = result[i * 6 + 2] / 10000.0F;
184+
box.x= result[i * 6 + 3];
185+
box.y = result[i * 6 + 4];
186+
box.width = result[i * 6 + 5] - box.x;
187+
box.height = result[i * 6 + 6] - box.y;
188+
if (box.m_score >= threshold) {
189+
boxes.push_back(box);
190+
}
191+
}
192+
auto IOU = [](BoxSE& a, BoxSE& b)->float {
193+
float i = static_cast<float>((a & b).area());
194+
float u = a.area() + b.area() - i;
195+
return i / u;
196+
};
197+
//Sort by Greater
198+
std::sort(boxes.begin(), boxes.end(), [](BoxSE& a, BoxSE& b)->bool {return a.m_score > b.m_score; });
199+
std::vector<bool> select(boxes.size(), true);
200+
std::vector<BoxSE> boxes2;
201+
for (int i = 0; i < boxes.size(); i++) {
202+
if (select[i] == true) {
203+
for (int j = i + 1; j < boxes.size(); j++) {
204+
if (select[j] == true) {
205+
if (IOU(boxes[i], boxes[j]) > 0.4) {
206+
select[j] = false;
207+
}
208+
}
209+
}
210+
boxes2.push_back(boxes[i]);
211+
}
212+
}
213+
WaitForSingleObject(m_mutex, INFINITE);
214+
return boxes2;
215+
}
216+
std::string GetObjectName(int idx) {
217+
return m_class_map[idx];
218+
}
219+
private:
220+
std::string GetKey() {
221+
//https://stackoverflow.com/questions/10654258/get-millisecond-part-of-time
222+
SYSTEMTIME stime;
223+
FILETIME ftime;
224+
FILETIME ftime_stamp;
225+
GetSystemTimeAsFileTime(&ftime_stamp);
226+
FileTimeToLocalFileTime(&ftime_stamp, &ftime);
227+
FileTimeToSystemTime(&ftime, &stime);
228+
char buf[256];
229+
sprintf_s(buf, "%d%d%d%d%d%d%d", stime.wYear, stime.wMonth, stime.wDay, stime.wHour, stime.wMinute, stime.wSecond, stime.wMilliseconds);
230+
return buf;
231+
}
232+
};
233+
#endif //FASTERRCNN_SE_7E1_A_2_FASTERRCNN_SE_HPP_INCLUDED
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FasterRCNN_SE_Detection_Example", "FasterRCNN_SE_Detection_Example.vcxproj", "{CE923C89-E8A4-454F-AD69-6649711F5FBD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Release|x64 = Release|x64
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{CE923C89-E8A4-454F-AD69-6649711F5FBD}.Debug|x64.ActiveCfg = Debug|x64
15+
{CE923C89-E8A4-454F-AD69-6649711F5FBD}.Debug|x64.Build.0 = Debug|x64
16+
{CE923C89-E8A4-454F-AD69-6649711F5FBD}.Release|x64.ActiveCfg = Release|x64
17+
{CE923C89-E8A4-454F-AD69-6649711F5FBD}.Release|x64.Build.0 = Release|x64
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{CE923C89-E8A4-454F-AD69-6649711F5FBD}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>FasterRCNN_SE_Detection_Example</RootNamespace>
17+
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
18+
</PropertyGroup>
19+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
20+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
21+
<ConfigurationType>Application</ConfigurationType>
22+
<UseDebugLibraries>true</UseDebugLibraries>
23+
<PlatformToolset>v140</PlatformToolset>
24+
<CharacterSet>Unicode</CharacterSet>
25+
</PropertyGroup>
26+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
27+
<ConfigurationType>Application</ConfigurationType>
28+
<UseDebugLibraries>false</UseDebugLibraries>
29+
<PlatformToolset>v140</PlatformToolset>
30+
<WholeProgramOptimization>true</WholeProgramOptimization>
31+
<CharacterSet>Unicode</CharacterSet>
32+
</PropertyGroup>
33+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
34+
<ImportGroup Label="ExtensionSettings">
35+
</ImportGroup>
36+
<ImportGroup Label="Shared">
37+
</ImportGroup>
38+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
39+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
40+
</ImportGroup>
41+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
42+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
43+
</ImportGroup>
44+
<PropertyGroup Label="UserMacros" />
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
46+
<LinkIncremental>true</LinkIncremental>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
49+
<LinkIncremental>false</LinkIncremental>
50+
<TargetName>$(ProjectName)</TargetName>
51+
</PropertyGroup>
52+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
53+
<ClCompile>
54+
<PrecompiledHeader>
55+
</PrecompiledHeader>
56+
<WarningLevel>Level3</WarningLevel>
57+
<Optimization>Disabled</Optimization>
58+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
59+
<SDLCheck>true</SDLCheck>
60+
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty/include/</AdditionalIncludeDirectories>
61+
</ClCompile>
62+
<Link>
63+
<SubSystem>Console</SubSystem>
64+
<GenerateDebugInformation>true</GenerateDebugInformation>
65+
<AdditionalLibraryDirectories>$(SolutionDir)3rdparty/lib/x64/Debug/</AdditionalLibraryDirectories>
66+
<AdditionalDependencies>opencv_world320d.lib;%(AdditionalDependencies)</AdditionalDependencies>
67+
</Link>
68+
<PostBuildEvent>
69+
<Command>xcopy /d /i /Y "$(SolutionDir)3rdparty\bin\x64\Debug\*.*" "$(TargetDir)"
70+
xcopy /d /i /Y "$(SolutionDir)3rdparty\bin\x64\*.*" "$(TargetDir)"</Command>
71+
</PostBuildEvent>
72+
</ItemDefinitionGroup>
73+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
74+
<ClCompile>
75+
<WarningLevel>Level3</WarningLevel>
76+
<PrecompiledHeader>
77+
</PrecompiledHeader>
78+
<Optimization>MaxSpeed</Optimization>
79+
<FunctionLevelLinking>true</FunctionLevelLinking>
80+
<IntrinsicFunctions>true</IntrinsicFunctions>
81+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
82+
<SDLCheck>true</SDLCheck>
83+
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty/include/</AdditionalIncludeDirectories>
84+
</ClCompile>
85+
<Link>
86+
<SubSystem>Console</SubSystem>
87+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
88+
<OptimizeReferences>true</OptimizeReferences>
89+
<GenerateDebugInformation>true</GenerateDebugInformation>
90+
<AdditionalLibraryDirectories>$(SolutionDir)3rdparty/lib/x64/Release/</AdditionalLibraryDirectories>
91+
<AdditionalDependencies>opencv_world320.lib;%(AdditionalDependencies)</AdditionalDependencies>
92+
</Link>
93+
<PostBuildEvent>
94+
<Command>xcopy /d /i /Y "$(SolutionDir)3rdparty\bin\x64\Release\*.*" "$(TargetDir)"
95+
xcopy /d /i /Y "$(SolutionDir)3rdparty\bin\x64\*.*" "$(TargetDir)"</Command>
96+
</PostBuildEvent>
97+
</ItemDefinitionGroup>
98+
<ItemGroup>
99+
<ClCompile Include="main.cpp" />
100+
</ItemGroup>
101+
<ItemGroup>
102+
<ClInclude Include="FasterRCNN_SE.h" />
103+
</ItemGroup>
104+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
105+
<ImportGroup Label="ExtensionTargets">
106+
</ImportGroup>
107+
</Project>

0 commit comments

Comments
 (0)