def test_get_exif_data2(self):
        '''
        Tests we can extract gps data from an image
        '''
        exif_test_image = os.path.join(settings.BASE_DIR,
                                       'gallery/tests/exif_test_2.jpg')
        exif_test_image_destination = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/exif_test.jpg'
        ])
        shutil.copy2(exif_test_image, exif_test_image_destination)

        image = Image(gallery=self.gallery,
                      family=self.family,
                      original_image=exif_test_image_destination)

        image._populate_exif_data()

        self.assertEqual(
            datetime(2015, 6, 21, 13, 50, 35).replace(tzinfo=utc),
            image.date_taken)
        self.assertEqual(True, image.latitude != 0)
        self.assertEqual(True, image.longitude != 0)

        #Clear up mess afterwards
        os.remove(exif_test_image_destination)
        image.delete_local_image_files()
        image.delete_remote_image_files()
示例#2
0
    def test_person_gallery_with_auto_open_image_does_not_load_for_another_family(self):
        '''
        Tests specified photo does not open if in another family
        '''

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

        image = Image(
                    gallery=self.gallery,
                    family=self.another_family,
                    original_image=self.test_image_destination,
                    thumbnail=self.test_image_destination,
                    large_thumbnail=self.test_image_destination
                    )
        image.save

        p = Person.objects.create(name='badger', family_id=self.family.id)
        self.client.login(email='*****@*****.**', password='******')
        response = self.client.get('/person={0}/photos/image={1}/'.format(p.id, image.id))

        self.assertEqual(404, response.status_code)

        image.delete_local_image_files()
        image.delete_remote_image_files()
示例#3
0
    def test_partial_update_remove_thumbnail(self):
        image = Image(gallery=self.gallery,
                      family=self.family,
                      original_image=''.join([
                          'galleries/',
                          str(self.family.id), '/',
                          str(self.gallery.id), '/test_image.jpg'
                      ]))
        image.save()

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/gallery/{0}/'.format(self.gallery.id)

        data = {
            'thumbnail_id': '',
        }

        response = client.patch(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue('"thumbnail":null' in response.content.decode("utf-8"))
        json.loads(response.content)

        image.delete_local_image_files()
        image.delete_remote_image_files()
示例#4
0
    def test_gallery_with_auto_open_image_loads(self):
        '''
        Tests that the gallery view loads when a photo to open by
        is specified
        '''

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

        im = Image(
                    gallery=self.gallery,
                    family=self.family,
                    original_image=self.test_image_destination,
                    thumbnail=self.test_image_destination,
                    large_thumbnail=self.test_image_destination
                )
        im.save()

        self.client.login(email='*****@*****.**', password='******')
        response = self.client.get('/gallery={0}/image={1}/'.format(self.gallery.id, im.id))

        im.delete_local_image_files()
        im.delete_remote_image_files()

        self.assertEqual(200, response.status_code)
        self.assertTemplateUsed(response, 'gallery/gallery.html')
示例#5
0
    def test_geocode_image_location_post(self):
        '''
        Test you can geocode image view
        '''

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

        im = Image(
                    gallery=self.gallery,
                    family=self.family,
                    original_image=self.test_image_destination,
                    thumbnail=self.test_image_destination,
                    large_thumbnail=self.test_image_destination
                )
        im.save()

        self.client.login(email='*****@*****.**', password='******')
        response = self.client.post('/image={0}/address/'.format(im.id), {'address': 'Freddie Mercury Statue Montreux Switzerland'})

        im.delete_local_image_files()
        im.delete_remote_image_files()

        self.assertEqual(200, response.status_code)
        self.assertEqual(True, b'46.43' in response.content)
        self.assertEqual(True, b'6.9' in response.content)

        im = Image.objects.get(id=im.id)
        self.assertEqual(46.43, round(im.latitude,2))
        self.assertEqual(6.9, round(im.longitude,1))
示例#6
0
    def test_partial_update(self):
        shutil.copy2(self.test_image, self.test_image_destination)

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

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

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/gallery/{0}/'.format(self.gallery.id)

        data = {
            'family_id': self.family2.id,  # try to switch families
            'title': 'new title',
            'description': 'new description',
            'thumbnail_id': image2.id,
        }

        response = client.patch(url, data, format='json')

        gallery = Gallery.objects.get(id=self.gallery.id)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual('new title', gallery.title)
        self.assertEqual(self.family.id, gallery.family_id)
        self.assertTrue(b'new title' in response.content)
        self.assertTrue(b'new description' in response.content)
        self.assertTrue(
            str(image2.thumbnail) in response.content.decode("utf-8"))

        image.delete_local_image_files()
        image.delete_remote_image_files()
        image2.delete_local_image_files()
        image2.delete_remote_image_files()
    def test_make_thumbnails_and_delete(self):
        '''
        Tests the make thumbnails routine
        '''
        #Copy test image to media area
        shutil.copy2(self.test_image, self.test_image_destination)
        image = Image(gallery=self.gallery,
                      family=self.family,
                      original_image=self.test_image_destination)
        image.make_thumbnails()

        PIL.Image.open(settings.MEDIA_ROOT + str(image.thumbnail))
        PIL.Image.open(settings.MEDIA_ROOT + str(image.large_thumbnail))
        PIL.Image.open(settings.MEDIA_ROOT + str(self.gallery.thumbnail))

        #Clear up mess afterwards
        image.delete_local_image_files()
        image.delete_remote_image_files()
示例#8
0
    def test_image_face_detect(self):

        # Upload new image
        new_test_image = os.path.join(
            settings.BASE_DIR,
            'facial_recognition/tests/test_image_woman_and_baby.jpg')
        new_test_image_destination = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/test_image_woman_and_baby.jpg'
        ])

        # Copy to test area
        shutil.copy2(new_test_image, new_test_image_destination)

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

        # Create a message to resize tag
        image_face_detect_queue_id = Queue.objects.get(
            name='image_face_detect').id
        message = Message.objects.create(queue_id=image_face_detect_queue_id,
                                         integer_data=new_image.id)

        image_face_detect([message])

        suggested_tags = SuggestedTag.objects.filter(image_id=new_image.id)

        self.assertEqual(2, suggested_tags.count())
        self.assertEqual(self.person.id, suggested_tags[0].person_id)

        new_image.delete_local_image_files()
        new_image.delete_remote_image_files()
