Ejemplo n.º 1
0
 def predict(self, im, dets):
     
     h, w, c = im.shape
     dets = convert_to_square(dets, self.expand)
     dets[:, 0:4] = np.round(dets[:, 0:4])
     [dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph] = pad(dets, w, h)
     num_boxes = dets.shape[0]
     cropped_ims = np.zeros((num_boxes, self.net_size, self.net_size, 3), dtype=np.float32)
     for i in range(num_boxes):
         tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
         tmp[dy[i]:edy[i] + 1, dx[i]:edx[i] + 1, :] = im[y[i]:ey[i] + 1, x[i]:ex[i] + 1, :]
         cropped_ims[i, :, :, :] = (cv2.resize(tmp, (self.net_size, self.net_size))-127.5) / 128
         
     # print(cropped_ims.shape)
     # cropped_ims.tofile('test_img.bin')
     # cls_scores, reg, landmark, animoji = self.L_ONet.predict(cropped_ims)
     cls_scores, reg,eye_eyebrow,mouth,multi = self.L_ONet.predict(cropped_ims)
     # print(reg.shape)
     # print(reg)
     #prob belongs to face
     cls_scores = cls_scores[:,1]        
     keep_inds = np.where(cls_scores > self.threshold)[0]        
     if len(keep_inds) > 0:
         #pickout filtered box
         boxes = dets[keep_inds]
         boxes[:, 4] = cls_scores[keep_inds]
         reg = reg[keep_inds]
         # landmark = landmark[keep_inds]
         # animoji = animoji[keep_inds]
         eye_eyebrow = eye_eyebrow[keep_inds]
         mouth = mouth[keep_inds]
         multi = multi[keep_inds]
         
     else:
         # return None, None, None, None, None
         return None, None, None,None,None,None
     
     wh = boxes[:,2:4] - boxes[:,0:2] + 1
     xy = boxes[:,0:2]
     # landmark = landmark.reshape(-1,5,2)*wh[:,None,:] + xy[:,None,:]
     # animoji = animoji.reshape(-1,70,2)*wh[:,None,:] + xy[:,None,:]       
     # eye = eye.reshape(-1,18,2)*wh[:,None,:] + xy[:,None,:]       
     eye_eyebrow = eye_eyebrow.reshape(-1,36,2)*wh[:,None,:] + xy[:,None,:]       
     mouth = mouth.reshape(-1,20,2)*wh[:,None,:] + xy[:,None,:]       
     multi = multi.reshape(-1,42,2)*wh[:,None,:] + xy[:,None,:]       
     
     boxes_c = calibrate_box(boxes, reg)
     
     
     boxes = boxes[py_nms(boxes, 0.6, "Minimum")]
     keep = py_nms(boxes_c, 0.6, "Minimum")
     boxes_c = boxes_c[keep]
     # landmark = landmark[keep]
     # animoji = animoji[keep]
     # eye = eye[keep]
     eye_eyebrow = eye_eyebrow[keep]
     mouth = mouth[keep]
     multi = multi[keep]
     # return boxes, boxes_c, landmark, animoji, reg
     return boxes, boxes_c, eye_eyebrow,mouth,multi, reg
Ejemplo n.º 2
0
    def predict(self, im):
        current_scale = self.current_scale
        im_resized = self.processed_image(im, current_scale)
        current_height, current_width, _ = im_resized.shape
        # fcn
        all_boxes = list()
        # print('pnet')
        # isprint = True

        while min(current_height, current_width) > self.net_size:
            cls_cls_map, reg = self.PNet.predict(im_resized)
            boxes = self.generate_bbox(cls_cls_map[:, :, 1], reg,
                                       current_scale)

            current_scale *= self.scale_factor
            im_resized = self.processed_image(im, current_scale)
            current_height, current_width, _ = im_resized.shape
            # if isprint:
            # print(cls_cls_map.shape)
            # print(cls_cls_map)
            # print(reg.shape)
            # print(reg)
            # print(im_resized.shape)
            # print(im_resized)
            # isprint = False

            if boxes.size == 0:
                continue
            keep = py_nms(boxes[:, :5], 0.5, 'Union')
            boxes = boxes[keep]
            all_boxes.append(boxes)

        if len(all_boxes) == 0:
            return None, None, None

        all_boxes = np.vstack(all_boxes)

        # merge the detection from first stage
        keep = py_nms(all_boxes[:, 0:5], 0.7, 'Union')
        all_boxes = all_boxes[keep]
        boxes = all_boxes[:, :5]

        bbw = all_boxes[:, 2] - all_boxes[:, 0] + 1
        bbh = all_boxes[:, 3] - all_boxes[:, 1] + 1

        # refine the boxes
        boxes_c = np.vstack([
            all_boxes[:, 0] + all_boxes[:, 5] * bbw,
            all_boxes[:, 1] + all_boxes[:, 6] * bbh,
            all_boxes[:, 2] + all_boxes[:, 7] * bbw,
            all_boxes[:, 3] + all_boxes[:, 8] * bbh, all_boxes[:, 4]
        ])
        boxes_c = boxes_c.T

        return boxes, boxes_c, None
