Ejemplo n.º 1
0
def saveFacialFeatures(demographicsData):
    print("calculating face features")
    allData = []
    with open(os.path.join(scriptFolder, "US10kData_FULL.p"), "wb") as f:
        for i, data in enumerate(demographicsData):
            attractiveScore = float(data['Attractive'])
            gender = GENDER_DICT[int(data['Gender'])]
            if not np.isnan(attractiveScore):
                impath = data['Filename']
                im = cv2.imread(impath)

                try:
                    landmarks, faceFeatures = getFaceFeatures(im)
                    mesh, pose, shape_coeffs, blendshape_coeffs = SFM_FACEFITTING.getMeshFromLandmarks(
                        landmarks, im, num_shape_coefficients_to_fit=10)
                    facefeaturesCNN = getFaceFeaturesCNN([im], [landmarks])

                    dataDict = {
                        "gender": gender,
                        "attractiveness": attractiveScore,
                        "landmarks": landmarks,
                        "facefeatures": faceFeatures,
                        "facefeatures3D": shape_coeffs,
                        "pose": pose,
                        "blendshape_coeffs": blendshape_coeffs,
                        "impath": impath,
                        "facefeaturesCNN": facefeaturesCNN,
                    }

                    allData.append(dataDict)
                    pickle.dump(dataDict, f)
                    print("%i / %i" % (i, len(demographicsData)))
                except:
                    continue

    allDataDF = pd.DataFrame(allData)
    allDataDF.to_pickle(os.path.join(scriptFolder, "US10kData.p"))

    return allDataDF
Ejemplo n.º 2
0
def getWebResults(im, predictor):
    facefeatures_bfm = getFaceFeaturesCNN([im])[0:99]

    rating = predictor.predict([facefeatures_bfm])[0]

    # new_facefeatures_bfm = facefeatures_bfm.copy()
    new_facefeatures_bfm = findBestFeaturesOptimisation(facefeatures_bfm, predictor)

    landmarks = getLandmarks(im)
    pose_bfm = BFM_FACEFITTING.getPoseFromShapeCeoffs(landmarks, im, facefeatures_bfm)
    mesh_bfm = BFM_FACEFITTING.getMeshFromShapeCeoffs(facefeatures_bfm)
    modelview, projection, viewport, indexs = decomposePose(mesh_bfm, pose_bfm, im)

    results = {}
    results["facefeatures"] = facefeatures_bfm.tolist()
    results["rating"] = rating
    results["new_facefeatures"] = new_facefeatures_bfm.tolist()
    results["modelview"] = modelview
    results["projection"] = projection
    results["viewport"] = viewport
    results["indexs"] = indexs

    return results
