為串流影片加上註解
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
為串流影片加上註解。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
Java
如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Python
如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Annotate a streaming video.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Label analysis](/video-intelligence/docs/streaming/label-analysis)\n\nCode sample\n-----------\n\n### Java\n\n\nTo authenticate to Video Intelligence, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.BidiStream.html;\n import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation;\n import com.google.cloud.videointelligence.v1p3beta1.LabelFrame;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html;\n import io.grpc.StatusRuntimeException;\n import java.io.IOException;\n import java.nio.file.Files;\n import java.nio.file.Path;\n import java.nio.file.Paths;\n import java.util.Arrays;\n import java.util.concurrent.TimeoutException;\n\n class StreamingLabelDetection {\n\n // Perform streaming video label detection\n static void streamingLabelDetection(String filePath)\n throws IOException, TimeoutException, StatusRuntimeException {\n // String filePath = \"path_to_your_video_file\";\n\n try (StreamingVideoIntelligenceServiceClient client =\n StreamingVideoIntelligenceServiceClient.create()) {\n\n Path path = Paths.get(filePath);\n byte[] data = Files.readAllBytes(path);\n // Set the chunk size to 5MB (recommended less than 10MB).\n int chunkSize = 5 * 1024 * 1024;\n int numChunks = (int) Math.ceil((double) data.length / chunkSize);\n\n StreamingLabelDetectionConfig labelConfig =\n StreamingLabelDetectionConfig.newBuilder().setStationaryCamera(false).build();\n\n StreamingVideoConfig streamingVideoConfig =\n StreamingVideoConfig.newBuilder()\n .setFeature(StreamingFeature.STREAMING_LABEL_DETECTION)\n .setLabelDetectionConfig(labelConfig)\n .build();\n\n BidiStream\u003cStreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse\u003e call =\n client.streamingAnnotateVideoCallable().call();\n\n // The first request must **only** contain the audio configuration:\n call.send(\n StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build());\n\n // Subsequent requests must **only** contain the audio data.\n // Send the requests in chunks\n for (int i = 0; i \u003c numChunks; i++) {\n call.send(\n StreamingAnnotateVideoRequest.newBuilder()\n .setInputContent(\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html#com_google_protobuf_ByteString_copyFrom_byte___(\n Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))\n .build());\n }\n\n // Tell the service you are done sending data\n call.closeSend();\n\n for (StreamingAnnotateVideoResponse response : call) {\n StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();\n\n for (LabelAnnotation annotation : annotationResults.getLabelAnnotationsList()) {\n String entity = annotation.getEntity().https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Descriptors.DescriptorValidationException.html#com_google_protobuf_Descriptors_DescriptorValidationException_getDescription__();\n\n // There is only one frame per annotation\n LabelFrame labelFrame = annotation.getFrames(0);\n double offset =\n labelFrame.getTimeOffset().getSeconds() + labelFrame.getTimeOffset().getNanos() / 1e9;\n float confidence = labelFrame.getConfidence();\n\n System.out.format(\"%fs: %s (%f)\\n\", offset, entity, confidence);\n }\n }\n }\n }\n }\n\n### Node.js\n\n\nTo authenticate to Video Intelligence, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * TODO(developer): Uncomment these variables before running the sample.\n */\n // const path = 'Local file to analyze, e.g. ./my-file.mp4';\n const {StreamingVideoIntelligenceServiceClient} =\n require('https://cloud.google.com/nodejs/docs/reference/video-intelligence/latest/overview.html').v1p3beta1;\n const fs = require('fs');\n\n // Instantiates a client\n const client = new https://cloud.google.com/nodejs/docs/reference/video-intelligence/latest/video-intelligence/v1p3beta1.streamingvideointelligenceserviceclient.html();\n // Streaming configuration\n const configRequest = {\n videoConfig: {\n feature: 'https://cloud.google.com/nodejs/docs/reference/video-intelligence/latest/video-intelligence/protos.google.cloud.videointelligence.v1p3beta1.streamingfeature.html',\n },\n };\n const readStream = fs.createReadStream(path, {\n highWaterMark: 5 * 1024 * 1024, //chunk size set to 5MB (recommended less than 10MB)\n encoding: 'base64',\n });\n //Load file content\n const chunks = [];\n readStream\n .on('data', chunk =\u003e {\n const request = {\n inputContent: chunk.toString(),\n };\n chunks.push(request);\n })\n .on('close', () =\u003e {\n // configRequest should be the first in the stream of requests\n stream.write(configRequest);\n for (let i = 0; i \u003c chunks.length; i++) {\n stream.write(chunks[i]);\n }\n stream.end();\n });\n\n const stream = client.streamingAnnotateVideo().on('data', response =\u003e {\n //Gets annotations for video\n const annotations = response.annotationResults;\n const labels = annotations.labelAnnotations;\n labels.forEach(label =\u003e {\n console.log(\n `Label ${label.entity.description} occurs at: ${\n label.frames[0].timeOffset.seconds || 0\n }` + `.${(label.frames[0].timeOffset.nanos / 1e6).toFixed(0)}s`\n );\n console.log(` Confidence: ${label.frames[0].confidence}`);\n });\n });\n\n### Python\n\n\nTo authenticate to Video Intelligence, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import videointelligence_v1p3beta1 as videointelligence\n\n # path = 'path_to_file'\n\n client = videointelligence.StreamingVideoIntelligenceServiceClient()\n\n # Set streaming config.\n config = videointelligence.StreamingVideoConfig(\n feature=(videointelligence.StreamingFeature.STREAMING_LABEL_DETECTION)\n )\n\n # config_request should be the first in the stream of requests.\n config_request = videointelligence.StreamingAnnotateVideoRequest(\n video_config=config\n )\n\n # Set the chunk size to 5MB (recommended less than 10MB).\n chunk_size = 5 * 1024 * 1024\n\n # Load file content.\n stream = []\n with io.open(path, \"rb\") as video_file:\n while True:\n data = video_file.read(chunk_size)\n if not data:\n break\n stream.append(data)\n\n def stream_generator():\n yield config_request\n for chunk in stream:\n yield videointelligence.StreamingAnnotateVideoRequest(input_content=chunk)\n\n requests = stream_generator()\n\n # streaming_annotate_video returns a generator.\n # The default timeout is about 300 seconds.\n # To process longer videos it should be set to\n # larger than the length (in seconds) of the stream.\n responses = client.streaming_annotate_video(requests, timeout=600)\n\n # Each response corresponds to about 1 second of video.\n for response in responses:\n # Check for errors.\n if response.error.message:\n print(response.error.message)\n break\n\n label_annotations = response.annotation_results.label_annotations\n\n # label_annotations could be empty\n if not label_annotations:\n continue\n\n for annotation in label_annotations:\n # Each annotation has one frame, which has a timeoffset.\n frame = annotation.frames[0]\n time_offset = (\n frame.time_offset.seconds + frame.time_offset.microseconds / 1e6\n )\n\n description = annotation.entity.description\n confidence = annotation.frames[0].confidence\n # description is in Unicode\n print(\n \"{}s: {} (confidence: {})\".format(time_offset, description, confidence)\n )\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=video)."]]