Ejemplo n.º 3
0
    def predict(self, im, dets):
        h, w, c = im.shape
        dets = convert_to_square(dets)
        dets[:, 0:4] = np.round(dets[:, 0:4])

        [dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph] = pad(dets, w, h)
        num_boxes = dets.shape[0]
        cropped_ims = np.zeros((num_boxes, self.net_size, self.net_size, 3),
                               dtype=np.float32)
        for i in range(num_boxes):
            tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
            tmp[dy[i]:edy[i] + 1, dx[i]:edx[i] + 1, :] = im[y[i]:ey[i] + 1,
                                                            x[i]:ex[i] + 1, :]
            cropped_ims[i, :, :, :] = (
                cv2.resize(tmp, (self.net_size, self.net_size)) - 127.5) / 128
        cls_scores, reg, _, _ = self.RNet.predict(cropped_ims)
        cls_scores = cls_scores[:, 1]
        keep_inds = np.where(cls_scores > self.threshold)[0]
        if len(keep_inds) > 0:
            boxes = dets[keep_inds]
            boxes[:, 4] = cls_scores[keep_inds]
            reg = reg[keep_inds]
        else:
            return None, None, None

        keep = py_nms(boxes, 0.6)
        boxes = boxes[keep]
        boxes_c = calibrate_box(boxes, reg[keep])
        return boxes, boxes_c, None
Ejemplo n.º 4
0
    def predict(self, im, dets):

        h, w, c = im.shape
        dets = convert_to_square(dets)
        dets[:, 0:4] = np.round(dets[:, 0:4])
        [dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph] = pad(dets, w, h)
        num_boxes = dets.shape[0]
        cropped_ims = np.zeros((num_boxes, self.net_size, self.net_size, 3),
                               dtype=np.float32)
        for i in range(num_boxes):
            tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
            tmp[dy[i]:edy[i] + 1, dx[i]:edx[i] + 1, :] = im[y[i]:ey[i] + 1,
                                                            x[i]:ex[i] + 1, :]
            cropped_ims[i, :, :, :] = (
                cv2.resize(tmp, (self.net_size, self.net_size)) - 127.5) / 128

        # cls_scores, reg,landmark,_ = self.ONet.predict(cropped_ims)
        cls_scores, reg, _ = self.ONet.predict(cropped_ims)
        #prob belongs to face
        cls_scores = cls_scores[:, 1]
        keep_inds = np.where(cls_scores > self.threshold)[0]
        if len(keep_inds) > 0:
            #pickout filtered box
            boxes = dets[keep_inds]
            boxes[:, 4] = cls_scores[keep_inds]
            reg = reg[keep_inds]
            # landmark = landmark[keep_inds]
        else:
            return None, None, None, None

        wh = boxes[:, 2:4] - boxes[:, 0:2] + 1
        xy = boxes[:, 0:2]
        # landmark = landmark.reshape(-1,5,2)*wh[:,None,:] + xy[:,None,:]
        boxes_c = calibrate_box(boxes, reg)

        boxes = boxes[py_nms(boxes, 0.6, "Minimum")]
        keep = py_nms(boxes_c, 0.6, "Minimum")
        boxes_c = boxes_c[keep]
        # landmark = landmark[keep]
        # return boxes, boxes_c, landmark, None
        return boxes, boxes_c, None
Ejemplo n.º 5
0
    def predict(self, im, dets):
        threshold=0.6
        net_size = 24
    
        h, w, c = im.shape
        dets = convert_to_square(dets)
        dets[:, 0:4] = np.round(dets[:, 0:4])

        [dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph] = pad(dets, w, h)
        num_boxes = dets.shape[0]
        cropped_ims = np.zeros((num_boxes, net_size, net_size, 3), dtype=np.float32)
        for i in range(num_boxes):
            tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
            tmp[dy[i]:edy[i] + 1, dx[i]:edx[i] + 1, :] = im[y[i]:ey[i] + 1, x[i]:ex[i] + 1, :]
            cropped_ims[i, :, :, :] = (cv2.resize(tmp, (net_size, net_size))-127.5) / 128
            
            
        cls_scores = []
        reg = []
        
        for img_ in cropped_ims:
            cls_scores_i, reg_i,_ = self.forward(img_.transpose(2,0,1),None)
            cls_scores.append(cls_scores_i.reshape(-1))
            reg.append(reg_i.reshape(-1))
            
        cls_scores = np.asarray(cls_scores)
        reg = np.asarray(reg)
        cls_scores = cls_scores[:,1]
        keep_inds = np.where(cls_scores > threshold)[0]
        if len(keep_inds) > 0:
            boxes = dets[keep_inds]
            boxes[:, 4] = cls_scores[keep_inds]
            reg = reg[keep_inds]
        else:
            return None, None, None
        
        
        keep = py_nms(boxes, 0.6)
        boxes = boxes[keep]
        boxes_c = calibrate_box(boxes, reg[keep])
        return boxes, boxes_c, None
