def test_detect_base64(self): """ API-Rest URL /estimation-requests/gender/face/base64 works for base64 requests. """ image = Image("main/samples/image1.jpg") # Black and white image.load_from_uri(as_gray=True) jpeg_b64_content = base64.b64encode(image.get_jpeg()) response = self.send_request(jpeg_b64_content, "base64", {}) self.assertEqual(len(response), 1) self.assertIn("Gender", response) # Color image.load_from_uri(as_gray=False) jpeg_b64_content = base64.b64encode(image.get_jpeg()) response = self.send_request(jpeg_b64_content, "base64", {}) self.assertEqual(len(response), 1) self.assertIn("Gender", response) jpeg_b64_content = base64.b64encode( b'asdasd1io23u897das0dasdoasijdoasidja') response = self.send_request(jpeg_b64_content, "base64", {}) self.assertEqual( response, { 'message': "Content of file not valid: Resource was empty. " "Couldn't perform the analysis on an empty resource." })
def test_detect_stream_service_parameter(self): """ STREAM can work with default and different services. :return: """ image = Image("main/samples/example_image.jpg") image.load_from_uri(as_gray=True) jpeg_content = image.get_jpeg() response = self.send_request( jpeg_content, "stream", {"service": "caffe-cnn-levi-hassner-gender-estimation"}) response2 = self.send_request(jpeg_content, "stream", {}) response3 = self.send_request(jpeg_content, "stream", {"service": "default"}) response4 = self.send_request(jpeg_content, "stream", {"service": "INVALID SERVICE TEST"}) self.assertEqual(len(response), 1) self.assertEqual(len(response2), 1) self.assertEqual(len(response3), 1) self.assertEqual(len(response4), 1) self.assertIn("Gender", response) self.assertIn("Gender", response2) self.assertIn("Gender", response3) self.assertEqual(set(response), set(response2)) self.assertEqual(set(response2), set(response3)) self.assertEqual( response4, {'message': "Service name 'INVALID SERVICE TEST' not valid."})
def test_apply_different_width_height_forcefully(self): """ AbsoluteSizeNormalizer is able to apply to an image forcefully a different width and height. :return: """ sample_image_to_test = Image(uri=self.image_sample_uri) sample_image_to_test.load_from_uri() sizes = [(400, 100), (500, 500), (100, 400)] for size in sizes: normalizer = AbsoluteSizeNormalizer(size[0], size[1], keep_aspect_ratio=False) image_result = normalizer.apply(sample_image_to_test) self.assertNotEqual(image_result, sample_image_to_test) self.assertNotEqual(image_result.get_blob(), sample_image_to_test.get_blob()) new_size = image_result.get_size() self.assertEqual(size, new_size)
def test_detect_stream(self): """ API-Rest URL /estimation-requests/gender/face/stream works for stream requests. """ image = Image("main/samples/example_image.jpg") # Grayscale image.load_from_uri(as_gray=True) jpeg_content = image.get_jpeg() response = self.send_request(jpeg_content, "stream", {}) self.assertEqual(len(response), 1) self.assertIn("Gender", response) # Color image.load_from_uri(as_gray=False) jpeg_content = image.get_jpeg() response = self.send_request(jpeg_content, "stream", {}) self.assertEqual(len(response), 1) self.assertIn("Gender", response) jpeg_content = b'asdasd1io23u897das0dasdoasijdoasidja' response = self.send_request(jpeg_content, "stream", {}) self.assertEqual( response, { 'message': "Content of file not valid: Resource was empty. " "Couldn't perform the analysis on an empty resource." })
def test_apply_equal_width_height_keeping_aspect_ratio(self): """ AbsoluteSizeNormalizer is able to apply to an image keeping the aspect ratio if the width and height are equal. :return: """ size = (500, 500) sample_image_to_test = Image(uri=self.image_sample_uri) sample_image_to_test.load_from_uri() normalizer = AbsoluteSizeNormalizer(size[0], size[1], keep_aspect_ratio=True) image_result = normalizer.apply(sample_image_to_test) self.assertNotEqual(image_result, sample_image_to_test) self.assertNotEqual(image_result.get_blob(), sample_image_to_test.get_blob()) original_size = sample_image_to_test.get_size() new_size = image_result.get_size() index, value = max(enumerate(original_size), key=lambda v: v[1]) self.assertEqual(size[index], new_size[index]) self.assertLess(new_size[index - 1], size[index - 1])
def test_apply_different_width_height_keeping_aspect_ratio(self): """ AbsoluteSizeNormalizer is able to apply to an image keeping the aspect ratio if the width and height are different. :return: """ sample_image_to_test = Image(uri=self.image_sample_uri) sample_image_to_test.load_from_uri() sizes = [(400, 100), (100, 400)] for size in sizes: normalizer = AbsoluteSizeNormalizer(size[0], size[1], keep_aspect_ratio=True) image_result = normalizer.apply(sample_image_to_test) self.assertNotEqual(image_result, sample_image_to_test) self.assertNotEqual(image_result.get_blob(), sample_image_to_test.get_blob()) new_size = image_result.get_size() index, value = min(enumerate(size), key=lambda v: v[1]) self.assertEqual(size[index], new_size[index]) self.assertLess(new_size[index - 1], size[index - 1])
def test_service_multiple_append_request_promises_equal_for_same_resource( self): """ Appending multiple request of a similar resource at the same time returns the same promise. """ image1 = Image(uri="main/samples/image1.jpg", image_id="1") image2 = Image(uri="main/samples/image1.jpg", image_id="2") # Images must be loaded. If not, they will have different md5 hashes image1.load_from_uri(True) image2.load_from_uri(True) result_promise1 = self.service.append_request(image1) result_promise2 = self.service.append_request(image2) result_promise3 = self.service.append_request(image1) # The promise must be equal for both self.assertEqual(result_promise1, result_promise3) self.assertEqual(result_promise1, result_promise2) result1 = result_promise1.get_resource() result2 = result_promise2.get_resource() self.assertIsNotNone(result1) self.assertIsNotNone(result2) self.assertGreater(len(result1.get_metadata()), 0) self.assertGreater(len(result2.get_metadata()), 0) self.assertEqual(result1, result2)
def test_service_single_append_request(self): """ Appending a single request works. """ image = Image("main/samples/image1.jpg") image.load_from_uri(True) result_promise = self.service.append_request(image) result = result_promise.get_resource() self.assertIsNotNone(result) self.assertGreater(len(result.get_metadata()), 0)
def test_detect_base64(self): """ API-Rest URL /ensemble-requests/faces/detection-estimation-age-gender/base64 works for base64 requests. """ image = Image("main/samples/image1.jpg") image.load_from_uri(as_gray=True) jpeg_b64_content = base64.b64encode(image.get_jpeg()) response = self.send_request(jpeg_b64_content, "base64", {}) self.assertEqual(len(response), 3) ids = [] for face_index, content in response.items(): self.assertIn('Bounding_box', content) self.assertIn('Gender', content) self.assertIn('Age_range', content) self.assertIn('Face_ID', content) self.assertNotIn(face_index, ids) ids.append(int(face_index))
def test_detect_stream_without_gender(self): """ Face-ensemble API-Rest works when requested without gender. """ image = Image("main/samples/image1.jpg") image.load_from_uri(as_gray=True) jpeg_content = image.get_jpeg() response = self.send_request(jpeg_content, "stream", {"service_gender": "none"}) self.assertEqual(len(response), 3) ids = [] for face_index, content in response.items(): self.assertIn('Bounding_box', content) self.assertNotIn('Gender', content) self.assertIn('Age_range', content) self.assertIn('Face_ID', content) self.assertNotIn(face_index, ids) ids.append(int(face_index))
def test_service_multiple_append_request(self): """ Appending multiple request at the same time works. """ # By default we use the OpenCV face detection algorithm image = Image("main/samples/image1.jpg") image2 = Image("main/samples/kids-6-to-12.jpg") image.load_from_uri(True) image2.load_from_uri(True) result_promise1 = self.service.append_request(image) result_promise2 = self.service.append_request(image2) result = result_promise1.get_resource() result2 = result_promise2.get_resource() self.assertIsNotNone(result) self.assertIsNotNone(result2) self.assertGreater(len(result.get_metadata()), 0) self.assertGreater(len(result2.get_metadata()), 0)