コード例 #1
0
ファイル: fabfile.py プロジェクト: zhang405744522/TensorFace
def process(code, div):
    import openface
    import openface.helper
    import dlib
    from openface.alignment import NaiveDlib  # Depends on dlib.
    code = int(code)
    div = int(div)
    dlibModelDir = os.path.join(fileDir, "./openface/models/dlib")
    dlibFaceMean = os.path.join(dlibModelDir, "mean.csv")
    dlibFacePredictor = os.path.join(dlibModelDir,
                                     "shape_predictor_68_face_landmarks.dat")
    align = NaiveDlib(dlibFaceMean, dlibFacePredictor)
    dataset = data.Dataset()
    last = time.time()
    count = 0
    for model, key, img in dataset.get_images(BUCKET_NAME):
        if hash(key) % div == code:
            bb = align.getLargestFaceBoundingBox(img)
            aligned = align.alignImg("affine", 224, img, bb)
            # print time.time() - last
            last = time.time()
            count += 1
            if not aligned is None:
                # print model,key,img.shape,bb,aligned.shape
                cv2.imwrite(
                    "output/face_{}".format(
                        key.replace('/', '_').replace('models', '')), aligned)
                # cv2.imshow("test",aligned)
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()
                # break
        if count % 20 == 0 and code == 0:
            local(
                'aws s3 mv output/ s3://aub3data/output/ --recursive --storage-class "REDUCED_REDUNDANCY"  --region "us-east-1"'
            )
コード例 #2
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    align = NaiveDlib(args.dlibFaceMean, args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        imgName = "{}/{}.png".format(outDir, imgObject.name)
        openface.helper.mkdirP(outDir)
        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB(cache=False)
            out = align.alignImg(args.method, args.size, rgb)
            if args.fallbackLfw and out is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(
                    os.path.join(args.fallbackLfw, imgObject.cls),
                    imgObject.name)
                shutil.copy(
                    deepFunneled, "{}/{}.jpg".format(
                        os.path.join(args.outputDir, imgObject.cls),
                        imgObject.name))

            if out is not None:
                io.imsave(imgName, out)
    print('nFallbacks:', nFallbacks)
コード例 #3
0
ファイル: fabfile.py プロジェクト: ChengLunHu/TensorFace
def process(code,div):
    import openface
    import openface.helper
    import dlib
    from openface.alignment import NaiveDlib  # Depends on dlib.
    code = int(code)
    div = int(div)
    dlibModelDir = os.path.join(fileDir, "./openface/models/dlib")
    dlibFaceMean = os.path.join(dlibModelDir, "mean.csv")
    dlibFacePredictor = os.path.join(dlibModelDir,"shape_predictor_68_face_landmarks.dat")
    align = NaiveDlib(dlibFaceMean,dlibFacePredictor)
    dataset = data.Dataset()
    last = time.time()
    count = 0
    for model,key,img in dataset.get_images(BUCKET_NAME):
        if hash(key) % div == code:
            bb = align.getLargestFaceBoundingBox(img)
            aligned  = align.alignImg("affine", 224, img, bb)
            # print time.time() - last
            last = time.time()
            count += 1
            if not aligned is None:
                # print model,key,img.shape,bb,aligned.shape
                cv2.imwrite("output/face_{}".format(key.replace('/','_').replace('models','')),aligned)
                # cv2.imshow("test",aligned)
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()
                # break
        if count % 20 == 0 and code == 0:
            local('aws s3 mv output/ s3://aub3data/output/ --recursive --storage-class "REDUCED_REDUNDANCY"  --region "us-east-1"')
コード例 #4
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    align = NaiveDlib(args.dlibFaceMean, args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        imgName = "{}/{}.png".format(outDir, imgObject.name)
        openface.helper.mkdirP(outDir)
        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB(cache=False)
            out = align.alignImg(args.method, args.size, rgb,
                outputPrefix=outDir,
                outputDebug=args.outputDebugImages)
            if args.fallbackLfw and out is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw,
                                                               imgObject.cls),
                                                  imgObject.name)
                shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(args.outputDir,
                                                                          imgObject.cls),
                                                             imgObject.name))

            if out is not None:
                io.imsave(imgName, out)
    print('nFallbacks:', nFallbacks)
