faster rcnn test demo ---repaired for video input and save the image, label, score et al. into .mat format

function script_faster_rcnn_demo()
close all;
clc;
clear mex;
clear is_valid_handle; % to clear init_key
run(fullfile(fileparts(fileparts(mfilename('fullpath'))), 'startup'));
%% -------------------- CONFIG --------------------
opts.caffe_version = 'caffe_faster_rcnn';
opts.gpu_id = auto_select_gpu;
active_caffe_mex(opts.gpu_id, opts.caffe_version); opts.per_nms_topN = 6000;
opts.nms_overlap_thres = 0.7;
opts.after_nms_topN = 300;
opts.use_gpu = true; opts.test_scales = 600; % %% -------------------- INIT_MODEL --------------------
% model_dir = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_vgg_16layers'); %% VGG-16
model_dir = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_ZF'); %% ZF
proposal_detection_model = load_proposal_detection_model(model_dir); proposal_detection_model.conf_proposal.test_scales = opts.test_scales;
proposal_detection_model.conf_detection.test_scales = opts.test_scales;
if opts.use_gpu
proposal_detection_model.conf_proposal.image_means = gpuArray(proposal_detection_model.conf_proposal.image_means);
proposal_detection_model.conf_detection.image_means = gpuArray(proposal_detection_model.conf_detection.image_means);
end % caffe.init_log(fullfile(pwd, 'caffe_log'));
% proposal net
rpn_net = caffe.Net(proposal_detection_model.proposal_net_def, 'test');
rpn_net.copy_from(proposal_detection_model.proposal_net);
% fast rcnn net
fast_rcnn_net = caffe.Net(proposal_detection_model.detection_net_def, 'test');
fast_rcnn_net.copy_from(proposal_detection_model.detection_net); % set gpu/cpu
if opts.use_gpu
caffe.set_mode_gpu();
else
caffe.set_mode_cpu();
end %% -------------------- WARM UP --------------------
% the first run will be slower; use an empty image to warm up for j = 1:2 % we warm up 2 times
im = uint8(ones(375, 500, 3)*128);
if opts.use_gpu
im = gpuArray(im);
end
[boxes, scores] = proposal_im_detect(proposal_detection_model.conf_proposal, rpn_net, im);
aboxes = boxes_filter([boxes, scores], opts.per_nms_topN, opts.nms_overlap_thres, opts.after_nms_topN, opts.use_gpu);
if proposal_detection_model.is_share_feature
[boxes, scores] = fast_rcnn_conv_feat_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
rpn_net.blobs(proposal_detection_model.last_shared_output_blob_name), ...
aboxes(:, 1:4), opts.after_nms_topN);
else
[boxes, scores] = fast_rcnn_im_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
aboxes(:, 1:4), opts.after_nms_topN);
end
end %% -------------------- TESTING --------------------
% im_names = {'001763.jpg', '004545.jpg', '000542.jpg', '000456.jpg', '001150.jpg'};
% these images can be downloaded with fetch_faster_rcnn_final_model.m
% for j = 1:length(im_names)
% im = imread(fullfile(pwd, im_names{j})); %% -------------------------- Video ------------------
%% initialization
video_names = {}; %% --------------path for test ------------
path = '/media/wangxiao/247317a3-e6b5-45d4-81d1-956930526746/video/test_video/';
savePath = '/media/wangxiao/247317a3-e6b5-45d4-81d1-956930526746/video/test_video_images/'; %% ---------------path for the 4TB drive----------
% path = '/media/wangxiao/Seagate/pedestrian data/';
% savePath = '/media/wangxiao/Seagate/pedestrian data/pedestrian frame/'; % video_dir = dir(path); video_dir = dir(fullfile(path, '*.avi'));
% if isempty(video_dir)
% video_dir = dir(fullfile(path, '*.mp4'));
% end
% if isempty(video_dir)
% video_dir = dir(fullfile(path, '*.rmvb'));
% end
% if isempty(video_dir)
% video_dir = dir(fullfile(path, '*.mkv'));
% end %%
k =1;
for i = 1:length(video_dir)
if ~video_dir(i).isdir
video_names{k} = video_dir(i).name; % 视频的名字 保存在video_name中;
k = k+1;
end
end
clear k;
running_time = []; %% 将视频load进来,切割为图像,存储
for j = 1:length(video_dir) %视频级别; % 弹出错误提示: Could not read file due to an unexpected error. Reason:
% Unable to initialize the video obtain properties ...
frames = VideoReader (strcat(path, video_dir(j).name));
numFrames =frames.NumberOfFrames;
for k = 1 : 30: numFrames
disp(['saving the ',num2str(k),'-th frames, please waiting....']);
frame = read(frames,k); %将frame存储起来;
frame = imresize(frame, [200, 300]); % 是否要对图像进行resize ?
imwrite(frame,[savePath,sprintf('%08d.png',k)]);
end
end %% 读取图像,输入给 faster rcnn
image_path = savePath;
imageFile = dir([image_path, '*.png']); for iii = 1:length(imageFile)
im = imread([image_path, imageFile(iii).name]); if opts.use_gpu
im = gpuArray(im);
end % test proposal
th = tic();
[boxes, scores] = proposal_im_detect(proposal_detection_model.conf_proposal, rpn_net, im);
t_proposal = toc(th);
th = tic();
aboxes = boxes_filter([boxes, scores], opts.per_nms_topN, opts.nms_overlap_thres, opts.after_nms_topN, opts.use_gpu);
t_nms = toc(th); % test detection
th = tic();
if proposal_detection_model.is_share_feature
[boxes, scores] = fast_rcnn_conv_feat_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
rpn_net.blobs(proposal_detection_model.last_shared_output_blob_name), ...
aboxes(:, 1:4), opts.after_nms_topN); else
[boxes, scores] = fast_rcnn_im_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
aboxes(:, 1:4), opts.after_nms_topN); end
t_detection = toc(th); fprintf('%s (%dx%d): time %.3fs (resize+conv+proposal: %.3fs, nms+regionwise: %.3fs)\n', ['the ' num2str(j) '-th image '], size(im, 2), size(im, 1), t_proposal + t_nms + t_detection, t_proposal, t_nms+t_detection);
running_time(end+1) = t_proposal + t_nms + t_detection; %% visualize 可视化;
classes = proposal_detection_model.classes; % 20类物体,包括行人这一类;
boxes_cell = cell(length(classes), 1); % 20*1 cell;
thres = 0.6; for i = 1:length(boxes_cell)
boxes_cell{i} = [boxes(:, (1+(i-1)*4):(i*4)), scores(:, i)];
boxes_cell{i} = boxes_cell{i}(nms(boxes_cell{i}, 0.3), :); I = boxes_cell{i}(:, 5) >= thres;
boxes_cell{i} = boxes_cell{i}(I, :);
end figure(j); [location, label, score] = output(im, boxes_cell, classes, 'voc');
if (score==0)
continue;
else
% disp(imageFile(iii).name);
save(['./mat results/' imageFile(iii).name '.mat' ], 'location', 'label', 'score', 'im');
end showboxes(im, boxes_cell, classes, 'voc'); pause(0.1); end % eval(['movie_' num2str(k) '_' class_filenames{NUM} '= im_names;']);
% clear im_names;
%
% eval(['movie_bb_' num2str(k) '_' class_filenames{NUM} '= video_cell;'])
% clear video_cell;
%
% if ~exist([fullfile(pwd, 'video_frames'),'/','video.mat'],'file')
% save('./video_frames/video.mat',['movie_' num2str(k) '_' class_filenames{NUM}]);
% save('./video_frames/video_bb.mat',['movie_bb_' num2str(k) '_' class_filenames{NUM}]);
% else
% save('./video_frames/video.mat',['movie_' num2str(k) '_' class_filenames{NUM}],'-append');
% save('./video_frames/video_bb.mat',['movie_bb_' num2str(k) '_' class_filenames{NUM}], '-append');
% end
%
% eval(['clear movie_' num2str(k)]);
% eval(['clear movie_bb_' num2str(k)]);
% save('im_boxes.mat','im_cell'); fprintf('mean time: %.3fs\n', mean(running_time));
caffe.reset_all();
clear mex; end function proposal_detection_model = load_proposal_detection_model(model_dir)
ld = load(fullfile(model_dir, 'model'));
proposal_detection_model = ld.proposal_detection_model;
clear ld; proposal_detection_model.proposal_net_def ...
= fullfile(model_dir, proposal_detection_model.proposal_net_def);
proposal_detection_model.proposal_net ...
= fullfile(model_dir, proposal_detection_model.proposal_net);
proposal_detection_model.detection_net_def ...
= fullfile(model_dir, proposal_detection_model.detection_net_def);
proposal_detection_model.detection_net ...
= fullfile(model_dir, proposal_detection_model.detection_net); end function aboxes = boxes_filter(aboxes, per_nms_topN, nms_overlap_thres, after_nms_topN, use_gpu)
% to speed up nms
if per_nms_topN > 0
aboxes = aboxes(1:min(length(aboxes), per_nms_topN), :);
end
% do nms
if nms_overlap_thres > 0 && nms_overlap_thres < 1
aboxes = aboxes(nms(aboxes, nms_overlap_thres, use_gpu), :);
end
if after_nms_topN > 0
aboxes = aboxes(1:min(length(aboxes), after_nms_topN), :);
end
end

