0
# Streaming Analysis
1
2
Real-time video analysis capabilities for processing video streams. The streaming API is available in the v1p3beta1 beta version and provides immediate feedback on video content as it's being processed.
3
4
## Capabilities
5
6
### StreamingVideoIntelligenceServiceClient
7
8
Synchronous client for streaming video analysis. Processes video streams in real-time and returns analysis results as they become available.
9
10
```python { .api }
11
class StreamingVideoIntelligenceServiceClient:
12
"""
13
Synchronous client for Streaming Video Intelligence Service.
14
15
Service that implements streaming video analysis using AI.
16
"""
17
18
def __init__(
19
self,
20
*,
21
credentials: Optional[ga_credentials.Credentials] = None,
22
transport: Optional[Union[str, StreamingVideoIntelligenceServiceTransport]] = None,
23
client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None,
24
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
25
):
26
"""
27
Instantiate the streaming video intelligence service client.
28
29
Parameters:
30
- credentials: The authorization credentials to attach to requests
31
- transport: The transport to use for communicating with the service
32
- client_options: Optional client options
33
- client_info: The client info used to send a user-agent string
34
"""
35
36
def streaming_annotate_video(
37
self,
38
requests: Iterator[Union[StreamingAnnotateVideoRequest, dict]],
39
retry: OptionalRetry = gapic_v1.method.DEFAULT,
40
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
41
metadata: Sequence[Tuple[str, Union[str, bytes]]] = (),
42
) -> Iterable[StreamingAnnotateVideoResponse]:
43
"""
44
Performs video annotation with streaming requests and responses.
45
46
Parameters:
47
- requests: Iterator of StreamingAnnotateVideoRequest objects
48
- retry: Designation of what errors, if any, should be retried
49
- timeout: The timeout for this request
50
- metadata: Strings which should be sent along with the request as metadata
51
52
Returns:
53
Iterable of StreamingAnnotateVideoResponse objects containing real-time analysis results.
54
"""
55
56
@classmethod
57
def from_service_account_file(
58
cls, filename: str, *args, **kwargs
59
) -> StreamingVideoIntelligenceServiceClient:
60
"""
61
Creates an instance of this client using the provided credentials file.
62
63
Parameters:
64
- filename: The path to the service account private key json file
65
66
Returns:
67
StreamingVideoIntelligenceServiceClient: The constructed client
68
"""
69
70
@classmethod
71
def from_service_account_info(
72
cls, info: dict, *args, **kwargs
73
) -> StreamingVideoIntelligenceServiceClient:
74
"""
75
Creates an instance of this client using the provided credentials info.
76
77
Parameters:
78
- info: The service account private key info in dict format
79
80
Returns:
81
StreamingVideoIntelligenceServiceClient: The constructed client
82
"""
83
```
84
85
### StreamingVideoIntelligenceServiceAsyncClient
86
87
Asynchronous version of the streaming video intelligence client for non-blocking stream processing.
88
89
```python { .api }
90
class StreamingVideoIntelligenceServiceAsyncClient:
91
"""
92
Asynchronous client for Streaming Video Intelligence Service.
93
94
Service that implements streaming video analysis using AI.
95
"""
96
97
def __init__(
98
self,
99
*,
100
credentials: Optional[ga_credentials.Credentials] = None,
101
transport: Optional[Union[str, StreamingVideoIntelligenceServiceAsyncTransport]] = None,
102
client_options: Optional[client_options_lib.ClientOptions] = None,
103
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
104
):
105
"""
106
Instantiate the async streaming video intelligence service client.
107
108
Parameters:
109
- credentials: The authorization credentials to attach to requests
110
- transport: The transport to use for communicating with the service
111
- client_options: Optional client options
112
- client_info: The client info used to send a user-agent string
113
"""
114
115
async def streaming_annotate_video(
116
self,
117
requests: AsyncIterator[Union[StreamingAnnotateVideoRequest, dict]],
118
retry: OptionalRetry = gapic_v1.method.DEFAULT,
119
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
120
metadata: Sequence[Tuple[str, Union[str, bytes]]] = (),
121
) -> AsyncIterable[StreamingAnnotateVideoResponse]:
122
"""
123
Performs video annotation with streaming requests and responses.
124
125
Parameters:
126
- requests: AsyncIterator of StreamingAnnotateVideoRequest objects
127
- retry: Designation of what errors, if any, should be retried
128
- timeout: The timeout for this request
129
- metadata: Strings which should be sent along with the request as metadata
130
131
Returns:
132
AsyncIterable of StreamingAnnotateVideoResponse objects containing real-time analysis results.
133
"""
134
135
@classmethod
136
def from_service_account_file(
137
cls, filename: str, *args, **kwargs
138
) -> StreamingVideoIntelligenceServiceAsyncClient:
139
"""
140
Creates an instance of this client using the provided credentials file.
141
142
Parameters:
143
- filename: The path to the service account private key json file
144
145
Returns:
146
StreamingVideoIntelligenceServiceAsyncClient: The constructed async client
147
"""
148
149
@classmethod
150
def from_service_account_info(
151
cls, info: dict, *args, **kwargs
152
) -> StreamingVideoIntelligenceServiceAsyncClient:
153
"""
154
Creates an instance of this client using the provided credentials info.
155
156
Parameters:
157
- info: The service account private key info in dict format
158
159
Returns:
160
StreamingVideoIntelligenceServiceAsyncClient: The constructed async client
161
"""
162
```
163
164
## Streaming Data Types
165
166
### StreamingAnnotateVideoRequest
167
168
Request object for streaming video analysis containing video data and configuration.
169
170
```python { .api }
171
class StreamingAnnotateVideoRequest:
172
"""
173
The streaming video annotation request message.
174
175
Attributes:
176
video_config: Provides information to the annotator that specifies how to process video
177
input_content: The video data bytes
178
"""
179
180
video_config: StreamingVideoConfig
181
input_content: bytes
182
```
183
184
### StreamingAnnotateVideoResponse
185
186
Response object containing streaming analysis results.
187
188
```python { .api }
189
class StreamingAnnotateVideoResponse:
190
"""
191
StreamingAnnotateVideoResponse contains analysis results for a stream.
192
193
Attributes:
194
error: If set, returns a google.rpc.Status message that specifies the error for the operation
195
annotation_results: Streaming annotation results
196
annotation_results_uri: Google Cloud Storage URI that stores annotation results
197
"""
198
199
error: status_pb2.Status
200
annotation_results: StreamingVideoAnnotationResults
201
annotation_results_uri: str
202
```
203
204
### StreamingVideoConfig
205
206
Configuration for streaming video processing.
207
208
```python { .api }
209
class StreamingVideoConfig:
210
"""
211
Config for streaming video content.
212
213
Attributes:
214
feature: Streaming video annotation feature
215
storage_config: Config for streaming storage
216
automl_action_recognition_config: Config for AUTOML_ACTION_RECOGNITION in streaming mode
217
automl_classification_config: Config for AUTOML_CLASSIFICATION in streaming mode
218
automl_object_tracking_config: Config for AUTOML_OBJECT_TRACKING in streaming mode
219
explicit_content_detection_config: Config for EXPLICIT_CONTENT_DETECTION in streaming mode
220
label_detection_config: Config for STREAMING_LABEL_DETECTION in streaming mode
221
object_tracking_config: Config for STREAMING_OBJECT_TRACKING in streaming mode
222
shot_change_detection_config: Config for SHOT_CHANGE_DETECTION in streaming mode
223
"""
224
225
feature: StreamingFeature
226
storage_config: StreamingStorageConfig
227
automl_action_recognition_config: StreamingAutomlActionRecognitionConfig
228
automl_classification_config: StreamingAutomlClassificationConfig
229
automl_object_tracking_config: StreamingAutomlObjectTrackingConfig
230
explicit_content_detection_config: StreamingExplicitContentDetectionConfig
231
label_detection_config: StreamingLabelDetectionConfig
232
object_tracking_config: StreamingObjectTrackingConfig
233
shot_change_detection_config: StreamingShotChangeDetectionConfig
234
```
235
236
### StreamingFeature
237
238
Enum for streaming-specific features.
239
240
```python { .api }
241
class StreamingFeature(Enum):
242
"""
243
Streaming video annotation feature.
244
245
Values:
246
STREAMING_FEATURE_UNSPECIFIED: Unspecified streaming feature
247
STREAMING_LABEL_DETECTION: Label detection in streaming mode
248
STREAMING_SHOT_CHANGE_DETECTION: Shot change detection in streaming mode
249
STREAMING_EXPLICIT_CONTENT_DETECTION: Explicit content detection in streaming mode
250
STREAMING_OBJECT_TRACKING: Object tracking in streaming mode
251
STREAMING_AUTOML_ACTION_RECOGNITION: AutoML action recognition in streaming mode
252
STREAMING_AUTOML_CLASSIFICATION: AutoML classification in streaming mode
253
STREAMING_AUTOML_OBJECT_TRACKING: AutoML object tracking in streaming mode
254
"""
255
256
STREAMING_FEATURE_UNSPECIFIED = 0
257
STREAMING_LABEL_DETECTION = 1
258
STREAMING_SHOT_CHANGE_DETECTION = 2
259
STREAMING_EXPLICIT_CONTENT_DETECTION = 3
260
STREAMING_OBJECT_TRACKING = 4
261
STREAMING_AUTOML_ACTION_RECOGNITION = 23
262
STREAMING_AUTOML_CLASSIFICATION = 21
263
STREAMING_AUTOML_OBJECT_TRACKING = 22
264
```
265
266
### StreamingVideoAnnotationResults
267
268
Results from streaming video analysis.
269
270
```python { .api }
271
class StreamingVideoAnnotationResults:
272
"""
273
Streaming annotation results corresponding to a portion of the video.
274
275
Attributes:
276
shot_annotations: Shot annotation results for the portion of the video
277
label_annotations: Label annotation results for the portion of the video
278
explicit_annotation: Explicit content annotation results for the portion of the video
279
object_annotations: Object tracking annotation results for the portion of the video
280
"""
281
282
shot_annotations: MutableSequence[VideoSegment]
283
label_annotations: MutableSequence[LabelAnnotation]
284
explicit_annotation: ExplicitContentAnnotation
285
object_annotations: MutableSequence[ObjectTrackingAnnotation]
286
```
287
288
## Usage Examples
289
290
### Basic Streaming Analysis
291
292
```python
293
from google.cloud import videointelligence_v1p3beta1
294
295
def stream_video_analysis():
296
# Create streaming client
297
client = videointelligence_v1p3beta1.StreamingVideoIntelligenceServiceClient()
298
299
# Configure streaming video analysis
300
video_config = videointelligence_v1p3beta1.StreamingVideoConfig(
301
feature=videointelligence_v1p3beta1.StreamingFeature.STREAMING_LABEL_DETECTION,
302
label_detection_config=videointelligence_v1p3beta1.StreamingLabelDetectionConfig()
303
)
304
305
# Create request generator
306
def request_generator():
307
# Initial configuration request
308
yield videointelligence_v1p3beta1.StreamingAnnotateVideoRequest(
309
video_config=video_config
310
)
311
312
# Stream video data chunks
313
with open('video_file.mp4', 'rb') as video_file:
314
while True:
315
chunk = video_file.read(1024 * 1024) # 1MB chunks
316
if not chunk:
317
break
318
yield videointelligence_v1p3beta1.StreamingAnnotateVideoRequest(
319
input_content=chunk
320
)
321
322
# Process streaming responses
323
responses = client.streaming_annotate_video(request_generator())
324
325
for response in responses:
326
if response.annotation_results:
327
for label in response.annotation_results.label_annotations:
328
print(f"Detected: {label.entity.description} (confidence: {label.segments[0].confidence})")
329
```
330
331
### Async Streaming Analysis
332
333
```python
334
import asyncio
335
from google.cloud import videointelligence_v1p3beta1
336
337
async def async_stream_analysis():
338
# Create async streaming client
339
client = videointelligence_v1p3beta1.StreamingVideoIntelligenceServiceAsyncClient()
340
341
# Configure for object tracking
342
video_config = videointelligence_v1p3beta1.StreamingVideoConfig(
343
feature=videointelligence_v1p3beta1.StreamingFeature.STREAMING_OBJECT_TRACKING,
344
object_tracking_config=videointelligence_v1p3beta1.StreamingObjectTrackingConfig()
345
)
346
347
async def async_request_generator():
348
# Configuration request
349
yield videointelligence_v1p3beta1.StreamingAnnotateVideoRequest(
350
video_config=video_config
351
)
352
353
# Simulate streaming video data
354
for i in range(10):
355
# In real usage, this would be actual video data
356
fake_video_data = b'video_chunk_' + str(i).encode()
357
yield videointelligence_v1p3beta1.StreamingAnnotateVideoRequest(
358
input_content=fake_video_data
359
)
360
await asyncio.sleep(0.1) # Simulate streaming delay
361
362
# Process async streaming responses
363
responses = client.streaming_annotate_video(async_request_generator())
364
365
async for response in responses:
366
if response.annotation_results and response.annotation_results.object_annotations:
367
for obj in response.annotation_results.object_annotations:
368
print(f"Tracked object: {obj.entity.description}")
369
370
# Run async streaming
371
asyncio.run(async_stream_analysis())
372
```
373
374
### Configuration Options
375
376
```python
377
from google.cloud import videointelligence_v1p3beta1
378
379
# Multiple feature configuration
380
video_config = videointelligence_v1p3beta1.StreamingVideoConfig(
381
feature=videointelligence_v1p3beta1.StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION,
382
explicit_content_detection_config=videointelligence_v1p3beta1.StreamingExplicitContentDetectionConfig(),
383
storage_config=videointelligence_v1p3beta1.StreamingStorageConfig(
384
enable_storage_annotation_result=True,
385
annotation_result_storage_directory="gs://your-bucket/results/"
386
)
387
)
388
```