首页 > 代码库 > 模式识别 - 有害视频检测程序的策略

模式识别 - 有害视频检测程序的策略

有害视频检测程序的策略


本文地址: http://blog.csdn.net/caroline_wendy/article/details/26346831


有害(色情\恐怖\暴力)视频, 严重危害网络的健康, 需要进行检测和过滤.


检测色情\恐怖视频, 通过检测程序, 检测出多个场景的概率, 然后进行排序, 当场景多余6个时, 只取最大的6个场景;

返回的概率值是前3个最大检测值场景的概率的均值;

色情\恐怖汇总时, 首先检测色情, 如果为色情视频, 则不进行恐怖的检测, 否则继续检测恐怖, 如果都不时, 则为未知视频.


代码:

#include "stdafx.h"

#include "VideoDetector.h"

using namespace std;

double detectPornVideo(	
	std::vector<PornSceneInfo>& infosSet, 
	const std::string _fileName, 
	const std::string _imagePath, 
	const std::string _imageName,
	const std::string _modelPath) 
{
	struct bigger {
		bool operator() (PornSceneInfo i, PornSceneInfo j) { return (i.prop>j.prop); }
	} mybigger;

	double finalResult(0.0);
	const int FirstNum(3); //计算最大3个的概率
	const int MaxImageNum(6); //最多返回6个结构体
	std::vector<PornSceneInfo> vResults;

	const std::string fileName(_fileName);
	const std::string imagePath(_imagePath); //提前新建文件夹
	const std::string imageName(_imageName);
	const std::string modelPath(_modelPath);
	const std::size_t shotInterval(100);
	const std::size_t  sceneShotNum(20);

	std::size_t  num = PornVideoSceneNum(fileName.c_str(), shotInterval, sceneShotNum);

	PornSceneInfo* resultSet = new PornSceneInfo[num];

	if (PornVideoDetector_Info(resultSet, imagePath.c_str(), fileName.c_str(), imageName.c_str(), 
		modelPath.c_str(), shotInterval, sceneShotNum)) 
	{
		for (std::size_t  i=0; i<num; ++i) {
			vResults.push_back(resultSet[i]);
		}
	} else {
		std::cout << " Failed to detect the video! " << std::endl;
	}

	delete[] resultSet;
	resultSet = nullptr;

	if (num > MaxImageNum) {
		std::sort(vResults.begin(), vResults.end(), mybigger);
		for(int i=0; i<MaxImageNum; ++i) {
			infosSet.push_back(vResults[i]); //从大到小排列, 最多包涵MaxImageNum个
		}
	} else {
		std::sort(vResults.begin(), vResults.end(), mybigger);
		infosSet = vResults;
	}

	if (num > FirstNum) {
		std::sort(vResults.begin(), vResults.end(), mybigger);

		for(int i=0; i<FirstNum; ++i) {
			finalResult += vResults[i].prop; //大于3个只取前3个
		}

		finalResult /= FirstNum;
	} else {
		for(std::size_t i=0; i<num; ++i) {
			finalResult += vResults[i].prop;
		}
		finalResult /= num;
	}

	return finalResult;
}

double detectHorrorVideo(
	std::vector<HorrorSceneInfo>& infosSet, 
	const std::string _fileName, 
	const std::string _imagePath, 
	const std::string _imageName,
	const std::string _modelPath) 
{
	struct bigger {
		bool operator() (HorrorSceneInfo i, HorrorSceneInfo j) { return (i.prop>j.prop); }
	} mybigger;

	double finalResult(0.0);
	const int FirstNum(3); //计算最大3个的概率
	const int MaxImageNum(6); //最多返回6个结构体
	std::vector<HorrorSceneInfo> vResults;

	const std::string fileName(_fileName);
	const std::string imagePath(_imagePath); //提前新建文件夹
	const std::string imageName(_imageName);
	const std::string modelPath(_modelPath);
	const std::size_t shotInterval(100);
	const std::size_t  sceneShotNum(20);

	std::size_t  num = HorrorVideoSceneNum(fileName.c_str(), shotInterval, sceneShotNum);

	HorrorSceneInfo* resultSet = new HorrorSceneInfo[num];

	if (HorrorVideoDetector_Info(resultSet, imagePath.c_str(), fileName.c_str(), 
			imageName.c_str(), modelPath.c_str(), shotInterval, sceneShotNum)) 
	{
		for (std::size_t  i=0; i<num; ++i) {
			vResults.push_back(resultSet[i]);
		}
	} else {
		std::cout << " Failed to detect the video! " << std::endl;
	}

	delete[] resultSet;
	resultSet = nullptr;

	if (num > MaxImageNum) {
		std::sort(vResults.begin(), vResults.end(), mybigger);
		for(int i=0; i<MaxImageNum; ++i) {
			infosSet.push_back(vResults[i]);
		}
	} else {
		std::sort(vResults.begin(), vResults.end(), mybigger);
		infosSet = vResults;
	}
	

	if (num > FirstNum) {
		std::sort(vResults.begin(), vResults.end(), mybigger);

		for(int i=0; i<FirstNum; ++i) {
			finalResult += vResults[i].prop;
		}

		finalResult /= FirstNum;
	} else {
		for(std::size_t i=0; i<num; ++i) {
			finalResult += vResults[i].prop;
		}
		finalResult /= num;
	}

	return finalResult;
}