Ejemplo n.º 6
0
    def predict(self, im):
        min_face_size=25
        stride=2
        threshold=0.6
        scale_factor = 0.79

        # self.min_face_size = min_face_size
        # self.stride = stride
        # self.threshold = threshold
        # self.scale_factor = scale_factor
        # # h, w, c = im.shape
        net_size = 12
            
        current_scale = float(net_size) / min_face_size  # find initial scale
        # print("current_scale", net_size, self.min_face_size, current_scale)
    
        # current_scale = self.current_scale
        im_resized = self.processed_image(im, current_scale)
        current_height, current_width, _ = im_resized.shape
        # print('numpys')
        # print(im_resized.shape)
        # print(im_resized)
        # isprint = True
        # fcn
        all_boxes = list()
        while min(current_height, current_width) > net_size:
            cls_cls_map, reg,_ = self.forward(im_resized.transpose(2,0,1),None)
            boxes = self.generate_bbox(cls_cls_map[1, :, :], reg, current_scale)

            current_scale *= scale_factor
            im_resized = self.processed_image(im, current_scale)
            current_height, current_width, _ = im_resized.shape
            # if isprint:
                # print(cls_cls_map.shape)
                # print(cls_cls_map.transpose(1,2,0))
                # print(reg.shape)
                # print(reg.transpose(1,2,0))
                # print(im_resized.shape)
                # print(im_resized)
                # isprint = False
                
            if boxes.size == 0:
                continue
            keep = py_nms(boxes[:, :5], 0.5, 'Union')
            boxes = boxes[keep]
            all_boxes.append(boxes)

        if len(all_boxes) == 0:
            return None, None, None

        all_boxes = np.vstack(all_boxes)

        # merge the detection from first stage
        keep = py_nms(all_boxes[:, 0:5], 0.7, 'Union')
        all_boxes = all_boxes[keep]
        boxes = all_boxes[:, :5]

        bbw = all_boxes[:, 2] - all_boxes[:, 0] + 1
        bbh = all_boxes[:, 3] - all_boxes[:, 1] + 1

        # refine the boxes
        boxes_c = np.vstack([all_boxes[:, 0] + all_boxes[:, 5] * bbw,
                             all_boxes[:, 1] + all_boxes[:, 6] * bbh,
                             all_boxes[:, 2] + all_boxes[:, 7] * bbw,
                             all_boxes[:, 3] + all_boxes[:, 8] * bbh,
                             all_boxes[:, 4]])
        boxes_c = boxes_c.T

        return boxes, boxes_c, None
Ejemplo n.º 7
0
 def predict(self, im, dets_):
     dets = dets_.copy()
     net_size = 48
     threshold = 0.7
     expand = 0.3
     
     h, w, c = im.shape
     dets = convert_to_square(dets, expand)
     dets[:, 0:4] = np.round(dets[:, 0:4])
     [dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph] = pad(dets, w, h)
     num_boxes = dets.shape[0]
     cropped_ims = np.zeros((num_boxes, net_size, net_size, 3), dtype=np.float32)
     for i in range(num_boxes):
         tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
         tmp[dy[i]:edy[i] + 1, dx[i]:edx[i] + 1, :] = im[y[i]:ey[i] + 1, x[i]:ex[i] + 1, :]
         cropped_ims[i, :, :, :] = (cv2.resize(tmp, (net_size, net_size))-127.5) / 128
     
     cls_scores = []
     reg = []
     landmark = []
     animoji = []
     
     for img_ in cropped_ims:
         cls_scores_i, reg_i,landmark_i, animoji_i = self.forward(img_.transpose(2,0,1),None)
         cls_scores.append(cls_scores_i.reshape(-1))
         reg.append(reg_i.reshape(-1))
         landmark.append(landmark_i.reshape(-1))
         animoji.append(animoji_i.reshape(-1))
         
     cls_scores = np.asarray(cls_scores)
     reg = np.asarray(reg)
     landmark = np.asarray(landmark)
     animoji = np.asarray(animoji)
     # print(reg.shape)
     # print(reg)
     # f.close()
     #prob belongs to face
     cls_scores = cls_scores[:,1]        
     keep_inds = np.where(cls_scores > threshold)[0]        
     if len(keep_inds) > 0:
         #pickout filtered box
         boxes = dets[keep_inds]
         boxes[:, 4] = cls_scores[keep_inds]
         reg = reg[keep_inds]
         landmark = landmark[keep_inds]
         animoji = animoji[keep_inds]
     else:
         return None, None, None
     
     wh = boxes[:,2:4] - boxes[:,0:2] + 1
     xy = boxes[:,0:2]
     landmark = landmark.reshape(-1,5,2)*wh[:,None,:] + xy[:,None,:]  
     animoji = animoji.reshape(-1,70,2)*wh[:,None,:] + xy[:,None,:]         
     boxes_c = calibrate_box(boxes, reg)
     
     
     boxes = boxes[py_nms(boxes, 0.6, "Minimum")]
     keep = py_nms(boxes_c, 0.6, "Minimum")
     boxes_c = boxes_c[keep]
     landmark = landmark[keep]
     
     return boxes, boxes_c, landmark, animoji,reg