1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
| public struct ANPR_OPTIONS
{
public int min_plate_size; // Минимальная площадь номера
public int max_plate_size; // Максимальная площадь номера
public int Detect_Mode; // Режимы детектирования
public int max_text_size; // Максимальное количество символов номера + 1
public int type_number; // Тип автомобильного номера
public int flags; // Дополнительные опции
};
#if WIN32
[DllImport("iANPRcapture_vc10_x86.dll")]
unsafe public static extern IntPtr CreateiANPRCapture(int max_frames, ANPR_OPTIONS Options, CvRect Rect);
[DllImport("iANPRcapture_vc10_x86.dll")]
unsafe public static extern void ReleaseiANPRCapture(ref IntPtr Capture);
[DllImport("iANPRcapture_vc10_x86.dll")]
unsafe public static extern int CreateMemoryForiANPRCapture(IntPtr Capture, int min_frames_with_plate, int frames_without_plate, int max_plates_in_mem);
[DllImport("iANPRcapture_vc10_x86.dll")]
unsafe public static extern int CreateLineIntersection(IntPtr Capture, CvPoint p1a, CvPoint p2a, CvPoint p1b, CvPoint p2b);
[DllImport("iANPRcapture_vc10_x86.dll")]
unsafe public static extern int AddFrameToiANPRCapture(IntPtr Capture, IntPtr Image, ref int AllNumber, [In, Out] CvRect[] Rects, IntPtr Texts);
[DllImport("iANPRcapture_vc10_x86.dll")]
unsafe public static extern int GetNumbersInMemory(IntPtr Capture, ref int AllNumber, IntPtr Texts, int Size_Texts, [In, Out] CvPoint[] Points, ref int all_point);
[DllImport("iANPRcapture_vc10_x86.dll")]
unsafe public static extern void LicenseCapture(sbyte[] key);
#elif WIN64
[DllImport("iANPRcapture_vc10_x64.dll")]
unsafe public static extern IntPtr CreateiANPRCapture(int max_frames, ANPR_OPTIONS Options, CvRect Rect);
[DllImport("iANPRcapture_vc10_x64.dll")]
unsafe public static extern void ReleaseiANPRCapture(ref IntPtr Capture);
[DllImport("iANPRcapture_vc10_x64.dll")]
unsafe public static extern int CreateMemoryForiANPRCapture(IntPtr Capture, int min_frames_with_plate, int frames_without_plate, int max_plates_in_mem);
[DllImport("iANPRcapture_vc10_x64.dll")]
unsafe public static extern int CreateLineIntersection(IntPtr Capture, CvPoint p1a, CvPoint p2a, CvPoint p1b, CvPoint p2b);
[DllImport("iANPRcapture_vc10_x64.dll")]
unsafe public static extern int AddFrameToiANPRCapture(IntPtr Capture, IntPtr Image, ref int AllNumber, [In, Out] CvRect[] Rects, IntPtr Texts);
[DllImport("iANPRcapture_vc10_x64.dll")]
unsafe public static extern int GetNumbersInMemory(IntPtr Capture, ref int AllNumber, IntPtr Texts, int Size_Texts, [In, Out] CvPoint[] Points, ref int all_point);
[DllImport("iANPRcapture_vc10_x64.dll")]
unsafe public static extern void LicenseCapture(sbyte[] key);
#endif
static void printHelp(string fullName)
{
Console.Write("Use: {0} <type_number> <path to video file>\n\n", fullName);
Console.WriteLine("type_number: 7 for Russian, 104 for Kazakhstan, 203 for Turkmenistan, 300 for Belarus vehicle registration plates");
Console.WriteLine("For more type_numbers please refer to iANPR SDK documentation\n");
Console.Write("Example: {0} 7 C:\\test.avi - recognition of russian vehicle registration plates from file test.avi\n", fullName);
Console.Write("Example: {0} 104 - recognition of kazakh vehicle registration plates from webcam\n", fullName);
}
unsafe static int Main(string[] args)
{
args = Environment.GetCommandLineArgs();
IntPtr capture = IntPtr.Zero;
IntPtr cvVideoWriter = IntPtr.Zero;
// filter input
if (args.Length < 2)
{
Console.WriteLine("Too few arguments. For help print {0} /?", args[0]);
return -1;
}
else if (args[1] == "help" || args[1] == "-help" || args[1] == "--help" || args[1] == "/?")
{
printHelp(args[0]);
return 0;
}
else if (args[1] != "0" && args[1] != "1" && args[1] != "2" && args[1] != "3" &&
args[1] != "4" && args[1] != "5" && args[1] != "6" && args[1] != "7" &&
args[1] != "100" && args[1] != "101" && args[1] != "102" && args[1] != "103" &&
args[1] != "104" && args[1] != "201" && args[1] != "202" && args[1] != "203" &&
args[1] != "300" && args[1] != "301" && args[1] != "302")
{
Console.Write("type_number is incorrect. Please refer to program help {0} /? or to iANPR SDK documentation\n", args[0]);
return -2;
}
else if (args.Length == 2)
capture = OCV.cvCaptureFromCAM(0);
else
capture = OCV.cvCaptureFromFile(args[2]);
if (capture == IntPtr.Zero)
{
Console.WriteLine("Can't load file or camera");
return -3;
}
ANPR_OPTIONS a = new ANPR_OPTIONS();
a.Detect_Mode = 0x02 | 0x04 | 0x08; // == ANPR_DETECTCOMPLEXMODE
a.min_plate_size = 500;
a.max_plate_size = 50000;
a.max_text_size = 20;
a.type_number = Convert.ToInt32(args[1]);
a.flags = 0;
IntPtr i_capture = IntPtr.Zero;
IntPtr grayFrame = IntPtr.Zero;
IntPtr image = OCV.cvCreateImage(OCV.cvSize(800, 600), 8, 3);
CvPoint[] Lines = new CvPoint[4];
for (int i = 0; ; i++)
{
IntPtr frame = IntPtr.Zero;
frame = OCV.cvQueryFrame(capture);
if (frame == IntPtr.Zero)
break;
if (grayFrame == IntPtr.Zero)
{
grayFrame = OCV.cvCreateImage(OCV.cvGetSize(frame), 8, 1);
OCV.cvZero(grayFrame);
((IplImage*)grayFrame)->origin = ((IplImage*)frame)->origin;
i_capture = CreateiANPRCapture(20, a, OCV.cvRect(0, 0, ((IplImage*)frame)->width, ((IplImage*)frame)->height));
CreateMemoryForiANPRCapture(i_capture, 10, 15, 100);
/*Lines[0].x = int( frame->width * 0.1f ); Lines[0].y = int( frame->height * 0.5f );
Lines[1].x = int( frame->width * 0.3f ); Lines[1].y = int( frame->height * 0.8f );
Lines[2].x = int( frame->width * 0.1f ); Lines[2].y = int( frame->height * 0.6f );
Lines[3].x = int( frame->width * 0.3f ); Lines[3].y = int( frame->height * 0.9f );*/
Lines[0].x = (int)(((IplImage*)frame)->width * 0.1f); Lines[0].y = (int)(((IplImage*)frame)->height * 0.6f);
Lines[1].x = (int)(((IplImage*)frame)->width * 0.3f); Lines[1].y = (int)(((IplImage*)frame)->height * 0.6f);
Lines[2].x = (int)(((IplImage*)frame)->width * 0.1f); Lines[2].y = (int)(((IplImage*)frame)->height * 0.7f);
Lines[3].x = (int)(((IplImage*)frame)->width * 0.3f); Lines[3].y = (int)(((IplImage*)frame)->height * 0.7f);
CreateLineIntersection(i_capture, Lines[0], Lines[1], Lines[2], Lines[3]);
}
OCV.cvCvtColor(frame, grayFrame, 6); // 6 == CV_BGR2GRAY
int tick1 = Environment.TickCount;
int i1 = -1000;
CvRect[] Rects = new CvRect[100];
int all = 100;
IntPtr res = Marshal.AllocHGlobal(all * IntPtr.Size);
for (int ii = 0; ii < all; ii++)
{
Marshal.WriteIntPtr(res, ii * sizeof (IntPtr), Marshal.AllocHGlobal(21));
IntPtr subString = (IntPtr) Marshal.PtrToStructure(res + ii * IntPtr.Size, typeof (IntPtr));
Marshal.WriteByte(subString, 20, 0);
}
if (a.type_number == 4 || a.type_number == 7)
i1 = AddFrameToiANPRCapture(i_capture, frame, ref all, Rects, res);
else
i1 = AddFrameToiANPRCapture(i_capture, grayFrame, ref all, Rects, res);
int tick2 = Environment.TickCount;
float processingTime = (float)(tick2 - tick1) / 1000;
Console.Write("Ret:{0}; num:{1}; time:{2:F3}; cand:{3} ", i1, i, processingTime, all);
if (i1 == 0)
{
for (int j = 0; j < all; j++)
{
IntPtr subStrPtr = (IntPtr)Marshal.PtrToStructure(res + j * IntPtr.Size, typeof(IntPtr));
string carNumber = Marshal.PtrToStringAnsi(subStrPtr);
if (carNumber.Length >= 1)
{
OCV.cvRectangle(frame, OCV.cvPoint(Rects[j].x, Rects[j].y), OCV.cvPoint(Rects[j].x + Rects[j].width,
Rects[j].y + Rects[j].height), OCV.CV_RGB(255, 255, 0), 2);
CvFont font = new CvFont();
float aa = 0.001f * ((IplImage*)frame)->width;
OCV.cvInitFont(ref font, 0/*==CV_FONT_HERSHEY_SIMPLEX*/, aa, aa, 0, 1, 8);
CvPoint pp2, pp1;
pp2.x = Rects[j].x;
pp2.y = Rects[j].y;
pp1.x = Rects[j].x + 1;
pp1.y = Rects[j].y + 1;
OCV.cvPutText(frame, carNumber, pp1, ref font, OCV.CV_RGB(0, 0, 0));
OCV.cvPutText(frame, carNumber, pp2, ref font, OCV.CV_RGB(0, 255, 0));
Console.Write(carNumber + "; ");
}
}
}//if ( i1 == 0 )
Console.WriteLine();
all = 100;
CvPoint[] Points = new CvPoint[1000];
int all_points = 1000;
GetNumbersInMemory(i_capture, ref all, res, 20, Points, ref all_points);
if (all > 0)
{
for (int j = 0; j < all; j++)
{
IntPtr subStrPtr = (IntPtr)Marshal.PtrToStructure(res + j * IntPtr.Size, typeof(IntPtr));
string carNumber = Marshal.PtrToStringAnsi(subStrPtr);
Console.Write("({0})", carNumber);
}
Console.WriteLine();
}
if (Lines[0].y != 0)
{
OCV.cvLine(frame, Lines[0], Lines[1], OCV.CV_RGB(0, 255, 255), 2);
OCV.cvLine(frame, Lines[2], Lines[3], OCV.CV_RGB(0, 255, 255), 2);
}
OCV.cvResize(frame, image);
OCV.cvShowImage("frame", image);
if (cvVideoWriter == IntPtr.Zero)
{
cvVideoWriter = OCV.cvCreateVideoWriter("out.avi", OCV.CV_FOURCC(Convert.ToSByte ('D'), Convert.ToSByte ('I'),
Convert.ToSByte ('V'), Convert.ToSByte ('3')), 20, OCV.cvGetSize(frame));
}
OCV.cvWriteFrame(cvVideoWriter, frame);
// Рисование траектории и вывод дополнительного окна
if (all > 0 && all_points > 1)
{
for (int j = 0; j < all_points; j++)
{
OCV.cvCircle(frame, Points[j], 5, OCV.CV_RGB(0, 0, 255), 3);
if (j > 0)
OCV.cvLine(frame, Points[j], Points[j - 1], OCV.CV_RGB(0, 0, 255), 2);
}
CvFont font = new CvFont();
float aa = 0.001f * ((IplImage*)frame)->width;
OCV.cvInitFont(ref font, 0/*==CV_FONT_HERSHEY_SIMPLEX*/, aa, aa, 0, 1, 8);
CvSize[] size = new CvSize[1];
int[] b = new int[1];
IntPtr subStrPtr = (IntPtr)Marshal.PtrToStructure(res, typeof(IntPtr));
string str = Marshal.PtrToStringAnsi(subStrPtr);
OCV.cvGetTextSize(subStrPtr, ref font, size, b);
OCV.cvRectangle(frame, OCV.cvPoint(0, 0), OCV.cvPoint(size[0].width + 2,
50), OCV.CV_RGB(255, 255, 255), -1/*==CV_FILLED*/ );
CvPoint pp2, pp1;
pp2.x = 0;
pp2.y = 40;
pp1.x = 1;
pp1.y = 41;
OCV.cvPutText(frame, str, pp1, ref font, OCV.CV_RGB(0, 0, 0));
OCV.cvPutText(frame, str, pp2, ref font, OCV.CV_RGB(0, 255, 0));
OCV.cvResize(frame, image);
OCV.cvShowImage("frame", image);
for(int j = 0; j < 60; j++ )
OCV.cvWriteFrame( cvVideoWriter, frame );
}
for (int j = 0; j < 100; j++)
{
IntPtr subString = (IntPtr)Marshal.PtrToStructure(res + j * IntPtr.Size, typeof(IntPtr));
Marshal.FreeHGlobal(subString);
}
Marshal.FreeHGlobal(res);
int c = OCV.cvWaitKey(20);
if (c == 32)
c = OCV.cvWaitKey(0); // pause
if (c == 27) break;
}
if (i_capture != IntPtr.Zero)
ReleaseiANPRCapture(ref i_capture);
OCV.cvReleaseCapture(ref capture);
OCV.cvReleaseImage(ref grayFrame);
OCV.cvReleaseImage(ref image);
OCV.cvReleaseVideoWriter(ref cvVideoWriter );
return 0;
} |