def main(nsplits,
         split_id,
         list_file,
         img_root_dir,
         mtcnn_model_dir,
         save_dir=None,
         rects_fn=None):
    if not save_dir:
        save_dir = './facescrub_mtcnn_aligned'

    if not osp.exists(save_dir):
        print('mkdir for aligned root dir: ', save_dir)
        os.makedirs(save_dir)

    save_aligned_dir = osp.join(save_dir, 'aligned_imgs')
    if not osp.exists(save_aligned_dir):
        print('mkdir for aligned/cropped face imgs: ', save_dir)
        os.makedirs(save_aligned_dir)

    save_rects_dir = osp.join(save_dir, 'face_rects')
    if not osp.exists(save_rects_dir):
        print('mkdir for face rects/landmarks: ', save_rects_dir)
        os.makedirs(save_rects_dir)

    aligner = FaceAligner(mtcnn_model_dir)

    #fp = open(list_file, 'r')
    #all_lines = fp.readlines()
    #fp.close()

    rects_list = load_rect_list(rects_fn)
    all_lines = rects_list

    total_line_cnt = len(all_lines)
    print('--->%d imgs in total' % total_line_cnt)

    if nsplits < 2:
        if split_id > 0:
            print('===> Will only process first %d imgs' % split_id)
            start_line = 0
            end_line = split_id
        else:
            print('===> Will process all of the images')
            start_line = 0
            end_line = total_line_cnt
    else:
        assert (split_id < nsplits)
        lines_per_split = float(total_line_cnt) / nsplits
        start_line = int(lines_per_split * split_id)
        end_line = int(lines_per_split * (split_id + 1))
        if end_line + 1 >= total_line_cnt:
            end_line = total_line_cnt

        print('===> Will only process imgs in the range [%d, %d)]' %
              (start_line, end_line))

    count = start_line

    fp_log = open(osp.join(save_dir, 'missing_imgs_split_%d.txt' % split_id),
                  'w')

    for line in all_lines[start_line:end_line]:
        #line = line.strip()
        print count

        count = count + 1
        img_fn = osp.join(img_root_dir, line['image'])

        print('===> Processing img: ' + img_fn)
        img = cv2.imread(img_fn)
        if img is None:
            print 'falied to read image: ', img_fn
            fp_log.write(img_fn + '\n')
            continue

        ht = img.shape[0]
        wd = img.shape[1]

        print 'image.shape:', img.shape

        spl = osp.split(line['image'])
        #sub_dir = osp.split(spl[0])[1]
        sub_dir = spl[0]
        print 'sub_dir: ', sub_dir

        if CHINESE_2_PINYIN:
            sub_dir = pinyin.get(sub_dir, format="strip")
            # replace the dot sign in names
            sub_dir = sub_dir.replace(u'\xb7', '-').encode('utf-8')

        base_name = osp.splitext(spl[1])[0]

        save_img_subdir = osp.join(save_aligned_dir, sub_dir)
        if not osp.exists(save_img_subdir):
            os.mkdir(save_img_subdir)

        save_rect_subdir = osp.join(save_rects_dir, sub_dir)
        if not osp.exists(save_rect_subdir):
            os.mkdir(save_rect_subdir)
        # print pts

        save_rects_fn = osp.join(save_rect_subdir, base_name + '.txt')
        fp_rect = open(save_rects_fn, 'w')

        #rect = get_rects_for_image(rects_list, base_name)
        rect = line['pts']
        # boxes, points = aligner.align_face(img, [rect])
        boxes, points = aligner.align_face(img, [rect])
        nfaces = len(boxes)
        fp_rect.write('%d\n' % nfaces)

        for i in range(nfaces):
            box = boxes[i]
            pts = points[i]

            if i:
                save_img_fn = osp.join(save_img_subdir,
                                       base_name + '_%d.jpg' % (i + 1))
            else:
                save_img_fn = osp.join(save_img_subdir, base_name + '.jpg')

            facial5points = np.reshape(pts, (2, -1))
            # dst_img = warp_and_crop_face(
            #     img, facial5points, reference_5pts, output_size)
            dst_img = aligner.get_face_chips(img, [box], [pts])[0]

            cv2.imwrite(save_img_fn, dst_img)
            print 'aligend face saved into: ', save_img_fn

            for it in box:
                fp_rect.write('%5.2f\t' % it)
            fp_rect.write('\n')

            for i in range(5):
                fp_rect.write('%5.2f\t%5.2f\n' %
                              (facial5points[0][i], facial5points[1][i]))

        fp_rect.close()
    fp_log.close()
