def recognize_celebrities(self): """ Detects celebrities in the image. :return: A tuple. The first element is the list of celebrities found in the image. The second element is the list of faces that were detected but did not match any known celebrities. """ try: response = self.rekognition_client.recognize_celebrities( Image=self.image) celebrities = [ RekognitionCelebrity(celeb) for celeb in response['CelebrityFaces'] ] other_faces = [ RekognitionFace(face) for face in response['UnrecognizedFaces'] ] logger.info("Found %s celebrities and %s other faces in %s.", len(celebrities), len(other_faces), self.image_name) except ClientError: logger.exception("Couldn't detect celebrities in %s.", self.image_name) raise else: return celebrities, other_faces
def test_do_celebrity_recognition(make_stubber, stub_runner, make_faces, 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) celebrities = [ RekognitionCelebrity(celebrity, time.time_ns()) for celebrity in make_faces(3, is_celebrity=True) ] with stub_runner(error_code, stop_on_method) as runner: runner.add(rekognition_stubber.stub_start_detection, 'start_celebrity_recognition', video.video, video.get_notification_channel(), job_id) runner.add(rekognition_stubber.stub_get_celebrity_recognition, job_id, job_status, celebrities) if error_code is None: got_celebrities = video.do_celebrity_recognition() assert ([celebrity.to_dict() for celebrity in celebrities ] == [celebrity.to_dict() for celebrity in got_celebrities]) else: with pytest.raises(ClientError) as exc_info: video.do_celebrity_recognition() assert exc_info.value.response['Error']['Code'] == error_code
def do_celebrity_recognition(self): """ Performs celebrity detection on the video. :return: The list of celebrity detection events found in the video. """ return self._do_rekognition_job( "celebrity recognition", self.rekognition_client.start_celebrity_recognition, self.rekognition_client.get_celebrity_recognition, lambda response: [ RekognitionCelebrity(celeb['Celebrity'], celeb['Timestamp']) for celeb in response['Celebrities']])
def recognize_celebrities(self): try: response = self.rekognition_client.recognize_celebrities( Image=self.image) celebrities = [ RekognitionCelebrity(celeb) for celeb in response['CelebrityFaces'] ] other_faces = [ RekognitionFace(face) for face in response['UnrecognizedFaces'] ] logger.info("Found %s celebrities and %s other faces in %s.", len(celebrities), len(other_faces), self.image_name) except ClientError: logger.exception("Couldn't detect celebrities in %s.", self.image_name) raise else: return celebrities, other_faces
def test_recognize_celebrities(make_stubber, make_faces, error_code): rekognition_client = boto3.client('rekognition') rekognition_stubber = make_stubber(rekognition_client) image = RekognitionImage(TEST_IMAGE, 'test-image', rekognition_client) celebrities = [ RekognitionCelebrity(face) for face in make_faces(3, is_celebrity=True) ] normals = [RekognitionFace(face) for face in make_faces(2)] rekognition_stubber.stub_recognize_celebrities(image.image, celebrities, normals, error_code=error_code) if error_code is None: got_celebrities, got_normals = image.recognize_celebrities() assert ([celeb.to_dict() for celeb in celebrities ] == [celeb.to_dict() for celeb in got_celebrities]) assert ([normal.to_dict() for normal in normals ] == [normal.to_dict() for normal in got_normals]) else: with pytest.raises(ClientError) as exc_info: image.recognize_celebrities() assert exc_info.value.response['Error']['Code'] == error_code