示例#9
0
    def test_save_and_rotate_image(self):
        '''
        Tests that we can save an image and rotate it without error
        '''

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

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

        image.rotate(90)

        #Clear up
        image.delete_local_image_files()
        image.delete_remote_image_files()
示例#10
0
    def test_geocode_image_location_post_other_family(self):
        '''
        Test another family cannot geocode image
        '''

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

        im = Image(
                    gallery=self.gallery,
                    family=self.family,
                    original_image=self.test_image_destination,
                    thumbnail=self.test_image_destination,
                    large_thumbnail=self.test_image_destination
                )
        im.save()

        self.client.login(email='*****@*****.**', password='******')
        response = self.client.post('/image={0}/address/'.format(im.id), {'address': 'Freddie Mercury Montreux'})

        im.delete_local_image_files()
        im.delete_remote_image_files()

        self.assertEqual(404, response.status_code)
示例#11
0
class ImageFaceDetectTest(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)

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

    def tearDown(self):

        try:
            self.image.delete_local_image_files()
            self.image.delete_remote_image_files()
        except:
            pass

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

    def test_image_face_detect(self):

        # Upload new image
        new_test_image = os.path.join(
            settings.BASE_DIR,
            'facial_recognition/tests/test_image_woman_and_baby.jpg')
        new_test_image_destination = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/test_image_woman_and_baby.jpg'
        ])

        # Copy to test area
        shutil.copy2(new_test_image, new_test_image_destination)

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

        # Create a message to resize tag
        image_face_detect_queue_id = Queue.objects.get(
            name='image_face_detect').id
        message = Message.objects.create(queue_id=image_face_detect_queue_id,
                                         integer_data=new_image.id)

        image_face_detect([message])

        suggested_tags = SuggestedTag.objects.filter(image_id=new_image.id)

        self.assertEqual(2, suggested_tags.count())
        self.assertEqual(self.person.id, suggested_tags[0].person_id)

        new_image.delete_local_image_files()
        new_image.delete_remote_image_files()
