예제 #1
0
    def detect_Onet(self, onet_detector, img, bounding_box):
        """
        used for detect_Onet 

        Parameters:
        ----------
            img : numpy.array
            onet_detector : class detector 
            bounding_box : numpy.array, shape(n,4 )
            
        Returns:
        -------
            score_box : numpy.array, shape(n,1 )
            pnet_box : numpy.array, shape(n,4 )
            landmark_box : numpy.array, shape(n,10 )
        
            detect_Onet
        """
        scale_img = np.zeros((len(bounding_box), 48, 48, 3))
        for idx, box in enumerate(bounding_box):
            scale_img[idx, :, :, :] = cv2.resize(
                img[int(box[1]):int(box[3]),
                    int(box[0]):int(box[2]), :], (48, 48))

        score_boxes, delta_boxes, landmark_boxes = onet_detector.predict(
            scale_img)
        idx = np.where(score_boxes[:, 1] > self.threshold[1])[0]

        if (len(idx) == 0):
            return [], [], []
        delta_boxes = delta_boxes[idx]
        bounding_box = bounding_box[idx]
        score_boxes = np.expand_dims(score_boxes[idx, 1], 1)
        bounding_box = np.hstack([bounding_box, score_boxes])
        landmark_boxes = landmark_boxes[idx]

        idx = NMS(bounding_box, 0.6, "Minimum")
        bounding_box = bounding_box[idx]
        delta_boxes = delta_boxes[idx]
        landmark_boxes = landmark_boxes[idx]

        NMS_box = np.hstack([bounding_box, delta_boxes, landmark_boxes])

        score_box, onet_box, landmark_box = self.calibrate_box(
            img, NMS_box, "Onet")

        return score_box, onet_box, landmark_box
예제 #2
0
    def detect_Onet(self, onet_detector, img, bounding_box):
        """
        input : detector , img , bounding_box
        output: score_box , onet_box , landmark_box
        
        format of input  :
            detector     : class detector 
            img          : np.array()
            bounding_box : list of box output from function(detect_Rnet)  -1*[r_face_x1,r_face_x2,r_face_y1,r_face_y2]
            
        format of output : 
            score_box    : list of score                  -1*[score]
            onet_box     : list of box after calibration  -1*[o_face_x1,o_face_x2,o_face_y1,o_face_y2]
            landmark_box : list of landmark               -1*[5*(o_landmark_x,o_landmark_y)]
        """
        score_box = []
        landmark_box = []

        scale_img = np.zeros((len(bounding_box), 48, 48, 3))

        for idx, box in enumerate(bounding_box):
            scale_img[idx, :, :, :] = cv2.resize(
                img[int(box[1]):int(box[3]),
                    int(box[0]):int(box[2]), :], (48, 48))

        bounding_boxes = onet_detector.predict(scale_img,
                                               scale=1,
                                               img_size=48,
                                               stride=8,
                                               threshold=self.threshold[2],
                                               boxes=bounding_box)

        NMS_box = NMS(bounding_boxes, 0.6)

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

        score_box, onet_box, landmark_box = self.calibrate_box(img, NMS_box)

        return score_box, onet_box, landmark_box
예제 #3
0
    def detect_Rnet(self, rnet_detector, img, bounding_box):
        """
        input : detector , img , bounding_box
        output: score_box , rnet_box , None
        
        format of input  :
            detector     : class detector 
            img          : np.array()
            bounding_box : list of box output from function(detect_Pnet)  -1*[p_face_x1,p_face_x2,p_face_y1,p_face_y2]
            
        format of output : 
            score_box : list of score                  -1*[score]
            rnet_box  : list of box after calibration  -1*[r_face_x1,r_face_x2,r_face_y1,r_face_y2]
        """
        score_box = []
        scale_img = np.zeros((len(bounding_box), 24, 24, 3))

        for idx, box in enumerate(bounding_box):
            scale_img[idx, :, :, :] = cv2.resize(
                img[int(box[1]):int(box[3]),
                    int(box[0]):int(box[2]), :], (24, 24))

        bounding_boxes = rnet_detector.predict(scale_img,
                                               scale=1,
                                               img_size=24,
                                               stride=4,
                                               threshold=self.threshold[1],
                                               boxes=bounding_box)

        NMS_box = NMS(bounding_boxes, 0.6)

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

        score_box, rnet_box, _ = self.calibrate_box(img, NMS_box)

        return score_box, rnet_box, None
