C3D
来源:互联网 发布:character mysql 编辑:程序博客网 时间:2024/05/21 17:05
#include<iostream> #include<stdio.h> #include<vector> #include<fstream> #include<opencv2/opencv.hpp> #define video_index_max 101 /************************************************************************函数功能:从已经获取ucf101数据集所有类名称的txt文件中读入视频类别名称,共101类输入参数:filename是.txt文件路径,videoname是保存101类的名称输出参数:************************************************************************/void ReadVideoClass(const std::string& filename, std::vector<std::string>&videoname){ std::fstream file0; file0.open(filename); if (!file0.is_open()) { std::cerr << "fail to open the all name file" << std::endl; exit(0); } char temp0[1000]; while (!file0.eof()) { file0.getline(temp0, 1000); std::string line(temp0); videoname.push_back(line); } if (videoname.size() != video_index_max) //如果读入的所有名称数量小于101类,则输入文件有误 { std::cerr << " video class file have error size" << std::endl; exit(0); } file0.close();}/************************************************************************函数功能:获取所有的从C3D网络输出并保存的.prob文件的路径输入参数:filename是.txt文件路径,videoname是保存101类的名称输出参数:************************************************************************/void ReadAllTestResultPath(const std::string& filename, std::vector<std::string>&ResultPath){ std::fstream file1; file1.open(filename);//读取视频文件路径 if (!file1.is_open()) { std::cerr << "fail to open the test result file" << std::endl; exit(0); } char temp[1000]; while (!file1.eof()) { file1.getline(temp, 1000); std::string line(temp); //去除可能存在的空格,并添加后缀.prob std::string PostfixLine;//测试结果文件保存路径 std::string::size_type npos0 = line.find_last_of(" "); if (npos0 == line.size() - 1) { PostfixLine = line.substr(0, npos0); PostfixLine += ".prob"; } else PostfixLine = line + ".prob"; std::cout << PostfixLine << std::endl; ResultPath.push_back(PostfixLine); } file1.close();}/************************************************************************函数功能:输入参数:输出参数:************************************************************************/void ReadLine(const std::string & path, std::string &Videopath, int &StartIndex, std::string& ground_truth){ std::string line = path; StartIndex = atoi(line.substr(line.find_last_of("/") + 1, line.size() - 5).c_str()); ground_truth = line.substr(line.find("output/") + strlen("output/"), line.find("/v_") - strlen("output/")); line.erase(0, 6); line.erase(line.find_last_of("/"), line.size()); line.insert(0, "F:/ucf101"); line += ".avi"; Videopath = line;}/************************************************************************函数功能:从.prob文件中读入C3D网络的输出结果输入参数:.prob文件路径输出参数:101类的预测输出结果 blob************************************************************************/bool read_blob(const std::string &probfile, std::vector<float>&blob){ /*读取模型提取的特征文件中的参数*/ std::ifstream fin(probfile, std::ios::binary); if (!fin.is_open()) { std::cerr << probfile << "file not exist " << std::endl; return 0; } int num = 0, channel = 0, length = 0, height = 0, width = 0; fin.read((char*)&num, sizeof(int)); fin.read((char*)&channel, sizeof(int)); if (channel != video_index_max) return 0; fin.read((char*)&length, sizeof(int)); fin.read((char*)&height, sizeof(int)); fin.read((char*)&width, sizeof(int)); float ptr = 0; for (int i = 0; i < channel; i++) { fin.read((char*)&ptr, sizeof(float)); blob.push_back(ptr); } return 1;}/************************************************************************函数功能:从数组vec中找出最大值,返回最大值和最大值对应的序号输入参数:数组vec,数组大小num,引用返回的数组最大值rate输出参数:最大值对应的序号************************************************************************/int find_sec_max(std::vector<float>vec, int num, float &rate){ int index = -1; if (vec.size() != num) return -1; float maxc = 0; for (int i = 0; i < vec.size(); i++) { if (vec[i]>maxc) { maxc = vec[i]; index = i; } } rate = maxc; return index;}int main(){ using namespace std; using namespace cv; std::vector<std::string>videoname; ReadVideoClass("output\\list.txt", videoname); std::vector<std::string>ResultPath; ReadAllTestResultPath("output\\last-test-output.lst", ResultPath); VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(320, 240)); vector<string>TestResultAll; vector<float>TestResultAllRate; vector<string>GroundTrueAll; for (size_t i = 0; i < ResultPath.size(); i++) { std::string Videopath; int StartIndex; std::string ground_truth; ReadLine(ResultPath[i], Videopath, StartIndex, ground_truth); std::vector<float>blob; int flag = read_blob(ResultPath[i], blob); if (!flag) { cerr << "blob read failed" << endl; exit(0); } float maxrate = 0; int TestResult = find_sec_max(blob, video_index_max, maxrate); VideoCapture capture; capture.open(Videopath); int count = StartIndex; int countcopy = count; Mat frame; while (countcopy--)//每次写视频时,都跳过前count帧开始写 { capture >> frame; } while (count < StartIndex + 16)//每个视频段的大小都是16帧, { capture >> frame;//先读入至frame中 writer << frame;//将frame保存到writer中 TestResultAll.push_back(videoname[TestResult]);//每一帧都赋予一个检测结果(string类型),并将检测结果保存在TestResultAll中 TestResultAllRate.push_back(maxrate);//每一帧每一帧都赋予一个检测概率(float类型),并将检测概率值保存在TestResultAllRate中 GroundTrueAll.push_back(ground_truth);//每一帧都有一个ground truth count++; } } //writer.release(); VideoCapture writercapture; writercapture.open("VideoTest.avi"); VideoWriter VideoTest("VideoTestAll.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(320, 240)); int count = 0; while (1) { Mat frame; writercapture >> frame; if (!frame.data) break; string result = TestResultAll[count]; float ratio = TestResultAllRate[count]; char stemp[200]; sprintf(stemp, "( %.3f )", ratio); string truth = GroundTrueAll[count]; cv::putText(frame, "test result: " + result + stemp, cv::Point(5, 25), CV_FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 255)); cv::putText(frame, "ground truth: " + truth, cv::Point(5, 48), CV_FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 255)); imshow("frame", frame); waitKey(30); VideoTest << frame; count++; } VideoTest.release(); return 0;}
阅读全文
0 0
- C3D
- C3D使用指南
- C3D-tensorflow
- C3D森林可视化解决方案
- C3D User Guide
- C3D Usage Summary
- C3D-tensorflow(1)
- Quux Sincpac C3D 2018
- 用C3D抽取特征
- C3D三维数据引擎平台www.c3d.net.cn
- C3D MiliEarth 升级 V1.3 支持海底
- .c3d和c3t文件的创建过程
- 在乌班图下编译C3D
- C3D:视频动作分类demo实现
- 配置caffe-C3D,提取视频特征
- python-tensorflow(1)C3D代码解析
- CV论文笔记(一) 3D ConvNets(C3D)
- Sincpac C3D v3.7.5297 for Civil 3D 2014 and 2015 Win64 1CD
- 安卓开发之二维码扫描(可以做自定义扫描区域)
- 数据结构递归之迷宫
- ez_setup.py(安装python下setuptools用)
- css清除浮动的三种方式
- 自定义view圆形回加载条
- C3D
- 深度学习引论(一):神经网络简要介绍&熟悉Matlab
- faster-rcnn系列学习之准备数据
- 深度学习之基础模型-DenseNet
- 初学css写个人网页遇到的问题
- 并发编程(9)-同步类容器和并发类容器
- 网络编程之Socket通信(Java)进阶版
- Leetcode算法学习日志-173 Binary Search Tree Iterator
- 数据进制(二进制、十进制、十六进制)的转换详解