From 9f3ad52a8cf175a12d45460789cbb409973692bd Mon Sep 17 00:00:00 2001 From: wuweizhen Date: Fri, 30 Mar 2018 12:30:56 +0800 Subject: [PATCH 1/2] fixed bugs --- util/align_face.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/util/align_face.py b/util/align_face.py index 6646e58..08d4fe8 100644 --- a/util/align_face.py +++ b/util/align_face.py @@ -302,10 +302,10 @@ def align(self, imgDim, rgbImg, bb=None, pad=None, ts=None, dlib_rects = [] for (x,y,w,h) in faces: dlib_rects.append(dlib.rectangle(int(x), int(y), int(x+w), int(y+h))) - if len(faces) > 0: - bb = max(dlib_rects, key=lambda rect: rect.width() * rect.height()) - else: - bb = None + if len(faces) > 0: + bb = max(dlib_rects, key=lambda rect: rect.width() * rect.height()) + else: + bb = None else: bb = self.getLargestFaceBoundingBox(rgbImg) if bb is None: @@ -324,15 +324,15 @@ def align(self, imgDim, rgbImg, bb=None, pad=None, ts=None, npLandmarkIndices = np.array(landmarkIndices) dstLandmarks = imgDim * MINMAX_TEMPLATE[npLandmarkIndices] - if ts is not None: - # reserve more area of forehead on a face - dstLandmarks[(0,1),1] = dstLandmarks[(0,1),1] + imgDim * float(ts) - dstLandmarks[2,1] = dstLandmarks[2,1] + imgDim * float(ts) / 2 if not only_crop: + if ts is not None: + # reserve more area of forehead on a face + dstLandmarks[(0,1),1] = dstLandmarks[(0,1),1] + imgDim * float(ts) + dstLandmarks[2,1] = dstLandmarks[2,1] + imgDim * float(ts) / 2 H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],dstLandmarks) return cv2.warpAffine(rgbImg, H, (imgDim, imgDim)) else: - return rgbImg[top:bottom, left:right] # crop is rgbImg[y: y + h, x: x + w] + return rgbImg[bb.top():bb.bottom(), bb.left():bb.right()] # crop is rgbImg[y: y + h, x: x + w] def write(vals, fName): From 790176c4e293c168754cb791ce429ce248a868de Mon Sep 17 00:00:00 2001 From: wuweizhen Date: Fri, 30 Mar 2018 13:24:29 +0800 Subject: [PATCH 2/2] fixed bugs --- util/align_face.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/util/align_face.py b/util/align_face.py index 08d4fe8..b823d4a 100644 --- a/util/align_face.py +++ b/util/align_face.py @@ -313,10 +313,13 @@ def align(self, imgDim, rgbImg, bb=None, pad=None, ts=None, if pad is not None: left = int(max(0, bb.left() - bb.width()*float(pad[0]))) top = int(max(0, bb.top() - bb.height()*float(pad[1]))) - right = int(min(rgbImg.shape[1], bb.right() + bb.width()*float(pad[2]))) - bottom = int(min(rgbImg.shape[0], bb.bottom()+bb.height()*float(pad[3]))) + right = int(min(rgbImg.shape[1]-1, bb.right() + bb.width()*float(pad[2]))) + bottom = int(min(rgbImg.shape[0]-1, bb.bottom()+bb.height()*float(pad[3]))) bb = dlib.rectangle(left, top, right, bottom) + if only_crop: + return rgbImg[bb.top():bb.bottom(), bb.left():bb.right()] + # crop is rgbImg[y: y + h, x: x + w] if landmarks is None: landmarks = self.findLandmarks(rgbImg, bb) @@ -324,15 +327,12 @@ def align(self, imgDim, rgbImg, bb=None, pad=None, ts=None, npLandmarkIndices = np.array(landmarkIndices) dstLandmarks = imgDim * MINMAX_TEMPLATE[npLandmarkIndices] - if not only_crop: - if ts is not None: - # reserve more area of forehead on a face - dstLandmarks[(0,1),1] = dstLandmarks[(0,1),1] + imgDim * float(ts) - dstLandmarks[2,1] = dstLandmarks[2,1] + imgDim * float(ts) / 2 - H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],dstLandmarks) - return cv2.warpAffine(rgbImg, H, (imgDim, imgDim)) - else: - return rgbImg[bb.top():bb.bottom(), bb.left():bb.right()] # crop is rgbImg[y: y + h, x: x + w] + if ts is not None: + # reserve more area of forehead on a face + dstLandmarks[(0,1),1] = dstLandmarks[(0,1),1] + imgDim * float(ts) + dstLandmarks[2,1] = dstLandmarks[2,1] + imgDim * float(ts) / 2 + H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],dstLandmarks) + return cv2.warpAffine(rgbImg, H, (imgDim, imgDim)) def write(vals, fName):