如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
importcom.google.api.gax.rpc.BidiStream;importcom.google.cloud.videointelligence.v1p3beta1.ExplicitContentFrame;importcom.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;importcom.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;importcom.google.cloud.videointelligence.v1p3beta1.StreamingFeature;importcom.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig;importcom.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults;importcom.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig;importcom.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient;importcom.google.protobuf.ByteString;importio.grpc.StatusRuntimeException;importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.util.Arrays;importjava.util.concurrent.TimeoutException;classStreamingExplicitContentDetection{// Perform streaming video detection for explicit contentstaticvoidstreamingExplicitContentDetection(StringfilePath)throwsIOException,TimeoutException,StatusRuntimeException{// String filePath = "path_to_your_video_file";try(StreamingVideoIntelligenceServiceClientclient=StreamingVideoIntelligenceServiceClient.create()){Pathpath=Paths.get(filePath);byte[]data=Files.readAllBytes(path);// Set the chunk size to 5MB (recommended less than 10MB).intchunkSize=5*1024*1024;intnumChunks=(int)Math.ceil((double)data.length/chunkSize);StreamingLabelDetectionConfiglabelConfig=StreamingLabelDetectionConfig.newBuilder().setStationaryCamera(false).build();StreamingVideoConfigstreamingVideoConfig=StreamingVideoConfig.newBuilder().setFeature(StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION).setLabelDetectionConfig(labelConfig).build();BidiStream<StreamingAnnotateVideoRequest,StreamingAnnotateVideoResponse>call=client.streamingAnnotateVideoCallable().call();// The first request must **only** contain the audio configuration:call.send(StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build());// Subsequent requests must **only** contain the audio data.// Send the requests in chunksfor(inti=0;i < numChunks;i++){call.send(StreamingAnnotateVideoRequest.newBuilder().setInputContent(ByteString.copyFrom(Arrays.copyOfRange(data,i*chunkSize,i*chunkSize+chunkSize))).build());}// Tell the service you are done sending datacall.closeSend();for(StreamingAnnotateVideoResponseresponse:call){StreamingVideoAnnotationResultsannotationResults=response.getAnnotationResults();for(ExplicitContentFrameframe:annotationResults.getExplicitAnnotation().getFramesList()){doubleoffset=frame.getTimeOffset().getSeconds()+frame.getTimeOffset().getNanos()/1e9;System.out.format("Offset: %f\n",offset);System.out.format("\tPornography: %s",frame.getPornographyLikelihood());}}}}}
Node.js
如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
/** * TODO(developer): Uncomment these variables before running the sample. */// const path = 'Local file to analyze, e.g. ./my-file.mp4';const{StreamingVideoIntelligenceServiceClient}=require('@google-cloud/video-intelligence').v1p3beta1;constfs=require('fs');// Instantiates a clientconstclient=newStreamingVideoIntelligenceServiceClient();// Streaming configurationconstconfigRequest={videoConfig:{feature:'STREAMING_EXPLICIT_CONTENT_DETECTION',},};constreadStream=fs.createReadStream(path,{highWaterMark:5*1024*1024,//chunk size set to 5MB (recommended less than 10MB)encoding:'base64',});//Load file contentconstchunks=[];readStream.on('data',chunk=>{constrequest={inputContent:chunk.toString(),};chunks.push(request);}).on('close',()=>{// configRequest should be the first in the stream of requestsstream.write(configRequest);for(leti=0;i < chunks.length;i++){stream.write(chunks[i]);}stream.end();});conststream=client.streamingAnnotateVideo().on('data',response=>{//Gets annotations for videoconstannotations=response.annotationResults;constexplicitContentResults=annotations.explicitAnnotation.frames;explicitContentResults.forEach(result=>{console.log(`Time: ${result.timeOffset.seconds||0}`+`.${(result.timeOffset.nanos/1e6).toFixed(0)}s`);console.log(` Pornography likelihood: ${result.pornographyLikelihood}`);});});
Python
如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
fromgoogle.cloudimportvideointelligence_v1p3beta1asvideointelligence# path = 'path_to_file'client=videointelligence.StreamingVideoIntelligenceServiceClient()# Set streaming config.config=videointelligence.StreamingVideoConfig(feature=(videointelligence.StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION))# config_request should be the first in the stream of requests.config_request=videointelligence.StreamingAnnotateVideoRequest(video_config=config)# Set the chunk size to 5MB (recommended less than 10MB).chunk_size=5*1024*1024# Load file content.stream=[]withio.open(path,"rb")asvideo_file:whileTrue:data=video_file.read(chunk_size)ifnotdata:breakstream.append(data)defstream_generator():yieldconfig_requestforchunkinstream:yieldvideointelligence.StreamingAnnotateVideoRequest(input_content=chunk)requests=stream_generator()# streaming_annotate_video returns a generator.# The default timeout is about 300 seconds.# To process longer videos it should be set to# larger than the length (in seconds) of the stream.responses=client.streaming_annotate_video(requests,timeout=900)# Each response corresponds to about 1 second of video.forresponseinresponses:# Check for errors.ifresponse.error.message:print(response.error.message)breakforframeinresponse.annotation_results.explicit_annotation.frames:time_offset=(frame.time_offset.seconds+frame.time_offset.microseconds/1e6)pornography_likelihood=videointelligence.Likelihood(frame.pornography_likelihood)print("Time: {}s".format(time_offset))print("\tpornogaphy: {}".format(pornography_likelihood.name))
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-10 (世界標準時間)。"],[],[],null,["# Explicit content\n\n| **Beta**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\n[*Explicit Content Detection*](/video-intelligence/docs/analyze-safesearch)\ndetects adult content within a video.\nAdult content is content generally inappropriate for those under under 18 years\nof age, including but is not limited to, nudity, sexual activities,\nand pornography. Such content detected in cartoons or anime is also identified..\n\nThe following code sample demonstrates how to detect the presence\nof explicit content using the streaming client library. \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.ExplicitContentFrame;\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 StreamingExplicitContentDetection {\n\n // Perform streaming video detection for explicit content\n static void streamingExplicitContentDetection(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_EXPLICIT_CONTENT_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 (ExplicitContentFrame frame :\n annotationResults.getExplicitAnnotation().getFramesList()) {\n\n double offset =\n frame.getTimeOffset().getSeconds() + frame.getTimeOffset().getNanos() / 1e9;\n\n System.out.format(\"Offset: %f\\n\", offset);\n System.out.format(\"\\tPornography: %s\", frame.getPornographyLikelihood());\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\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 explicitContentResults = annotations.explicitAnnotation.frames;\n explicitContentResults.forEach(result =\u003e {\n console.log(\n `Time: ${result.timeOffset.seconds || 0}` +\n `.${(result.timeOffset.nanos / 1e6).toFixed(0)}s`\n );\n console.log(` Pornography likelihood: ${result.pornographyLikelihood}`);\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=(\n videointelligence.StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION\n )\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=900)\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 for frame in response.annotation_results.explicit_annotation.frames:\n time_offset = (\n frame.time_offset.seconds + frame.time_offset.microseconds / 1e6\n )\n pornography_likelihood = videointelligence.Likelihood(\n frame.pornography_likelihood\n )\n\n print(\"Time: {}s\".format(time_offset))\n print(\"\\tpornogaphy: {}\".format(pornography_likelihood.name))\n\n\u003cbr /\u003e"]]