Analizzare i video per le etichette
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
L'API Video Intelligence può identificare le entità mostrate nel filmato
utilizzando la LABEL_DETECTION. Questa funzionalità identifica oggetti, località, attività, specie animali, prodotti e altro ancora.
L'analisi può essere suddivisa come segue:
Livello frame: Le entità vengono identificate ed etichettate all'interno di ogni frame
(con un campionamento di un frame al secondo).
Livello ripresa: Le riprese vengono rilevate automaticamente all'interno di ogni segmento
(o video). Le entità vengono quindi identificate ed etichettate all'interno di ogni ripresa.
Livello segmento: È possibile specificare i segmenti di un video selezionati dall'utente
per l'analisi indicando gli offset temporali di inizio e fine ai fini
dell'annotazione (vedi VideoSegment).
Le entità vengono quindi identificate ed etichettate all'interno di ogni segmento. Se non vengono specificati segmenti, l'intero video viene trattato come un unico segmento.
Annotare un file locale
Ecco un esempio di esecuzione dell'analisi video per le etichette in un file locale.
Cerchi qualcosa di più approfondito? Consulta il nostro tutorial dettagliato su Python
tutorial.
REST
Inviare la richiesta di elaborazione
Di seguito viene illustrato come inviare una richiesta POST al
videos:annotate
metodo. Puoi configurare il
LabelDetectionMode
per le annotazioni a livello di ripresa e/o frame. Ti consigliamo di utilizzare SHOT_AND_FRAME_MODE. L'esempio utilizza il token di accesso per un account di servizio configurato per il progetto utilizzando Google Cloud CLI. Per
istruzioni sull'installazione di Google Cloud CLI, sulla configurazione di un progetto con un account di
servizio e sul recupero di un token di accesso, consulta la
guida rapida di Video Intelligence.
Prima di utilizzare i dati della richiesta,
apporta le sostituzioni seguenti:
Se la richiesta ha esito positivo, Video Intelligence restituisce il nome dell'operazione.
Ottenere i risultati
Per ottenere i risultati della richiesta, devi inviare una richiesta GET a
projects.locations.operations
risorsa. Di seguito viene illustrato come inviare una richiesta di questo tipo.
Prima di utilizzare i dati della richiesta,
apporta le sostituzioni seguenti:
OPERATION_NAME: il nome dell'operazione restituito dall'API Video Intelligence. Il nome dell'operazione ha il formato
projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID
PROJECT_NUMBER: l'identificatore numerico del tuo Google Cloud progetto
Metodo HTTP e URL:
GET https://videointelligence.googleapis.com/v1/OPERATION_NAME
Per inviare la richiesta, espandi una di queste opzioni:
funclabel(wio.Writer,filestring)error{ctx:=context.Background()client,err:=video.NewClient(ctx)iferr!=nil{returnfmt.Errorf("video.NewClient: %w",err)}deferclient.Close()fileBytes,err:=os.ReadFile(file)iferr!=nil{returnerr}op,err:=client.AnnotateVideo(ctx,&videopb.AnnotateVideoRequest{Features:[]videopb.Feature{videopb.Feature_LABEL_DETECTION,},InputContent:fileBytes,})iferr!=nil{returnfmt.Errorf("AnnotateVideo: %w",err)}resp,err:=op.Wait(ctx)iferr!=nil{returnfmt.Errorf("Wait: %w",err)}printLabels:=func(labels[]*videopb.LabelAnnotation){for_,label:=rangelabels{fmt.Fprintf(w,"\tDescription: %s\n",label.Entity.Description)for_,category:=rangelabel.CategoryEntities{fmt.Fprintf(w,"\t\tCategory: %s\n",category.Description)}for_,segment:=rangelabel.Segments{start,_:=ptypes.Duration(segment.Segment.StartTimeOffset)end,_:=ptypes.Duration(segment.Segment.EndTimeOffset)fmt.Fprintf(w,"\t\tSegment: %s to %s\n",start,end)}}}// A single video was processed. Get the first result.result:=resp.AnnotationResults[0]fmt.Fprintln(w,"SegmentLabelAnnotations:")printLabels(result.SegmentLabelAnnotations)fmt.Fprintln(w,"ShotLabelAnnotations:")printLabels(result.ShotLabelAnnotations)fmt.Fprintln(w,"FrameLabelAnnotations:")printLabels(result.FrameLabelAnnotations)returnnil}
Java
// Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClienttry(VideoIntelligenceServiceClientclient=VideoIntelligenceServiceClient.create()){// Read file and encode into Base64Pathpath=Paths.get(filePath);byte[]data=Files.readAllBytes(path);AnnotateVideoRequestrequest=AnnotateVideoRequest.newBuilder().setInputContent(ByteString.copyFrom(data)).addFeatures(Feature.LABEL_DETECTION).build();// Create an operation that will contain the response when the operation completes.OperationFuture<AnnotateVideoResponse,AnnotateVideoProgress>response=client.annotateVideoAsync(request);System.out.println("Waiting for operation to complete...");for(VideoAnnotationResultsresults:response.get().getAnnotationResultsList()){// process video / segment level label annotationsSystem.out.println("Locations: ");for(LabelAnnotationlabelAnnotation:results.getSegmentLabelAnnotationsList()){System.out.println("Video label: "+labelAnnotation.getEntity().getDescription());// categoriesfor(EntitycategoryEntity:labelAnnotation.getCategoryEntitiesList()){System.out.println("Video label category: "+categoryEntity.getDescription());}// segmentsfor(LabelSegmentsegment:labelAnnotation.getSegmentsList()){doublestartTime=segment.getSegment().getStartTimeOffset().getSeconds()+segment.getSegment().getStartTimeOffset().getNanos()/1e9;doubleendTime=segment.getSegment().getEndTimeOffset().getSeconds()+segment.getSegment().getEndTimeOffset().getNanos()/1e9;System.out.printf("Segment location: %.3f:%.2f\n",startTime,endTime);System.out.println("Confidence: "+segment.getConfidence());}}// process shot label annotationsfor(LabelAnnotationlabelAnnotation:results.getShotLabelAnnotationsList()){System.out.println("Shot label: "+labelAnnotation.getEntity().getDescription());// categoriesfor(EntitycategoryEntity:labelAnnotation.getCategoryEntitiesList()){System.out.println("Shot label category: "+categoryEntity.getDescription());}// segmentsfor(LabelSegmentsegment:labelAnnotation.getSegmentsList()){doublestartTime=segment.getSegment().getStartTimeOffset().getSeconds()+segment.getSegment().getStartTimeOffset().getNanos()/1e9;doubleendTime=segment.getSegment().getEndTimeOffset().getSeconds()+segment.getSegment().getEndTimeOffset().getNanos()/1e9;System.out.printf("Segment location: %.3f:%.2f\n",startTime,endTime);System.out.println("Confidence: "+segment.getConfidence());}}// process frame label annotationsfor(LabelAnnotationlabelAnnotation:results.getFrameLabelAnnotationsList()){System.out.println("Frame label: "+labelAnnotation.getEntity().getDescription());// categoriesfor(EntitycategoryEntity:labelAnnotation.getCategoryEntitiesList()){System.out.println("Frame label category: "+categoryEntity.getDescription());}// segmentsfor(LabelSegmentsegment:labelAnnotation.getSegmentsList()){doublestartTime=segment.getSegment().getStartTimeOffset().getSeconds()+segment.getSegment().getStartTimeOffset().getNanos()/1e9;doubleendTime=segment.getSegment().getEndTimeOffset().getSeconds()+segment.getSegment().getEndTimeOffset().getNanos()/1e9;System.out.printf("Segment location: %.3f:%.2f\n",startTime,endTime);System.out.println("Confidence: "+segment.getConfidence());}}}}
Node.js
// Imports the Google Cloud Video Intelligence library + Node's fs libraryconstvideo=require('@google-cloud/video-intelligence').v1;constfs=require('fs');constutil=require('util');// Creates a clientconstclient=newvideo.VideoIntelligenceServiceClient();/** * TODO(developer): Uncomment the following line before running the sample. */// const path = 'Local file to analyze, e.g. ./my-file.mp4';// Reads a local video file and converts it to base64constreadFile=util.promisify(fs.readFile);constfile=awaitreadFile(path);constinputContent=file.toString('base64');// Constructs requestconstrequest={inputContent:inputContent,features:['LABEL_DETECTION'],};// Detects labels in a videoconst[operation]=awaitclient.annotateVideo(request);console.log('Waiting for operation to complete...');const[operationResult]=awaitoperation.promise();// Gets annotations for videoconstannotations=operationResult.annotationResults[0];constlabels=annotations.segmentLabelAnnotations;labels.forEach(label=>{console.log(`Label ${label.entity.description} occurs at:`);label.segments.forEach(segment=>{consttime=segment.segment;if(time.startTimeOffset.seconds===undefined){time.startTimeOffset.seconds=0;}if(time.startTimeOffset.nanos===undefined){time.startTimeOffset.nanos=0;}if(time.endTimeOffset.seconds===undefined){time.endTimeOffset.seconds=0;}if(time.endTimeOffset.nanos===undefined){time.endTimeOffset.nanos=0;}console.log(`\tStart: ${time.startTimeOffset.seconds}`+`.${(time.startTimeOffset.nanos/1e6).toFixed(0)}s`);console.log(`\tEnd: ${time.endTimeOffset.seconds}.`+`${(time.endTimeOffset.nanos/1e6).toFixed(0)}s`);console.log(`\tConfidence: ${segment.confidence}`);});});
Python
Per ulteriori informazioni sull'installazione e sull'utilizzo della libreria client dell'API Video Intelligence
per Python, consulta Librerie client dell'API Video Intelligence.
"""Detect labels given a file path."""video_client=videointelligence.VideoIntelligenceServiceClient()features=[videointelligence.Feature.LABEL_DETECTION]withio.open(path,"rb")asmovie:input_content=movie.read()operation=video_client.annotate_video(request={"features":features,"input_content":input_content})print("\nProcessing video for label annotations:")result=operation.result(timeout=90)print("\nFinished processing.")# Process video/segment level label annotationssegment_labels=result.annotation_results[0].segment_label_annotationsfori,segment_labelinenumerate(segment_labels):print("Video label description: {}".format(segment_label.entity.description))forcategory_entityinsegment_label.category_entities:print("\tLabel category description: {}".format(category_entity.description))fori,segmentinenumerate(segment_label.segments):start_time=(segment.segment.start_time_offset.seconds+segment.segment.start_time_offset.microseconds/1e6)end_time=(segment.segment.end_time_offset.seconds+segment.segment.end_time_offset.microseconds/1e6)positions="{}s to {}s".format(start_time,end_time)confidence=segment.confidenceprint("\tSegment {}: {}".format(i,positions))print("\tConfidence: {}".format(confidence))print("\n")# Process shot level label annotationsshot_labels=result.annotation_results[0].shot_label_annotationsfori,shot_labelinenumerate(shot_labels):print("Shot label description: {}".format(shot_label.entity.description))forcategory_entityinshot_label.category_entities:print("\tLabel category description: {}".format(category_entity.description))fori,shotinenumerate(shot_label.segments):start_time=(shot.segment.start_time_offset.seconds+shot.segment.start_time_offset.microseconds/1e6)end_time=(shot.segment.end_time_offset.seconds+shot.segment.end_time_offset.microseconds/1e6)positions="{}s to {}s".format(start_time,end_time)confidence=shot.confidenceprint("\tSegment {}: {}".format(i,positions))print("\tConfidence: {}".format(confidence))print("\n")# Process frame level label annotationsframe_labels=result.annotation_results[0].frame_label_annotationsfori,frame_labelinenumerate(frame_labels):print("Frame label description: {}".format(frame_label.entity.description))forcategory_entityinframe_label.category_entities:print("\tLabel category description: {}".format(category_entity.description))# Each frame_label_annotation has many frames,# here we print information only about the first frame.frame=frame_label.frames[0]time_offset=frame.time_offset.seconds+frame.time_offset.microseconds/1e6print("\tFirst frame time offset: {}s".format(time_offset))print("\tFirst frame confidence: {}".format(frame.confidence))print("\n")
Ecco un esempio di esecuzione dell'analisi video per le etichette in un file in Cloud Storage.
REST
Per ulteriori informazioni sull'installazione e sull'utilizzo della libreria client dell'API Video Intelligence
per Python, consulta Librerie client dell'API Video Intelligence.
Inviare la richiesta di elaborazione
Di seguito viene illustrato come inviare una richiesta POST al metodo
annotate. L'esempio utilizza il token di accesso per un account di servizio configurato per il progetto utilizzando Google Cloud CLI. Per
istruzioni sull'installazione di Google Cloud CLI, sulla configurazione di un progetto con un account di
servizio e sul recupero di un token di accesso, consulta la
guida rapida di Video Intelligence.
Prima di utilizzare i dati della richiesta,
apporta le sostituzioni seguenti:
INPUT_URI: un bucket Cloud Storage che contiene
il file che vuoi annotare, incluso il nome del file. Deve
iniziare con gs://.
PROJECT_NUMBER: l'identificatore numerico del tuo Google Cloud progetto
Metodo HTTP e URL:
POST https://videointelligence.googleapis.com/v1/videos:annotate
Se la richiesta ha esito positivo, Video Intelligence restituisce il nome dell'operazione.
Ottenere i risultati
Per ottenere i risultati della richiesta, devi inviare una richiesta GET a
projects.locations.operations
risorsa. Di seguito viene illustrato come inviare una richiesta di questo tipo.
Prima di utilizzare i dati della richiesta,
apporta le sostituzioni seguenti:
OPERATION_NAME: il nome dell'operazione restituito dall'API Video Intelligence. Il nome dell'operazione ha il formato
projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID
PROJECT_NUMBER: l'identificatore numerico del tuo Google Cloud progetto
Metodo HTTP e URL:
GET https://videointelligence.googleapis.com/v1/OPERATION_NAME
Per inviare la richiesta, espandi una di queste opzioni:
Copia l'annotazione dall'origine al bucket di destinazione: (vedi Copiare file e oggetti)
gcloud storage cp gcs_uri gs://my-bucket
Nota: se l'URI GCS di output viene fornito dall'utente, l'annotazione viene archiviata in quell'URI GCS.
Go
funclabelURI(wio.Writer,filestring)error{ctx:=context.Background()client,err:=video.NewClient(ctx)iferr!=nil{returnfmt.Errorf("video.NewClient: %w",err)}deferclient.Close()op,err:=client.AnnotateVideo(ctx,&videopb.AnnotateVideoRequest{Features:[]videopb.Feature{videopb.Feature_LABEL_DETECTION,},InputUri:file,})iferr!=nil{returnfmt.Errorf("AnnotateVideo: %w",err)}resp,err:=op.Wait(ctx)iferr!=nil{returnfmt.Errorf("Wait: %w",err)}printLabels:=func(labels[]*videopb.LabelAnnotation){for_,label:=rangelabels{fmt.Fprintf(w,"\tDescription: %s\n",label.Entity.Description)for_,category:=rangelabel.CategoryEntities{fmt.Fprintf(w,"\t\tCategory: %s\n",category.Description)}for_,segment:=rangelabel.Segments{start,_:=ptypes.Duration(segment.Segment.StartTimeOffset)end,_:=ptypes.Duration(segment.Segment.EndTimeOffset)fmt.Fprintf(w,"\t\tSegment: %s to %s\n",start,end)}}}// A single video was processed. Get the first result.result:=resp.AnnotationResults[0]fmt.Fprintln(w,"SegmentLabelAnnotations:")printLabels(result.SegmentLabelAnnotations)fmt.Fprintln(w,"ShotLabelAnnotations:")printLabels(result.ShotLabelAnnotations)fmt.Fprintln(w,"FrameLabelAnnotations:")printLabels(result.FrameLabelAnnotations)returnnil}
Java
// Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClienttry(VideoIntelligenceServiceClientclient=VideoIntelligenceServiceClient.create()){// Provide path to file hosted on GCS as "gs://bucket-name/..."AnnotateVideoRequestrequest=AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.LABEL_DETECTION).build();// Create an operation that will contain the response when the operation completes.OperationFuture<AnnotateVideoResponse,AnnotateVideoProgress>response=client.annotateVideoAsync(request);System.out.println("Waiting for operation to complete...");for(VideoAnnotationResultsresults:response.get().getAnnotationResultsList()){// process video / segment level label annotationsSystem.out.println("Locations: ");for(LabelAnnotationlabelAnnotation:results.getSegmentLabelAnnotationsList()){System.out.println("Video label: "+labelAnnotation.getEntity().getDescription());// categoriesfor(EntitycategoryEntity:labelAnnotation.getCategoryEntitiesList()){System.out.println("Video label category: "+categoryEntity.getDescription());}// segmentsfor(LabelSegmentsegment:labelAnnotation.getSegmentsList()){doublestartTime=segment.getSegment().getStartTimeOffset().getSeconds()+segment.getSegment().getStartTimeOffset().getNanos()/1e9;doubleendTime=segment.getSegment().getEndTimeOffset().getSeconds()+segment.getSegment().getEndTimeOffset().getNanos()/1e9;System.out.printf("Segment location: %.3f:%.3f\n",startTime,endTime);System.out.println("Confidence: "+segment.getConfidence());}}// process shot label annotationsfor(LabelAnnotationlabelAnnotation:results.getShotLabelAnnotationsList()){System.out.println("Shot label: "+labelAnnotation.getEntity().getDescription());// categoriesfor(EntitycategoryEntity:labelAnnotation.getCategoryEntitiesList()){System.out.println("Shot label category: "+categoryEntity.getDescription());}// segmentsfor(LabelSegmentsegment:labelAnnotation.getSegmentsList()){doublestartTime=segment.getSegment().getStartTimeOffset().getSeconds()+segment.getSegment().getStartTimeOffset().getNanos()/1e9;doubleendTime=segment.getSegment().getEndTimeOffset().getSeconds()+segment.getSegment().getEndTimeOffset().getNanos()/1e9;System.out.printf("Segment location: %.3f:%.3f\n",startTime,endTime);System.out.println("Confidence: "+segment.getConfidence());}}// process frame label annotationsfor(LabelAnnotationlabelAnnotation:results.getFrameLabelAnnotationsList()){System.out.println("Frame label: "+labelAnnotation.getEntity().getDescription());// categoriesfor(EntitycategoryEntity:labelAnnotation.getCategoryEntitiesList()){System.out.println("Frame label category: "+categoryEntity.getDescription());}// segmentsfor(LabelSegmentsegment:labelAnnotation.getSegmentsList()){doublestartTime=segment.getSegment().getStartTimeOffset().getSeconds()+segment.getSegment().getStartTimeOffset().getNanos()/1e9;doubleendTime=segment.getSegment().getEndTimeOffset().getSeconds()+segment.getSegment().getEndTimeOffset().getNanos()/1e9;System.out.printf("Segment location: %.3f:%.2f\n",startTime,endTime);System.out.println("Confidence: "+segment.getConfidence());}}}}
Node.js
// Imports the Google Cloud Video Intelligence libraryconstvideo=require('@google-cloud/video-intelligence').v1;// Creates a clientconstclient=newvideo.VideoIntelligenceServiceClient();/** * TODO(developer): Uncomment the following line before running the sample. */// const gcsUri = 'GCS URI of the video to analyze, e.g. gs://my-bucket/my-video.mp4';constrequest={inputUri:gcsUri,features:['LABEL_DETECTION'],};// Detects labels in a videoconst[operation]=awaitclient.annotateVideo(request);console.log('Waiting for operation to complete...');const[operationResult]=awaitoperation.promise();// Gets annotations for videoconstannotations=operationResult.annotationResults[0];constlabels=annotations.segmentLabelAnnotations;labels.forEach(label=>{console.log(`Label ${label.entity.description} occurs at:`);label.segments.forEach(segment=>{consttime=segment.segment;if(time.startTimeOffset.seconds===undefined){time.startTimeOffset.seconds=0;}if(time.startTimeOffset.nanos===undefined){time.startTimeOffset.nanos=0;}if(time.endTimeOffset.seconds===undefined){time.endTimeOffset.seconds=0;}if(time.endTimeOffset.nanos===undefined){time.endTimeOffset.nanos=0;}console.log(`\tStart: ${time.startTimeOffset.seconds}`+`.${(time.startTimeOffset.nanos/1e6).toFixed(0)}s`);console.log(`\tEnd: ${time.endTimeOffset.seconds}.`+`${(time.endTimeOffset.nanos/1e6).toFixed(0)}s`);console.log(`\tConfidence: ${segment.confidence}`);});});
Python
"""Detects labels given a GCS path."""video_client=videointelligence.VideoIntelligenceServiceClient()features=[videointelligence.Feature.LABEL_DETECTION]mode=videointelligence.LabelDetectionMode.SHOT_AND_FRAME_MODEconfig=videointelligence.LabelDetectionConfig(label_detection_mode=mode)context=videointelligence.VideoContext(label_detection_config=config)operation=video_client.annotate_video(request={"features":features,"input_uri":path,"video_context":context})print("\nProcessing video for label annotations:")result=operation.result(timeout=180)print("\nFinished processing.")# Process video/segment level label annotationssegment_labels=result.annotation_results[0].segment_label_annotationsfori,segment_labelinenumerate(segment_labels):print("Video label description: {}".format(segment_label.entity.description))forcategory_entityinsegment_label.category_entities:print("\tLabel category description: {}".format(category_entity.description))fori,segmentinenumerate(segment_label.segments):start_time=(segment.segment.start_time_offset.seconds+segment.segment.start_time_offset.microseconds/1e6)end_time=(segment.segment.end_time_offset.seconds+segment.segment.end_time_offset.microseconds/1e6)positions="{}s to {}s".format(start_time,end_time)confidence=segment.confidenceprint("\tSegment {}: {}".format(i,positions))print("\tConfidence: {}".format(confidence))print("\n")# Process shot level label annotationsshot_labels=result.annotation_results[0].shot_label_annotationsfori,shot_labelinenumerate(shot_labels):print("Shot label description: {}".format(shot_label.entity.description))forcategory_entityinshot_label.category_entities:print("\tLabel category description: {}".format(category_entity.description))fori,shotinenumerate(shot_label.segments):start_time=(shot.segment.start_time_offset.seconds+shot.segment.start_time_offset.microseconds/1e6)end_time=(shot.segment.end_time_offset.seconds+shot.segment.end_time_offset.microseconds/1e6)positions="{}s to {}s".format(start_time,end_time)confidence=shot.confidenceprint("\tSegment {}: {}".format(i,positions))print("\tConfidence: {}".format(confidence))print("\n")# Process frame level label annotationsframe_labels=result.annotation_results[0].frame_label_annotationsfori,frame_labelinenumerate(frame_labels):print("Frame label description: {}".format(frame_label.entity.description))forcategory_entityinframe_label.category_entities:print("\tLabel category description: {}".format(category_entity.description))# Each frame_label_annotation has many frames,# here we print information only about the first frame.frame=frame_label.frames[0]time_offset=frame.time_offset.seconds+frame.time_offset.microseconds/1e6print("\tFirst frame time offset: {}s".format(time_offset))print("\tFirst frame confidence: {}".format(frame.confidence))print("\n")
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2026-09-15 UTC."],[],[]]