Exemplo n.º 1
0
def get_faces_dimens(image_string, bounds):
    try:
        result = indicoio.facial_localization(image_string)
        faces = []
        for face in result:
            x1, y1 = face["top_left_corner"]
            x2, y2 = face["bottom_right_corner"]
            faces.append((x1, y1, x2, y2))
        return faces
    except Exception as e:
        logger.error(e)
Exemplo n.º 2
0
 def _get_single_face_corners(self, img):
     """
     returns x and y coords of upper and lower left pixel of face in img (a PIL Image object)
     """
     try:
         face_corners = indicoio.facial_localization(img)[0]
         x0, y0 = face_corners['top_left_corner']
         x1, y1 = face_corners['bottom_right_corner']
         return (x0, y0, x1, y1)
     except:
         return ()
Exemplo n.º 3
0
def extractFaces(imgPath):
    faceArray = []
    faces = indicoio.facial_localization(imgPath)
    img = cv2.imread(imgPath)

    count = 0
    for face in faces:
        if (face['bottom_right_corner'][1] - face['top_left_corner'][1]) > (len(img[0]) * 0.2):
            count += 1
            faceImg = img[
                face['top_left_corner'][1] : face['bottom_right_corner'][1],
                face['top_left_corner'][0] : face['bottom_right_corner'][0]
            ]
            storageLocation = faceImgStorageDir + str(int(time.time())) + str(count) + '.jpg'
            cv2.imwrite(storageLocation, faceImg)
            faceArray.append(storageLocation)

    return faceArray
Exemplo n.º 4
0
def recordFaceData(directory, nickname):
    allImages = []
    count = 0

    for file in os.listdir(directory):
        if file.endswith('.jpg'):
            allImages.append(file)

    currentRecordedFeatures = {}
    currentRecordedFeatures['names'] = []
    currentRecordedFeatures['features'] = []

    print 'Caching...'
    for image in allImages:
        print 'Collecting info ... ', count
        count += 1

        face = indicoio.facial_localization(directory + '/' + image)
        if len(face) > 0:
            face = face[0]

            img = cv2.imread(directory + '/' + image)
            faceImg = img[
                face['top_left_corner'][1] : face['bottom_right_corner'][1],
                face['top_left_corner'][0] : face['bottom_right_corner'][0]
            ]

            facialFeatures = indicoio.facial_features(faceImg)

            if (count % 50) == 0:
                cv2.imwrite('images/faces/' + str(int(time.time())) + '.jpg', faceImg)

            currentRecordedFeatures['names'].append(nickname)
            currentRecordedFeatures['features'].append(facialFeatures)

    np.save(featuresCacheFile + nickname + '.npy', [currentRecordedFeatures])
    print 'Done'
    return currentRecordedFeatures
Exemplo n.º 5
0
 def test_facial_localization_crop(self):
     test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
     res = facial_localization(test_face, crop=True)[0]
     self.assertTrue(res.get("image"))
Exemplo n.º 6
0
 def test_facial_localization_sensitivity(self):
     test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
     low_sens = facial_localization(test_face, sensitivity=0.1)
     high_sens = facial_localization(test_face, sensitivity=0.9)
     self.assertEqual(len(low_sens), 1)
     self.assertTrue(len(high_sens) > 1)
Exemplo n.º 7
0
 def test_facial_localization(self):
     test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
     res = facial_localization(test_face)[0]
     self.assertTrue(res["top_left_corner"][0] < res["bottom_right_corner"][0])
     self.assertTrue(res["top_left_corner"][1] < res["bottom_right_corner"][1])
Exemplo n.º 8
0
 def test_facial_localization_crop(self):
     test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
     res = facial_localization(test_face, crop=True)[0]
     self.assertTrue(res.get("image"))
Exemplo n.º 9
0
def capture():
    try:
        global foo
        img = cam.get_image()
        pygame.image.save(img, "test" + str(foo) + ".jpg")
        os.rename(
            "/home/lx_user/Documents/programming/web/uanimate/test" +
            str(foo) + ".jpg",
            "/home/lx_user/Documents/programming/web/uanimate/test/test" +
            str(foo) + ".jpg")
        dict = indicoio.facial_localization(
            "/home/lx_user/Documents/programming/web/uanimate/test/test" +
            str(foo) + ".jpg")
        image = pygame.image.load(
            "/home/lx_user/Documents/programming/web/uanimate/test/test" +
            str(foo) + ".jpg")
        c_id = 0
        #fear is disgust
        happy = 0.0
        sad = 0.0
        angry = 0.0
        fear = 0.0
        surprise = 0.0
        neutral = 0.0
        total = 0.0

        for i in range(0, int(len(dict))):
            if (dict[i]['bottom_right_corner'][0] -
                    dict[i]['top_left_corner'][0] > 60000
                    or dict[i]['bottom_right_corner'][1] -
                    dict[i]['top_left_corner'][1] > 60000
                    or dict[i]['top_left_corner'][0] > 60000
                    or dict[i]['top_left_corner'][1] > 60000):
                continue
            crop_rect = (dict[i]['top_left_corner'][0],
                         dict[i]['top_left_corner'][1],
                         abs(dict[i]['bottom_right_corner'][0] -
                             dict[i]['top_left_corner'][0]),
                         abs(dict[i]['bottom_right_corner'][1] -
                             dict[i]['top_left_corner'][1]))
            cropped = image.subsurface(crop_rect)
            pygame.image.save(cropped,
                              str(foo) + "cropped" + str(c_id) + ".jpg")

            # code for the emotion
            results = indicoio.fer(str(foo) + "cropped" + str(c_id) + ".jpg")
            # print(results)

            if ('Happy' in results):
                happy += results['Happy']
            if ('Sad' in results):
                sad += results['Sad']
            if ('Angry' in results):
                angry += results['Angry']
            if ('Fear' in results):
                fear += results['Fear']
            if ('Surprise' in results):
                surprise += results['Surprise']
            total += (happy + sad + angry + fear + surprise)

            os.rename(
                "/home/lx_user/Documents/programming/web/uanimate/" +
                str(foo) + "cropped" + str(c_id) + ".jpg",
                "/home/lx_user/Documents/programming/web/uanimate/test/" +
                str(foo) + "cropped" + str(c_id) + ".jpg")
            c_id += 1

        #open the file for reading:
        file = open(
            '/home/lx_user/Documents/programming/web/uanimate/results.txt',
            'w')
        #convert to string:
        file.write(
            str(happy) + " " + str(sad) + " " + str(angry) + " " + str(fear) +
            " " + str(surprise) + " " + str(total))
        file.close()
        foo += 1
    except (RuntimeError, TypeError, NameError, ValueError, IndexError,
            IOError, KeyError):
        print("wtf")