Skip to content

Commit 3bc1871

Browse files
committed
Uploading two human detection methods(python & matlab)
0 parents  commit 3bc1871

940 files changed

Lines changed: 59089 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Matlab/CaptureVideo.m

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
2+
3+
4+
%% Initialization
5+
% initialize the video writer for saving original video
6+
OriVideo = VideoWriter('videos/original.avi');
7+
OriVideo.FrameRate = 10;
8+
open(OriVideo);
9+
10+
% % initialize the vidoe writer for saving the detected human video
11+
% DetVideo = VideoWriter('/Users/Alex/Desktop/HumanDetection/detected.avi');
12+
% DetVideo.FrameRate = 10 ;
13+
% open(DetVideo);
14+
15+
% perameters
16+
load('models/AcfCaltech+Detector.mat')
17+
load('M.mat')
18+
NumberOfFrames = 300;
19+
20+
% get video input object
21+
vid = videoinput('macvideo',1);
22+
triggerconfig(vid, 'manual');
23+
start (vid)
24+
set(vid, 'ReturnedColorSpace', 'RGB')
25+
26+
% shape inserter for draw bbs on detected video
27+
shapeInserter = vision.ShapeInserter('BorderColor','Custom', ...
28+
'CustomBorderColor', uint8([0 255 0]), 'LineWidth', 2);
29+
30+
% % read video
31+
% xyloObj = VideoReader('/Users/Alex/Desktop/HumanDetection/test/o4_wifi.avi');
32+
33+
% creat array for saving real detected positions
34+
real_positions = {};
35+
36+
% creat struct for saving each frame detections
37+
Detections = struct(...
38+
'bx', {}, ...
39+
'by', {}, ...
40+
'xp', {}, ...
41+
'yp', {}, ...
42+
'ht', {}, ...
43+
'wd', {}, ...
44+
'sc', {}, ...
45+
'xi', {}, ...
46+
'yi', {}, ...
47+
'xw', {}, ...
48+
'yw', {});
49+
50+
%% detection and show results
51+
figure
52+
% for i = 1:xyloObj.NumberOfFrames;
53+
for i = 1:NumberOfFrames;
54+
% real time human detection on video
55+
subplot(1,2,1)
56+
% % capture frame from web camera
57+
im = getsnapshot(vid);
58+
% % capture frame from PETS dataset for multi-tracking
59+
% im = imread(['img/' sprintf('frame_%04d.jpg', i)]);
60+
% % capture frame from original video
61+
% im = read(xyloObj, i);
62+
63+
% resize image for detection speed
64+
im = imresize(im, 0.6);
65+
% write the original video into file 60% image will be loaded quicker
66+
writeVideo(OriVideo,im);
67+
% write the calibration image
68+
if i == 1;
69+
cali = imresize(im, 0.6);
70+
imwrite(cali, 'cali.jpg');
71+
end
72+
73+
im = imresize(im, 0.6);
74+
% detect bbs in each frame
75+
bbs=acfDetect(im, detector); % [x y w h]
76+
% filter the bbs with score bigger than 50
77+
indice = bbs(:,5)>45;
78+
bbs = bbs(indice, :);
79+
% show result
80+
imshow(im);
81+
map = imread('map.jpg');
82+
% show map on 2nd subplot
83+
subplot(1,2,2);
84+
imshow(map);
85+
hold on;
86+
87+
% compute human real height
88+
top = [bbs(:,1)+bbs(:,3)/2, bbs(:,2)];
89+
bot = [bbs(:,1)+bbs(:,3)/2, bbs(:,2)+bbs(:,4)];
90+
H = zeros(size(bbs,1), 1);
91+
P_bot = zeros(4,1);
92+
P_top = zeros(4,1);
93+
real_positions = zeros(size(bbs,1),2);
94+
range = zeros(size(bbs,1),1);
95+
% loop for each bb
96+
for j = 1 : size(bbs, 1)
97+
% get image position of each bounding box
98+
p_top = [top(j,:),1]';
99+
p_bot = [bot(j,:),1]';
100+
% compute real world bbs bottom position
101+
P_bot_trans = M(:,[1,2,4]) \ p_bot;% inv(M)*p_top
102+
P_bot_trans = P_bot_trans / P_bot_trans(3);
103+
P_bot([1,2,4]) = P_bot_trans;
104+
P_bot(3) = 0;
105+
% computer real world bbs top height
106+
P_top_trans = M(:,[2,3,4])\(p_top-5*M(:,1));
107+
H(j) = P_top_trans(2)/P_top_trans(3);
108+
% write true human height on video
109+
subplot(1,2,1);
110+
text(bbs(j,1),bbs(j,2)-10,['People height=' num2str(H(j))],'FontSize',10,'color','w'); %[sprintf('x=%04d', P_bot(1)) sprintf('y=%04d', P_bot(2))]
111+
% record all detections
112+
real_positions(j,:) = P_bot(1:2);
113+
% % compute whether the bbs in ground truth range or not
114+
115+
% judge1 = 0.5*bot(j,1)+bot(j,2)-352.5;
116+
% judge2 = 0.133*bot(j,1)-bot(j,2)+131;
117+
% if (judge1>0 && judge2>0)
118+
% range(j) = 1;
119+
% else
120+
% range(j) = 0;
121+
% end
122+
123+
% draw points on 2-d plane
124+
if (H(j)>1.25 && H(j)<2.4)
125+
subplot(1,2,2);
126+
p_map = [224-P_bot(2)*82/1.4, 356-P_bot(1)*82/1.4];
127+
plot(p_map(1),p_map(2),'.','MarkerSize',30);
128+
end
129+
end
130+
subplot(1,2,1);
131+
% draw the passed bbs
132+
% indice = H>0.5 & H<2.8;
133+
% bbs = bbs(indice, :);
134+
% real_positions = real_positions(indice, :);
135+
% % draw the bbs in ground truth area
136+
% indice = find(range ==1);
137+
% bbs = bbs(indice, :);
138+
bbApply('draw',bbs);
139+
pause(.05);
140+
% write detected video
141+
if ~isempty(bbs)
142+
rectangles = int32(bbs(:,1:4));
143+
% rectangles([1,2,3,4],:) = rectangles([2,1,4,3],:);
144+
DetIm = step(shapeInserter, im, rectangles); % [row column height width]
145+
else
146+
DetIm = im;
147+
end
148+
% writeVideo(DetVideo,DetIm);
149+
% Create a new detection
150+
numOfBoxes = size(bbs, 1);
151+
bx = zeros(1, numOfBoxes);
152+
by = zeros(1, numOfBoxes);
153+
xp = zeros(1, numOfBoxes);
154+
yp = zeros(1, numOfBoxes);
155+
ht = zeros(1, numOfBoxes);
156+
wd = zeros(1, numOfBoxes);
157+
sc = zeros(1, numOfBoxes);
158+
xi = zeros(1, numOfBoxes);
159+
yi = zeros(1, numOfBoxes);
160+
xw = zeros(1, numOfBoxes);
161+
yw = zeros(1, numOfBoxes);
162+
163+
for j = 1: numOfBoxes
164+
bx(j) = bbs(j, 1);
165+
by(j) = bbs(j, 2);
166+
xp(j) = real_positions(1)*1000;
167+
yp(j) = real_positions(2)*1000;
168+
ht(j) = bbs(j, 4);
169+
wd(j) = bbs(j, 3);
170+
sc(j) = bbs(j, 5);
171+
xi(j) = bbs(j, 1)+bbs(j, 3)/2;
172+
yi(j) = bbs(j, 2)+bbs(j, 4);
173+
xw(j) = xp(j);
174+
yw(j) = yp(j);
175+
176+
end
177+
178+
newDetection = struct(...
179+
'bx', bx, ... % bounding box left top x (pixel)
180+
'by', by, ... % bounding box left top y (pixel)
181+
'xp', xp, ... % bottom center real world position x (mm)
182+
'yp', yp, ... % bottom center real world position y (mm)
183+
'ht', ht, ... % bounding box height (pixel)
184+
'wd', wd, ... % bounding box width (pixel)
185+
'sc', sc, ... % score of each bounding box
186+
'xi', xi, ... % bottom center image position x (pixel)
187+
'yi', yi, ... % bottom center image position y (pixel)
188+
'xw', xw, ... % same as xp
189+
'yw', yw); % same as yp
190+
191+
% add new detection to Detections
192+
Detections(end+1) = newDetection;
193+
194+
end
195+
% % close the opened video writer
196+
close(OriVideo);
197+
% close(DetVideo);
198+
% % close camera input object
199+
stop(vid)
200+
delete(imaqfind)
201+
202+
203+
% I = imread('/Users/Alex/Desktop/HumanDetection/one.jpg');
204+
%
205+
% bbs=acfDetect(I, detector);
206+
% figure(1); im(I); bbApply('draw',bbs); pause(.1);

Matlab/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
%%
2+
% Please read this file before using the 'kalman.m' function to track
3+
% objects
4+
% 1) There are four important m files:
5+
% *--- 'calibration.m': calibrate the camera, key points must be
6+
% pointed out from bottom right, counterclockwise, base to top.
7+
% *--- 'CaptureVideo.m': capture video from web camera in real time, and
8+
% the captured video is saved in the file named 'videos' as
9+
% 'original.avi', and the first frame of the video is saved as 'cali.jpg'
10+
% for camera calibration.
11+
% *--- 'ShowCapturedVideo.m': show the detection result of any specific
12+
% video, and the input video's path is set by the parameter
13+
% 'ShowWhichVideoResult'.
14+
% *--- 'kalman.m': show the tracking result of any specific video, and
15+
% the input video's path is set by the parameter 'TrackWhichVideo'
16+
% 2) show/not show bounding box score is determined by the function 'bbApply.m'
17+
% at line 300.
18+
% 3) When calibrate the camera, just run the 'calibration.m' file and it
19+
% will use the lastest 'cali.jpg' as input image.
20+

0 commit comments

Comments
 (0)