|
| 1 | +function [] = sample() |
| 2 | + |
| 3 | +% finding path where all files in the program are stored |
| 4 | +%[path,name,ext] = fileparts(mfilename('C:\Users\Lenovo\Desktop\Fall-Detection-master\videos')); |
| 5 | + |
| 6 | +% reading the video |
| 7 | +%camera=ipcam('http://192.168.43.1:8080/video'); |
| 8 | +%webcamlist |
| 9 | +%while true |
| 10 | + %picture=camera.snapshot; |
| 11 | + % image(picture); |
| 12 | + %drawnow; |
| 13 | +%end |
| 14 | + |
| 15 | +%vid = webcam; |
| 16 | +%vid = vision.VideoFileReader('video.mp4'); |
| 17 | + |
| 18 | +% initializing foreground and blob detectors |
| 19 | +detector = vision.ForegroundDetector(... |
| 20 | + 'NumTrainingFrames',10,'NumGaussians',5,... |
| 21 | + 'MinimumBackgroundRatio', 0.7,'InitialVariance',0.05,... |
| 22 | + 'LearningRate',0.0002); |
| 23 | +blob = vision.BlobAnalysis(... |
| 24 | + 'CentroidOutputPort', true, 'AreaOutputPort', true, ... |
| 25 | + 'BoundingBoxOutputPort', true, ... |
| 26 | + 'MinimumBlobAreaSource', 'Property', 'MinimumBlobArea', 500); |
| 27 | + |
| 28 | +%duration of mhi (or) max. value of mhi (or) no of frames taken into acc. for mhi |
| 29 | +tmhi = 15; |
| 30 | + |
| 31 | +%strel parameters |
| 32 | +strelType = 'square'; |
| 33 | +strelSize = 5; |
| 34 | + |
| 35 | +%tolerance while find coordinate of y at min. x in counding ellipse |
| 36 | +%tolerance for yxmin <= Cy+1 in degrees |
| 37 | +noiseYxmin = 3; |
| 38 | + |
| 39 | +%resize Parameter (fraction of input frame) |
| 40 | +resizeFactor = 0.25; |
| 41 | + |
| 42 | +% Fall Detection Parameters |
| 43 | +%threshold for detecting high motion |
| 44 | +thresMotion = 1.8; |
| 45 | +%threshold speed for concluding fall |
| 46 | +thSpeed = 2; |
| 47 | +%threshold orientation change for concluding fall |
| 48 | +thOrChg = 15; |
| 49 | +%threshold area change for concluding fall |
| 50 | +thAreaChg = 15; |
| 51 | +%no of frames that form a fall sequence |
| 52 | +noFrFallSeq = 5; |
| 53 | + |
| 54 | +%object that contains possible fall sequences |
| 55 | +%object contains ->speed , ->noFrames, ->avgOrChg, ->avgAreaChg |
| 56 | +%noFrames - no. of frames that have been taken into acc. till now |
| 57 | +posFalls = struct([]); |
| 58 | + |
| 59 | +prevOr = []; |
| 60 | + |
| 61 | +frameNo = 0; |
| 62 | +vidobj = imaq.VideoDevice('winvideo', 1); |
| 63 | +%while ~isDone(camera) |
| 64 | +while true |
| 65 | + pause(0.1); |
| 66 | + frame = step(vidobj); |
| 67 | + %frame=snapshot(vid); |
| 68 | + %frame = step(camera); |
| 69 | + frameNo = frameNo+1; |
| 70 | + %resizing original frame |
| 71 | + %frame = imresize(frame,resizeFactor); |
| 72 | + |
| 73 | + %assigning initial value to motion history image |
| 74 | + if frameNo == 1 |
| 75 | + mhimage = zeros(size(frame,1),size(frame,2)); |
| 76 | + end |
| 77 | + |
| 78 | + %detecting foreground mask |
| 79 | + fgMask = step(detector,frame); |
| 80 | + %modifying mask to close gaps and fill holes |
| 81 | + fgClose = modifyMask(fgMask,strelType,strelSize); |
| 82 | + |
| 83 | + %finding largest blob |
| 84 | + [area,centroid,box] = step(blob,fgClose); |
| 85 | + pos = find(area==max(area)); |
| 86 | + box = box(pos,:); |
| 87 | + |
| 88 | + speed = [];orientation = [];area = []; |
| 89 | + if ~isempty(box) |
| 90 | + %fgBBox - the mask after inside bouding box |
| 91 | + %removing cordinates outside bounding box |
| 92 | + fgBBox = maskInsideBBox(fgClose,box); |
| 93 | + [mhimage,speed] = calcSpeed(mhimage,fgBBox,tmhi); |
| 94 | + if speed >= thresMotion |
| 95 | + posFalls = initializeFallObj(posFalls,size(posFalls,2)+1); |
| 96 | + end |
| 97 | + |
| 98 | + filledCannyMask = logical(edge(fgBBox,'Roberts')); |
| 99 | + [xcoord,ycoord] = coordInsideMask(filledCannyMask,box); |
| 100 | + ellipse = fitellipse(xcoord,ycoord); |
| 101 | + if ~isempty(ellipse) |
| 102 | + orientation = calcOrientation(ellipse,noiseYxmin); |
| 103 | + area = pi*ellipse(3)*ellipse(4); |
| 104 | + |
| 105 | + %output |
| 106 | + subplot(2,3,1); |
| 107 | + imshow(frame); |
| 108 | + title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20); |
| 109 | + subplot(2,3,3); |
| 110 | + imshow(fgMask); |
| 111 | + title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20); |
| 112 | + subplot(2,3,4); |
| 113 | + imshow(uint8((mhimage*255)/tmhi)); |
| 114 | + title(sprintf('Motion History Image\nSpeed - %f',speed),'FontSize',20); |
| 115 | + subplot(2,3,6); |
| 116 | + imshow(filledCannyMask); |
| 117 | + drawEllipse(ellipse(1),ellipse(2),ellipse(3),ellipse(4),ellipse(5)); |
| 118 | + title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20); |
| 119 | + else |
| 120 | + %output |
| 121 | + subplot(2,3,1); |
| 122 | + imshow(frame); |
| 123 | + title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20); |
| 124 | + subplot(2,3,3); |
| 125 | + imshow(fgMask); |
| 126 | + title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20); |
| 127 | + subplot(2,3,4); |
| 128 | + imshow(uint8((mhimage*255)/tmhi)); |
| 129 | + title(sprintf('Motion History Image\nSpeed - '),'FontSize',20); |
| 130 | + subplot(2,3,6); |
| 131 | + imshow(filledCannyMask); |
| 132 | + title(sprintf('Shape of body\nOrientation - ')); |
| 133 | + end |
| 134 | + else |
| 135 | + %output |
| 136 | + subplot(2,3,1); |
| 137 | + imshow(frame); |
| 138 | + title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20); |
| 139 | + subplot(2,3,3); |
| 140 | + imshow(fgMask); |
| 141 | + title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20); |
| 142 | + subplot(2,3,4); |
| 143 | + imshow(uint8((mhimage*255)/tmhi)); |
| 144 | + title(sprintf('Motion History Image\nSpeed - '),'FontSize',20); |
| 145 | + subplot(2,3,6); |
| 146 | + imshow(zeros(size(fgMask))); |
| 147 | + title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20); |
| 148 | + end |
| 149 | + |
| 150 | + %no possible fall sequences |
| 151 | + if isempty(posFalls) |
| 152 | + if ~isempty(orientation) && ~isempty(area) |
| 153 | + prevOr = orientation; |
| 154 | + prevArea = area; |
| 155 | + else |
| 156 | + %do not increment last changed frame number |
| 157 | + end |
| 158 | + continue; |
| 159 | + end |
| 160 | + |
| 161 | + %no object is found in foreground mask |
| 162 | + if isempty(speed) || isempty(orientation) || isempty(area) |
| 163 | + % speed,orientation change, area change have to assigned a certain |
| 164 | + % value |
| 165 | + posFalls = struct([]); |
| 166 | + %do not increment last changed frame number |
| 167 | + continue; |
| 168 | + end |
| 169 | + |
| 170 | + if isempty(prevOr) |
| 171 | + orChg = 0; |
| 172 | + areaChg = 0; |
| 173 | + else |
| 174 | + orChg = findOrChg(orientation,prevOr); |
| 175 | + areaChg = 20;%have to find it. |
| 176 | + end |
| 177 | + prevOr = orientation; |
| 178 | + prevArea = area; |
| 179 | + [fallDetected,posFalls] = updateCheckPosFalls(posFalls,size(posFalls,2),noFrFallSeq,orChg,areaChg,speed,thOrChg,thAreaChg,thSpeed); |
| 180 | + if fallDetected == true |
| 181 | + sgtitle(sprintf('FALL DETECTED'),'FontSize',30); |
| 182 | + subplot(2,3,1); |
| 183 | + imshow(frame); |
| 184 | + title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20); |
| 185 | + subplot(2,3,3); |
| 186 | + imshow(fgMask); |
| 187 | + title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20); |
| 188 | + subplot(2,3,4); |
| 189 | + imshow(uint8((mhimage*255)/tmhi)); |
| 190 | + title(sprintf('Motion History Image\nSpeed - %f',speed),'FontSize',20); |
| 191 | + subplot(2,3,6); |
| 192 | + imshow(filledCannyMask); |
| 193 | + title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20); |
| 194 | + sendemail(); %send email alert |
| 195 | + pause(4); |
| 196 | + sgtitle(''); |
| 197 | + end |
| 198 | +end |
| 199 | + |
0 commit comments