Ejemplo n.º 3
0
        ])

        max_I = 100
        for i, impath in enumerate(glob.glob(srcFolder)):
            if i >= max_I:
                break
            print(impath)
            im = cv2.imread(impath)
            filename = os.path.basename(impath)

            landmarks = getLandmarks(im)
            mesh_sfm, pose_sfm, facefeatures_sfm, blendshape_coeffs_sfm = SFM_FACEFITTING.getMeshFromLandmarks(
                landmarks, im, num_iterations=300)
            # mesh_sfm, pose_sfm, facefeatures_sfm, blendshape_coeffs_sfm = SFM_FACEFITTING.getMeshFromLandmarks(landmarks, im, num_iterations=5000)

            facefeatures_bfm = getFaceFeaturesCNN([im])[0:99]
            pose_bfm = BFM_FACEFITTING.getPoseFromShapeCeoffs(
                landmarks, im, facefeatures_bfm)
            mesh_bfm = BFM_FACEFITTING.getMeshFromShapeCeoffs(facefeatures_bfm)

            num_coeffs = 12
            coeff_deltas = []
            for c in range(num_coeffs):
                coeffs_up = [0] * num_coeffs
                coeffs_dn = [0] * num_coeffs
                coeffs_up[c] = 1
                coeffs_dn[c] = -1
                coeff_deltas.append(coeffs_up)
                coeff_deltas.append(coeffs_dn)

            # for coeff_delta_vec in coeff_deltas:
    #
    # gradientX, gradientY = np.gradient(y_preds)

    # plt.figure()
    # plt.title('svms gradient')
    # Q = plt.quiver(X, Y, gradientX, gradientY, units='width')#
    # plt.show()

    # impath = r"E:\Facedata\RateMe\21_F_423ekl\HeC768w.jpg"
    # impath = r"E:\Facedata\RateMe\21_F_42mxvl\fWIPj9e.jpg"
    impath = r"E:\Facedata\RateMe\21_M_5mx3xt\a2Odd4w.jpg"
    im = cv2.imread(impath)
    landmarks = getLandmarks(im)

    # mesh_sfm, pose_sfm, shape_coeffs_sfm, blendshape_coeffs_sfm = SFM_FACEFITTING.getMeshFromLandmarks(landmarks, im, num_iterations=300)
    shape_coeffs_bfm = getFaceFeaturesCNN([im])[0:99]
    pose_bfm = BFM_FACEFITTING.getPoseFromShapeCeoffs(landmarks, im,
                                                      shape_coeffs_bfm)
    mesh_bfm = BFM_FACEFITTING.getMeshFromShapeCeoffs(shape_coeffs_bfm)

    # new_shape_coeffs = np.array(shape_coeffs_sfm).copy()
    new_shape_coeffs = np.array(shape_coeffs_bfm).copy()
    distance_moved = 0
    target_distance = 2
    poss = []
    while distance_moved < target_distance:
        pos = new_shape_coeffs[:2]

        v = vectorAtPos(vectors, pos)

        v *= 0.1
Ejemplo n.º 5
0
    dstFolder = "./results/"
    impath = r"E:\Facedata\RateMe\21_F_423ekl\HeC768w.jpg"
    impath = r"C:\Users\ellio\PycharmProjects\circlelines\Beautifier\face3D\me\0132.jpg"
    # impath = r"E:\Facedata\RateMe\18_M_2wbhfw\n5DesJ4.jpg"
    im = cv2.imread(impath)
    filename = os.path.basename(impath)

    outfile_bfm = os.path.join(dstFolder, "%s_bfm.ply" % filename)
    outfile_sfm_orig = os.path.join(dstFolder, "%s_sfm_orig.ply" % filename)
    outfile_com_orig = os.path.join(dstFolder, "%s_com_orig.ply" % filename)
    outfile_sfm_opt = os.path.join(dstFolder, "%s_sfm_opt.ply" % filename)
    outfile_com_opt = os.path.join(dstFolder, "%s_com_opt.ply" % filename)

    #bfm model
    bfm_features = getFaceFeaturesCNN([im])
    S, T = utils.projectBackBFM(BFM_MODEL, bfm_features)
    utils.write_ply(outfile_bfm, S, T, BFM_FACES)
    print(bfm_features)

    # sfm model
    landmarks = getLandmarks(im)
    sfm_mesh, pose, sfm_features, blendshape_coeffs = SFM_FACEFITTING.getMeshFromLandmarks(landmarks, im)
    sfm_features = np.array(sfm_features)
    print(sfm_features)

    verts_sfm = np.array(sfm_mesh.vertices)[:, :3]
    verts_sfm = verts_sfm + np.array([0, 0, np.max(S[:, 2]) - np.max(verts_sfm[:, 2])])
    text_sfm = np.ones_like(verts_sfm) * 128
    faces_sfm = np.array(sfm_mesh.tvi)
    utils.write_ply(outfile_sfm_orig, verts_sfm, text_sfm, faces_sfm)
