github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/ml/gcp/videointelligenceml_test_it.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 # pytype: skip-file 18 19 """An integration test that labels entities appearing in a video and checks 20 if some expected entities were properly recognized.""" 21 22 import unittest 23 24 import hamcrest as hc 25 import pytest 26 27 import apache_beam as beam 28 from apache_beam.testing.test_pipeline import TestPipeline 29 from apache_beam.testing.util import assert_that 30 from apache_beam.testing.util import matches_all 31 32 # Protect against environments where Google Cloud VideoIntelligence client is 33 # not available. 34 try: 35 from apache_beam.ml.gcp.videointelligenceml import AnnotateVideoWithContext 36 from google.cloud.videointelligence import enums 37 from google.cloud.videointelligence import types 38 except ImportError: 39 AnnotateVideoWithContext = None 40 41 42 def extract_entities_descriptions(response): 43 for result in response.annotation_results: 44 for segment in result.segment_presence_label_annotations: 45 yield segment.entity.description 46 47 48 @pytest.mark.it_postcommit 49 @unittest.skipIf( 50 AnnotateVideoWithContext is None, 'GCP dependencies are not installed') 51 class VideoIntelligenceMlTestIT(unittest.TestCase): 52 VIDEO_PATH = 'gs://apache-beam-samples/advanced_analytics/video/' \ 53 'gbikes_dinosaur.mp4' 54 55 def test_label_detection_with_video_context(self): 56 with TestPipeline(is_integration_test=True) as p: 57 output = ( 58 p 59 | beam.Create([( 60 self.VIDEO_PATH, 61 types.VideoContext( 62 label_detection_config=types.LabelDetectionConfig( 63 label_detection_mode=enums.LabelDetectionMode.SHOT_MODE, 64 model='builtin/latest')))]) 65 | AnnotateVideoWithContext(features=[enums.Feature.LABEL_DETECTION]) 66 | beam.ParDo(extract_entities_descriptions) 67 | beam.combiners.ToList()) 68 69 # Search for at least one entity that contains 'bicycle'. 70 assert_that( 71 output, matches_all([hc.has_item(hc.contains_string('bicycle'))])) 72 73 74 if __name__ == '__main__': 75 unittest.main()