예제 #4
0
def main():
    saver = tf.train.import_meta_graph(graph_path)

    f1 = open(os.path.join(save_dir, 'pos_%d.txt' % (img_size)), 'w')
    f2 = open(os.path.join(save_dir, 'neg_%d.txt' % (img_size)), 'w')
    f3 = open(os.path.join(save_dir, 'par_%d.txt' % (img_size)), 'w')

    with tf.Session() as sess:

        saver.restore(sess, model_path)
        graph = tf.get_default_graph()

        with open(WIDER_spilt_dir) as filenames:
            p = 0
            idx = 0
            neg_idx = 0
            pos_idx = 0
            par_idx = 0
            for line in filenames.readlines():
                line = line.strip().split(' ')
                if (p == 0):
                    pic_dir = line[0]
                    p = 1
                    boxes = []
                elif (p == 1):
                    k = int(line[0])
                    p = 2
                elif (p == 2):
                    b = []
                    k = k - 1
                    if (k == 0):
                        p = 0
                    for i in range(4):
                        b.append(int(line[i]))
                    boxes.append(b)
                    # format of boxes is [x,y,w,h]
                    if (p == 0):
                        img = cv2.imread(
                            os.path.join(WIDER_dir,
                                         pic_dir).replace('/', '\\'))
                        h, w, c = img.shape
                        print(pic_dir)
                        if (min(h, w) < 20):
                            continue

                        scales = []
                        total_box = []
                        pro = map_shape / min_face_size
                        small = min(img.shape[0:2]) * pro

                        while small >= 12:
                            scales.append(pro)
                            pro *= factor
                            small *= factor

                        for scale in scales:

                            scale_img = cv2.resize(
                                img, ((int(img.shape[1] * scale)),
                                      (int(img.shape[0] * scale))))
                            bounding_boxes = featuremap(
                                sess, graph, scale_img, scale, map_shape,
                                stride, threshold)

                            if (bounding_boxes):
                                for box in bounding_boxes:
                                    total_box.append(box)

                        NMS_box = NMS(total_box, 0.7)
                        neg_num = 0
                        for box_ in NMS_box:

                            box = box_.copy()
                            if ((box[0] < 0) | (box[1] < 0) | (box[2] > w) |
                                (box[3] > h) |
                                (box[2] - box[0] <= min_face_size) |
                                (box[3] - box[1] <= min_face_size)):
                                continue
                            # format of total_box: [x1,y1,x2,y2,score,offset_x1,offset_y1,offset_x2,offset_y2,10*landmark]

                            t_box = [0] * 4
                            t_w = box[2] - box[0] + 1
                            t_h = box[3] - box[1] + 1

                            t_box[0] = box[5] * t_w + box[0]
                            t_box[1] = box[6] * t_h + box[1]
                            t_box[2] = box[7] * t_w + box[2]
                            t_box[3] = box[8] * t_h + box[3]
                            # calculate ground truth predict-face boxes

                            if ((t_box[0] < 0) | (t_box[1] < 0) |
                                (t_box[2] > w) | (t_box[3] > h) |
                                (t_box[2] - t_box[0] <= min_face_size) |
                                (t_box[3] - t_box[1] <= min_face_size)):
                                continue

                            ti_box = t_box.copy()
                            ti_box = [int(_) for _ in ti_box]

                            Iou = IoU(np.array(t_box), np.array(boxes))

                            if ((np.max(Iou) < 0.3) & (neg_num < 60)):
                                resized_img = cv2.resize(
                                    img[ti_box[1]:ti_box[3],
                                        ti_box[0]:ti_box[2], :],
                                    (img_size, img_size))
                                cv2.imwrite(
                                    os.path.join(negative_dir,
                                                 'neg_%d.jpg' % (neg_idx)),
                                    resized_img)
                                f2.write(
                                    os.path.join(negative_dir, 'neg_%d.jpg' %
                                                 (neg_idx)) + ' 0\n')
                                neg_idx = neg_idx + 1
                                neg_num = neg_num + 1

                            else:
                                x1, y1, w1, h1 = boxes[np.argmax(Iou)]

                                offset_x1 = (x1 - t_box[0]
                                             ) / float(t_box[2] - t_box[0] + 1)
                                offset_y1 = (y1 - t_box[1]
                                             ) / float(t_box[3] - t_box[1] + 1)
                                offset_x2 = (x1 + w1 - t_box[2]
                                             ) / float(t_box[2] - t_box[0] + 1)
                                offset_y2 = (y1 + h1 - t_box[3]
                                             ) / float(t_box[3] - t_box[1] + 1)

                                if (np.max(Iou) > 0.65):
                                    resized_img = cv2.resize(
                                        img[ti_box[1]:ti_box[3],
                                            ti_box[0]:ti_box[2], :],
                                        (img_size, img_size))
                                    cv2.imwrite(
                                        os.path.join(positive_dir,
                                                     'pos_%d.jpg' % (pos_idx)),
                                        resized_img)
                                    f1.write(
                                        os.path.join(positive_dir,
                                                     'pos_%d.jpg' %
                                                     (pos_idx)) +
                                        ' 1 %.2f %.2f %.2f %.2f\n' %
                                        (offset_x1, offset_y1, offset_x2,
                                         offset_y2))
                                    pos_idx = pos_idx + 1

                                elif (np.max(Iou) > 0.4):
                                    resized_img = cv2.resize(
                                        img[ti_box[1]:ti_box[3],
                                            ti_box[0]:ti_box[2], :],
                                        (img_size, img_size))
                                    cv2.imwrite(
                                        os.path.join(par_dir,
                                                     'par_%d.jpg' % (par_idx)),
                                        resized_img)
                                    f3.write(
                                        os.path.join(par_dir, 'par_%d.jpg' %
                                                     (par_idx)) +
                                        ' -1 %.2f %.2f %.2f %.2f\n' %
                                        (offset_x1, offset_y1, offset_x2,
                                         offset_y2))
                                    par_idx = par_idx + 1
                        idx += 1
                        if (idx % 100 == 0):
                            print('idx: ', idx, " ;neg_idx: ", neg_idx,
                                  " ;pos_idx: ", pos_idx, " ;par_idx: ",
                                  par_idx)
                            print(time.time() - begin)
        print(
            "pics all done,neg_pics %d in total,pos_pics %d in total,par_pics %d in total"
            % (neg_idx, pos_idx, par_idx))

    f1.close()
    f2.close()
    f3.close()
