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_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_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_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 /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_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_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 setUp(self): """ Basic set up for the unit tests. """ self.algorithm = LeviHassnerCNNGenderEstimationAlgorithm() self.sampleImageToTest = Image("main/samples/example_image.jpg") self.gender_to_match = Gender(GENDER_FEMALE)
def _build_image_from_uri(uri): """ Builds an image instance from a URI. This method won't make checks or loads from the specified URI. :param uri: uri to create the image. :return: image instance wrapping the URI. """ return Image(uri=uri)
def setUp(self): """ Basic set up for the unit tests. """ self.algorithm = LeviHassnerCNNAgeEstimationAlgorithm() self.sampleImageToTest = Image("main/samples/example_image.jpg") self.age_to_match = AgeRange(0, 2)
def test_apply_without_load_raise_exception(self): """ AbsoluteSizeNormalizer raises exception if image is not loaded when applied. """ sample_image_to_test = Image(uri=self.image_sample_uri) normalizer = AbsoluteSizeNormalizer(500, 500, keep_aspect_ratio=True) with self.assertRaises(ImageNotLoaded): normalizer.apply(sample_image_to_test)
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_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_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 _build_image_from_content(content, as_gray=True): """ Builds the image resource from the image raw content. :param content: Image content to be loaded. :return: Image resource containing the content of the image parsed by OpenCV loader. """ if as_gray: read_flag = cv2.IMREAD_GRAYSCALE else: read_flag = cv2.IMREAD_COLOR nparr = numpy.fromstring(content, numpy.uint8) content = cv2.imdecode(nparr, read_flag) return Image(uri="memorycontent", image_id="memory", blob_content=content)
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)
def __init__(self, config_file=DEFAULT_CONFIG_FILE, ignore_service_when_algorithm_not_available=False): """ Initializes the object with the config file loaded into memory. It will check that the syntax of the file is correct. :param ignore_service_when_algorithm_not_available: if set to true, when an algorithm is not available, the service that owns it will be discarded. Otherwise an exception will be thrown. """ settings_loader = configparser.ConfigParser() settings_loader._interpolation = configparser.ExtendedInterpolation() _ = settings_loader.read(config_file, encoding="UTF-8") image = Image(config_file) # Stores each of the services definition by section name ( algorithm, name, description, workers, GPU ) self.services_definition = {} self.web_app_definition = {'ip': '0.0.0.0', 'port': 1025} self._build_available_services_definition(settings_loader) self._check_definitions_correctness( ignore_service_when_algorithm_not_available)
def setUp(self): """ Basic set up for the unit tests. """ self.algorithm = MTCNNFaceDetectionAlgorithm() self.sampleImageToTest = Image("main/samples/image1.jpg")