|
| 1 | +""" Hej |
| 2 | +""" |
| 3 | +import numpy as np |
| 4 | +import cv2 |
| 5 | +from math import floor |
| 6 | +#from scipy import misc |
| 7 | +import scipy.io as sio |
| 8 | + |
| 9 | +def detect_face(img, minsize, pnet, rnet, onet, threshold, factor): |
| 10 | + # im: input image |
| 11 | + # minsize: minimum of faces' size |
| 12 | + # pnet, rnet, onet: caffemodel |
| 13 | + # threshold: threshold=[th1 th2 th3], th1-3 are three steps's threshold |
| 14 | + # fastresize: resize img from last scale (using in high-resolution images) if fastresize==true |
| 15 | + factor_count=0 |
| 16 | + total_boxes=np.empty((0,9)) |
| 17 | + points=[] |
| 18 | + h=img.shape[0] |
| 19 | + w=img.shape[1] |
| 20 | + minl=np.amin([h, w]) |
| 21 | + #img=single(img); |
| 22 | +# if fastresize |
| 23 | +# im_data=(single(img)-127.5)*0.0078125; |
| 24 | +# end |
| 25 | + m=12.0/minsize |
| 26 | + minl=minl*m |
| 27 | + # creat scale pyramid |
| 28 | + scales=[] |
| 29 | + while minl>=12: |
| 30 | + scales += [m*np.power(factor, factor_count)] |
| 31 | + minl = minl*factor |
| 32 | + factor_count += 1 |
| 33 | + |
| 34 | + # first stage |
| 35 | + #for j = 1:size(scales,2) |
| 36 | + for j in range(len(scales)): |
| 37 | + scale=scales[j] |
| 38 | + hs=int(np.ceil(h*scale)) |
| 39 | + ws=int(np.ceil(w*scale)) |
| 40 | + #im_data=(imResample(img,[hs ws],'bilinear')-127.5)*0.0078125; |
| 41 | + #im_data = (misc.imresize(img, (hs, ws), interp='bilinear')-127.5)*0.0078125 |
| 42 | + #im_data = (cv2.resize(img, (hs, ws), interpolation=cv2.INTER_LINEAR)-127.5)*0.0078125 |
| 43 | + #im_data = (cv2.resize(img, (hs, ws), interpolation=cv2.INTER_NEAREST)-127.5)*0.0078125 |
| 44 | + #im_data = (img[0:hs,0:ws,:]-127.5)*0.0078125 |
| 45 | + im_data = imResample2(img, (hs, ws), 'bilinear') |
| 46 | + im_data = (im_data-127.5)*0.0078125 |
| 47 | + |
| 48 | + # PNet.blobs('data').reshape([hs ws 3 1]); |
| 49 | + # out=PNet.forward({im_data}); |
| 50 | + |
| 51 | + img_x = np.expand_dims(im_data, 0) |
| 52 | + img_y = np.transpose(img_x, (0,2,1,3)) |
| 53 | + out = pnet(img_y) |
| 54 | + out0 = np.transpose(out[0], (0,2,1,3)) |
| 55 | + #out0 = out[0] |
| 56 | + out1 = np.transpose(out[1], (0,2,1,3)) |
| 57 | + #out1 = out[1] |
| 58 | + |
| 59 | + #boxes=generateBoundingBox(out{2}(:,:,2), out{1}, scale, threshold[0]); |
| 60 | + boxes, reg = generateBoundingBox(out1[0,:,:,1].copy(), out0[0,:,:,:].copy(), scale, threshold[0]) |
| 61 | + |
| 62 | + # inter-scale nms |
| 63 | + pick = nms(boxes.copy(), 0.5, 'Union') |
| 64 | + boxes = boxes[pick,:] # boxes=boxes(pick,:); |
| 65 | + if boxes.size>0: # if ~isempty(boxes) |
| 66 | + total_boxes = np.append(total_boxes, boxes, axis=0) # total_boxes=[total_boxes;boxes]; |
| 67 | + |
| 68 | + numbox = total_boxes.shape[0] # numbox=size(total_boxes,1); |
| 69 | + if numbox>0: # if ~isempty(total_boxes) |
| 70 | + pick = nms(total_boxes.copy(), 0.7, 'Union') # pick=nms(total_boxes,0.7,'Union'); |
| 71 | + total_boxes = total_boxes[pick,:] # total_boxes=total_boxes(pick,:); |
| 72 | + regw = total_boxes[:,2]-total_boxes[:,0] # regw=total_boxes(:,3)-total_boxes(:,1); |
| 73 | + regh = total_boxes[:,3]-total_boxes[:,1] # regh=total_boxes(:,4)-total_boxes(:,2); |
| 74 | + # total_boxes=[total_boxes(:,1)+total_boxes(:,6).*regw total_boxes(:,2)+total_boxes(:,7).*regh total_boxes(:,3)+total_boxes(:,8).*regw total_boxes(:,4)+total_boxes(:,9).*regh total_boxes(:,5)]; |
| 75 | + qq1 = total_boxes[:,0]+total_boxes[:,5]*regw |
| 76 | + qq2 = total_boxes[:,1]+total_boxes[:,6]*regh |
| 77 | + qq3 = total_boxes[:,2]+total_boxes[:,7]*regw |
| 78 | + qq4 = total_boxes[:,3]+total_boxes[:,8]*regh |
| 79 | + total_boxes = np.transpose(np.vstack([qq1, qq2, qq3, qq4, total_boxes[:,4]])) |
| 80 | + total_boxes = rerec(total_boxes.copy()) # total_boxes=rerec(total_boxes); |
| 81 | + total_boxes[:,0:4] = np.fix(total_boxes[:,0:4]) # total_boxes(:,1:4)=fix(total_boxes(:,1:4)); |
| 82 | + dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph = pad(total_boxes.copy(), w, h) |
| 83 | + |
| 84 | + numbox = total_boxes.shape[0] # numbox=size(total_boxes,1); |
| 85 | + if numbox>0: |
| 86 | + # second stage |
| 87 | + tempimg = np.zeros((24,24,3,numbox)) # tempimg=zeros(24,24,3,numbox); |
| 88 | + for k in range(0,numbox): # for k=1:numbox |
| 89 | + tmp = np.zeros((tmph[k],tmpw[k],3)) # tmp=zeros(tmph(k),tmpw(k),3); |
| 90 | + tmp[dy[k]-1:edy[k],dx[k]-1:edx[k],:] = img[y[k]-1:ey[k],x[k]-1:ex[k],:] # tmp(dy(k):edy(k),dx(k):edx(k),:)=img(y(k):ey(k),x(k):ex(k),:); |
| 91 | + if tmp.shape[0]>0 and tmp.shape[1]>0 or tmp.shape[0]==0 and tmp.shape[1]==0: # if size(tmp,1)>0 && size(tmp,2)>0 || size(tmp,1)==0 && size(tmp,2)==0 |
| 92 | + tempimg[:,:,:,k] = imResample2(tmp, (24, 24), 'bilinear') # tempimg(:,:,:,k)=imResample(tmp,[24 24],'bilinear'); |
| 93 | + else: |
| 94 | + return np.empty() # total_boxes = []; return; |
| 95 | + tempimg = (tempimg-127.5)*0.0078125 # tempimg=(tempimg-127.5)*0.0078125; |
| 96 | + # RNet.blobs('data').reshape([24 24 3 numbox]); |
| 97 | + # out=RNet.forward({tempimg}); |
| 98 | + tempimg1 = np.transpose(tempimg, (3,1,0,2)) |
| 99 | + out = rnet(tempimg1) |
| 100 | + out0 = np.transpose(out[0]) |
| 101 | + out1 = np.transpose(out[1]) |
| 102 | + score = out1[1,:] # score=squeeze(out{2}(2,:)); |
| 103 | + ipass = np.where(score>threshold[1]) # pass=find(score>threshold(2)); |
| 104 | + total_boxes = np.hstack([total_boxes[ipass[0],0:4].copy(), np.expand_dims(score[ipass].copy(),1)]) # total_boxes=[total_boxes(pass,1:4) score(pass)']; |
| 105 | + mv = out0[:,ipass[0]] # mv=out{1}(:,pass); |
| 106 | + if total_boxes.shape[0]>0: # if size(total_boxes,1)>0 |
| 107 | + pick = nms(total_boxes, 0.7, 'Union') # pick=nms(total_boxes,0.7,'Union'); |
| 108 | + total_boxes = total_boxes[pick,:] # total_boxes=total_boxes(pick,:); |
| 109 | + total_boxes = bbreg(total_boxes.copy(), np.transpose(mv[:,pick])) # total_boxes=bbreg(total_boxes,mv(:,pick)'); |
| 110 | + total_boxes = rerec(total_boxes.copy()) # total_boxes=rerec(total_boxes); |
| 111 | + |
| 112 | + numbox = total_boxes.shape[0] # numbox=size(total_boxes,1); |
| 113 | + if numbox>0: |
| 114 | + # third stage |
| 115 | + total_boxes=np.fix(total_boxes) # total_boxes=fix(total_boxes); |
| 116 | + dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph = pad(total_boxes.copy(), w, h) # [dy edy dx edx y ey x ex tmpw tmph]=pad(total_boxes,w,h); |
| 117 | + tempimg = np.zeros((48,48,3,numbox)) # tempimg=zeros(48,48,3,numbox); |
| 118 | + for k in range(0,numbox): # for k=1:numbox |
| 119 | + tmp = np.zeros((tmph[k],tmpw[k],3))# tmp=zeros(tmph(k),tmpw(k),3); |
| 120 | + tmp[dy[k]-1:edy[k],dx[k]-1:edx[k],:] = img[y[k]-1:ey[k],x[k]-1:ex[k],:] # tmp(dy(k):edy(k),dx(k):edx(k),:)=img(y(k):ey(k),x(k):ex(k),:); |
| 121 | + if tmp.shape[0]>0 and tmp.shape[1]>0 or tmp.shape[0]==0 and tmp.shape[1]==0: # if size(tmp,1)>0 && size(tmp,2)>0 || size(tmp,1)==0 && size(tmp,2)==0 |
| 122 | + tempimg[:,:,:,k] = imResample2(tmp, (48, 48), 'bilinear') # tempimg(:,:,:,k)=imResample(tmp,[48 48],'bilinear'); |
| 123 | + else: |
| 124 | + return np.empty() # total_boxes = []; return; |
| 125 | + tempimg = (tempimg-127.5)*0.0078125 # tempimg=(tempimg-127.5)*0.0078125; |
| 126 | + tempimg1 = np.transpose(tempimg, (3,1,0,2)) |
| 127 | + out = onet(tempimg1) # out=ONet.forward({tempimg}); |
| 128 | + out0 = np.transpose(out[0]) |
| 129 | + out1 = np.transpose(out[1]) |
| 130 | + out2 = np.transpose(out[2]) |
| 131 | + score = out2[1,:] # score=squeeze(out{3}(2,:)); |
| 132 | + points = out1 # points=out{2}; |
| 133 | + ipass = np.where(score>threshold[2]) # pass=find(score>threshold(3)); |
| 134 | + points = points[:,ipass[0]] # points=points(:,pass); |
| 135 | + total_boxes = np.hstack([total_boxes[ipass[0],0:4].copy(), np.expand_dims(score[ipass].copy(),1)]) # total_boxes=[total_boxes(pass,1:4) score(pass)']; |
| 136 | + mv = out0[:,ipass[0]] # mv=out{1}(:,pass); |
| 137 | + |
| 138 | + w = total_boxes[:,2]-total_boxes[:,0]+1 # w=total_boxes(:,3)-total_boxes(:,1)+1; |
| 139 | + h = total_boxes[:,3]-total_boxes[:,1]+1 # h=total_boxes(:,4)-total_boxes(:,2)+1; |
| 140 | + points[0:5,:] = np.tile(w,(5, 1))*points[0:5,:] + np.tile(total_boxes[:,0],(5, 1))-1 # points(1:5,:)=repmat(w',[5 1]).*points(1:5,:)+repmat(total_boxes(:,1)',[5 1])-1; |
| 141 | + points[5:10,:] = np.tile(h,(5, 1))*points[5:10,:] + np.tile(total_boxes[:,1],(5, 1))-1 # points(6:10,:)=repmat(h',[5 1]).*points(6:10,:)+repmat(total_boxes(:,2)',[5 1])-1; |
| 142 | + if total_boxes.shape[0]>0: # if size(total_boxes,1)>0 |
| 143 | + total_boxes = bbreg(total_boxes.copy(), np.transpose(mv)) # total_boxes=bbreg(total_boxes,mv(:,:)'); |
| 144 | + pick = nms(total_boxes.copy(), 0.7, 'Min') # pick=nms(total_boxes,0.7,'Min'); |
| 145 | + total_boxes = total_boxes[pick,:] # total_boxes=total_boxes(pick,:); |
| 146 | + points = points[:,pick] # points=points(:,pick); |
| 147 | + |
| 148 | + return total_boxes, points |
| 149 | + |
| 150 | + |
| 151 | +# function [boundingbox] = bbreg(boundingbox,reg) |
| 152 | +def bbreg(boundingbox,reg): |
| 153 | + # calibrate bouding boxes |
| 154 | + if reg.shape[1]==1: # if size(reg,2)==1 |
| 155 | + reg = np.reshape(reg, (reg.shape[2], reg.shape[3])) # reg=reshape(reg,[size(reg,3) size(reg,4)])'; |
| 156 | + |
| 157 | + w = boundingbox[:,2]-boundingbox[:,0]+1 # w=[boundingbox(:,3)-boundingbox(:,1)]+1; |
| 158 | + h = boundingbox[:,3]-boundingbox[:,1]+1 # h=[boundingbox(:,4)-boundingbox(:,2)]+1; |
| 159 | + b1 = boundingbox[:,0]+reg[:,0]*w |
| 160 | + b2 = boundingbox[:,1]+reg[:,1]*h |
| 161 | + b3 = boundingbox[:,2]+reg[:,2]*w |
| 162 | + b4 = boundingbox[:,3]+reg[:,3]*h |
| 163 | + boundingbox[:,0:4] = np.transpose(np.vstack([b1, b2, b3, b4 ])) # boundingbox(:,1:4)=[boundingbox(:,1)+reg(:,1).*w boundingbox(:,2)+reg(:,2).*h boundingbox(:,3)+reg(:,3).*w boundingbox(:,4)+reg(:,4).*h]; |
| 164 | + return boundingbox |
| 165 | + |
| 166 | +def generateBoundingBox(map, reg, scale, t): |
| 167 | + # use heatmap to generate bounding boxes |
| 168 | + stride=2 |
| 169 | + cellsize=12 |
| 170 | + |
| 171 | + map = np.transpose(map) # map=map'; |
| 172 | + dx1 = np.transpose(reg[:,:,0]) # dx1=reg(:,:,1)'; |
| 173 | + dy1 = np.transpose(reg[:,:,1]) # dy1=reg(:,:,2)'; |
| 174 | + dx2 = np.transpose(reg[:,:,2]) # dx2=reg(:,:,3)'; |
| 175 | + dy2 = np.transpose(reg[:,:,3]) # dy2=reg(:,:,4)'; |
| 176 | + y, x = np.where(map > t) # [y x]=find(map>=t); |
| 177 | + if y.shape[0]==1: # if size(y,1)==1 |
| 178 | + # *** not checked *** |
| 179 | +# y=y';x=x';score=map(a)';dx1=dx1';dy1=dy1';dx2=dx2';dy2=dy2'; |
| 180 | + y = np.transpose(y) |
| 181 | + x = np.transpose(x) |
| 182 | + dx1 = np.transpose(dx1) |
| 183 | + dy1 = np.transpose(dy1) |
| 184 | + dx2 = np.transpose(dx2) |
| 185 | + dy2 = np.transpose(dy2) |
| 186 | + score = map[(y,x)] |
| 187 | + else: |
| 188 | + score = map[(y,x)] |
| 189 | + |
| 190 | + reg = np.transpose(np.vstack([ dx1[(y,x)], dy1[(y,x)], dx2[(y,x)], dy2[(y,x)] ])) # reg=[dx1(a) dy1(a) dx2(a) dy2(a)]; |
| 191 | + if reg.size==0: # if isempty(reg) |
| 192 | + # *** not checked *** |
| 193 | + reg = np.empty((0,3)) # reg=reshape([],[0 3]); |
| 194 | + bb = np.transpose(np.vstack([y,x])) # boundingbox=[y x]; |
| 195 | + q1 = np.fix((stride*bb+1)/scale) |
| 196 | + q2 = np.fix((stride*bb+cellsize-1+1)/scale) |
| 197 | + boundingbox = np.hstack([q1, q2, np.expand_dims(score,1), reg]) # boundingbox=[fix((stride*(boundingbox-1)+1)/scale) fix((stride*(boundingbox-1)+cellsize-1+1)/scale) score reg]; |
| 198 | + return boundingbox, reg |
| 199 | + |
| 200 | +# function pick = nms(boxes,threshold,type) |
| 201 | +def nms(boxes,threshold,type): |
| 202 | + if boxes.size==0: # if isempty(boxes) |
| 203 | + return np.empty((0,3)) ###### check dimension ####### |
| 204 | + x1 = boxes[:,0] |
| 205 | + y1 = boxes[:,1] |
| 206 | + x2 = boxes[:,2] |
| 207 | + y2 = boxes[:,3] |
| 208 | + s = boxes[:,4] |
| 209 | + area = (x2-x1+1) * (y2-y1+1) |
| 210 | + I = np.argsort(s) # [vals, I] = sort(s); |
| 211 | + #vals = s[I] |
| 212 | + pick = np.zeros_like(s, dtype=np.int16) # s*0; |
| 213 | + counter = 0 |
| 214 | + while I.size>0: # ~isempty(I) |
| 215 | + #last = I.size |
| 216 | + i = I[-1] |
| 217 | + pick[counter] = i |
| 218 | + counter += 1 |
| 219 | + idx = I[0:-1] |
| 220 | + xx1 = np.maximum(x1[i], x1[idx]) # xx1 = max(x1(i), x1(I(1:last-1))); |
| 221 | + yy1 = np.maximum(y1[i], y1[idx]) # yy1 = max(y1(i), y1(I(1:last-1))); |
| 222 | + xx2 = np.minimum(x2[i], x2[idx]) # xx2 = min(x2(i), x2(I(1:last-1))); |
| 223 | + yy2 = np.minimum(y2[i], y2[idx]) # yy2 = min(y2(i), y2(I(1:last-1))); |
| 224 | + w = np.maximum(0.0, xx2-xx1+1) # w = max(0.0, xx2-xx1+1); |
| 225 | + h = np.maximum(0.0, yy2-yy1+1) # h = max(0.0, yy2-yy1+1); |
| 226 | + inter = w * h # inter = w.*h; |
| 227 | + if type is 'Min': # if strcmp(type,'Min') |
| 228 | + o = inter / np.minimum(area[i], area[idx]) # o = inter ./ min(area(i),area(I(1:last-1))); |
| 229 | + else: |
| 230 | + o = inter / (area[i] + area[idx] - inter) # o = inter ./ (area(i) + area(I(1:last-1)) - inter); |
| 231 | + I = I[np.where(o<=threshold)] # I = I(find(o<=threshold)); |
| 232 | + pick = pick[0:counter] # pick = pick(1:(counter-1)); |
| 233 | + return pick |
| 234 | + |
| 235 | +# function [dy edy dx edx y ey x ex tmpw tmph] = pad(total_boxes,w,h) |
| 236 | +def pad(total_boxes, w, h): |
| 237 | + # compute the padding coordinates (pad the bounding boxes to square) |
| 238 | + tmpw = total_boxes[:,2]-total_boxes[:,0]+1 # tmpw=total_boxes(:,3)-total_boxes(:,1)+1; |
| 239 | + tmph = total_boxes[:,3]-total_boxes[:,1]+1 # tmph=total_boxes(:,4)-total_boxes(:,2)+1; |
| 240 | + numbox=total_boxes.shape[0] |
| 241 | + |
| 242 | + dx = np.ones((numbox,1)) # dx=ones(numbox,1); |
| 243 | + dy = np.ones((numbox,1)) # dy=ones(numbox,1); |
| 244 | + edx=tmpw.copy() |
| 245 | + edy=tmph.copy() |
| 246 | + |
| 247 | + x = total_boxes[:,0].copy() # x=total_boxes(:,1); |
| 248 | + y = total_boxes[:,1].copy() # y=total_boxes(:,2); |
| 249 | + ex = total_boxes[:,2].copy() # ex=total_boxes(:,3); |
| 250 | + ey = total_boxes[:,3].copy() # ey=total_boxes(:,4); |
| 251 | + |
| 252 | + tmp = np.where(ex>w) # tmp=find(ex>w); |
| 253 | + edx[tmp] = np.expand_dims(-ex[tmp]+w+tmpw[tmp],1) # edx(tmp)=-ex(tmp)+w+tmpw(tmp);ex(tmp)=w; |
| 254 | + ex[tmp] = w |
| 255 | + |
| 256 | + tmp = np.where(ey>h) # tmp=find(ey>h); |
| 257 | + edy[tmp] = np.expand_dims(-ey[tmp]+h+tmph[tmp],1) # edy(tmp)=-ey(tmp)+h+tmph(tmp);ey(tmp)=h; |
| 258 | + ey[tmp] = h |
| 259 | + |
| 260 | + tmp = np.where(x<1) # tmp=find(x<1); |
| 261 | + dx[tmp] = np.expand_dims(2-x[tmp],1) # dx(tmp)=2-x(tmp); |
| 262 | + x[tmp] = 1 |
| 263 | + |
| 264 | + tmp = np.where(y<1) # tmp=find(y<1); |
| 265 | + dy[tmp] = np.expand_dims(2-y[tmp],1) # dy(tmp)=2-y(tmp);y(tmp)=1; |
| 266 | + y[tmp] = 1 |
| 267 | + |
| 268 | + return dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph |
| 269 | + |
| 270 | +# function [bboxA] = rerec(bboxA) |
| 271 | +def rerec(bboxA): |
| 272 | + # convert bboxA to square |
| 273 | + #bboxB = bboxA[:,0:4] # bboxB=bboxA(:,1:4); |
| 274 | + h = bboxA[:,3]-bboxA[:,1] # h=bboxA(:,4)-bboxA(:,2); |
| 275 | + w = bboxA[:,2]-bboxA[:,0] # w=bboxA(:,3)-bboxA(:,1); |
| 276 | + l = np.maximum(w, h) # l=max([w h]')'; |
| 277 | + bboxA[:,0] = bboxA[:,0]+w*0.5-l*0.5 # bboxA(:,1)=bboxA(:,1)+w.*0.5-l.*0.5; |
| 278 | + bboxA[:,1] = bboxA[:,1]+h*0.5-l*0.5 # bboxA(:,2)=bboxA(:,2)+h.*0.5-l.*0.5; |
| 279 | + bboxA[:,2:4] = bboxA[:,0:2] + np.transpose(np.tile(l,(2,1))) # bboxA(:,3:4)=bboxA(:,1:2)+repmat(l,[1 2]); |
| 280 | + return bboxA |
| 281 | + |
| 282 | +def imResample2(img, sz, method): |
| 283 | + h=img.shape[0] |
| 284 | + w=img.shape[1] |
| 285 | + hs, ws = sz |
| 286 | + dx = float(w) / ws |
| 287 | + dy = float(h) / hs |
| 288 | + im_data = np.zeros((hs,ws,3)) |
| 289 | + for a1 in range(0,hs): |
| 290 | + for a2 in range(0,ws): |
| 291 | + for a3 in range(0,3): |
| 292 | + im_data[a1,a2,a3] = img[floor(a1*dy),floor(a2*dx),a3] |
| 293 | + return im_data |
0 commit comments