@@ -107,19 +107,9 @@ public static void analyzeFaces(String gcsUri) throws Exception {
107
107
// [START detect_faces]
108
108
// Instantiate a com.google.cloud.videointelligence.v1beta2.VideoIntelligenceServiceClient
109
109
try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient .create ()) {
110
- // detect shot and frame
111
- LabelDetectionConfig labelDetectionConfig = LabelDetectionConfig .newBuilder ()
112
- .setLabelDetectionMode (LabelDetectionMode .SHOT_AND_FRAME_MODE )
113
- .build ();
114
-
115
- VideoContext videoContext = VideoContext .newBuilder ()
116
- .setLabelDetectionConfig (labelDetectionConfig )
117
- .build ();
118
-
119
110
AnnotateVideoRequest request = AnnotateVideoRequest .newBuilder ()
120
111
.setInputUri (gcsUri )
121
- .setVideoContext (videoContext )
122
- .addFeatures (Feature .LABEL_DETECTION )
112
+ .addFeatures (Feature .FACE_DETECTION )
123
113
.build ();
124
114
125
115
// asynchronously perform facial analysis on videos
@@ -140,17 +130,26 @@ public static void analyzeFaces(String gcsUri) throws Exception {
140
130
+ segment .getSegment ().getEndTimeOffset ().getNanos () / 1e9 ;
141
131
System .out .printf ("Segment location : %.3f:%.3f\n " , startTime , endTime );
142
132
}
143
- // printing info on the first frame
144
- FaceFrame frame = faceAnnotation .getFrames (0 );
145
- double timeOffset = frame .getTimeOffset ().getSeconds ()
146
- + frame .getTimeOffset ().getNanos () / 1e9 ;
147
- System .out .printf ("First frame time offset: %.3fs" , timeOffset );
148
- // print info on the first normalized bounding box
149
- NormalizedBoundingBox box = frame .getNormalizedBoundingBoxesList ().get (0 );
150
- System .out .printf ("Left: %.3f\n " , box .getLeft ());
151
- System .out .printf ("Top: %.3f\n " , box .getTop ());
152
- System .out .printf ("Bottom: %.3f\n " , box .getBottom ());
153
- System .out .printf ("Right: %.3f\n " , box .getRight ());
133
+ try {
134
+ // printing info on the first frame
135
+ if (faceAnnotation .getFramesCount () > 0 ) {
136
+ System .out .println (faceAnnotation .getFramesList ().get (0 ));
137
+ FaceFrame frame = faceAnnotation .getFrames (0 );
138
+ double timeOffset = frame .getTimeOffset ().getSeconds ()
139
+ + frame .getTimeOffset ().getNanos () / 1e9 ;
140
+ System .out .printf ("First frame time offset: %.3fs" , timeOffset );
141
+ // print info on the first normalized bounding box
142
+ NormalizedBoundingBox box = frame .getNormalizedBoundingBoxesList ().get (0 );
143
+ System .out .printf ("Left: %.3f\n " , box .getLeft ());
144
+ System .out .printf ("Top: %.3f\n " , box .getTop ());
145
+ System .out .printf ("Bottom: %.3f\n " , box .getBottom ());
146
+ System .out .printf ("Right: %.3f\n " , box .getRight ());
147
+ } else {
148
+ System .out .println ("No frames found in annotation" );
149
+ }
150
+ } catch (IndexOutOfBoundsException ioe ) {
151
+ System .out .println ("Could not retrieve frame: " + ioe .getMessage ());
152
+ }
154
153
}
155
154
}
156
155
if (!faceFound ) {
0 commit comments