示例#12
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()
示例#13
0
class TestTagViews(TestCase):  # pragma: no cover
    '''
    Test class for the gallery views
    '''
    def setUp(self):
        '''
        Creates credientials as all views require login
        '''
        self.family = Family()
        self.family.save()

        self.user = User.objects.create_user(
            email='*****@*****.**',
            password='******',
            name='White Queen',
            family_id=self.family.id)
        self.person = Person.objects.create(name='White Queen',
                                            family=self.family)
        self.person2 = Person.objects.create(name='Black Queen',
                                             family=self.family)
        self.person3 = Person.objects.create(name='As It Began',
                                             family=self.family)

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

        self.test_image = os.path.join(settings.BASE_DIR,
                                       'gallery/tests/test_image.jpg')
        self.test_image_destination = ''.join([
            settings.MEDIA_ROOT, '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=self.test_image_destination,
                           thumbnail=self.test_image_destination,
                           large_thumbnail=self.test_image_destination)
        self.image.save()

        self.tag1 = Tag.objects.create(image=self.image,
                                       person=self.person,
                                       x1=1,
                                       x2=2,
                                       y1=3,
                                       y2=4)
        self.tag2 = Tag.objects.create(image=self.image,
                                       person=self.person2,
                                       x1=5,
                                       x2=6,
                                       y1=7,
                                       y2=8)

        self.another_family = Family.objects.create()
        self.another_user = User.objects.create_user(
            email='*****@*****.**',
            password='******',
            name='Queen Of Hearts',
            family_id=self.another_family.id)
        self.other_family_person = Person.objects.create(
            name='Queen Adreena', family=self.another_family)

        self.other_family_gallery = Gallery.objects.create(
            family_id=self.another_family.id, title="gallery")
        self.other_family_image = Image(
            gallery=self.other_family_gallery,
            family=self.another_family,
            original_image=self.test_image_destination,
            thumbnail=self.test_image_destination,
            large_thumbnail=self.test_image_destination)
        self.other_family_image.save()

    def tearDown(self):
        try:
            self.image.delete_local_image_files()
            self.image.delete_remote_image_files()
        except:
            pass

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

    def test_list(self):

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        client.force_authenticate(user=self.user)
        url = '/api/image_tagging/?image_id={0}'.format(self.image.id)

        response = client.get(url, format='json')

        # Check it contains both tags
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(b'"person_id":1' in response.content)
        self.assertTrue(b'"person_id":2' in response.content)
        json.loads(response.content)

    def test_list_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/image_tagging/?image_id={0}'.format(self.image.id)
        response = client.get(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        json.loads(response.content)

    def test_list_other_family(self):

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        client.force_authenticate(user=self.another_user)
        url = '/api/image_tagging/?image_id={0}'.format(self.image.id)

        response = client.get(url, format='json')

        self.assertFalse(b'"person_id":1' in response.content)
        self.assertFalse(b'"person_id":2' in response.content)
        json.loads(response.content)

    def test_list_filter_by_person(self):

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        client.force_authenticate(user=self.user)
        url = '/api/image_tagging/?image_id={0}&person_id={1}'.format(
            self.image.id, self.person.id)

        response = client.get(url, format='json')

        # Check it contains both tags
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(b'"person_id":1' in response.content)
        self.assertFalse(b'"person_id":2' in response.content)
        json.loads(response.content)

    def test_destroy(self):

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        client.force_authenticate(user=self.user)
        url = '/api/image_tagging/{0}/'.format(self.tag1.id)

        response = client.delete(url, format='json')

        # Check its deleted
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        tags = Tag.objects.filter(id=self.tag1.id)
        self.assertEqual(0, tags.count())
        json.loads(response.content)

    def test_destroy_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/image_tagging/{0}/'.format(self.tag1)
        response = client.delete(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        json.loads(response.content)

    def test_destroy_other_family(self):

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        client.force_authenticate(user=self.another_user)
        url = '/api/image_tagging/{0}/'.format(self.tag1.id)

        response = client.delete(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        json.loads(response.content)

    def test_create(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)

        data = {
            'person_id': self.person3.id,
            'image_id': self.image.id,
            'x1': 0.1,
            'x2': 0.2,
            'y1': 0.3,
            'y2': 0.4,
        }

        url = '/api/image_tagging/'
        response = client.post(url, data, format='json')

        new_tag = Tag.objects.get(person_id=self.person3.id)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(0.1, new_tag.x1)
        json.loads(response.content)

    def test_create_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        data = {
            'person_id': self.person3.id,
            'image_id': self.image.id,
            'x1': 0.1,
            'x2': 0.2,
            'y1': 0.3,
            'y2': 0.4,
        }

        url = '/api/image_tagging/'
        response = client.post(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        json.loads(response.content)

    def test_create_person_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)

        data = {
            'person_id': self.other_family_person.id,
            'image_id': self.image.id,
            'x1': 0.1,
            'x2': 0.2,
            'y1': 0.3,
            'y2': 0.4,
        }

        url = '/api/image_tagging/'
        response = client.post(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        json.loads(response.content)

    def test_create_image_other_family(self):

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)

        data = {
            'person_id': self.person3.id,
            'image_id': self.other_family_image.id,
            'x1': 0.1,
            'x2': 0.2,
            'y1': 0.3,
            'y2': 0.4,
        }

        url = '/api/image_tagging/'
        response = client.post(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        json.loads(response.content)

    def test_create_invalid_x1(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)

        data = {
            'person_id': self.person3.id,
            'image_id': self.image.id,
            'x1': 'invalid!',
            'x2': 0.2,
            'y1': 0.3,
            'y2': 0.4,
        }

        url = '/api/image_tagging/'
        response = client.post(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        json.loads(response.content)
示例#14
0
class SuggestedTagTestCase(TestCase):  # pragma: no cover
    '''
    Tests for the image class
    '''
    def setUp(self):
        '''
        Need to create a family and a gallery and image
        '''
        self.family = Family()
        self.family.save()

        self.user = User.objects.create_user(
            email='*****@*****.**',
            password='******',
            name='White Queen',
            family_id=self.family.id)

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

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

        self.test_image = os.path.join(settings.BASE_DIR,
                                       'gallery/tests/test_image.jpg')
        self.test_image_destination = ''.join([
            settings.MEDIA_ROOT, '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.image2 = Image(gallery=self.gallery,
                            family=self.family,
                            original_image=''.join([
                                'galleries/',
                                str(self.family.id), '/',
                                str(self.gallery.id), '/test_image.jpg'
                            ]))
        self.image2.save()

        self.another_family = Family()
        self.another_family.save()
        self.another_user = User.objects.create_user(
            email='*****@*****.**',
            password='******',
            name='Queen Of Hearts',
            family_id=self.another_family.id)

        self.suggested_tag = SuggestedTag.objects.create(
            image_id=self.image.id, x1=0.1001, y1=0.2, x2=0.3, y2=0.4)

    def test_convert_to_tag(self):
        '''
        Tests that we can rotate a tag correctly
        '''
        new_tag = self.suggested_tag.convertToTag(person_id=self.person.id)

        self.assertTrue(new_tag.id > 0)
        self.assertEqual(self.person.id, new_tag.person_id)
        self.assertEqual(0.1001, new_tag.x1)

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

    def test_list_suggested_tags(self):

        SuggestedTag.objects.create(image_id=self.image2.id,
                                    x1=0.1123,
                                    y1=0.2,
                                    x2=0.3,
                                    y2=0.4)

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        client.force_authenticate(user=self.user)
        url = '/api/suggested_image_tagging/?image_id={0}'.format(
            self.image.id)

        response = client.get(url, format='json')

        # Check it contains both tags
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(b'0.1001' in response.content)
        self.assertFalse(b'0.1123' in response.content)

    def test_list_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/suggested_image_tagging/?image_id={0}'.format(
            self.image.id)
        response = client.get(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_list_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.another_user)
        url = '/api/suggested_image_tagging/?image_id={0}'.format(
            self.image.id)
        response = client.get(url, format='json')

        self.assertFalse(b'0.1001' in response.content)

    def test_destroy(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/suggested_image_tagging/{0}/'.format(self.suggested_tag.id)

        response = client.delete(url, format='json')

        # Check its deleted
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        tags = SuggestedTag.objects.filter(id=self.suggested_tag.id)
        self.assertEqual(0, tags.count())

    def test_destroy_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/suggested_image_tagging/{0}/'.format(self.suggested_tag)
        response = client.delete(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_destroy_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.another_user)
        url = '/api/suggested_image_tagging/{0}/'.format(self.suggested_tag.id)

        response = client.delete(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_partial_update(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/suggested_image_tagging/{0}/'.format(self.suggested_tag.id)
        data = {'person_id': self.person.id}

        response = client.patch(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(b'0.1001' in response.content)

        # Check suggested tag is deleted
        tags = SuggestedTag.objects.filter(id=self.suggested_tag.id)
        self.assertEqual(0, tags.count())

    def test_partial_update_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/suggested_image_tagging/{0}/'.format(self.suggested_tag.id)
        data = {'person_id': self.person.id}

        response = client.patch(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_partial_update_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.another_user)
        url = '/api/suggested_image_tagging/{0}/'.format(self.suggested_tag.id)
        data = {'person_id': self.person.id}

        response = client.patch(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
示例#15
0
class ImageApiTestCase(TestCase):
    '''
    Tests for the Image API
    '''
    def setUp(self):

        self.family = Family()
        self.family.save()

        self.user = User.objects.create_user(email='*****@*****.**',
                                             password='******',
                                             name='Phil Collins',
                                             family=self.family)

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

        self.gallery = Gallery.objects.create(title="test_gallery",
                                              family_id=self.family.id)
        self.test_image = os.path.join(settings.BASE_DIR,
                                       'gallery/tests/test_image.jpg')
        self.test_image_destination = ''.join([
            settings.MEDIA_ROOT, '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()

        #Tag person 1 in image
        self.tag = Tag.objects.create(image=self.image,
                                      person=self.person,
                                      x1=1,
                                      x2=2,
                                      y1=3,
                                      y2=4)

        self.gallery2 = Gallery.objects.create(title="test_gallery2",
                                               family_id=self.family.id)
        self.test_image2 = os.path.join(settings.BASE_DIR,
                                        'gallery/tests/test_image.jpg')
        self.test_image2_destination = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery2.id), '/test_image.jpg'
        ])

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

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

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

        self.family2 = Family()
        self.family2.save()

        self.user2 = User.objects.create_user(
            email='*****@*****.**',
            password='******',
            name='Phillip Bailey',
            family=self.family2)

        self.person2 = Person(name='Phillip Bailey',
                              gender='M',
                              email='*****@*****.**',
                              family_id=self.family2.id,
                              language='en',
                              user_id=self.user2.id)
        self.person2.save()

        super(ImageApiTestCase, self).setUp()

    def test_list_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        response = client.get('/api/image/?page=1', format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_list_page1(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        # Check this works with JWT token
        auth_details = {
            'email': '*****@*****.**',
            'password': '******'
        }
        auth_response = client.post('/api/auth/obtain_token/',
                                    auth_details,
                                    format='json')
        token = json.loads(auth_response.content)["access"]

        client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
        response = client.get('/api/image/?page=1', format='json')

        # Check it contains both images
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(self.image.thumbnail).encode() in response.content)
        self.assertTrue(
            str(self.image2.thumbnail).encode() in response.content)

    def test_list_page1_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')

        # Login with other user
        auth_details = {
            'email': '*****@*****.**',
            'password': '******'
        }
        auth_response = client.post('/api/auth/obtain_token/',
                                    auth_details,
                                    format='json')
        token = json.loads(auth_response.content)["access"]

        client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
        response = client.get('/api/image/?page=1', format='json')

        # Check it contains neither images
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertFalse(
            str(self.image.thumbnail).encode() in response.content)
        self.assertFalse(
            str(self.image2.thumbnail).encode() in response.content)

    def test_list_by_gallery(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/image/?gallery_id={0}'.format(self.gallery2.id)
        response = client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertFalse(
            str(self.image.thumbnail).encode() in response.content)
        self.assertTrue(
            str(self.image2.thumbnail).encode() in response.content)

    def test_list_by_tagged_person(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/image/?person_id={0}'.format(self.person.id)
        response = client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(self.image.thumbnail).encode() in response.content)
        self.assertFalse(
            str(self.image2.thumbnail).encode() in response.content)

    def test_retrieve_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/image/{0}/'.format(self.image.id)
        response = client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_retrieve(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/image/{0}/'.format(self.image.id)
        response = client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(self.image.thumbnail).encode() in response.content)

    def test_retrieve_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user2)
        url = '/api/image/{0}/'.format(self.image.id)
        response = client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_create(self):
        '''
        test that we can upload a file
        '''
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)

        url = '/api/image/'
        with open(self.test_image, 'rb') as fp:

            data = {
                'picture': fp,
                'gallery_id': self.gallery.id,
            }

            response = client.post(url, data)

        # Check image loads
        image_id = json.loads(response.content)['id']
        image = Image.objects.get(id=image_id)

        image.delete_local_image_files()
        image.delete_remote_image_files()

        self.assertEqual(200, response.status_code)
        self.assertEqual('test_image', image.title)
        self.assertTrue('Phil Collins', image.uploaded_by.name)

    def test_create_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/image/'
        with open(self.test_image, 'rb') as fp:

            data = {
                'picture': fp,
                'gallery_id': self.gallery.id,
            }

            response = client.post(url, data)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user2)
        url = '/api/image/'
        with open(self.test_image, 'rb') as fp:

            data = {
                'picture': fp,
                'gallery_id': self.gallery.id,
            }

            response = client.post(url, data)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_destroy(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/image/{0}/'.format(self.image.id)
        response = client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        count = Image.objects.filter(id=self.image.id).count()
        self.assertEqual(0, count)

    def test_destroy_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/image/{0}/'.format(self.image.id)
        response = client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_destroy_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user2)
        url = '/api/image/{0}/'.format(self.image.id)
        response = client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_partial_update(self):
        self.image.upload_files_to_s3()

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/image/{0}/'.format(self.image.id)

        data = {
            'title': 'new title',
            'description': 'new description',
            'anticlockwise_angle': 90,
            'latitude': 10,
            'longitude': 20,
        }

        response = client.patch(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(b'new title' in response.content)
        self.assertTrue(b'new description' in response.content)
        self.assertTrue(b'10' in response.content)
        self.assertTrue(b'20' in response.content)

        self.image = Image.objects.get(id=self.image.id)
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()

    def test_partial_update_requires_authentication(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        url = '/api/image/{0}/'.format(self.image.id)

        data = {
            'title': 'new title',
            'description': 'new description',
            'anticlockwise_angle': 90,
            'latitude': 10,
            'longitude': 20,
        }

        response = client.patch(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_partial_update_other_family(self):
        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user2)
        url = '/api/image/{0}/'.format(self.image.id)

        data = {
            'title': 'new title',
            'description': 'new description',
            'anticlockwise_angle': 90,
            'latitude': 10,
            'longitude': 20,
        }

        response = client.patch(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_partial_update_invalid_title(self):
        self.image.upload_files_to_s3()

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/image/{0}/'.format(self.image.id)

        data = {
            'title': '    ',
            'description': 'new description',
            'anticlockwise_angle': 90,
            'latitude': 10,
            'longitude': 20,
        }

        response = client.patch(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        self.image = Image.objects.get(id=self.image.id)
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()

    def test_partial_update_optional_data_missing(self):
        self.image.upload_files_to_s3()

        client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
        client.force_authenticate(user=self.user)
        url = '/api/image/{0}/'.format(self.image.id)

        data = {
            'title': 'new title',
            'description': 'new description',
        }

        response = client.patch(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(b'new title' in response.content)
        self.assertTrue(b'new description' in response.content)
        self.assertTrue(b'"longitude":0.0' in response.content)
        self.assertTrue(b'"latitude":0.0' in response.content)

        self.image = Image.objects.get(id=self.image.id)
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()
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)
示例#17
0
class PersonDeletedUpdateFaceModelTest(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 = ''.join([
            settings.MEDIA_ROOT, 'galleries/',
            str(self.family.id), '/',
            str(self.gallery.id), '/test_image_woman_and_baby.jpg'
        ])

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

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

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

        self.tag2 = Tag.objects.create(image_id=self.image2.id,
                                       x1=0.312,
                                       y1=0.239,
                                       x2=0.732,
                                       y2=0.811,
                                       person_id=self.person2.id,
                                       face_detected=True)

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

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

        self.image2.delete_local_image_files()
        self.image2.delete_remote_image_files()

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

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

    def test_update_family_model(self):
        data = {'family_id': self.family.id, 'person_id': self.person.id}

        update_family_model(self.family.id, [data])
        face_model = FaceModel.objects.filter(family_id=self.family.id).first()

        X = pickle.loads(face_model.fit_data_faces)

        self.assertEqual(1, len(X))

    def test_person_deleted_update_face_model(self):

        person_deleted_update_face_model_id = Queue.objects.get(
            name='person_deleted_update_face_model').id

        data = {'family_id': self.family.id, 'person_id': self.person.id}

        message_encoded = json.dumps(data)

        message = Message.objects.create(
            queue_id=person_deleted_update_face_model_id,
            string_data=message_encoded)

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

        X = pickle.loads(face_model.fit_data_faces)

        self.assertEqual(1, len(X))
示例#18
0
class ResizeTagsTestCase(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)

        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.3,
                                      y1=0.2,
                                      x2=0.5,
                                      y2=0.4,
                                      person_id=self.person.id)

    def test_tag_resizes(self):

        # Create a message to resize tag
        resize_tag_queue_id = Queue.objects.get(name='resize_tag').id
        message = Message.objects.create(queue_id=resize_tag_queue_id,
                                         integer_data=self.tag.id)

        resize_tags([message])

        resized_tag = Tag.objects.get(pk=self.tag.id)

        self.assertTrue(abs(0.279 - resized_tag.x1) < 0.001)
        self.assertTrue(abs(0.188 - resized_tag.y1) < 0.001)
        self.assertTrue(abs(0.536 - resized_tag.x2) < 0.001)
        self.assertTrue(abs(0.381 - resized_tag.y2) < 0.001)

        #Clear up
        self.image.delete_local_image_files()
        self.image.delete_remote_image_files()
示例#19
0
class TagTestCase(TestCase):  # pragma: no cover
    '''
    Tests for the image class
    '''
    def setUp(self):
        '''
        Need to create a family and a gallery and image
        '''
        self.family = Family()
        self.family.save()

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

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

        self.test_image = os.path.join(settings.BASE_DIR,
                                       'gallery/tests/test_image.jpg')
        self.test_image_destination = ''.join([
            settings.MEDIA_ROOT, '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()

    def test_rotate_tag(self):
        '''
        Tests that we can rotate a tag correctly
        '''
        tag = Tag.objects.create(image_id=self.image.id,
                                 x1=0.1,
                                 y1=0.2,
                                 x2=0.3,
                                 y2=0.4,
                                 person_id=self.person.id)
        tag.rotate(90)

        self.assertTrue(abs(0.2 - tag.x1) < 0.0001)
        self.assertTrue(abs(0.7 - tag.y1) < 0.0001)
        self.assertTrue(abs(0.4 - tag.x2) < 0.0001)
        self.assertTrue(abs(0.9 - tag.y2) < 0.0001)

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