faster rcnn test demo ---repaired for video input and save the image, label, score et al. into .mat format的更多相关文章

  1. Faster RCNN算法demo代码解析

    一. Faster-RCNN代码解释 先看看代码结构: Data: This directory holds (after you download them): Caffe models pre-t ...

  2. Windows下如何采用微软的Caffe配置Faster R-CNN

    前言 比较简单的一篇博客.https://github.com/microsoft/caffe 微软的Caffe以在Windows下编译简单而受到了很多人的喜爱(包括我),只用改改prop配置然后无脑 ...

  3. faster rcnn 源码学习-------数据读入及RoIDataLayer相关模块解读

    参考博客:::https://www.cnblogs.com/Dzhen/p/6845852.html 非常全面的解读参考:::https://blog.csdn.net/DaVinciL/artic ...

  4. caffe学习一:ubuntu16.04下跑Faster R-CNN demo (基于caffe). (亲测有效,记录经历两天的吐血经历)

    兜兜转转,兜兜转转; 一次有一次,这次终于把Faster R-CNN 跑通了. 重要提示1:在开始跑Faster R-CNN之前一定要搞清楚用的是Python2 还是Python3. 不然你会无限次陷 ...

  5. faster rcnn训练详解

    http://blog.csdn.net/zy1034092330/article/details/62044941 py-faster-rcnn训练自己的数据:流程很详细并附代码 https://h ...

  6. (原)faster rcnn的tensorflow代码的理解

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/10043864.html 参考网址: 论文:https://arxiv.org/abs/1506.01 ...

  7. faster r-cnn 在CPU配置下训练自己的数据

    因为没有GPU,所以在CPU下训练自己的数据,中间遇到了各种各样的坑,还好没有放弃,特以此文记录此过程. 1.在CPU下配置faster r-cnn,参考博客:http://blog.csdn.net ...

  8. 如何才能将Faster R-CNN训练起来?

    如何才能将Faster R-CNN训练起来? 首先进入 Faster RCNN 的官网啦,即:https://github.com/rbgirshick/py-faster-rcnn#installa ...

  9. 新人如何运行Faster RCNN的tensorflow代码

    0.目的 刚刚学习faster rcnn目标检测算法,在尝试跑通github上面Xinlei Chen的tensorflow版本的faster rcnn代码时候遇到很多问题(我真是太菜),代码地址如下 ...

