스트리밍 동영상의 유해성 콘텐츠 감지
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
스트리밍 동영상의 유해성 콘텐츠를 감지합니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
Java
Video Intelligence에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다.
자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.
Node.js
Video Intelligence에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다.
자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.
Python
Video Intelligence에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다.
자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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)."]]