コード例 #5
0
ファイル: align-dlib.py プロジェクト: woshiqchi/openface
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    if args.landmarks == 'outerEyesAndNose':
        landmarkIndices = NaiveDlib.OUTER_EYES_AND_NOSE
    elif args.landmarks == 'innerEyesAndBottomLip':
        landmarkIndices = NaiveDlib.INNER_EYES_AND_BOTTOM_LIP
    else:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    align = NaiveDlib(args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB()
            if rgb is not None:
                print(imgName, type(rgb), rgb.shape)
                outRgb = align.alignImg('affine',
                                        args.size,
                                        rgb,
                                        landmarkIndices=landmarkIndices)
            else:
                outRgb = None
            if args.fallbackLfw and outRgb is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(
                    os.path.join(args.fallbackLfw, imgObject.cls),
                    imgObject.name)
                shutil.copy(
                    deepFunneled, "{}/{}.jpg".format(
                        os.path.join(args.outputDir, imgObject.cls),
                        imgObject.name))

            if outRgb is not None:
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)

    if args.fallbackLfw:
        print('nFallbacks:', nFallbacks)
コード例 #6
0
ファイル: align-dlib.py プロジェクト: lucafeudi/openface
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    if args.landmarks == 'outerEyesAndNose':
        landmarkIndices = NaiveDlib.OUTER_EYES_AND_NOSE
    elif args.landmarks == 'innerEyesAndBottomLip':
        landmarkIndices = NaiveDlib.INNER_EYES_AND_BOTTOM_LIP
    else:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    align = NaiveDlib(args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB()
            if rgb is not None:
                print(imgName, type(rgb), rgb.shape)
                outRgb = align.alignImg('affine', args.size, rgb,
                                        landmarkIndices=landmarkIndices)
            else:
                outRgb = None
            if args.fallbackLfw and outRgb is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw,
                                                               imgObject.cls),
                                                  imgObject.name)
                shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(args.outputDir,
                                                                          imgObject.cls),
                                                             imgObject.name))

            if outRgb is not None:
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)

    if args.fallbackLfw:
        print('nFallbacks:', nFallbacks)
コード例 #7
0
ファイル: fabfile.py プロジェクト: gepolv/TensorFace
def process():
    dlibModelDir = os.path.join(fileDir, "./openface/models/dlib")
    dlibFaceMean = os.path.join(dlibModelDir, "mean.csv")
    dlibFacePredictor = os.path.join(dlibModelDir,"shape_predictor_68_face_landmarks.dat")
    align = NaiveDlib(dlibFaceMean,dlibFacePredictor)
    dataset = data.Dataset()
    for model,key,img in dataset.get_images(BUCKET_NAME):
        bb = align.getLargestFaceBoundingBox(img)
        aligned  = align.alignImg("affine", 224, img, bb)
        if not aligned is None:
            print model,key,img.shape,bb,aligned.shape
            cv2.imwrite("test/face_{}".format(key.replace('/','_')),aligned)
            # cv2.imshow("test",aligned)
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()
            # break
        else:
            print "No face found"
コード例 #8
0
def process():
    dlibModelDir = os.path.join(fileDir, "./openface/models/dlib")
    dlibFaceMean = os.path.join(dlibModelDir, "mean.csv")
    dlibFacePredictor = os.path.join(dlibModelDir,
                                     "shape_predictor_68_face_landmarks.dat")
    align = NaiveDlib(dlibFaceMean, dlibFacePredictor)
    dataset = data.Dataset()
    for model, key, img in dataset.get_images(BUCKET_NAME):
        bb = align.getLargestFaceBoundingBox(img)
        aligned = align.alignImg("affine", 224, img, bb)
        if not aligned is None:
            print model, key, img.shape, bb, aligned.shape
            cv2.imwrite("test/face_{}".format(key.replace('/', '_')), aligned)
            # cv2.imshow("test",aligned)
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()
            # break
        else:
            print "No face found"
