averageFaces.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:FaceAnalysis 作者: ElliotSalisbury 项目源码 文件源码
def averageFacesImage(df, outpath):
    detector = dlib.get_frontal_face_detector()
    numRows = 7
    facesPerRow = 26
    imageSize = 30

    FULLIm = np.zeros((numRows * imageSize, facesPerRow * imageSize*2, 3), dtype=np.uint8)

    # df = df[df["numImages"] >= 2]
    df = df.sort(['attractiveness'])
    grouped = df.groupby("gender")
    for i, (gender, group) in enumerate(grouped):
        fullIm = np.zeros((numRows * imageSize, facesPerRow * imageSize, 3), dtype=np.uint8)

        numPeople = group.shape[0]
        numPeoplePerRow = int(numPeople / numRows)
        for j in range(numRows):
            rowPeople = group.iloc[numPeoplePerRow*j:numPeoplePerRow*(j+1)]

            # randomSampleIndexs = np.linspace(0,rowPeople.shape[0]-1,facesPerRow).astype(np.int32)
            randomSampleIndexs = random.sample(range(numPeoplePerRow), facesPerRow)

            impathss = np.array(rowPeople["impaths"].as_matrix().tolist())
            attractiveness = np.array(rowPeople["attractiveness"].as_matrix().tolist())
            for l,k in enumerate(randomSampleIndexs):
                impaths = impathss[k]
                randomImIndexs = random.sample(range(len(impaths)), len(impaths))

                for index in randomImIndexs:
                    im = cv2.imread(impaths[index])

                    rects = detector(im, 1)
                    if len(rects) != 1:
                        continue
                    rect = rects[0]
                    ryt = max(rect.top() - (rect.height()*0.2)      ,0)
                    ryb = min(rect.bottom() + (rect.height()*0.2)   , im.shape[0])
                    rxl = max(rect.left() - (rect.width()*0.2)      ,0)
                    rxr = min(rect.right() + (rect.width()*0.2)     ,im.shape[1])

                    faceim = im[ryt:ryb, rxl:rxr]
                    if faceim.shape[0] < 40 or faceim.shape[1] < 40:
                        continue
                    faceim = ensureImageSmallestDimension(faceim, imageSize)

                    y=j*imageSize
                    x=l*imageSize
                    print("%d,%d"%(x,y))

                    fullIm[y:y + imageSize, x:x + imageSize, :] = faceim[:imageSize, :imageSize, :]

                    x = (l*(imageSize*2)) + (imageSize*((i+j)%2))
                    FULLIm[y:y + imageSize, x:x + imageSize, :] = faceim[:imageSize, :imageSize, :]
                    cv2.imshow("faces", FULLIm)
                    cv2.waitKey(1)
                    break

        cv2.imwrite(os.path.join(outpath, "allfaces_%s.jpg"%gender),fullIm)
    cv2.imwrite(os.path.join(outpath, "allfaces.jpg" ), FULLIm)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号