def main(args):

    net = args.input_size
    anno_file = 'wider_face_train.txt'
    #   im_dir = 'head_data/images'

    save_dir = 'native_' + str(net)
    pos_save_dir = save_dir + '/positive'
    part_save_dir = save_dir + '/part'
    neg_save_dir = save_dir + '/negative'
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    if not os.path.exists(pos_save_dir):
        os.mkdir(pos_save_dir)
    if not os.path.exists(part_save_dir):
        os.mkdir(part_save_dir)
    if not os.path.exists(neg_save_dir):
        os.mkdir(neg_save_dir)

    f1 = open(os.path.join(save_dir, 'pos_' + str(net) + '.txt'), 'w')
    f2 = open(os.path.join(save_dir, 'neg_' + str(net) + '.txt'), 'w')
    f3 = open(os.path.join(save_dir, 'part_' + str(net) + '.txt'), 'w')
    with open(anno_file, 'r') as f:
        annotations = f.readlines()
    num = len(annotations)
    print('%d pics in total' % num)
    p_idx = 0  # positive
    n_idx = 0  # negative
    d_idx = 0  # dont care
    idx = 0
    box_idx = 0
    for annotation in annotations:
        annotation = annotation.strip().split(' ')
        im_path = annotation[0]
        bbox = list(map(float, annotation[1:]))
        boxes = np.array(bbox, dtype=np.float32).reshape(-1, 4)
        img = cv2.imread(os.path.join(im_path))
        idx += 1
        if idx % 10000 == 0:
            print(idx, 'images done')

        height, width, channel = img.shape

        neg_num = 0
        while neg_num < 50:
            size = npr.randint(40, min(width, height) / 2)
            nx = npr.randint(0, width - size)
            ny = npr.randint(0, height - size)
            crop_box = np.array([nx, ny, nx + size, ny + size])

            Iou = IoU(crop_box, boxes)

            cropped_im = img[ny:ny + size, nx:nx + size, :]
            resized_im = cv2.resize(cropped_im, (net, net),
                                    interpolation=cv2.INTER_LINEAR)

            try:
                if np.max(Iou) < 0.3:
                    save_file = os.path.join(neg_save_dir, '%s.jpg' % n_idx)
                    f2.write(save_dir + '/negative/%s' % n_idx + ' 0\n')
                    cv2.imwrite(save_file, resized_im)
                    n_idx += 1
                    neg_num += 1
            except:
                print(im_path)
                print("crop_box:", crop_box)
                print("boxes:", boxes)
                print("Iou:", Iou)
                exit(-1)
            print('%s images done, pos: %s part: %s neg: %s' %
                  (idx, p_idx, d_idx, n_idx))

        for box in boxes:
            x1, y1, x2, y2 = box
            w = x2 - x1 + 1
            h = y2 - y1 + 1

            if max(w, h) < 40 or x1 < 0 or y1 < 0:
                continue

            for i in range(20):
                size = npr.randint(int(min(w, h) * 0.8),
                                   np.ceil(1.25 * max(w, h)))

                delta_x = npr.randint(-w * 0.2, w * 0.2)
                delta_y = npr.randint(-h * 0.2, h * 0.2)

                nx1 = int(max(x1 + w / 2 + delta_x - size / 2, 0))
                ny1 = int(max(y1 + h / 2 + delta_y - size / 2, 0))
                nx2 = nx1 + size
                ny2 = ny1 + size

                if nx2 > width or ny2 > height:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])

                offset_x1 = (x1 - nx1) / float(size)
                offset_y1 = (y1 - ny1) / float(size)
                offset_x2 = (x2 - nx2) / float(size)
                offset_y2 = (y2 - ny2) / float(size)

                cropped_im = img[ny1:ny2, nx1:nx2, :]
                resized_im = cv2.resize(cropped_im, (net, net),
                                        interpolation=cv2.INTER_LINEAR)

                box_ = box.reshape(1, -1)
                if IoU(crop_box, box_) >= 0.65:
                    save_file = os.path.join(pos_save_dir, '%s.jpg' % p_idx)
                    f1.write(save_dir + '/positive/%s' % p_idx +
                             ' 1 %.2f %.2f %.2f %.2f\n' %
                             (offset_x1, offset_y1, offset_x2, offset_y2))
                    cv2.imwrite(save_file, resized_im)
                    p_idx += 1
                elif IoU(crop_box, box_) >= 0.4:
                    save_file = os.path.join(part_save_dir, '%s.jpg' % d_idx)
                    f3.write(save_dir + '/part/%s' % d_idx +
                             ' -1 %.2f %.2f %.2f %.2f\n' %
                             (offset_x1, offset_y1, offset_x2, offset_y2))
                    cv2.imwrite(save_file, resized_im)
                    d_idx += 1
            box_idx += 1
            print('%s images done, pos: %s part: %s neg: %s' %
                  (idx, p_idx, d_idx, n_idx))

    f1.close()
    f2.close()
    f3.close()
