Beispiel #1
0
def test_fer2013_data():
    train_image, train_label, test_image, test_label = fdata.load_kaggle_face_data()

    for i, mat in enumerate(train_image):
        cv2.imshow("mat", mat)
        cv2.waitKey(0)

        landmark = fapi.get_feature_points_fromimage(mat)

        if landmark is None:
            print "人脸识别出错"
        else:
            print "调用face++ api成功"

        add_landmark(landmark)
        '''
        这部分查看一下人脸的特征变形,这里需要计算人脸剖分的方案,需要覆盖全图
        '''
        point_dict = from_lanmark_to_points(landmark)
        triangles = trianglulation(point_dict)
        # draw_triangle(mat, triangles, point_dict)

        mask = draw_mask(triangles, point_dict, mat.shape)
        img = np.array(mask * mat, dtype=np.uint8)
        img = cv2.equalizeHist(img)
        cv2.imshow("mask", img)
        cv2.imshow("mat", mat)
        cv2.waitKey(0)
Beispiel #2
0
def test_face_trianglulation(mat):
    mat = cv2.resize(mat, (400, 400))

    landmark = fapi.get_feature_points_fromimage(mat)
    # 这里为了能够更好的提取人脸,手动添加两个特征点

    if landmark is None:
        print "人脸识别出错"
    else:
        print "调用face++ api成功"

    add_landmark(landmark)
    add_border_point(landmark, mat.shape)
    '''
    这部分查看一下人脸的特征变形,这里需要计算人脸剖分的方案,需要覆盖全图
    '''
    point_dict = from_lanmark_to_points(landmark)
    triangles = trianglulation(point_dict)
    draw_triangle(mat, triangles, point_dict)

    mask = draw_mask(triangles, point_dict, mat.shape)
    img = np.array(mask * mat, dtype=np.uint8)
    img[0] = cv2.equalizeHist(img[0])
    img[1] = cv2.equalizeHist(img[1])
    img[2] = cv2.equalizeHist(img[2])
    cv2.imshow("mat", mat)
    cv2.waitKey(0)
Beispiel #3
0
def feature_flow_extract():
    """
    人脸区域光流提取
    :return:
    """
    p = "../dataset/datashow/face_aligen/EP02_01f/"
    pf = "../dataset/datashow/flow_aligen/EP02_01f/"
    file_name_list = os.listdir(p)
    dir = p
    for file in file_name_list:
        if file[-3:] != "jpg":
            continue
        img = cv2.imread(dir + file)

        landmarks = fd.get_feature_points_fromimage(img)
        points = fd.from_lanmark_to_points(landmarks)
        fd.supply_landmark(points)
        regions = fd.get_region_by_landmark()
        points_list = []
        mask = np.zeros(img.shape[0:2])
        for i in regions:
            tmp_point = []
            for mark_name in i:
                tmp_point.append(points[mark_name])
            points_list.append(tmp_point)
            fd.draw_region(img, tmp_point)
            fd.draw_region_mask(mask, tmp_point)

        # 光流
        U, V = ft.flow_reader(pf + "reg_flow" + file[3:-4] + ".xml")
        flow = np.zeros((U.shape[0], U.shape[1], 2))
        value = (U * np.sqrt(1 / 2.0) + V * np.sqrt(1 / 2.0)) * mask
        value = cv2.normalize(value, None, 0, 1, cv2.NORM_MINMAX)
        plt.imshow(value * mask, cmap="gray")
        plt.show()
        for i in range(U.shape[0]):
            for j in range(U.shape[1]):
                if mask[i][j] == 1:
                    flow[i][j] = (U[i][j], V[i][j])
        opt = of.OpticalFlow()
        clolc = opt.visual_flow(flow)
        cv2.imshow("a", clolc)
        cv2.waitKey(0)

        cv2.imshow("a", img)
        cv2.waitKey(0)
        print points_list
Beispiel #4
0
def get_masked_face(mat, shape):
    landmark = fapi.get_feature_points_fromimage(mat)
    # 这里为了能够更好的提取人脸,手动添加两个特征点

    if landmark is None:
        return None

    add_landmark(landmark)
    '''
    这部分查看一下人脸的特征变形,这里需要计算人脸剖分的方案,需要覆盖全图
    '''
    point_dict = from_lanmark_to_points(landmark)
    triangles = trianglulation(point_dict)

    mask = draw_mask(triangles, point_dict, mat.shape)
    img = np.array(mask * mat, dtype=np.uint8)

    #img[0] = cv2.equalizeHist(img[0])
    #img[1] = cv2.equalizeHist(img[1])
    #img[2] = cv2.equalizeHist(img[2])
    result = img[landmark["forehead_left"]['y']:landmark["contour_chin"]['y'],landmark["contour_left1"]['x']:landmark["contour_right1"]['x']]
    return cv2.resize(result, shape)