예제 #5
0
    def detect_Pnet(self, pnet_detector, img):
        """
        used for detect_Pnet 

        Parameters:
        ----------
            img : numpy.array
            pnet_detector : class detector 

            
        Returns:
        -------
            score_box : numpy.array, shape(n,1 )
            pnet_box : numpy.array, shape(n,4 )
            []
        
            detect_Pnet
        """
        factor = self.factor
        pro = 12 / self.min_face_size
        scales = []
        total_box = []
        small = min(img.shape[0:2]) * pro

        while small >= 12:
            scales.append(pro)
            pro *= factor
            small *= factor
        p = 0
        for scale in scales:

            crop_img = img
            scale_img = cv2.resize(crop_img,
                                   ((int(crop_img.shape[1] * scale)),
                                    (int(crop_img.shape[0] * scale))))
            scale_img1 = np.reshape(scale_img,
                                    (-1, scale_img.shape[0],
                                     scale_img.shape[1], scale_img.shape[2]))

            score_boxes, delta_boxes, _ = pnet_detector.predict(scale_img1)

            bounding_boxes = self.generate_box(score_box=score_boxes[:, :, 1],
                                               bounding_box=delta_boxes,
                                               img_size=12,
                                               scale=scale,
                                               stride=2,
                                               threshold=self.threshold[0])

            a = time.time()
            if (len(bounding_boxes) != 0):
                idx = NMS(bounding_boxes[:, 0:5], 0.5)
                NMS_bounding_boxes = bounding_boxes[idx]
                total_box.append(NMS_bounding_boxes)
            p += time.time() - a
        a = time.time()
        if (len(total_box) == 0):
            return [], [], []
        total_box = np.vstack(total_box)
        idx = NMS(total_box, 0.7)
        NMS_box = total_box[idx]
        p += time.time() - a
        #print("NMS: ",p)

        score_box, pnet_box, _ = self.calibrate_box(img, NMS_box)

        return score_box, pnet_box, []
                while small >= 20:
                    scales.append(pro)
                    pro *= factor
                    small *= factor

                #方式1
                P_graph = tf.Graph()
                with P_graph.as_default():
                    P_saver = tf.train.import_meta_graph(P_graph_path)
                    with tf.Session(graph=P_graph) as sess:
                        P_saver.restore(sess, P_model_path)
                        bounding_boxes = featuremap1(sess, P_graph, img,
                                                     scales, P_map_shape, 2,
                                                     threshold)

                P_NMS_box = NMS(bounding_boxes, 0.7)
                P_net_box = []
                for box_ in P_NMS_box:

                    box = box_.copy()
                    if ((box[0] < 0) | (box[1] < 0) | (box[2] > w) |
                        (box[3] > h) | (box[2] - box[0] <= 20) |
                        (box[3] - box[1] <= 20)):
                        continue
                    # format of total_box: [x1,y1,x2,y2,score,offset_x1,offset_y1,offset_x2,offset_y2,10*landmark]

                    t_box = [0] * 4
                    t_w = box[2] - box[0] + 1
                    t_h = box[3] - box[1] + 1

                    t_box[0] = box[5] * t_w + box[0]