Ejemplo n.º 2
0
def main(args):

    image_size = 48
    save_dir = str(image_size)
    anno_file = 'wider_face_train.txt'
    im_dir = 'WIDER_train/images/'
    neg_save_dir = save_dir+'/negative'
    pos_save_dir = save_dir+'/positive'
    part_save_dir = save_dir+'/part'
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    if not os.path.exists(pos_save_dir):
        os.mkdir(pos_save_dir)
    if not os.path.exists(part_save_dir):
        os.mkdir(part_save_dir)
    if not os.path.exists(neg_save_dir):
        os.mkdir(neg_save_dir)

    f1 = open(save_dir+'/pos_48.txt', 'w')
    f2 = open(save_dir+'/neg_48.txt', 'w')
    f3 = open(save_dir+'/part_48.txt', 'w')
    threshold = [0.6, 0.6]
    with open(anno_file, 'r') as f:
        annotations = f.readlines()
    num = len(annotations)
    print('%d pics in total' % num)

    p_idx = 0  # positive
    n_idx = 0  # negative
    d_idx = 0  # dont care
    image_idx = 0
    with tf.device('/gpu:0'):
        minsize = 20
        factor = 0.709
        model_file_pnet = args.pnet_model
        model_file_rnet = args.rnet_model
        with tf.Graph().as_default():
            config = tf.ConfigProto(allow_soft_placement=True)
            config.gpu_options.per_process_gpu_memory_fraction = 0.5
            config.gpu_options.allow_growth = True
            with tf.Session(config=config) as sess:
                image_pnet = tf.placeholder(tf.float32, [None, None, None, 3])
                pnet = PNet({'data': image_pnet}, mode='test')
                out_tensor_pnet = pnet.get_all_output()
                image_rnet = tf.placeholder(tf.float32, [None, 24, 24, 3])
                rnet = RNet({'data': image_rnet}, mode='test')
                out_tensor_rnet = rnet.get_all_output()

                saver_pnet = tf.train.Saver([v for v in tf.global_variables()
                                             if v.name[0:4] == 'pnet'])
                saver_rnet = tf.train.Saver([v for v in tf.global_variables()
                                             if v.name[0:4] == 'rnet'])
                saver_pnet.restore(sess, model_file_pnet)
                saver_rnet.restore(sess, model_file_rnet)

                def pnet_fun(img): return sess.run(
                    out_tensor_pnet, feed_dict={image_pnet: img})

                def rnet_fun(img): return sess.run(
                    out_tensor_rnet, feed_dict={image_rnet: img})

                for annotation in annotations:
                    annotation = annotation.strip().split(' ')
                    bbox = list(map(float, annotation[1:]))
                    gts = np.array(bbox, dtype=np.float32).reshape(-1, 4)
                    #img_path = im_dir + annotation[0] + '.jpg'
                    img_path = annotation[0]
                    img = cv2.imread(img_path)
                    rectangles = detect_face_24net(img, minsize,
                                                   pnet_fun, rnet_fun,
                                                   threshold, factor)
                    image_idx += 1
                    view_bar(image_idx, num)
                    for box in rectangles:
                        lis = box.astype(np.int32)
                        mask = lis < 0
                        lis[mask] = 0
                        x_left, y_top, x_right, y_bottom, _ = lis
                        crop_w = x_right - x_left + 1
                        crop_h = y_bottom - y_top + 1
                        # ignore box that is too small or beyond image border
                        if crop_w < image_size or crop_h < image_size:
                            continue

                        Iou = IoU(box, gts)
                        cropped_im = img[y_top: y_bottom+1, x_left: x_right+1]
                        resized_im = cv2.resize(cropped_im,
                                                (image_size, image_size),
                                                interpolation=cv2.INTER_LINEAR)

                        # save negative images and write label
                        if np.max(Iou) < 0.3:
                            # Iou with all gts must below 0.3
                            save_file = os.path.join(neg_save_dir,
                                                     '%s.jpg' % n_idx)
                            f2.write('%s/negative/%s' %
                                     (image_size, n_idx) + ' 0\n')
                            cv2.imwrite(save_file, resized_im)
                            n_idx += 1
                        else:
                            # find gt_box with the highest iou
                            idx = np.argmax(Iou)
                            assigned_gt = gts[idx]
                            x1, y1, x2, y2 = assigned_gt

                            # compute bbox reg label
                            offset_x1 = (x1 - x_left) / float(crop_w)
                            offset_y1 = (y1 - y_top) / float(crop_h)
                            offset_x2 = (x2 - x_right) / float(crop_w)
                            offset_y2 = (y2 - y_bottom) / float(crop_h)

                            if np.max(Iou) >= 0.65:
                                save_file = os.path.join(pos_save_dir,
                                                         '%s.jpg' % p_idx)
                                f1.write('%s/positive/%s' %
                                         (image_size, p_idx) +
                                         ' 1 %.2f %.2f %.2f %.2f\n' %
                                         (offset_x1, offset_y1,
                                          offset_x2, offset_y2))
                                cv2.imwrite(save_file, resized_im)
                                p_idx += 1

                            elif np.max(Iou) >= 0.4:
                                save_file = os.path.join(part_save_dir,
                                                         '%s.jpg' % d_idx)
                                f3.write('%s/part/%s' % (image_size, d_idx) +
                                         ' -1 %.2f %.2f %.2f %.2f\n' %
                                         (offset_x1, offset_y1,
                                          offset_x2, offset_y2))
                                cv2.imwrite(save_file, resized_im)
                                d_idx += 1

    f1.close()
    f2.close()
    f3.close()
