检测流式视频中的露骨内容
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
检测流式视频中的露骨内容。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Java
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。
如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。
如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。
如需了解详情,请参阅为本地开发环境设置身份验证。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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,["# Detect explicit content in a streaming video.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Explicit content](/video-intelligence/docs/streaming/explicit-content)\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.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\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)."]]