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

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

 

时间: 2024-10-24 00:49:18

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

深度学习-faster rcnn运行demo出现错误

问题描述 faster rcnn运行demo出现错误 faster rcnn配置好之后运行 ./tools/demo.py出现如下错误:: Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: Python 解决方案 https://www.zhihu.com/question/38455242/answer/77002913 解决方案二: 两种可能: 1.makefile 文件下LIBRARIES += op

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

  如何才能将Faster R-CNN训练起来?   首先进入 Faster RCNN 的官网啦,即:https://github.com/rbgirshick/py-faster-rcnn#installation-sufficient-for-the-demo 先用提供的 model 自己测试一下效果嘛... 按照官网安装教程,安装基本需求.   Installation (sufficient for the demo)   Clone the Faster R-CNN repositor

faster rcn...-faster rcnn的train.prototxt 没有data层

问题描述 faster rcnn的train.prototxt 没有data层 最近在用faster rcnn训练数据,需要修改train.prototxt中data层的参数num_classes,但是train.prototxt没有data层,那我该如何修改呢?

论文阅读之:Is Faster R-CNN Doing Well for Pedestrian Detection?

  Is Faster R-CNN Doing Well for Pedestrian Detection? ECCV 2016   Liliang Zhang & Kaiming He     原文链接:http://arxiv.org/pdf/1607.07032v2.pdf Code : https://github.com/zhangliliang/RPN_BF/tree/RPN-pedestrian     摘要:行人检测被人 argue 说是特定课题,而不是general 的物体检测

Faster RCNN 运行自己的数据,刚开始正常,后来就报错: Index exceeds matrix dimensions. Error in ori_demo (line 114) boxes_cell{i} = [boxes(:, (1+(i-1)*4):(i*4)), scores(

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 = '

移动端html5视频开发指南

在页面添加一个视频 <video src="demo.webm" type="video/webm"> <p>您的浏览器不支持video元素.</p> </video> 为video元素指定多个视频格式 并不是所有的浏览器都支持同一种格式,source可以让开发者为video元素指定多个视频格式 <video controls> <source src="demo.webm" ty

Html5多媒体相关的API---video

在HTML5中,新增了两个元素---video元素与audio元素,其中video元素专门用来播放网络上的视频或电影,而audio元素专门用来播放网络上的音频数据. 我们先来看看video元素的相关知识点. 一:HTML5中的video标签支持3种常用的视频格式: 1.Ogg = 带有Theora 视频编码和Vorbis 音频编码的 Ogg 文件: 2.MPEG4 = 带有H.264 视频编码和AAC 音频编码的MPEG 4 文件: 3.WebM = 带有VP8 视频编码和Vorbis 音频编码

【前沿】何恺明大神ICCV2017最佳论文Mask R-CNN的Keras/TensorFlow/Pytorch 代码实现

我们提出了一个概念上简单.灵活和通用的用于目标实例分割(object instance segmentation)的框架.我们的方法能够有效地检测图像中的目标,同时还能为每个实例生成一个高质量的分割掩码(segmentation mask).这个方面被称为 Mask R-CNN,是在 Faster R-CNN 上的扩展--在其已有的用于边界框识别的分支上添加了一个并行的用于预测目标掩码的分支.Mask R-CNN 的训练很简单,仅比 Faster R-CNN 多一点计算开销,运行速度为 5 fp

Video ++孙兆民:人工智能行业报告——视频内容识别行业分析

像素的世界已经延伸到图像之外,虽然视频对于机器学习研究人员来说一直都是个挑战,但现在的技术能够使得从视频中提取信息变得跟从图像中提取信息一样简单.人工智能这个新兴"工具"的出现,为人类加速前往高度智慧化形态提供能量,重构着整个视频行业的结构和协作方式. 下面这份分析报告由Video++提供,并在AI研习社作以分享.这份报告并非针对人工智能领域全局观,而是解构视频行业,围绕国内视频内容识别这一垂直领域的创业公司展开讨论,从市场.产品.技术.商业模式.人才等多个维度出发,浅析人工智能技术在