def main(annotation_fp, image_dir, model_fp, output_dir):
    image_size = 24
    save_dir = os.path.join(output_dir, str(image_size))

    neg_save_dir = save_dir + '/negative'
    pos_save_dir = save_dir + '/positive'
    part_save_dir = save_dir + '/part'
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    if not os.path.exists(pos_save_dir):
        os.mkdir(pos_save_dir)
    if not os.path.exists(part_save_dir):
        os.mkdir(part_save_dir)
    if not os.path.exists(neg_save_dir):
        os.mkdir(neg_save_dir)

    f1 = open(save_dir + '/pos_24.txt', 'w')
    f2 = open(save_dir + '/neg_24.txt', 'w')
    f3 = open(save_dir + '/part_24.txt', 'w')
    threshold = 0.6
    with open(annotation_fp, 'r') as f:
        annotations = f.readlines()
    num = len(annotations)
    print('%d pics in total' % num)

    p_idx = 0  # positive
    n_idx = 0  # negative
    d_idx = 0  # dont care
    image_idx = 0
    with tf.device('/gpu:0'):
        minsize = 20
        factor = 0.709
        with tf.Graph().as_default():
            config = tf.ConfigProto(allow_soft_placement=True)
            config.gpu_options.per_process_gpu_memory_fraction = 0.5
            with tf.Session(config=config) as sess:
                image = tf.placeholder(tf.float32, [None, None, None, 3])
                pnet = PNet({'data': image}, mode='test')
                out_tensor = pnet.get_all_output()
                init_op = tf.global_variables_initializer()
                sess.run(init_op)
                saver = tf.train.Saver()
                saver.restore(sess, model_fp)

                def pnet_fun(img):
                    return sess.run(out_tensor, feed_dict={image: img})

                for annotation in annotations:
                    annotation = annotation.strip().split(' ')
                    bbox = list(map(float, annotation[1:5]))
                    gts = np.array(bbox, dtype=np.float32).reshape(-1, 4)
                    img_path = os.path.join(image_dir, annotation[0])
                    img = cv2.imread(img_path)
                    rectangles = detect_face_12net(img, minsize, pnet_fun,
                                                   threshold, factor)
                    image_idx += 1

                    view_bar(image_idx, num)
                    for box in rectangles:
                        lis = box.astype(np.int32)
                        mask = lis < 0
                        lis[mask] = 0
                        x_left, y_top, x_right, y_bottom, _ = lis
                        crop_w = x_right - x_left + 1
                        crop_h = y_bottom - y_top + 1
                        # ignore box that is too small or beyond image border
                        if crop_w < image_size or crop_h < image_size:
                            continue

                        iou = IoU(box, gts)
                        cropped_im = img[y_top:y_bottom + 1,
                                         x_left:x_right + 1]
                        resized_im = cv2.resize(cropped_im,
                                                (image_size, image_size),
                                                interpolation=cv2.INTER_LINEAR)

                        # save negative images and write label
                        if np.max(iou) < 0.3:
                            filename = str(n_idx) + '.jpg'
                            # Iou with all gts must below 0.3
                            save_file = os.path.join(neg_save_dir, filename)
                            f2.write(
                                os.path.join(neg_save_dir, filename) + ' 0\n')
                            cv2.imwrite(save_file, resized_im)
                            n_idx += 1
                        else:
                            # find gt_box with the highest iou
                            idx = np.argmax(iou)
                            assigned_gt = gts[idx]
                            x1, y1, x2, y2 = assigned_gt

                            # compute bbox reg label
                            offset_x1 = (x1 - x_left) / float(crop_w)
                            offset_y1 = (y1 - y_top) / float(crop_h)
                            offset_x2 = (x2 - x_right) / float(crop_w)
                            offset_y2 = (y2 - y_bottom) / float(crop_h)

                            if np.max(iou) >= 0.65:
                                filename = str(p_idx) + '.jpg'
                                save_file = os.path.join(
                                    pos_save_dir, filename)
                                f1.write(
                                    os.path.join(pos_save_dir, filename) +
                                    ' 1 %.2f %.2f %.2f %.2f\n' %
                                    (offset_x1, offset_y1, offset_x2,
                                     offset_y2))
                                cv2.imwrite(save_file, resized_im)
                                p_idx += 1

                            elif np.max(iou) >= 0.4:
                                filename = str(d_idx) + '.jpg'
                                save_file = os.path.join(
                                    part_save_dir, filename)
                                f3.write(
                                    os.path.join(part_save_dir, filename) +
                                    ' -1 %.2f %.2f %.2f %.2f\n' %
                                    (offset_x1, offset_y1, offset_x2,
                                     offset_y2))
                                cv2.imwrite(save_file, resized_im)
                                d_idx += 1

    f1.close()
    f2.close()
    f3.close()