Blame view

vpt/src/VPT_timeout/sort/SnapShot.cpp 13.3 KB
144cd8c4   Hu Chunming   v6.0.0 替换模型
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  #include "SnapShot.h"

  #include <time.h>

  #ifdef _MSC_VER

          #include <io.h>

          #include <direct.h>

  	#include <windows.h>

          #define _ACCESS _access

          #define _MKDIR(a) _mkdir((a))

  #else

          #include <unistd.h>

          #include <stdarg.h>

          #include <sys/stat.h>

          #define _ACCESS access

  	#define _MKDIR(a) mkdir((a),0755)

  	#define DWORD unsigned int

  	#define LPVOID void*

  #endif

  

  #define ACCESS _ACCESS

  #define MKDIR(a) _MKDIR((a))

  

  //static int index = 0;

  

  int CreateDir(char *pszDir)

  {

  	int i = 0;

  	int iRet;

  	int iLen = strlen(pszDir);

  	if (pszDir[iLen - 1] != '\\' && pszDir[iLen - 1] != '/')

  	{

  		pszDir[iLen] = '/';

  		pszDir[iLen + 1] = '\0';

  	}

  	iLen = strlen(pszDir);

  

  	if (iLen>2 && ((pszDir[i] == '\\' && pszDir[i + 1] == '\\') || (pszDir[i] == '/' && pszDir[i + 1] == '/')))

  	{

  		i = 2;

  		for (; i <= iLen; i++)

  			if (pszDir[i] == '\\' || pszDir[i] == '/')

  				break;

  

  		i++;

  

  		for (; i <= iLen; i++)

  		{

  			if (pszDir[i] == '\\' || pszDir[i] == '/')

  			{

  				pszDir[i] = '\0';

  				iRet = ACCESS(pszDir, 0);

  				if (iRet != 0)

  				{

  					iRet = MKDIR(pszDir);

  					if (iRet != 0)

  					{

  						cout << "Create Folder Failed!" << endl;

  						return -1;

  

  					}

  				}

  				pszDir[i] = '/';

  

  			}

  		}

  		return 0;

  	}

  

  

  	if (pszDir[i] != '\\' && pszDir[i] != '/')

  		i = 0;

  	else

  		i = 1;

  	for (; i <= iLen; i++)

  	{

  		if (pszDir[i] == '\\' || pszDir[i] == '/')

  		{

  			pszDir[i] = '\0';

  			iRet = ACCESS(pszDir, 0);

  			if (iRet != 0)

  			{

  				iRet = MKDIR(pszDir);

  				if (iRet != 0)

  				{

  

  					return -1;

  

  				}

  			}

  			pszDir[i] = '/';

  

  		}

  	}

  	return 0;

  }

  

  void CreateResultFolder(char* resultFolder)

  {

  	if (strlen(resultFolder) > 240)  //太长

  	{

  		cout << "Folder Name is too Long!" << endl;

  		return;

  	}

  	//else if (strlen(resultFolder) < 1)  //太短,最少一个字符

  	//{

  	//	cout << "Folder Name is too Short!" << endl;

  	//	return;

  	//}

  

  	char childFolder[2][260] = { "SnapshotLittle", "Snapshot" };

  	char dir[260];

  

  	sprintf(dir, "%s%s/", resultFolder, childFolder[0]);

  

  	if (CreateDir(dir) != 0)

  	{

  		cout << "Create Folder Failed!" << endl; return;

  	}

  

  	sprintf(dir, "%s%s/", resultFolder, childFolder[1]);

  	

  	if (CreateDir(dir) != 0)

  	{

  		cout << "Create Folder Failed!" << endl; return;

  	}

  }

  

  DWORD ThreadProcess(LPVOID param);

  

  SnapShot::SnapShot(char* ResDir, VIDEO_OBJECT_SNAPSHOT_CALLBACK VideoObjSnapshotCallBack, SNAPSHOT_PARAMETER *ssParam)

  {

  	if (ResDir == NULL)

  	{

  		saveSnapshot = false;

  		return;

  	}

  		

  	saveSnapshot = true;

  	strcpy(mResDir, ResDir);

  	int iLen = strlen(mResDir);

  	

  	if (iLen !=0 && mResDir[iLen - 1] != '\\' && mResDir[iLen - 1] != '/')

  	{

  		mResDir[iLen] = '/';

  		mResDir[iLen + 1] = '\0';

  	}

  

  	CreateResultFolder(mResDir);

  

  	mVideoObjSnapshotCallBack = NULL;

  	if (VideoObjSnapshotCallBack != NULL)

  		mVideoObjSnapshotCallBack = VideoObjSnapshotCallBack;

  

  	checkBoxSize = ssParam->selectFromMinbox;

  	checkDistance = ssParam->selectFromMindistance;

  	

  	memcpy(minBoxSize, ssParam->minBoxsize, sizeof(MRECT)* DETECTTYPE);	  //复制参数

  	memcpy(minDistance, ssParam->minDistance, sizeof(int)* EDGES);	      //复制参数

  

  	ProcessThread = boost::thread(ThreadProcess, this);

  	beginSaveSnapshot = false;

  

  	saveTimes = 0;

  }

  

  SnapShot::~SnapShot()

  {

  	ProcessThread.interrupt();   //中断处理线程

  	ProcessThread.join();        //等待处理线程结束了 函数继续

  

  	mVideoObjSnapshotCallBack = NULL;

  }

  

  void SnapShot::SnapshotProcess(VPT_Result *result, cv::Mat img, vector<int> deleteTrackers, int frameCount)

  {

  

  	if (saveSnapshot)

  	{

   

  		for (int i = 0; i < result->objCount; i++)

  		{

  			if (snapshots.find(result->obj[i].id) == snapshots.end())

        	AddSnapshot(result->obj[i], img, frameCount);

  			else 

  				UpdateSnapshot(result->obj[i], img, frameCount);

  		}

  

  		SaveSnapshot(deleteTrackers);

  	}

   

  }

  

  void SnapShot::AddSnapshot(VPT_ObjInfo object, cv::Mat img, int frameCount)

  {

  	OBJECT_SNAPSHOT_INFO newObjInfo;

  	newObjInfo.objInfo.left = object.left;

  	newObjInfo.objInfo.right = object.right;

  	newObjInfo.objInfo.top = object.top;

  	newObjInfo.objInfo.bottom = object.bottom;

  	newObjInfo.objInfo.objectID = object.id;

  	newObjInfo.objInfo.objectIndex = object.index;

  	newObjInfo.objInfo.taskFrameCount = frameCount;

  

  	newObjInfo.area = (newObjInfo.objInfo.right - newObjInfo.objInfo.left) * (newObjInfo.objInfo.top - newObjInfo.objInfo.bottom);

  	

  	//double begin = 0, end = 0;

  	//begin = clock();

  

  	//cv::Rect rect(newObjInfo.objInfo.left, newObjInfo.objInfo.top, newObjInfo.objInfo.right - newObjInfo.objInfo.left, newObjInfo.objInfo.bottom - newObjInfo.objInfo.top);

  	//img(rect).copyTo(newObjInfo.objImg);  /*newObjInfo.objImg = img(rect);*/ 

  	img.copyTo(newObjInfo.objImg);    //保存原图

  

  	/*end = clock();

  	printf("Copy Once Time: %.2f   \n", end - begin);*/

  

  	newObjInfo.flags[0] = newObjInfo.objInfo.left < minDistance[0] + SCALE_OUT ? 0 : 1;           //left

  	newObjInfo.flags[1] = newObjInfo.objInfo.top < minDistance[1] + SCALE_OUT ? 0 : 1;            //top

  	newObjInfo.flags[2] = newObjInfo.objInfo.right > img.cols - minDistance[2] - SCALE_OUT ? 0 : 1;   //right

  	newObjInfo.flags[3] = newObjInfo.objInfo.bottom > img.rows - minDistance[3] - SCALE_OUT ? 0 : 1;     //bottom

  	

  

  	snapshots[newObjInfo.objInfo.objectID] = newObjInfo;

    snapshots[newObjInfo.objInfo.objectID].voteIndex.insert(snapshots[newObjInfo.objInfo.objectID].voteIndex.begin(), DETECTTYPE, 0);

    snapshots[newObjInfo.objInfo.objectID].voteIndex[newObjInfo.objInfo.objectIndex]++;

  }

  

  

  //******** 判断面积是否合法函数 **********//

  // 目的:选取框最大最完整的那帧

  // 1. 比之前面积小的不要

  // 2. 突变的面积不要

  bool SnapShot::LegalArea(int maxArea, int lastArea, int left, int top, int right, int bottom)

  {

  	//不需要通过MINBOX来判断,直接返回true

  	if (checkBoxSize == false)

  		return true;

  

  	int newArea = (bottom - top)*(right - left);

  	if (newArea < maxArea)

  		return false;

  	else if ((newArea - lastArea) / lastArea > 0.5)  //阈值测试之后再确定,看是否需要分种类来确定不同的阈值

  		return false;

  	else return true;

  }

  

  

  //******** 判断位置是否合法函数 **********//

  // 目的:剔除掉靠近摄像头时,面积最大,但是检测物体已经不完整的情况

  // 1. 设定阈值,靠近边缘不要了(阈值目前设定为自身宽高的20%

  bool SnapShot::LegalPos(bitset<EDGESIZE> flags, int left, int top, int right, int bottom, int imgHeight, int imgWidth)

  {

  	//不需要通过MINDISTANCE来判断,直接返回true

  	if (checkDistance == false)

  		return true;

  

  	int pos[4] = { left, top, right, bottom };

  	int edges[4] = { 0, 0, imgWidth, imgHeight };

  

  	for (int i = 0; i < EDGESIZE; i++)

  	{

  		if (flags[i])  //是需要判断的边

  		{

  			if (abs(pos[i] - edges[i]) < minDistance[i])

  				return false;

  		}

  	}

  	return true;

  }

  

  

  void SnapShot::UpdateSnapshot(VPT_ObjInfo object, cv::Mat img, int frameCount)

  {

  	//**************** 策略一 *************************//

  	//选取面积最大的,剔除掉突变的

  	//选取那些远离边框位置的

  	int maxArea = (snapshots[object.id].objInfo.right - snapshots[object.id].objInfo.left)*(snapshots[object.id].objInfo.bottom - snapshots[object.id].objInfo.top);

  	if (LegalArea(maxArea, snapshots[object.id].area, object.left, object.top, object.right, object.bottom) 

  		&& LegalPos(snapshots[object.id].flags, object.left, object.top, object.right, object.bottom, img.rows, img.cols))

  	{

  		snapshots[object.id].objInfo.left = object.left;

  		snapshots[object.id].objInfo.right = object.right;

  		snapshots[object.id].objInfo.top = object.top;

  		snapshots[object.id].objInfo.bottom = object.bottom;

  		snapshots[object.id].objInfo.objectID = object.id;

  		snapshots[object.id].objInfo.taskFrameCount = frameCount;

  		snapshots[object.id].voteIndex[object.index]++;

  		snapshots[object.id].objImg.release();

  

  		//cv::Rect rect(snapshots[object.id].objInfo.left, snapshots[object.id].objInfo.top,

  		//	snapshots[object.id].objInfo.right - snapshots[object.id].objInfo.left, snapshots[object.id].objInfo.bottom - snapshots[object.id].objInfo.top);

  		//img(rect).copyTo(snapshots[object.id].objImg);  /*newObjInfo.objImg = img(rect);*/ 

  

  		img.copyTo(snapshots[object.id].objImg);    ////保存原图,快照效果不好时,可以适当的外扩

  	}

  	snapshots[object.id].area = (snapshots[object.id].objInfo.right - snapshots[object.id].objInfo.left) * (snapshots[object.id].objInfo.top - snapshots[object.id].objInfo.bottom);

  }

  

  static string type1[DETECTTYPE] = { "person", "bike", "motor", "tricycle", "car", "bigbus", "lorry", "tractor", "midibus" };

  

  DWORD ThreadProcess(LPVOID param)

  {

  	SnapShot *pThreadParam = (SnapShot *)param;

  

  	try

  	{

  		while (true)

  		{

  			if (pThreadParam->beginSaveSnapshot)

  			{

  				{

  					boost::mutex::scoped_lock lock(pThreadParam->threadMutex);

  					pThreadParam->deleteID = pThreadParam->tempDeleteID;

  					pThreadParam->tempDeleteID.clear();

  					pThreadParam->beginSaveSnapshot = false;

  				}

  

  

  				for (int i = 0; i < pThreadParam->deleteID.size(); i++)

  				{

  					char imgName[260];

  					sprintf(imgName, "%sSnapshot/%d.jpg", pThreadParam->mResDir, pThreadParam->deleteID[i]);

  					imwrite(imgName, pThreadParam->snapshots[pThreadParam->deleteID[i]].objImg);

  					

  					cv::Mat littleImg;

  					cv::Rect rect(pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.left, pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.top,

  						pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.right - pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.left,

  						pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.bottom - pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.top);

  					pThreadParam->snapshots[pThreadParam->deleteID[i]].objImg(rect).copyTo(littleImg);

  					

  					char objName[260];

  					sprintf(objName, "%sSnapshotLittle/%d.jpg", pThreadParam->mResDir, pThreadParam->deleteID[i]/*, type1[pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.objectIndex].c_str()*/);

  					imwrite(objName, littleImg);

                             

  					/*strcpy(snapshots[deleteTrackers[i]].objInfo.firstPicPath, imgName);

  					strcpy(snapshots[deleteTrackers[i]].objInfo.secondPicPath, objName);

  					

  					if (mVideoObjSnapshotCallBack != NULL)

  					mVideoObjSnapshotCallBack(&snapshots[deleteTrackers[i]].objInfo);

  

  

  					char objName[260];

  					sprintf(objName, "%sSnapshotLittle//%d.jpg", pThreadParam->mResDir, pThreadParam->deleteID[i]);

  					imwrite(objName, pThreadParam->snapshots[pThreadParam->deleteID[i]].objImg);*/

  					int maxCount = 0;

  					int index = 0;

  					for (int ii = 0; ii < DETECTTYPE; ii++)

  					{

  						if (pThreadParam->snapshots[pThreadParam->deleteID[i]].voteIndex[ii] > maxCount)

  						{

  							maxCount = pThreadParam->snapshots[pThreadParam->deleteID[i]].voteIndex[ii];

  							index = ii;

  						}

  					}

  

  					pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.objectIndex = index;

  					strcpy(pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.firstPicPath, objName);

  					strcpy(pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo.secondPicPath, objName);

  

  					if (pThreadParam->mVideoObjSnapshotCallBack != NULL)

  						pThreadParam->mVideoObjSnapshotCallBack(&pThreadParam->snapshots[pThreadParam->deleteID[i]].objInfo);

  

  

  					pThreadParam->snapshots[pThreadParam->deleteID[i]].objImg.release();

  					pThreadParam->snapshots.erase(pThreadParam->snapshots.find(pThreadParam->deleteID[i]));

  				}

  				pThreadParam->deleteID.clear();

  				

  			}

  			

  			boost::this_thread::sleep(boost::posix_time::milliseconds(10));

  		}

  	}

  	catch (...)

  	{

  		std::cout << "Save Process Thread is Finished" << std::endl;

  	}

  

  	return 0;

  }

  

  

  void SnapShot::SaveSnapshot(vector<int> deleteTrackers)

  {

  	for (int i = 0; i < deleteTrackers.size(); i++)

  	{

  		int width = snapshots[deleteTrackers[i]].objInfo.right - snapshots[deleteTrackers[i]].objInfo.left;

  		int height = snapshots[deleteTrackers[i]].objInfo.bottom - snapshots[deleteTrackers[i]].objInfo.top;

  		

  		if (width > minBoxSize[snapshots[deleteTrackers[i]].objInfo.objectIndex].width && height > minBoxSize[snapshots[deleteTrackers[i]].objInfo.objectIndex].height)

  		{

  			saveTimes++;

  		//	cout << saveTimes << endl;

  			boost::mutex::scoped_lock lock(threadMutex);

  			tempDeleteID.push_back(deleteTrackers[i]);  //将需要保存快照的ID传入

  		}

  		else

  		{

  			snapshots[deleteTrackers[i]].objImg.release();

  			snapshots.erase(snapshots.find(deleteTrackers[i]));

  		}

  	}

  

  	if (!tempDeleteID.empty())

  	{

  		boost::mutex::scoped_lock lock(threadMutex);

  		beginSaveSnapshot = true;

  	}

  }

  

  //char imgName[260];

  //sprintf(imgName, "%sSnapshot//%d.jpg", mResDir, deleteTrackers[i]);

  //imwrite(imgName, snapshots[deleteTrackers[i]].objImg);

  //

  //Mat objImg;

  ///*Rect rect(snapshots[deleteTrackers[i]].objInfo.left, snapshots[deleteTrackers[i]].objInfo.top,

  //snapshots[deleteTrackers[i]].objInfo.right - snapshots[deleteTrackers[i]].objInfo.left, snapshots[deleteTrackers[i]].objInfo.bottom - snapshots[deleteTrackers[i]].objInfo.top);*/

  ////snapshots[deleteTrackers[i]].objImg/*(rect)*/.copyTo(objImg); 

  //

  //char objName[260];

  //sprintf(objName, "%sSnapshotLittle//%d.jpg", mResDir, deleteTrackers[i]);

  ///*imwrite(objName, objImg);*/

  //

  //strcpy(snapshots[deleteTrackers[i]].objInfo.firstPicPath, imgName);

  //strcpy(snapshots[deleteTrackers[i]].objInfo.secondPicPath, objName);

  //

  //if (mVideoObjSnapshotCallBack != NULL)

  //mVideoObjSnapshotCallBack(&snapshots[deleteTrackers[i]].objInfo);