Ejemplo n.º 6
0
def saveFacialFeatures(combinedcsvpath):
    df = pd.read_csv(combinedcsvpath)
    df.drop('Rating Text', 1)
    df.drop('Submission Title', 1)

    #filter out the weird ages
    filtered = df.loc[df['Submission Age'] >= 18]
    filtered = filtered.loc[filtered['Submission Age'] < 50]

    grouped = filtered.groupby([
        'Submission Id', "Submission Created UTC", "Folder", "Submission Age",
        "Submission Gender"
    ])

    sfile = open(os.path.join(scriptFolder, "RateMeData_FULL.p"), "wb")
    pickler = pickle.Pickler(sfile)
    allData = []
    for i, (uniques, group) in enumerate(grouped):
        submissionId = uniques[0]
        submissionCreatedUTC = uniques[1]
        folder = uniques[2]
        submissionAge = uniques[3]
        submissionGender = uniques[4]

        ratings = np.array(group["Rating"].as_matrix().tolist())
        ratingDecimal = np.array(group["Decimal"].as_matrix().tolist())
        ratingAuthor = np.array(group["Rating Author"].as_matrix().tolist())
        ratingPostedUTC = np.array(
            group["Rating Posted UTC"].as_matrix().tolist())
        ratingText = np.array(group["Rating Text"].as_matrix().tolist())

        comments = [(ratingAuthor[k], ratingPostedUTC[k], ratings[k],
                     ratingDecimal[k], ratingText[k])
                    for k in range(len(ratings))]

        nonEmptyRatings = [
            ratings[k] for k in range(len(ratings))
            if not math.isnan(ratings[k])
        ]
        _, attractiveness_mean, attractiveness_std = scipy.stats.t.fit(
            nonEmptyRatings)

        # cleanedRatings = reject_outliers(np.array(nonEmptyRatings))
        # attractiveness_mean = np.mean(cleanedRatings)
        # attractiveness_std = np.std(cleanedRatings)

        #get the image files:
        types = ('*.jpg', '*.png', '*.bmp')
        impaths = []
        for type in types:
            impaths.extend(glob.glob(os.path.join(folder, type)))

        usedImPaths = []
        ims = []
        landmarkss = []
        faceFeaturess = []

        numBodyShots = 0
        numBuddyShots = 0
        for impath in impaths:
            im = cv2.imread(impath)
            # im = ensureImageLessThanMax(im)

            try:
                landmarks, faceFeatures = getFaceFeatures(im)

                usedImPaths.append(impath)
                ims.append(im)
                landmarkss.append(landmarks)
                faceFeaturess.append(faceFeatures)
            except Exception as e:
                if "No face" in str(e):
                    numBodyShots += 1
                elif "Multiple faces" in str(e):
                    numBuddyShots += 1

                continue

        if len(ims) > 0:
            print("(%i/%i) #i:%i" % (i, len(grouped), len(ims)))
            print(usedImPaths)
            meshs, poses, shape_coeffs, blendshape_coeffss = SFM_FACEFITTING.getMeshFromMultiLandmarks(
                landmarkss,
                ims,
                num_shape_coefficients_to_fit=-1,
                num_iterations=300)

            facefeaturesCNN = getFaceFeaturesCNN(ims, landmarkss)

            dataDict = {
                "submissionId": submissionId,
                "submissionCreatedUTC": submissionCreatedUTC,
                "gender": submissionGender,
                "age": submissionAge,
                "comments": comments,
                "ratings": nonEmptyRatings,
                "attractiveness": attractiveness_mean,
                "attractiveness_std": attractiveness_std,
                "numUsableImages": len(ims),
                "numSubmittedImages": len(impaths),
                "numBodyShots": numBodyShots,
                "numBuddyShots": numBuddyShots,
                "impaths": usedImPaths,
                "landmarkss": landmarkss,
                "facefeaturess": faceFeaturess,
                # "meshs": meshs, can be regenerated from the coeffs below
                "poses": poses,
                "facefeatures3D": shape_coeffs,
                "blendshape_coeffss": blendshape_coeffss,
                "facefeaturesCNN": facefeaturesCNN,
            }
            allData.append(dataDict)
            pickler.dump(dataDict)

    sfile.close()

    allDataDF = pd.DataFrame(allData)
    allDataDF = allDataDF.sample(frac=1).reset_index(drop=True)
    allDataDF.to_pickle(os.path.join(scriptFolder, "RateMeData.p"))

    return allDataDF