Beispiel #5
0
def test_face_deform():
    """
        对已知剖分结果的三角面进行变形
    """

    src = cv2.imread("../../dataset/S076_006_00000001.png")
    src = cv2.resize(src, (src.shape[1], src.shape[0]))

    dst = cv2.imread("../../dataset/S076_006_00000009.png")
    dst = cv2.resize(dst, (src.shape[1], src.shape[0]))

    print src.shape
    print dst.shape

    rows = src.shape[0]
    cols = src.shape[1]
    seat = np.zeros((rows, cols, 2))
    for col in range(cols):
        for row in range(rows):
            seat[row][col][0] = row
            seat[row][col][1] = col

    src_landmark = fapi.get_feature_points_fromimage(src)
    dst_landmark = fapi.get_feature_points_fromimage(dst)
    if dst_landmark is None or src_landmark is None:
        print "人脸识别出错"
    else:
        print "特征点识别成功"

    add_landmark(src_landmark)
    add_landmark(dst_landmark)

    shape = src.shape
    add_border_point(src_landmark, (shape[1], shape[0]))
    add_border_point(dst_landmark, (shape[1], shape[0]))

    '''
    这部分查看一下人脸的特征变形,这里需要计算人脸剖分的方案,需要覆盖全图
    '''
    src_points = from_lanmark_to_points(src_landmark)
    dst_points = from_lanmark_to_points(dst_landmark)
    triangles = trianglulation(src_points)

    affine_mats = compute_affine_mat(triangles, src_points, dst_points)

    warped_images = []
    warped_seat = []
    for M in affine_mats:
        I = cv2.warpAffine(src, M, (shape[1], shape[0]))
        I_seat = cv2.warpAffine(seat, M, (shape[1], shape[0]))
        warped_images.append(I)
        warped_seat.append(I_seat)

    masks = draw_masks(triangles, dst_points, shape)
    mask_seat = draw_masks(triangles, dst_points, seat.shape, color=(1, 1))

    result, one = combine_image(warped_images, masks, shape)
    result_seat, one_seat = combine_image(warped_seat, mask_seat, seat.shape)

    result_img = result + one * src
    # draw_triangle(result_img,triangles, src_points)
    result = result_seat + one_seat * seat
    result[:,:,0] = cv2.normalize(cv2.absdiff(result[:,:,0],seat[:,:,0]),None, 0, 1, cv2.NORM_MINMAX)
    result[:,:,1] = cv2.normalize(cv2.absdiff(result[:,:,1],seat[:,:,1]),None, 0, 1, cv2.NORM_MINMAX)

    cv2.imshow("dst1", result[:,:,0])
    cv2.imshow("dst2", result[:,:,1])
    cv2.imshow("dst", result_img/255)
    print result

    cv2.imshow("src", src)
    cv2.waitKey(0)
Beispiel #6
0
def camse2_rect_aligened():
    """
    将CAMSE2的数据中的人脸,和特征点全部提取出来
    :return:
    """
    """`
    人脸对齐:
    1。读取光流,文件中的内容
    2。读取两帧图像
    3。读取第一帧图像的特征点
    4。计算由光流变到第二帧后的位置。
    5。计算仿射变换
    6。对第二张图像进行仿射变换来对齐
    """
    counter_index = range(
        0, 83
    )  # [ 21, 20, 19, 56, 15, 14, 41, 65, 32, 76, 36, 35,44,68 , 60, 30,46]
    path = "D:\\CAME2\\CASME2-coding-20140508.csv"
    rect_root = "D:\\CAME2\\Face_Rect\\"
    flow_root = "D:\\CAME2\\Flow\\"
    aligned_root = "D:\\CAME2\\Face_Rect_Aligned\\"
    file = open(path)
    lines = file.readlines()
    for index, line in enumerate(lines[1:]):

        meta = line.split(",")

        subject = "sub%02d" % int(meta[0])
        if os.path.exists(aligned_root + subject):
            pass
        else:
            os.mkdir(aligned_root + subject)
        sqe = meta[1]

        if os.path.exists(aligned_root + subject + "\\" + sqe):
            pass
        else:
            os.mkdir(aligned_root + subject + "\\" + sqe)

        begin = int(meta[3])
        end = int(meta[5])

        image1_file_name = rect_root + subject + "\\" + sqe + "\\" + "img" + str(
            begin) + ".jpg"
        image1 = cv2.imread(image1_file_name)
        aligned_name = aligned_root + subject + "\\" + sqe + "\\" + "img" + str(
            begin) + ".jpg"
        cv2.imwrite(aligned_name, image1[10:-10, 10:-10, :])
        vd = fd.get_feature_points_fromimage(image1)
        points = np.array(fd.from_lanmark_to_points(vd).values())

        feature_pos = points[counter_index]

        for i in range(begin + 1, end + 1, 1):
            flow_name = flow_root + subject + "\\" + sqe + "\\" + "reg_flow" + str(
                i - 1) + ".xml"
            image2_file_name = rect_root + subject + "\\" + sqe + "\\" + "img" + str(
                i) + ".jpg"
            image2 = cv2.imread(image2_file_name)
            aligned_name = aligned_root + subject + "\\" + sqe + "\\" + "img" + str(
                i) + ".jpg"
            if os.path.exists(aligned_name):
                continue
            U, V = flow_reader(flow_name)
            feature_back = []
            for pos in feature_pos:
                pos[0] = max(pos[0], 0)
                pos[1] = max(pos[1], 0)
                pos[0] = min(pos[0], image2.shape[1] - 1)
                pos[1] = min(pos[1], image2.shape[0] - 1)
                new_pos = (pos[0] + U[pos[1]][pos[0]],
                           pos[1] + V[pos[1]][pos[0]])
                feature_back.append(new_pos)
            fl = of.OpticalFlow()
            mat = fl.calc_affine_mat(np.array(feature_back, dtype=np.float32),
                                     np.array(feature_pos, dtype=np.float32))

            B = cv2.warpAffine(image2, mat.T,
                               (image2.shape[1], image2.shape[0]))
            cv2.imwrite(aligned_name, B[10:-10, 10:-10, :])