def test_do_content_moderation(make_stubber, stub_runner, make_moderation_labels, monkeypatch, error_code, stop_on_method): rekognition_client = boto3.client('rekognition') rekognition_stubber = make_stubber(rekognition_client) job_id = 'test-job-id' job_status = 'TESTING' video = mock_video(monkeypatch, 'SUCCEEDED', rekognition_client) labels = [ RekognitionModerationLabel(label, time.time_ns()) for label in make_moderation_labels(3) ] with stub_runner(error_code, stop_on_method) as runner: runner.add(rekognition_stubber.stub_start_detection, 'start_content_moderation', video.video, video.get_notification_channel(), job_id) runner.add(rekognition_stubber.stub_get_content_moderation, job_id, job_status, labels) if error_code is None: got_labels = video.do_content_moderation() assert ([label.to_dict() for label in labels ] == [label.to_dict() for label in got_labels]) else: with pytest.raises(ClientError) as exc_info: video.do_content_moderation() assert exc_info.value.response['Error']['Code'] == error_code
def do_content_moderation(self): """ Performs content moderation on the video. :return: The list of moderation labels found in the video. """ return self._do_rekognition_job( "content moderation", self.rekognition_client.start_content_moderation, self.rekognition_client.get_content_moderation, lambda response: [ RekognitionModerationLabel(label['ModerationLabel'], label['Timestamp']) for label in response['ModerationLabels']])
def detect_moderation_labels(self): try: response = self.rekognition_client.detect_moderation_labels( Image=self.image) labels = [ RekognitionModerationLabel(label) for label in response['ModerationLabels'] ] logger.info("Found %s moderation labels in %s.", len(labels), self.image_name) except ClientError: logger.exception("Couldn't detect moderation labels in %s.", self.image_name) raise else: return labels
def detect_moderation_labels(self): """ Detects moderation labels in the image. Moderation labels identify content that may be inappropriate for some audiences. :return: The list of moderation labels found in the image. """ try: response = self.rekognition_client.detect_moderation_labels( Image=self.image) labels = [RekognitionModerationLabel(label) for label in response['ModerationLabels']] logger.info( "Found %s moderation labels in %s.", len(labels), self.image_name) except ClientError: logger.exception( "Couldn't detect moderation labels in %s.", self.image_name) raise else: return labels
def test_detect_moderation_labels(make_stubber, make_moderation_labels, error_code): rekognition_client = boto3.client('rekognition') rekognition_stubber = make_stubber(rekognition_client) image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client) labels = [ RekognitionModerationLabel(label) for label in make_moderation_labels(3) ] rekognition_stubber.stub_detect_moderation_labels(image.image, labels, error_code=error_code) if error_code is None: got_labels = image.detect_moderation_labels() assert ([label.to_dict() for label in labels ] == [label.to_dict() for label in got_labels]) else: with pytest.raises(ClientError) as exc_info: image.detect_moderation_labels() assert exc_info.value.response['Error']['Code'] == error_code