Skip to content

Commit 3dbc926

Browse files
committed
add python,cs interface
1 parent e96e1da commit 3dbc926

28 files changed

+1024
-4
lines changed
-43.5 KB
Binary file not shown.

Yolov3_SpringEdition_Test/YOLOv3SE.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ class YOLOv3 {
6868
using YoloTrainType = void(*)(char* _base_dir, char* _datafile, char* _cfgfile);
6969
using YoloDetectFromFileType = int(*)(char* img_path, int* _net, float threshold, float* result, int result_sz);
7070
using YoloDetectFromImageType = int(*)(float* data, int w, int h, int c, int* _net, float threshold, float* result, int result_sz);
71+
using YoloReleaseType = void(*)(int* net);
7172
private:
7273
YoloLoadType YoloLoad = nullptr;
7374
YoloTrainType YoloTrain = nullptr;
7475
YoloDetectFromFileType YoloDetectFromFile = nullptr;
7576
YoloDetectFromImageType YoloDetectFromImage = nullptr;
77+
YoloReleaseType YoloRelease=nullptr;
7678
protected:
7779
int* m_network = nullptr;
7880
#ifdef _WIN32
@@ -113,6 +115,7 @@ class YOLOv3 {
113115
}
114116
void Release() {
115117
if (this->m_hmod != nullptr) {
118+
YoloRelease(m_network);
116119
#ifdef _WIN32
117120
FreeLibrary(this->m_hmod);
118121
#else
@@ -217,13 +220,14 @@ class YOLOv3 {
217220
std::string dll = "libYOLOv3SE.dll";
218221
m_hmod = LoadLibraryA(dll.c_str());
219222
if (m_hmod == nullptr) {
220-
::MessageBoxA(NULL, "libYOLOv3SE.dll not found. or can't load dependency dll(cudnn64_5)", "Fatal", MB_OK);
223+
::MessageBoxA(NULL, "libYOLOv3SE.dll not found. or can't load dependency dll(cudnn64_7)", "Fatal", MB_OK);
221224
exit(1);
222225
}
223226
YoloLoad = (YoloLoadType)GetProcAddress(m_hmod, "YoloLoad");
224227
YoloTrain = (YoloTrainType)GetProcAddress(m_hmod, "YoloTrain");
225228
YoloDetectFromFile = (YoloDetectFromFileType)GetProcAddress(m_hmod, "YoloDetectFromFile");
226229
YoloDetectFromImage = (YoloDetectFromImageType)GetProcAddress(m_hmod, "YoloDetectFromImage");
230+
YoloRelease = (YoloReleaseType)GetProcAddress(m_hmod, "YoloRelease");
227231
#else
228232
m_hmod = dlopen("libYOLOv3SE.so",RTLD_LAZY);
229233
if (m_hmod == nullptr) {
@@ -235,6 +239,7 @@ class YOLOv3 {
235239
YoloTrain = (YoloTrainType)dlsym(m_hmod, "YoloTrain");
236240
YoloDetectFromFile = (YoloDetectFromFileType)dlsym(m_hmod, "YoloDetectFromFile");
237241
YoloDetectFromImage = (YoloDetectFromImageType)dlsym(m_hmod, "YoloDetectFromImage");
242+
YoloRelease = (YoloReleaseType)dlsym(m_hmod, "YoloRelease");
238243
#endif
239244
}
240245
~YOLOv3() {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

Yolov3_SpringEdition_Test/Yolov3_SpringEdition_CSTest/Form1.Designer.cs

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Data;
6+
using System.Drawing;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using System.Windows.Forms;
11+
using System.Runtime.InteropServices;
12+
13+
14+
namespace YOLOv3_CS_Example
15+
{
16+
public partial class Form1 : Form
17+
{
18+
[DllImport("libYOLOv3SE.dll", EntryPoint = "YoloLoad", CallingConvention = CallingConvention.Cdecl)]
19+
static extern IntPtr YoloLoad(String cfg,String weights);
20+
[DllImport("libYOLOv3SE.dll", EntryPoint = "YoloDetectFromFile", CallingConvention = CallingConvention.Cdecl)]
21+
static extern int YoloDetectFromFile(String img_path,IntPtr net,float threshold,float[] result,int result_sz);
22+
[DllImport("libYOLOv3SE.dll", EntryPoint = "YoloRelease", CallingConvention = CallingConvention.Cdecl)]
23+
static extern int YoloRelease(IntPtr net);
24+
25+
String image_file;
26+
IntPtr net = IntPtr.Zero;
27+
public Form1()
28+
{
29+
InitializeComponent();
30+
pictureBox1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left);
31+
btn_load.Anchor = (AnchorStyles.Bottom);
32+
btn_detect.Anchor = (AnchorStyles.Bottom);
33+
btn_openimage.Anchor = (AnchorStyles.Bottom);
34+
btn_release.Anchor = (AnchorStyles.Bottom);
35+
}
36+
37+
private void OnClickLoad(object sender, EventArgs e)
38+
{
39+
if (net != IntPtr.Zero)
40+
{
41+
YoloRelease(net);
42+
}
43+
String weights="", cfg="";
44+
OpenFileDialog ofd = new OpenFileDialog();
45+
ofd.Filter = "Weights Files(*.weights)|*.weights|All files (*.*)|*.*";
46+
if (ofd.ShowDialog() == DialogResult.OK)
47+
{
48+
weights = ofd.FileName;
49+
ofd.Filter = "Cfg Files(*.cfg)|*.cfg|All files (*.*)|*.*";
50+
if (ofd.ShowDialog() == DialogResult.OK)
51+
{
52+
cfg = ofd.FileName;
53+
}
54+
}
55+
if (cfg != "" && weights != "")
56+
{
57+
this.net=YoloLoad(cfg, weights);
58+
}else
59+
{
60+
this.net = IntPtr.Zero;
61+
}
62+
}
63+
64+
private void OnClickDetect(object sender, EventArgs e)
65+
{
66+
if (this.net != IntPtr.Zero)
67+
{
68+
float[] result = new float[1024];
69+
int n=YoloDetectFromFile(image_file, net, 0.5F, result, 1024);
70+
FileStream fs = new FileStream(image_file, FileMode.Open, FileAccess.Read);
71+
Image img = System.Drawing.Image.FromStream(fs);
72+
fs.Close();
73+
Graphics g = Graphics.FromImage(img);
74+
Pen pen = new Pen(Color.Red, 5);
75+
76+
for (int i = 0; i < n; i++)
77+
{
78+
int c= (int)result[i * 6 + 0];
79+
int x = (int)result[i * 6 + 2];
80+
int y = (int)result[i * 6 + 3];
81+
int w = (int)result[i * 6 + 4];
82+
int h = (int)result[i * 6 + 5];
83+
Rectangle rect = new Rectangle(x, y, w, h);
84+
g.DrawRectangle(pen, rect);
85+
}
86+
pictureBox1.Image = img;
87+
}
88+
}
89+
90+
private void OnClickRelease(object sender, EventArgs e)
91+
{
92+
if (net != IntPtr.Zero)
93+
{
94+
YoloRelease(net);
95+
}
96+
}
97+
98+
private void OnClickOpenImage(object sender, EventArgs e)
99+
{
100+
OpenFileDialog ofd = new OpenFileDialog();
101+
ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
102+
103+
104+
if (ofd.ShowDialog() == DialogResult.OK)
105+
{
106+
107+
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
108+
Image img = System.Drawing.Image.FromStream(fs);
109+
fs.Close();
110+
111+
pictureBox1.Image = img;
112+
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
113+
this.image_file = ofd.FileName;
114+
}
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)