随机推荐

  1. hdu 1950 最长上升子序列

    //Accepted 3540 KB 62 ms //dp 最长上升子序列 #include <cstdio> #include <cstring> #include < ...

  2. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

  3. Ubuntu 14.10 下安装navicat

    1 下载navicat,网址http://www.navicat.com.cn/download,我下载的是navicat111_premium_cs.tar.gz 2 解压到合适的位置 3 进入解压 ...

  4. spark与Hadoop区别

    2分钟读懂Hadoop和Spark的异同 2016.01.25 11:15:59 来源:51cto作者:51cto ( 0 条评论 )   谈到大数据,相信大家对Hadoop和Apache Spark ...

  5. hdu 2094

    ps: 原本听说用set容器做是最好的...然而我并不懂..就用C语言做了...就是先输入赢得一组和输的一组,然后把重复的删掉,再比较赢得一组里没有出现在输的一组里的人数,如果是1就是YES,其他就是 ...

  6. Asp.Net中Ajax实现登陆判断

    Default.aspx: <head runat="server"> <title>无标题页</title> <script type= ...

  7. Emacs+highlight-parentheses高亮括号

    EmacsWiki上关于它的介绍HighlightParentheses,下载最新版请通过作者的GitHub:https://github.com/nschum/highlight-parenthes ...

  8. 前端页面js与flash交互——js获取flash对象,并传递参数

    背景介绍: 最近在搞一个项目,涉及到图片选取,裁剪,上传等,由于浏览器安全性问题,js无法获取到<input type="file">中选取的文件路径,而且对照片的裁剪 ...

  9. Xcode7推出的新优惠:免证书测试

    1.准备 注意:一定要让你的真机设备的系统版本和app的系统版本想对应,如果不对应就会出现一个很常见的问题:could not find developer disk image 2.首先先安装Xco ...

  10. 表视图控制器(TableViewController)(一)

    1 创建一个UITableViewController并展示简单数据 1.1 问题 有很多移动客户端的应用都是采用表的形式来展示数据,因为表视图能使数据看起来更规整.更有调理,比如微信界面就是使用的表 ...