def main(nsplits,
         split_id,
         list_file,
         img_root_dir,
         mtcnn_model_dir,
         save_dir=None):
    if not save_dir:
        save_dir = './aligned_root_dir'

    if not osp.exists(save_dir):
        print('mkdir for aligned root dir: ', save_dir)
        os.makedirs(save_dir)

    save_aligned_dir = osp.join(save_dir, 'aligned_imgs')
    if not osp.exists(save_aligned_dir):
        print('mkdir for aligned/cropped face imgs: ', save_dir)
        os.makedirs(save_aligned_dir)

    save_rects_dir = osp.join(save_dir, 'face_rects')
    if not osp.exists(save_rects_dir):
        print('mkdir for face rects/landmarks: ', save_rects_dir)
        os.makedirs(save_rects_dir)

    # aligner = MtcnnAligner(mtcnn_model_dir, False)
    aligner = FaceAligner(mtcnn_model_dir, False)

    fp = open(list_file, 'r')
    all_lines = fp.readlines()
    fp.close()

    total_line_cnt = len(all_lines)
    print('--->%d imgs in total' % total_line_cnt)

    if nsplits < 2:
        if split_id > 0:
            print('===> Will only process first %d imgs' % split_id)
            start_line = 0
            end_line = split_id
        else:
            print('===> Will process all of the images')
            start_line = 0
            end_line = total_line_cnt
    else:
        assert (split_id < nsplits)
        lines_per_split = float(total_line_cnt) / nsplits
        start_line = int(lines_per_split * split_id)
        end_line = int(lines_per_split * (split_id + 1))
        if end_line + 1 >= total_line_cnt:
            end_line = total_line_cnt

        print('===> Will only process imgs in the range [%d, %d)]' %
              (start_line, end_line))

    count = start_line

    for line in all_lines[start_line:end_line]:
        line = line.strip()
        print count

        count = count + 1
        img_fn = osp.join(img_root_dir, line)

        print('===> Processing img: ' + img_fn)
        img = cv2.imread(img_fn)
        ht = img.shape[0]
        wd = img.shape[1]

        print 'image.shape:', img.shape
        # GT_RECT = [0,0,img.shape[0],img.shape[1]]
        GT_RECT = [
            int(wd * 0.25),
            int(ht * 0.25),
            int(wd * 0.75),
            int(ht * 0.72)
        ]

        # print 'face rect: ', gt
        # boxes, points = aligner.align_face(img, [GT_RECT])
        boxes, points = aligner.align_face(img, [rect])

        box = boxes[0]
        pts = points[0]

        spl = osp.split(line)
        sub_dir = spl[0]
        base_name = spl[1]

        save_img_subdir = osp.join(save_aligned_dir, sub_dir)
        if not osp.exists(save_img_subdir):
            os.mkdir(save_img_subdir)

        save_rect_subdir = osp.join(save_rects_dir, sub_dir)
        if not osp.exists(save_rect_subdir):
            os.mkdir(save_rect_subdir)
        # print pts

        save_img_fn = osp.join(save_img_subdir, base_name)

        facial5points = np.reshape(pts, (2, -1))
        # dst_img = warp_and_crop_face(
        #     img, facial5points, reference_5pts, output_size)
        dst_img = aligner.get_face_chips(img, [box], [pts])[0]
        cv2.imwrite(save_img_fn, dst_img)

        save_rect_fn = osp.join(save_rect_subdir,
                                osp.splitext(base_name)[0] + '.txt')
        fp_rect = open(save_rect_fn, 'w')
        for it in box:
            fp_rect.write('%5.2f\t' % it)
        fp_rect.write('\n')

        for i in range(5):
            fp_rect.write('%5.2f\t%5.2f\n' %
                          (facial5points[0][i], facial5points[1][i]))
        fp_rect.close()
