class ProfilePhotoProcessTest(TestCase):  # pragma: no cover
    def setUp(self):
        '''
        Need to create a family and a gallery
        '''
        self.family = Family()
        self.family.save()

        self.gallery = Gallery.objects.create(title="test_gallery",
                                              family_id=self.family.id)

        clear_directory(settings.FACE_RECOG_TRAIN_TEST_DIR)

        self.test_image = os.path.join(
            settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg')
        self.test_image_destination = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/test_image.jpg'
        ])
        self.test_image_s3_key = ''.join([
            'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/test_image.jpg'
        ])

        directory = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id)
        ])
        if not os.path.exists(directory):
            os.makedirs(directory)

        #Copy test image to media area
        shutil.copy2(self.test_image, self.test_image_destination)

        self.image = Image(gallery=self.gallery,
                           family=self.family,
                           original_image=''.join([
                               'galleries/',
                               str(self.family.id), '/',
                               str(self.gallery.id), '/test_image.jpg'
                           ]))
        self.image.save()
        self.image.upload_files_to_s3()

        self.person = Person(name='Wallace',
                             gender='M',
                             email='*****@*****.**',
                             family_id=self.family.id,
                             language='en')
        self.person.save()

        self.tag = Tag.objects.create(image_id=self.image.id,
                                      x1=0.279,
                                      y1=0.188,
                                      x2=0.536,
                                      y2=0.381,
                                      person_id=self.person.id,
                                      face_detected=True)

        # Upload new image
        self.test_image2 = os.path.join(
            settings.BASE_DIR,
            'facial_recognition/tests/test_image_woman_and_baby.jpg')
        self.test_image2_destination = settings.MEDIA_ROOT + 'profile_photos/test_image_woman_and_baby.jpg'

        # Create a trained model
        process_family(self.family.id)

    def tearDown(self):
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()

        try:
            os.remove(self.test_image_destination)
        except:
            pass

        try:
            os.remove(self.test_image2_destination)
        except:
            pass

        self.person.remove_local_images()
        self.person.remove_remote_images()

    def test_profile_photo_process(self):

        # Copy to test area
        shutil.copy2(self.test_image2, self.test_image2_destination)

        # Add profile photo
        self.person.set_profile_image_crop_rotate_resize(
            self.test_image2_destination, 372, 406, 878, 1378, 0, True)
        self.person.save()

        profile_photo_process_id = Queue.objects.get(
            name='profile_photo_process').id

        message = Message.objects.create(queue_id=profile_photo_process_id,
                                         integer_data=self.person.id)

        profile_photo_process([message])
        face_model = FaceModel.objects.filter(family_id=self.family.id).first()

        X = pickle.loads(face_model.fit_data_faces)

        message = Message.objects.get(pk=message.id)

        self.assertEqual(1, len(X))
        self.assertEqual(False, message.error)
Example #2
0
class TrainTestCase(TestCase):  # pragma: no cover
    def setUp(self):
        '''
        Need to create a family and a gallery
        '''
        self.family = Family()
        self.family.save()

        self.gallery = Gallery.objects.create(title="test_gallery",
                                              family_id=self.family.id)

        clear_directory(settings.FACE_RECOG_TRAIN_TEST_DIR)

        self.test_image = os.path.join(
            settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg')
        self.test_image_destination = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/test_image.jpg'
        ])
        self.test_image_s3_key = ''.join([
            'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/test_image.jpg'
        ])

        directory = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id)
        ])
        if not os.path.exists(directory):
            os.makedirs(directory)

        #Copy test image to media area
        shutil.copy2(self.test_image, self.test_image_destination)

        self.image = Image(gallery=self.gallery,
                           family=self.family,
                           original_image=''.join([
                               'galleries/',
                               str(self.family.id), '/',
                               str(self.gallery.id), '/test_image.jpg'
                           ]))
        self.image.save()
        self.image.upload_files_to_s3()

        self.person = Person(name='Wallace',
                             gender='M',
                             email='*****@*****.**',
                             family_id=self.family.id,
                             language='en')
        self.person.save()

        self.tag = Tag.objects.create(image_id=self.image.id,
                                      x1=0.279,
                                      y1=0.188,
                                      x2=0.536,
                                      y2=0.381,
                                      person_id=self.person.id,
                                      face_detected=True)

    def test_get_file_for_tag(self):

        dir_name = settings.FACE_RECOG_TRAIN_TEST_DIR
        file = get_file_for_tag(self.tag, self.image, dir_name)

        self.assertIsNotNone(file)

        #Clear up
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()

    def test_process_file(self):

        X = []
        y = []
        file = self.test_image_destination

        process_file(file, X, y, self.person.id)

        self.assertEqual(1, len(X))
        self.assertEqual(1, len(y))

        #Clear up
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()

    def test_process_person(self):
        path = settings.MEDIA_ROOT + 'profile_photos/large_test_image1.jpg'
        shutil.copy2(self.test_image, path)

        self.person.set_profile_image_crop_rotate_resize(
            path, 1, 1, 1200, 1700, 0, True)
        self.person.save()

        X = []
        y = []

        process_person(self.person, X, y)

        self.person.remove_local_images()
        self.person.remove_remote_images()

        self.assertEqual(2, len(X))
        self.assertEqual(2, len(y))

        #Clear up
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()

    def test_process_family(self):
        path = settings.MEDIA_ROOT + 'profile_photos/large_test_image1.jpg'
        shutil.copy2(self.test_image, path)

        self.person.set_profile_image_crop_rotate_resize(
            path, 1, 1, 1200, 1700, 0, True)
        self.person.save()

        process_family(self.family.id)

        self.person.remove_local_images()
        self.person.remove_remote_images()

        face_model = FaceModel.objects.get(family_id=self.family.id)

        self.assertTrue(self.person.id in face_model.fit_data_person_ids)
        self.assertIsNotNone(face_model.trained_knn_model)

        #Clear up
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()