forked from pxnguyen/videotext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charDetVideo.m
156 lines (139 loc) · 4.51 KB
/
charDetVideo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function varargout=charDetVideo(action,varargin)
% Detect the initial detections for characters
%
% USAGE:
% hms = charDetVideo('gethm',videos,models)
% bbs = charDetVideo('getbbs',hms,models)
%
% INPUTS
% frames: [4-D] array containing the frames in order
%
% Outputs:
% bbs_videos: matrix of bounding box output
%
varargout = cell(1,max(1,nargout));
[varargout{:}] = feval(action,varargin{:});
end
% This function returns the heatmap of the videos
function isGood=gethm(vidobject,models)
configs = configsgen; nFrames = vidobject.NumberOfFrames;
newTempFolder = fullfile('temp','hms',vidobject.Name);
if ~exist(newTempFolder,'dir'); mkdir(newTempFolder); end
saveFrame = @(sf,hms,scales) save(sf,'hms','scales');
parfor iFrame=1:nFrames
iFrame
fprintf('Working on frame %d...\n',iFrame);
sf = fullfile(newTempFolder,sprintf('%d.mat',iFrame));
if exist(sf,'file') > 0; fprintf('Skipped\n'); continue; end
try
I = read(vidobject,iFrame);
catch e
continue
end
tic; [hms,scales] = get_filter_responses(I,models,configs); toc;
saveFrame(sf,hms,scales);
end
isGood=true;
end
function isGood=getbbs(vidobject,models)
% Return the bbs from the previously collected hms
configs = configsgen;
nFrames = vidobject.NumberOfFrames;
new_temp_folder = fullfile('temp','bbs',vidobject.Name);
if ~exist(new_temp_folder,'dir'); mkdir(new_temp_folder); end
savebbs = @(sf,bbs) save(sf,'bbs');
parfor iFrame=1:nFrames
iFrame
fprintf('Working on frame %d...\n',iFrame);
hms_path = fullfile('temp','hms',vidobject.Name);
% load the hms
lstruct = load(fullfile(hms_path,sprintf('%d.mat',iFrame)));
hms = lstruct.hms;
sf = fullfile(new_temp_folder,sprintf('%d.mat',iFrame));
if exist(sf,'file') > 0; fprintf('Skipped\n'); continue; end;
tic; bbs = getbbsHelper(models,hms,configs); toc;
% Save the bbs
savebbs(sf,bbs);
end
isGood=true;
end
function bbs=getbbsHelper(models,hms,scales,configs)
initthres = configs.initThres;
total_bbs = zeros(1e6,6);
total = 0;
nScales = length(scales);
for level=1:nScales
hms_scale = hms{level};
if isempty(hms_scale); continue; end;
current_scale = scales(level);
for model_index = 1:length(models)
currentModel = models{model_index};
chadDims = currentModel.char_dims;
hm = hms_scale{model_index} + models{model_index}.bias;
ind = find(hm > initthres); % Get the locations of the response
[y,x] = ind2sub(size(hm),ind);
if (isempty(x)); continue; end;
scores = hm(ind);
if (size(x,2) > 1); x = x'; y = y'; scores = scores'; end
%Correct the position
x = x * configs.bin_size/current_scale;
y = y * configs.bin_size/current_scale;
width = floor(chadDims(2)/current_scale);
height = floor(chadDims(1)/current_scale);
if currentModel.char_index > 62
ind = strfind(configs.alphabets,currentModel.char_index);
else
ind = currentModel.char_index;
end
bbType = ones(length(x),1)*ind;
bbs = [x,y,repmat(width,length(x),1),repmat(height,...
length(x),1),scores,bbType];
current_count = size(bbs,1);
if current_count > 0
total_bbs(total+1:total+current_count,:) = bbs;
total = total + current_count;
end
end
end
bbs=total_bbs(1:total,:);
end
function bbs=getbbsHelper2(models,hms,scales,iFrame,configs)
initthres = configs.initThres;
total_bbs = zeros(1e6,6);
total = 0;
nScales = length(scales);
for level=1:nScales
hms_scale = hms{level};
if isempty(hms_scale); continue; end;
current_scale = scales(level);
for iModel = 1:length(models)
currentModel = models{iModel};
chadDims = currentModel.char_dims;
hm = hms_scale{iModel}(:,:,iFrame) + models{iModel}.bias;
ind = find(hm > initthres); % Get the locations of the response
[y,x] = ind2sub(size(hm),ind);
if (isempty(x)); continue; end;
scores = hm(ind);
if (size(x,2) > 1); x = x'; y = y'; scores = scores'; end
%Correct the position
x = x * configs.bin_size/current_scale;
y = y * configs.bin_size/current_scale;
width = floor(chadDims(2)/current_scale);
height = floor(chadDims(1)/current_scale);
if currentModel.char_index > 62
ind = strfind(configs.alphabets,currentModel.char_index);
else
ind = currentModel.char_index;
end
bbType = ones(length(x),1)*ind;
bbs = [x,y,repmat(width,length(x),1),repmat(height,...
length(x),1),scores,bbType];
current_count = size(bbs,1);
if current_count > 0
total_bbs(total+1:total+current_count,:) = bbs;
total = total + current_count;
end
end
end
bbs=total_bbs(1:total,:);
end