|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2019 Google Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +namespace Google\Cloud\Samples\Video; |
| 19 | + |
| 20 | +// [START video_analyze_object_tracking] |
| 21 | +use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient; |
| 22 | +use Google\Cloud\VideoIntelligence\V1\Feature; |
| 23 | + |
| 24 | +/** |
| 25 | + * Finds labels in the video. |
| 26 | + * |
| 27 | + * @param string $path File path to a video file to analyze. |
| 28 | + * @param array $options optional Array of options to pass to |
| 29 | + * OperationResponse::pollUntilComplete. This is useful |
| 30 | + * for increasing the "pollingIntervalSeconds" option. |
| 31 | + */ |
| 32 | +function analyze_object_tracking_file($path, array $options = []) |
| 33 | +{ |
| 34 | + # Instantiate a client. |
| 35 | + $video = new VideoIntelligenceServiceClient(); |
| 36 | + |
| 37 | + # Read the local video file |
| 38 | + $inputContent = file_get_contents($path); |
| 39 | + |
| 40 | + # Execute a request. |
| 41 | + $operation = $video->annotateVideo([ |
| 42 | + 'inputContent' => $inputContent, |
| 43 | + 'features' => [Feature::OBJECT_TRACKING] |
| 44 | + ]); |
| 45 | + |
| 46 | + # Wait for the request to complete. |
| 47 | + $operation->pollUntilComplete($options); |
| 48 | + |
| 49 | + # Print the results. |
| 50 | + if ($operation->operationSucceeded()) { |
| 51 | + $results = $operation->getResult()->getAnnotationResults()[0]; |
| 52 | + # Process video/segment level label annotations |
| 53 | + $objectAnnotation = $results->getObjectAnnotations()[0]; |
| 54 | + |
| 55 | + printf('Video object entity: %s' . PHP_EOL, $objectAnnotation->getEntity()->getEntityId()); |
| 56 | + printf('Video object description: %s' . PHP_EOL, $objectAnnotation->getEntity()->getDescription()); |
| 57 | + |
| 58 | + $startTimeOffset = $objectAnnotation->getSegment()->getStartTimeOffset(); |
| 59 | + $startSeconds = $startTimeOffset->getSeconds(); |
| 60 | + $startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00; |
| 61 | + $startTime = $startSeconds + $startNanoseconds; |
| 62 | + $endTimeOffset = $objectAnnotation->getSegment()->getEndTimeOffset(); |
| 63 | + $endSeconds = $endTimeOffset->getSeconds(); |
| 64 | + $endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00; |
| 65 | + $endTime = $endSeconds + $endNanoseconds; |
| 66 | + printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime); |
| 67 | + printf(' Confidence: %f' . PHP_EOL, $objectAnnotation->getConfidence()); |
| 68 | + |
| 69 | + foreach ($objectAnnotation->getFrames() as $objectAnnotationFrame) { |
| 70 | + $startSeconds = $objectAnnotationFrame->getTimeOffset()->getSeconds(); |
| 71 | + $startNanoseconds = $objectAnnotationFrame->getTimeOffset()->getNanos(); |
| 72 | + $timeOffSet = $startSeconds + $startNanoseconds/1000000000.00; |
| 73 | + printf(' Time offset: %ss' . PHP_EOL, $timeOffSet); |
| 74 | + $boundingBox = $objectAnnotationFrame->getNormalizedBoundingBox(); |
| 75 | + printf(' Bounding box position:' . PHP_EOL); |
| 76 | + printf(' Left: %s', $boundingBox->getLeft()); |
| 77 | + printf(' Top: %s', $boundingBox->getTop()); |
| 78 | + printf(' Right: %s', $boundingBox->getRight()); |
| 79 | + printf(' Bottom: %s', $boundingBox->getBottom()); |
| 80 | + } |
| 81 | + print(PHP_EOL); |
| 82 | + } else { |
| 83 | + print_r($operation->getError()); |
| 84 | + } |
| 85 | +} |
| 86 | +// [END video_analyze_object_tracking] |
0 commit comments