コード例 #9
0
def main(args):
    align = NaiveDlib(args.dlibFacePredictor)

    bgrImg = cv2.imread(args.img)
    if bgrImg is None:
        raise Exception("Unable to load image: {}".format(imgPath))
    rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)

    bb = align.getLargestFaceBoundingBox(rgbImg)
    if bb is None:
        raise Exception("Unable to find a face: {}".format(imgPath))

    landmarks = align.align(rgbImg, bb)
    if landmarks is None:
        raise Exception("Unable to align image: {}".format(imgPath))
    alignedFace = align.alignImg("affine", args.size, rgbImg, bb, landmarks)

    bl = (bb.left(), bb.bottom())
    tr = (bb.right(), bb.top())
    cv2.rectangle(bgrImg, bl, tr, color=(153, 255, 204), thickness=3)
    for landmark in landmarks:
        cv2.circle(bgrImg, center=landmark, radius=3, color=(102, 204, 255), thickness=-1)
    print("Saving image to 'annotated.png'")
    cv2.imwrite("annotated.png", bgrImg)
コード例 #10
0
ファイル: face_service.py プロジェクト: kanak87/oldboy_rep
class FaceService(object):
    def __init__(self):
        self.align = NaiveDlib(os.path.join(dlibModelDir, "mean.csv"),
                               os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))
        self.net = openface.TorchWrap(os.path.join(openfaceModelDir, 'nn4.v1.t7'),
                                      imgDim=96, cuda=False)

        self.identities = []

        if os.path.exists("trained_images.pickle"):
            self.trained_images = pickle.load(open('trained_images.pickle', 'rb'))
            identities_set = set()
            for trained_image in self.trained_images.values():
                identities_set.add(trained_image.identity)

            self.identities = list(identities_set)
            self.identities.sort()
        else:
            self.trained_images = {}

        if os.path.exists("svm.pickle"):
            self.svm = pickle.load(open('svm.pickle', 'rb'))
        else:
            self.svm = None

    def training(self, identity, images):
        X = []
        y = []

        training_result = []
        identities_set = set()

        for img in self.trained_images.values():
            X.append(img.rep)
            y.append(img.identity)
            identities_set.add(img.identity)

        identities_set.add(identity)
        self.identities = list(identities_set)
        self.identities.sort()

        for image in images:
            bbs = self.align.getAllFaceBoundingBoxes(image)

            if len(bbs) is not 1:
                training_result.append('0 or many people in image')
                continue
                # raise Exception('0 or many people in image')

            bb = bbs[0]
            alignedFace = self.align.alignImg("affine", 96, image, bb)
            if alignedFace is None:
                training_result.append('not exist face in image')
                continue

            # save_array(alignedFace, "train.png")

            phash = str(imagehash.phash(Image.fromarray(alignedFace)))
            if phash in self.trained_images:
                rep = self.trained_images[phash].rep
                training_result.append('already trained')
            else:
                rep = self.net.forwardImage(alignedFace)
                self.trained_images[phash] = Face(rep, identity)

                X.append(rep)
                y.append(identity)

                training_result.append(0)

        for index, identity in enumerate(self.identities):
            for i in range(len(y)):
                if y[i] == identity:
                    y[i] = index

        if len(self.identities) > 1:
            X = np.vstack(X)
            y = np.array(y)

            param_grid = [
                {'C': [1, 10, 100, 1000],
                 'kernel': ['linear']},
                {'C': [1, 10, 100, 1000],
                 'gamma': [0.001, 0.0001],
                 'kernel': ['rbf']}
            ]
            print "*" * 60
            for x in X:
                print x[:4]
            print y
            self.svm = GridSearchCV(SVC(C=0.5, probability=True), param_grid, cv=5).fit(X, y)

        return training_result

    def remove_face(self, identity):
        X = []
        y = []

        remove_faces = []
        identities_set = set()

        for key, value in self.trained_images.items():
            if value.identity == identity:
                remove_faces.append(key)
            else:
                X.append(value.rep)
                y.append(value.identity)
                identities_set.add(value.identity)

        self.identities = list(identities_set)
        self.identities.sort()

        for key in remove_faces:
            del self.trained_images[key]

        for index, identity in enumerate(self.identities):
            for i in range(len(y)):
                if y[i] == identity:
                    y[i] = index

        if len(self.identities) > 1:
            X = np.vstack(X)
            y = np.array(y)

            param_grid = [
                {'C': [1, 10, 100, 1000],
                 'kernel': ['linear']},
                {'C': [1, 10, 100, 1000],
                 'gamma': [0.001, 0.0001],
                 'kernel': ['rbf']}
            ]
            print "*" * 60
            for x in X:
                print x[:4]
            print y
            self.svm = GridSearchCV(SVC(C=0.5, probability=True), param_grid, cv=5).fit(X, y)
        else:
            self.svm = None

    def predict(self, image):
        result_priority_queue = PriorityQueue()
        results = []

        bbs = self.align.getAllFaceBoundingBoxes(image)

        for bb_index, bb in enumerate(bbs):
            alignedFace = self.align.alignImg("affine", 96, image, bb)
            if alignedFace is None:
                continue

            phash = str(imagehash.phash(Image.fromarray(alignedFace)))
            if phash in self.trained_images:
                identity = self.trained_images[phash].identity
                result_priority_queue.put_nowait((-1.0, identity, bb_index))
            else:
                rep = self.net.forwardImage(alignedFace)
                if self.svm is not None:
                    result_proba_list = self.svm.predict_proba(rep)
                    identity = np.argmax(result_proba_list[0])
                    print str(result_proba_list[0]) + " " + str(bb)
                    for index, prob in enumerate(result_proba_list[0]):
                        result_priority_queue.put_nowait((prob * -1.0, self.identities[index], bb_index))
                else:
                    result_priority_queue.put_nowait((0.0, -1, bb_index))

        matched_identities = []
        matched_bb_indices = []
        threshold = 0.6

        while len(matched_identities) != len(bbs) and result_priority_queue.empty() is False:
            detectedFaceInfo = result_priority_queue.get_nowait()

            identity = detectedFaceInfo[1]
            probability = detectedFaceInfo[0] * -1.0
            bb_index = detectedFaceInfo[2]
            # print detectedFaceInfo

            if identity in matched_identities:
                # print "matched_bbs : " + str(matched_identities)
                continue

            matched_bb_indices.append(bb_index)
            matched_identities.append(identity)

            if probability < threshold:
                results.append((-1, bbs[bb_index], 0.0))
            else:
                results.append((identity, bbs[bb_index], probability))

                # print '+' + str(results[len(results) - 1])

        for bb_index, bb in enumerate(bbs):
            if bb_index in matched_bb_indices:
                continue

            results.append((-1, bb, 0.0))

        return results
コード例 #11
0
ファイル: test_dlib.py プロジェクト: kanak87/oldboy_rep
from PIL import Image
import numpy as np

params = list()
params.append(cv2.IMWRITE_PNG_COMPRESSION)
params.append(9)

modelDir = os.path.join(fileDir, "../project", 'models')
dlibModelDir = os.path.join(modelDir, 'dlib')
openfaceModelDir = os.path.join(modelDir, 'openface')
sys.path.append(openfaceModelDir)

align = NaiveDlib(os.path.join(dlibModelDir, "mean.csv"),
                  os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))

video_capture = cv2.VideoCapture(0)

ret, frame = video_capture.read()
sleep(1)
ret, frame = video_capture.read()

image = frame

cv2.imwrite('img.png', frame, params)
bbs = align.getAllFaceBoundingBoxes(image)

print len(bbs)

bb = bbs[0]
alignedFace = align.alignImg("affine", 96, image, bb)
cv2.imwrite('img2.png', alignedFace, params)