ms_region_surveilance.cpp 5.94 KB

#include "RegionSurveillanceProcess.h"
#include "MSRegionSurveilanceCpu.h"
#include "MSRegionSurveilanceVpt.h"
#include "MSRegionSurveilanceHst.h"
#include "ComposeTool.h"
#include "authority.h"
#include <boost/thread/thread_pool.hpp>
#ifdef _MSC_VER
#define productSN "5A72239196F54B678D311E7FC11849DC" //windows 视频结构化-图片接口
#else
#define productSN "7CF8B4797F9E441686145BED07F662DE" //linux 视频结构化--图片接口
#endif
MS_RegionSurveillance MS1;
struct RS_HANDLE
{
	IRegionSurveillanceVpt * vpthandle;
	IRegionSurveillanceCpu * cpuhandle;
	int licence_status = -1;
	int thrd_status = -1;
	boost::thread thrd;
};
void check_thread(RS_HANDLE* handle)
{
	int res = -1;
#ifndef _MSC_VER
	char wtime[15];
	memset(wtime, 0, 15);
	char * time = wtime;
#endif
	while (1)
	{
		//printf("xxx check status on process...\n");
#ifdef _MSC_VER
		res = sy_licence(productSN);
#else
		res = sy_licence(productSN, &time);
		//printf("--------------wtime in thread: %s, status: %d\n", wtime, licence_status);
#endif
		if (res < 0)
		{
			handle->licence_status = handle->licence_status - 1;
		}
		else
		{
			if (handle->licence_status < 0)
			{
				handle->licence_status = 0;
			}
		}
		boost::this_thread::sleep(boost::posix_time::seconds(300)); //5min
}
		}
int rs_init(void **handle, rs_param param)
{
	int ret = SUCCESS;
#ifdef AUTHORIZATION
#ifdef _WIN32
	if (SUCCESS == (ret = sy_licence(productSN)))
#elif __linux__
	char* wtime = new char[15];
	memset(wtime, 0, 15);
	if (SUCCESS == (ret = sy_licence(productSN, &wtime)))
#endif
#else
	ret = sy_time_check(2021, 3);
	if (ret == SUCCESS)
#endif
	{
		RS_HANDLE * rshandle = new RS_HANDLE;
		RegionSurveillanceProcess* regin = nullptr;
		rshandle->cpuhandle = new IRegionSurveillanceCpu;
		rshandle->vpthandle = new IRegionSurveillanceVpt;
		//else if (param.mode == 2)
		//	regin = new IRegionSurveillanceHst;


		*handle = rshandle;

		printf("no init vpt\n");
		auto rt = 1;// rshandle->vpthandle->rs_init(param);
		
		cv::Mat srcImage(param.image.h_, param.image.w_, CV_8UC3, param.image.data_);
		IplImage  img = IplImage(srcImage);
		IplImage * gray = cvCreateImage(cvSize(srcImage.cols, srcImage.rows), 8, 1);
		cvCvtColor(&img, gray, CV_RGB2GRAY);
		sy_img image;
		//image.set_data(srcImage.cols, srcImage.rows, srcImage.channels(), (unsigned char *)srcImage.data);
		image.set_data(gray->width, gray->height, gray->nChannels, (unsigned char *)gray->imageData);
		param.image = image;

		rt = rshandle->cpuhandle->rs_init(param);
		cvReleaseImage(&gray);
		if (rt == 1)
		{
			rshandle->licence_status = 0;
#ifdef AUTHORIZATION
			rshandle->thrd = boost::thread(check_thread, tools);
#endif
			rshandle->thrd_status = 0;
			ret = SUCCESS;
		}
		else
		{
			ret = FAILED;
		}
	}
	else
	{
		ret  = AUTHOR_TIMEEXPIRED_ERROR;
	}
#ifdef AUTHORIZATION
#ifdef __linux__
	if (wtime)
	{
		delete[] wtime;
		wtime = NULL;
	}
#endif
#endif
	return ret;
}