void videoDetector(
	const char* const _fileName, /*文件名*/
	const char* const _imagePath, /*图片路径*/
	const char* const _imageName, /*图片名称*/
	int& _signal, /*标志位, 0未知, 1色情, 2恐怖*/
	double& _prop, /*概率*/
	std::vector<SceneInfo>& _infosSet
)
{
	_signal = 0;
	_prop = 0.0;

	const std::string fileName(_fileName);
	const std::string imagePath(_imagePath); //提前新建文件夹
	const std::string imageName(_imageName);
	const std::string modelPath("./models");

	double pornResult(-1.0);
	double horrorResult(-1.0);
	std::vector<PornSceneInfo> pornInfosSet;
	std::vector<HorrorSceneInfo> horrorInfosSet;

	try {
		pornResult = detectPornVideo(pornInfosSet, fileName, imagePath, imageName, modelPath);
	} catch (std::exception ex) {
		std::cout << ex.what() << std::endl;
	}

	std::vector<SceneInfo> infosSet(pornInfosSet.size());

	for (std::size_t  i=0; i<pornInfosSet.size(); ++i) 
	{
		std::cout << " Scene[" << i << "] Prop : " << pornInfosSet[i].prop << std::endl;
		std::cout << " Scene[" << i << "] Image Path : " << pornInfosSet[i].imagePath << std::endl;
		std::cout << " Scene[" << i << "] Begin Time : " << pornInfosSet[i].btime << std::endl;
		std::cout << " Scene[" << i << "] End Time : " << pornInfosSet[i].etime << std::endl;
		std::cout << " Scene[" << i << "] Begin Pos : " << pornInfosSet[i].bpos << std::endl;
		std::cout << " Scene[" << i << "] End Pos : " << pornInfosSet[i].epos << std::endl;

		infosSet[i].prop = pornInfosSet[i].prop;
		strcpy(infosSet[i].imagePath, pornInfosSet[i].imagePath);
		infosSet[i].btime = pornInfosSet[i].btime;
		infosSet[i].etime = pornInfosSet[i].etime;
		infosSet[i].bpos = pornInfosSet[i].bpos;
		infosSet[i].epos = pornInfosSet[i].epos;
	}

	if (pornResult > 0.7) { //返回色情
		std::cout << "Porn Result = " << pornResult << std::endl;
		_signal = 1;
		_prop = pornResult;
		_infosSet = infosSet;
		return;
	}

	try {
		horrorResult = detectHorrorVideo(horrorInfosSet, fileName, imagePath, imageName, modelPath);
	} catch (std::exception ex) {
		std::cout << ex.what() << std::endl;
	}

	for (std::size_t i=0; i<horrorInfosSet.size(); ++i) 
	{
		std::cout << " Scene[" << i << "] Prop : " << horrorInfosSet[i].prop << std::endl;
		std::cout << " Scene[" << i << "] Image Path : " << horrorInfosSet[i].imagePath << std::endl;
		std::cout << " Scene[" << i << "] Begin Time : " << horrorInfosSet[i].btime << std::endl;
		std::cout << " Scene[" << i << "] End Time : " << horrorInfosSet[i].etime << std::endl;
		std::cout << " Scene[" << i << "] Begin Pos : " << horrorInfosSet[i].bpos << std::endl;
		std::cout << " Scene[" << i << "] End Pos : " << horrorInfosSet[i].epos << std::endl;

		infosSet[i].prop = horrorInfosSet[i].prop;
		strcpy(infosSet[i].imagePath,horrorInfosSet[i].imagePath);
		infosSet[i].btime = horrorInfosSet[i].btime;
		infosSet[i].etime = horrorInfosSet[i].etime;
		infosSet[i].bpos = horrorInfosSet[i].bpos;
		infosSet[i].epos = horrorInfosSet[i].epos;
	}

	if (horrorResult > 0.6) { //返回恐怖
		std::cout << "Horror Result = " << horrorResult << std::endl;
		_signal = 2;
		_prop = horrorResult;
		_infosSet = infosSet;
		return;
	}

	_infosSet = infosSet;
	std::cout << "Porn Result = " << pornResult << std::endl;
	std::cout << "Horror Result = " << horrorResult << std::endl;

	return;
}