def main(img_list_file, root_dir, mtcnn_model_dir, save_dir=None):
    if not save_dir:
        save_dir = './aligned_images'

    if not osp.exists(save_dir):
        print('mkdir for aligned faces, aligned root dir: ', save_dir)
        os.makedirs(save_dir)

    aligned_save_dir = osp.join(save_dir, 'aligned_faces')
    if not osp.exists(aligned_save_dir):
        print('mkdir for aligned faces, aligned images dir: ',
              aligned_save_dir)
        os.makedirs(aligned_save_dir)

    #aligner = MtcnnAligner(mtcnn_model_dir, False)
    aligner = FaceAligner(mtcnn_model_dir)

    fp = open(img_list_file, 'r')

    fn_rlt = osp.join(save_dir, 'fd_rlt.json')
    fp_rlt = open(fn_rlt, 'w')
    fp_rlt.write('[\n')

    count = 0
    for line in fp:
        print line
        line_split = line.split()

        img_fn = line_split[0]
        id_num = line_split[1]

        img_fn_split = img_fn.split('/')

        img_fn = osp.join(root_dir, img_fn)

        print 'process image: ', img_fn, " id_num: ", id_num
        #for root,dirs,files in path_walk:
        err_msg = ''

        if not count:
            fp_rlt.write(',\n')

        count = count + 1
        print 'count: ', count

        overlap_thresh_0 = overlap_thresh

        save_subdir = osp.join(aligned_save_dir, img_fn_split[-2])
        save_img_fn = osp.join(save_subdir, img_fn_split[-1])

        if not osp.exists(save_subdir):
            os.makedirs(save_subdir)

        image = cv2.imread(img_fn)

        print image.shape
        boxes, points = aligner.align_face(image, [GT_RECT])

        box = boxes[0]
        pts = points[0]

        facial5points = np.reshape(points, (2, -1))
        #dst_img = warp_and_crop_face(image, facial5points, reference_5pts, output_size)
        dst_img = aligner.get_face_chips(image, [box], [pts])[0]
        cv2.imwrite(save_img_fn, dst_img)

        item = {}

        tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts, 'id': id_num}
        item['faces'] = tmp
        #item['id'] = data[u'url'].line_splitit('/')[-3]
        item['shape'] = image.shape
        json_str = json.dumps(item, indent=2)

        fp_rlt.write(json_str + '\n')
        fp_rlt.flush()

    fp_rlt.write(']\n')
    fp_rlt.close()
    fp.close()
Esempio n. 4
0
def main(json_file, save_dir=None, save_img=True, show_img=True):
    if not osp.exists(json_file):
        print 'Cannot find json file: ' + json_file
        pass

    if save_dir is None:
        save_dir = './fa_facex_rlt'

    save_json = 'mtcnn_align_rlt.json'
    model_path = "../../model"

    fp_json = open(json_file, 'r')
    facex_response = json.load(fp_json)
    fp_json.close()

    if (not facex_response or not isinstance(facex_response, dict)
            or 'facex_det' not in facex_response):
        print 'Invalid json file: ' + json_file
        pass

    facex_det_response = facex_response['facex_det']

    if not osp.exists(save_dir):
        os.makedirs(save_dir)

    fp_rlt = open(osp.join(save_dir, save_json), 'w')
    results = []

    for item in facex_det_response:
        img_path = item['name']
        print '===> Processing image: ' + img_path

        if 'detections' not in item:
            continue

        face_rects = []
        for face in item['detections']:
            face_rects.append(face['pts'])

        img = cv2.imread(img_path)

        aligner = FaceAligner(model_path, False)

        rlt = {}
        rlt["filename"] = img_path
        rlt["faces"] = []
        rlt['face_count'] = 0

        t1 = time.clock()
        bboxes, points = aligner.align_face(img, face_rects)
        t2 = time.clock()

        n_boxes = len(face_rects)
        print(
            "-->Alignment cost %f seconds, processed %d face rects, avg time: %f seconds"
            % ((t2 - t1), n_boxes, (t2 - t1) / n_boxes))

        if bboxes is not None and len(bboxes) > 0:
            for (box, pts) in zip(bboxes, points):
                #                box = box.tolist()
                #                pts = pts.tolist()
                tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts}
                rlt['faces'].append(tmp)

        rlt['face_count'] = len(bboxes)

        rlt['message'] = 'success'
        results.append(rlt)

        spl = osp.split(img_path)
        sub_dir = osp.split(spl[0])[1]
        base_name = spl[1]

        save_img_subdir = osp.join(save_dir, sub_dir)
        if not osp.exists(save_img_subdir):
            os.mkdir(save_img_subdir)


#        save_rect_subdir = osp.join(save_dir, sub_dir)
#        if not osp.exists(save_rect_subdir):
#            os.mkdir(save_rect_subdir)
# print pts

        save_img_fn = osp.join(save_img_subdir, base_name)
        print 'save face chip into ', save_img_fn

        # facial5points = np.reshape(pts, (2, -1))
        # dst_img = warp_and_crop_face(
        #     img, facial5points, reference_5pts, output_size)
        dst_img = aligner.get_face_chips(img, [box], [pts], True)[0]
        cv2.imwrite(save_img_fn, dst_img)

    json.dump(results, fp_rlt, indent=2)
    fp_rlt.close()