VPTTest13.cppbk 6.37 KB
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>

#ifdef _MSC_VER
#include "../VPT/VPT.h"
#include <windows.h>
#include <cv.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>
#else
#include "VPT.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<unistd.h>
#endif
//#include "../VPT/header.h"

#ifndef _MSC_VER
#include<time.h>
#include <sys/time.h>
#define Sleep(a) usleep((a)*1000)
#define MACRO_COUNT_TIME_START(name)  struct timeval macro_tv_start_##name;\
		struct timeval macro_tv_end_##name;\
	gettimeofday(&macro_tv_start_##name,NULL);
#define MACRO_COUNT_TIME_END(name,___total_count___) gettimeofday(&macro_tv_end_##name,NULL);\
		printf("%s time cost: %.2f ms \n", #name, ( (double)(macro_tv_end_##name.tv_sec-macro_tv_start_##name.tv_sec)*1000000+(double)(macro_tv_end_##name.tv_usec-macro_tv_start_##name.tv_usec)   )/1000/___total_count___);
#endif

using namespace cv;
using namespace std;

//????????
const int detectType = 12;

//???? ???谐? 摩?谐? ???殖? 小?统? ???? ???? ??拉?? ?邪?
string type[detectType] = { "person", "bike", "motor", "tricycle", "car", "bigbus", "lorry", "tractor", "midibus", "garbage", "illegalOperation", "disFurniture" };
static int color[detectType][3] = { { 41, 244, 222 }, { 255, 128, 0 }, { 100, 13, 244 }, { 255, 215, 0 }, { 18, 220, 87 }, { 0, 128, 0 }, \
{0, 128, 255}, { 255, 0, 0 }, { 91, 46, 0 },{78, 18, 185}, { 255, 15, 0 }, { 82, 168, 52 } };


//???乜??战????氐?????
void SnapshotInfoCallback(VIDEO_OBJECT_SNAPSHOT *videoObjectSnapshot)
{
	cout << videoObjectSnapshot->objectID << " " << videoObjectSnapshot->left << " " << videoObjectSnapshot->right << " " << videoObjectSnapshot->top << " " << videoObjectSnapshot->bottom
		<< " " << videoObjectSnapshot->firstPicPath << " " << videoObjectSnapshot->secondPicPath << " " << videoObjectSnapshot->taskFrameCount << " " << videoObjectSnapshot->objectIndex << endl;
}


//??示??量
Mat imgResized;
bool img_show = false;
bool show = true;

#ifdef _MSC_VER
//??示?叱?
DWORD WINAPI DiaplayThread(LPVOID param)
{
	LARGE_INTEGER nFreq, nSaveBeginTime, nSaveEndTime;
	QueryPerformanceFrequency(&nFreq);

	while (show)
	{
		if (img_show)
		{
			cv::imshow("RESULT", imgResized);
			cv::waitKey(1);
			img_show = false;
		}
		Sleep(1);
	}
	return 0;
}
#endif


int main()
{
cout << "begin " << endl;


	//??????始??
	CvFont font;
	cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0, 0, 1, 3);
	int fontFace = CV_FONT_HERSHEY_COMPLEX;
	double fontScale = 1;
	int thickness = 2;

	//??????量
	Mat img;
	VideoCapture cap;
 
#ifdef _MSC_VER
	cap.open("G://TestData//?顺???//??????频1.avi");
#else
  cap.open("qst.avi");
#endif

	cap.read(img); 
	VideoWriter writer("720.avi", CV_FOURCC('D', 'I', 'V', 'X'), 25.0, Size(img.cols, img.rows));

	int frameCount = 0;
	const int maxResultCount = 100;

#ifdef _MSC_VER
	LARGE_INTEGER nFreq, nBeginTime, nEndTime;
	QueryPerformanceFrequency(&nFreq);
#endif
	//??始??
	//???毡???????????
	SNAPSHOT_PARAMETER ssparam;
	ssparam.selectFromMinbox = true;
	ssparam.selectFromMindistance = true;

	//1.??????小?????颍?????目??小????小????????小时?????锌??盏谋???
	MRECT m_boxsize[DETECTTYPE] = { MRECT(20, 20), MRECT(50, 80), MRECT(50, 80), MRECT(60, 80), MRECT(80, 80), MRECT(90, 90), MRECT(100, 100), MRECT(100, 100), MRECT(100, 100) };  //???? ???谐? 摩?谐? ???殖? 小?统? ???? ???? ??拉?? ?邪?
	//2.????目?????牖???呖?????小???耄??目??位???牖???呖?小????小????时???俑??驴???
	int m_distance[EDGES] = { 20, 30, 20, 30 };   //left, top, right, bottom 

	memcpy(ssparam.minBoxsize, m_boxsize, sizeof(MRECT)* DETECTTYPE);
	memcpy(ssparam.minDistance, m_distance, sizeof(int)* EDGES);

	void * tools = NULL;
	VPT_PARAM vparam;
	vparam.gpuid = 0;
	vparam.maxResultCount = maxResultCount;        //?????惴?诓???????????前??????
	vparam.SnaoshotParameter = &ssparam;


#ifdef _MSC_VER
	int res = VPT_Init(tools, vparam, "G:/TestRes", SnapshotInfoCallback);
#else
	int res = VPT_Init(tools, vparam, "./", SnapshotInfoCallback);
#endif


	if (res != 0)
	{
		cout << "VPT Init Failed!" << endl;
		system("pause");
		return 0;
	}

	//??????量
	VPT_Result result;
	result.obj = new VPT_ObjInfo[maxResultCount];   //?????獠???战?????????前??????

#ifdef _MSC_VER
	//??????示?叱?
	DWORD dwThreadID = 0;
	HANDLE handle_display = CreateThread(NULL, 0, DiaplayThread, 0, 0, &dwThreadID);
#endif

	int start = 0;
	cap.set(CV_CAP_PROP_POS_FRAMES, start);

	double totalTime = 0.0;
	//????

ofstream file("res.txt");

while(cap.read(img))
{
    start++;
		cout << "---------------- " << frameCount << " -----------------" << endl;

#ifdef _MSC_VER
		QueryPerformanceCounter(&nBeginTime);
#else
		MACRO_COUNT_TIME_START(vpt_process);
#endif
		VPT_Process(tools, img.data, img.cols, img.rows, img.channels(), frameCount, &result, true);
   result.objCount = 0;
//usleep(160*1000);
#ifdef _MSC_VER
		QueryPerformanceCounter(&nEndTime);
		totalTime += (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart;
		printf("Total Time: %.2fms  \n", (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart);
#else
		MACRO_COUNT_TIME_END(vpt_process, 1);
#endif
		for (int c = 0; c < result.objCount; c++)
		{
			char str_i[100];

			sprintf(str_i, "%s",type[result.obj[c].index].c_str());
			int colorIndex = result.obj[c].index;


			file << start << " " << result.obj[c].id << " " << result.obj[c].index << " " << result.obj[c].left << " " <<
				result.obj[c].top << " " << result.obj[c].right << " " << result.obj[c].bottom << endl;

			rectangle(img, cvPoint(result.obj[c].left - 5, result.obj[c].top - 15), cvPoint(result.obj[c].right + 5, result.obj[c].bottom + 10), cv::Scalar(color[colorIndex][0], color[colorIndex][1], color[colorIndex][2]), 3, 1);
			cv::putText(img, str_i, cv::Point(result.obj[c].left, result.obj[c].top), fontFace, fontScale, cv::Scalar(color[colorIndex][0], color[colorIndex][1], color[colorIndex][2]), thickness, 8);
		}

		cv::resize(img, imgResized, cv::Size(1280, 720));
    writer << img;
		img_show = true;
		frameCount++;
	}
	cout << "AVG TIME: " << totalTime / 5000 << endl;

	//?头?
	show = false;
	delete[] result.obj;
	cap.release();
	VPT_Release(tools);

#ifdef _MSC_VER
	system("pause");
#endif
	return 0;
}