VPTTest.cppbk20200221 4.75 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>
#endif

#ifndef _MSC_VER
#include<time.h>
#include <sys/time.h>
#include <unistd.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 = 9;

//行人 自行车 摩托车 三轮车 小型车 大车 卡车 拖拉机 中巴
string type[detectType] = { "person", "bike", "motor", "tricycle", "car", "bigbus", "lorry", "tractor", "midibus" };
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 } };


//返回快照结果回调函数
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;
}

typedef struct init_param
{
	char *rtsp_url;
	char *snapshot_save_folder;
}init_param;

void *algorthim_thread(void *arg)
{
  init_param *tmp = (init_param*)arg;
	//初始化
	//快照保存参数设置
	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);

	const int maxResultCount = 100;

	void * tools = NULL;
	VPT_PARAM vparam;
	vparam.gpuid = 0;
	vparam.maxResultCount = maxResultCount;        //限制算法内部输出的最大前景个数
	vparam.SnaoshotParameter = &ssparam;
	int res = VPT_Init(tools, vparam, tmp->snapshot_save_folder, SnapshotInfoCallback);

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

	//结果变量
	VPT_Result result;
	result.obj = new VPT_ObjInfo[maxResultCount];   //设置外部接收结果的最大前景个数
  
	cv::Mat img;
	VideoCapture cap;
	cap.open(tmp->rtsp_url);
	int frameCount = 0;
	while (cap.read(img))
	{
		cout << "---------------- " << frameCount << " -----------------" << endl;
		VPT_Process(tools, img.data, img.cols, img.rows, img.channels(), frameCount, &result, true);
		frameCount++;
	}

	//释放
	delete[] result.obj;
	cap.release();

	VPT_Release(tools);
}


int main(int argc, char** argv)
{
	init_param param1;
	param1.rtsp_url = argv[1];
	param1.snapshot_save_folder = argv[2];
  printf("thread1: %s %s\n", 	param1.rtsp_url , 	param1.snapshot_save_folder);
  
	pthread_t tid1;
	pthread_create(&tid1, NULL, algorthim_thread, (void*)&param1);
	printf("child pthread1 create\n");

	init_param param2;
	param2.rtsp_url = argv[3];
	param2.snapshot_save_folder = argv[4];
printf("thread2: %s %s\n", 	param2.rtsp_url , 	param2.snapshot_save_folder);
	pthread_t tid2;
	pthread_create(&tid2, NULL, algorthim_thread, (void*)&param2);
	printf("child pthread2 create\n");

	init_param param3;
	param3.rtsp_url = argv[5];
	param3.snapshot_save_folder = argv[6];
printf("thread3: %s %s\n", 	param3.rtsp_url , 	param3.snapshot_save_folder);

	pthread_t tid3;
	pthread_create(&tid3, NULL, algorthim_thread, (void*)&param3);
	printf("child pthread3 create\n");

	pthread_join(tid1, NULL);
	printf("child pthread1 exit\n");

	pthread_join(tid2, NULL);
	printf("child pthread2 exit\n");

	pthread_join(tid3, NULL);
	printf("child pthread3 exit\n");
	

	printf("\n all finish \n");
	return 0;
}