int rs_init_region(void *handle, int num_roi, region_info* region_infos, bool iflog)
{
	RS_HANDLE* regin = (RS_HANDLE*)handle;
	if (!regin)
	{
		printf("handle is NULL \n");
		return -1;
	}
	std::vector<region_info> vpt;
	std::vector<region_info> cpu;
	for (int i = 0; i < num_roi; ++i)
	{
		if (region_infos[i].alarm_human)
			vpt.push_back(region_infos[i]);
		if (region_infos[i].alarm_stay)
			cpu.push_back(region_infos[i]);
	}
	auto rt = 0;
	if (!cpu.empty())
		rt = regin->cpuhandle->rs_init_region(cpu.size(), cpu.data(), iflog);
	if(!vpt.empty())
		rt = regin->vpthandle->rs_init_region(vpt.size(), vpt.data(), iflog);
	if (rt == 1)
	{
		return SUCCESS;
	}
	else
	{
		return FAILED;
	}

}

// 初始化背景 返回0 正确,非0错误
int rs_init_background(void *handle, sy_img img_data)
{
	RS_HANDLE* regin = (RS_HANDLE*)handle;
	if (!regin)
	{
		printf("handle is NULL \n");
		return -1;
	}

	regin->cpuhandle->rs_init_backgroud(img_data);
}

int rs_detect(void *handle, sy_img img_data, int num_roi, region_info* region_infos, rs_result *result)
{
	RS_HANDLE* regin = (RS_HANDLE*)handle;
	if (!regin)
	{
		printf("handle is NULL \n");
		return -1;
	}
	ComposeTool tool;
	std::vector<region_info> vpt;
	std::vector<region_info> cpu;
	for (int i = 0; i < num_roi; ++i)
	{
		if (region_infos[i].alarm_human)
			vpt.push_back(region_infos[i]);
		if (region_infos[i].alarm_stay)
			cpu.push_back(region_infos[i]);
	}
	auto rt = 0;
	rs_result result_cpu;
	rs_result result_vpt;
	if (!cpu.empty())
	{
		cv::Mat srcImage(img_data.h_, img_data.w_, CV_8UC3, img_data.data_);
		IplImage  img = IplImage(srcImage);
		IplImage * gray = cvCreateImage(cvSize(srcImage.cols, srcImage.rows), 8, 1);
		cvCvtColor(&img, gray, CV_RGB2GRAY);
		sy_img image;
		
	//	image.set_data(srcImage.cols, srcImage.rows, srcImage.channels(), (unsigned char *)srcImage.data);
		image.set_data(gray->width, gray->height, gray->nChannels, (unsigned char *)gray->imageData);
		rt = regin->cpuhandle->rs_detect(image, cpu.data(), cpu.size(), &result_cpu);
		cvReleaseImage(&gray);
	}
	if (!vpt.empty())
	{
		//rt = regin->vpthandle->rs_detect(img_data, vpt.data(), &result_vpt);
	}
	if ((!cpu.empty()) && !vpt.empty())
	{
		tool.compose_res(&result_cpu, &result_vpt,result);
	}
	else if (!cpu.empty())
	{
		*result = result_cpu;
	}
	else if (!vpt.empty())
	{
		*result = result_vpt;
	}
	if (rt == 1)
	{
		return SUCCESS;
	}
	else
	{
		return FAILED;
	}
}  

int rs_get_arrowdir(void *handle, sy_point p_roi0, sy_point p_roi1, sy_point dir)
{
	RS_HANDLE* regin = (RS_HANDLE*)handle;
	if (!regin)
	{
		printf("handle is NULL \n");
		return FAILED;
	}
	if (1 == regin->vpthandle->rs_get_arrowdir(p_roi0, p_roi1, dir))
	{
		return SUCCESS;
	}
	else
	{
		return FAILED;
	}
}

void rs_release(void **handle)
{
	RS_HANDLE* regin = (RS_HANDLE*)handle;
	if (regin->thrd_status == 0)
	{
		regin->thrd.interrupt();
		regin->thrd.join();
		regin->thrd_status = -1;
	}
	if (!regin)
	{
		printf("handle is NULL \n");
		return;
	}
	if (regin->cpuhandle)
	{
		delete regin->cpuhandle;
		regin->cpuhandle = nullptr;
	}
		
	if (regin->vpthandle)
	{
		delete regin->vpthandle;
		regin->vpthandle = nullptr;
	}
		
	delete regin;
}