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

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


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

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

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


代码:

[cpp]  view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2.   
  3. #include "VideoDetector.h"  
  4.   
  5. using namespace std;  
  6.   
  7. double detectPornVideo(   
  8.     std::vector<PornSceneInfo>& infosSet,   
  9.     const std::string _fileName,   
  10.     const std::string _imagePath,   
  11.     const std::string _imageName,  
  12.     const std::string _modelPath)   
  13. {  
  14.     struct bigger {  
  15.         bool operator() (PornSceneInfo i, PornSceneInfo j) { return (i.prop>j.prop); }  
  16.     } mybigger;  
  17.   
  18.     double finalResult(0.0);  
  19.     const int FirstNum(3); //计算最大3个的概率  
  20.     const int MaxImageNum(6); //最多返回6个结构体  
  21.     std::vector<PornSceneInfo> vResults;  
  22.   
  23.     const std::string fileName(_fileName);  
  24.     const std::string imagePath(_imagePath); //提前新建文件夹  
  25.     const std::string imageName(_imageName);  
  26.     const std::string modelPath(_modelPath);  
  27.     const std::size_t shotInterval(100);  
  28.     const std::size_t  sceneShotNum(20);  
  29.   
  30.     std::size_t  num = PornVideoSceneNum(fileName.c_str(), shotInterval, sceneShotNum);  
  31.   
  32.     PornSceneInfo* resultSet = new PornSceneInfo[num];  
  33.   
  34.     if (PornVideoDetector_Info(resultSet, imagePath.c_str(), fileName.c_str(), imageName.c_str(),   
  35.         modelPath.c_str(), shotInterval, sceneShotNum))   
  36.     {  
  37.         for (std::size_t  i=0; i<num; ++i) {  
  38.             vResults.push_back(resultSet[i]);  
  39.         }  
  40.     } else {  
  41.         std::cout << " Failed to detect the video! " << std::endl;  
  42.     }  
  43.   
  44.     delete[] resultSet;  
  45.     resultSet = nullptr;  
  46.   
  47.     if (num > MaxImageNum) {  
  48.         std::sort(vResults.begin(), vResults.end(), mybigger);  
  49.         for(int i=0; i<MaxImageNum; ++i) {  
  50.             infosSet.push_back(vResults[i]); //从大到小排列, 最多包涵MaxImageNum个  
  51.         }  
  52.     } else {  
  53.         std::sort(vResults.begin(), vResults.end(), mybigger);  
  54.         infosSet = vResults;  
  55.     }  
  56.   
  57.     if (num > FirstNum) {  
  58.         std::sort(vResults.begin(), vResults.end(), mybigger);  
  59.   
  60.         for(int i=0; i<FirstNum; ++i) {  
  61.             finalResult += vResults[i].prop; //大于3个只取前3个  
  62.         }  
  63.   
  64.         finalResult /= FirstNum;  
  65.     } else {  
  66.         for(std::size_t i=0; i<num; ++i) {  
  67.             finalResult += vResults[i].prop;  
  68.         }  
  69.         finalResult /= num;  
  70.     }  
  71.   
  72.     return finalResult;  
  73. }  
  74.   
  75. double detectHorrorVideo(  
  76.     std::vector<HorrorSceneInfo>& infosSet,   
  77.     const std::string _fileName,   
  78.     const std::string _imagePath,   
  79.     const std::string _imageName,  
  80.     const std::string _modelPath)   
  81. {  
  82.     struct bigger {  
  83.         bool operator() (HorrorSceneInfo i, HorrorSceneInfo j) { return (i.prop>j.prop); }  
  84.     } mybigger;  
  85.   
  86.     double finalResult(0.0);  
  87.     const int FirstNum(3); //计算最大3个的概率  
  88.     const int MaxImageNum(6); //最多返回6个结构体  
  89.     std::vector<HorrorSceneInfo> vResults;  
  90.   
  91.     const std::string fileName(_fileName);  
  92.     const std::string imagePath(_imagePath); //提前新建文件夹  
  93.     const std::string imageName(_imageName);  
  94.     const std::string modelPath(_modelPath);  
  95.     const std::size_t shotInterval(100);  
  96.     const std::size_t  sceneShotNum(20);  
  97.   
  98.     std::size_t  num = HorrorVideoSceneNum(fileName.c_str(), shotInterval, sceneShotNum);  
  99.   
  100.     HorrorSceneInfo* resultSet = new HorrorSceneInfo[num];  
  101.   
  102.     if (HorrorVideoDetector_Info(resultSet, imagePath.c_str(), fileName.c_str(),   
  103.             imageName.c_str(), modelPath.c_str(), shotInterval, sceneShotNum))   
  104.     {  
  105.         for (std::size_t  i=0; i<num; ++i) {  
  106.             vResults.push_back(resultSet[i]);  
  107.         }  
  108.     } else {  
  109.         std::cout << " Failed to detect the video! " << std::endl;  
  110.     }  
  111.   
  112.     delete[] resultSet;  
  113.     resultSet = nullptr;  
  114.   
  115.     if (num > MaxImageNum) {  
  116.         std::sort(vResults.begin(), vResults.end(), mybigger);  
  117.         for(int i=0; i<MaxImageNum; ++i) {  
  118.             infosSet.push_back(vResults[i]);  
  119.         }  
  120.     } else {  
  121.         std::sort(vResults.begin(), vResults.end(), mybigger);  
  122.         infosSet = vResults;  
  123.     }  
  124.       
  125.   
  126.     if (num > FirstNum) {  
  127.         std::sort(vResults.begin(), vResults.end(), mybigger);  
  128.   
  129.         for(int i=0; i<FirstNum; ++i) {  
  130.             finalResult += vResults[i].prop;  
  131.         }  
  132.   
  133.         finalResult /= FirstNum;  
  134.     } else {  
  135.         for(std::size_t i=0; i<num; ++i) {  
  136.             finalResult += vResults[i].prop;  
  137.         }  
  138.         finalResult /= num;  
  139.     }  
  140.   
  141.     return finalResult;  
  142. }  
  143.   
  144. void videoDetector(  
  145.     const charconst _fileName, /*文件名*/  
  146.     const charconst _imagePath, /*图片路径*/  
  147.     const charconst _imageName, /*图片名称*/  
  148.     int& _signal, /*标志位, 0未知, 1色情, 2恐怖*/  
  149.     double& _prop, /*概率*/  
  150.     std::vector<SceneInfo>& _infosSet  
  151. )  
  152. {  
  153.     _signal = 0;  
  154.     _prop = 0.0;  
  155.   
  156.     const std::string fileName(_fileName);  
  157.     const std::string imagePath(_imagePath); //提前新建文件夹  
  158.     const std::string imageName(_imageName);  
  159.     const std::string modelPath("./models");  
  160.   
  161.     double pornResult(-1.0);  
  162.     double horrorResult(-1.0);  
  163.     std::vector<PornSceneInfo> pornInfosSet;  
  164.     std::vector<HorrorSceneInfo> horrorInfosSet;  
  165.   
  166.     try {  
  167.         pornResult = detectPornVideo(pornInfosSet, fileName, imagePath, imageName, modelPath);  
  168.     } catch (std::exception ex) {  
  169.         std::cout << ex.what() << std::endl;  
  170.     }  
  171.   
  172.     std::vector<SceneInfo> infosSet(pornInfosSet.size());  
  173.   
  174.     for (std::size_t  i=0; i<pornInfosSet.size(); ++i)   
  175.     {  
  176.         std::cout << " Scene[" << i << "] Prop : " << pornInfosSet[i].prop << std::endl;  
  177.         std::cout << " Scene[" << i << "] Image Path : " << pornInfosSet[i].imagePath << std::endl;  
  178.         std::cout << " Scene[" << i << "] Begin Time : " << pornInfosSet[i].btime << std::endl;  
  179.         std::cout << " Scene[" << i << "] End Time : " << pornInfosSet[i].etime << std::endl;  
  180.         std::cout << " Scene[" << i << "] Begin Pos : " << pornInfosSet[i].bpos << std::endl;  
  181.         std::cout << " Scene[" << i << "] End Pos : " << pornInfosSet[i].epos << std::endl;  
  182.   
  183.         infosSet[i].prop = pornInfosSet[i].prop;  
  184.         strcpy(infosSet[i].imagePath, pornInfosSet[i].imagePath);  
  185.         infosSet[i].btime = pornInfosSet[i].btime;  
  186.         infosSet[i].etime = pornInfosSet[i].etime;  
  187.         infosSet[i].bpos = pornInfosSet[i].bpos;  
  188.         infosSet[i].epos = pornInfosSet[i].epos;  
  189.     }  
  190.   
  191.     if (pornResult > 0.7) { //返回色情  
  192.         std::cout << "Porn Result = " << pornResult << std::endl;  
  193.         _signal = 1;  
  194.         _prop = pornResult;  
  195.         _infosSet = infosSet;  
  196.         return;  
  197.     }  
  198.   
  199.     try {  
  200.         horrorResult = detectHorrorVideo(horrorInfosSet, fileName, imagePath, imageName, modelPath);  
  201.     } catch (std::exception ex) {  
  202.         std::cout << ex.what() << std::endl;  
  203.     }  
  204.   
  205.     for (std::size_t i=0; i<horrorInfosSet.size(); ++i)   
  206.     {  
  207.         std::cout << " Scene[" << i << "] Prop : " << horrorInfosSet[i].prop << std::endl;  
  208.         std::cout << " Scene[" << i << "] Image Path : " << horrorInfosSet[i].imagePath << std::endl;  
  209.         std::cout << " Scene[" << i << "] Begin Time : " << horrorInfosSet[i].btime << std::endl;  
  210.         std::cout << " Scene[" << i << "] End Time : " << horrorInfosSet[i].etime << std::endl;  
  211.         std::cout << " Scene[" << i << "] Begin Pos : " << horrorInfosSet[i].bpos << std::endl;  
  212.         std::cout << " Scene[" << i << "] End Pos : " << horrorInfosSet[i].epos << std::endl;  
  213.   
  214.         infosSet[i].prop = horrorInfosSet[i].prop;  
  215.         strcpy(infosSet[i].imagePath,horrorInfosSet[i].imagePath);  
  216.         infosSet[i].btime = horrorInfosSet[i].btime;  
  217.         infosSet[i].etime = horrorInfosSet[i].etime;  
  218.         infosSet[i].bpos = horrorInfosSet[i].bpos;  
  219.         infosSet[i].epos = horrorInfosSet[i].epos;  
  220.     }  
  221.   
  222.     if (horrorResult > 0.6) { //返回恐怖  
  223.         std::cout << "Horror Result = " << horrorResult << std::endl;  
  224.         _signal = 2;  
  225.         _prop = horrorResult;  
  226.         _infosSet = infosSet;  
  227.         return;  
  228.     }  
  229.   
  230.     _infosSet = infosSet;  
  231.     std::cout << "Porn Result = " << pornResult << std::endl;  
  232.     std::cout << "Horror Result = " << horrorResult << std::endl;  
  233.   
  234.     return;  
  235. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值