1414 * See the License for the specific language governing permissions and
1515 * limitations under the License.
1616 */
17-
1817// [START vision_face_detection]
1918namespace Google \Cloud \Samples \Vision ;
2019
21- // [START import_client_library ]
20+ // [START vision_face_detection_tutorial_imports ]
2221use Google \Cloud \Vision \V1 \ImageAnnotatorClient ;
2322
24- // [END import_client_library]
25-
26- // $path = 'path/to/your/image.jpg'
23+ // [END vision_face_detection_tutorial_imports]
2724
2825function detect_face ($ path , $ outFile = null )
2926{
30- // [START get_vision_service ]
27+ // [START vision_face_detection_tutorial_client ]
3128 $ imageAnnotator = new ImageAnnotatorClient ();
32- // [END get_vision_service ]
33-
34- // [START detect_face ]
29+ // [END vision_face_detection_tutorial_client ]
30+
31+ // [START vision_face_detection_tutorial_send_request ]
3532 # annotate the image
33+ // $path = 'path/to/your/image.jpg'
3634 $ image = file_get_contents ($ path );
3735 $ response = $ imageAnnotator ->faceDetection ($ image );
3836 $ faces = $ response ->getFaceAnnotations ();
39- // [END detect_face ]
37+ // [END vision_face_detection_tutorial_send_request ]
4038
4139 # names of likelihood from google.cloud.vision.enums
4240 $ likelihoodName = ['UNKNOWN ' , 'VERY_UNLIKELY ' , 'UNLIKELY ' ,
@@ -62,7 +60,9 @@ function detect_face($path, $outFile = null)
6260 print ('Bounds: ' . join (', ' ,$ bounds ) . PHP_EOL );
6361 print (PHP_EOL );
6462 }
63+ // [END vision_face_detection]
6564
65+ # [START vision_face_detection_tutorial_process_response]
6666 # draw box around faces
6767 if ($ faces && $ outFile ) {
6868 $ imageCreateFunc = [
@@ -79,14 +79,14 @@ function detect_face($path, $outFile = null)
7979 'jpg ' => 'imagejpeg ' ,
8080 'jpeg ' => 'imagejpeg ' ,
8181 ];
82-
82+
8383 copy ($ path , $ outFile );
8484 $ ext = strtolower (pathinfo ($ path , PATHINFO_EXTENSION ));
8585 if (!array_key_exists ($ ext , $ imageCreateFunc )) {
8686 throw new \Exception ('Unsupported image extension ' );
8787 }
8888 $ outputImage = call_user_func ($ imageCreateFunc [$ ext ], $ outFile );
89- # [START highlight_image]
89+
9090 foreach ($ faces as $ face ) {
9191 $ vertices = $ face ->getBoundingPoly ()->getVertices ();
9292 if ($ vertices ) {
@@ -97,9 +97,12 @@ function detect_face($path, $outFile = null)
9797 imagerectangle ($ outputImage , $ x1 , $ y1 , $ x2 , $ y2 , 0x00ff00 );
9898 }
9999 }
100- # [END highlight_image]
100+ # [END vision_face_detection_tutorial_process_response]
101+ # [START vision_face_detection_tutorial_run_application]
101102 call_user_func ($ imageWriteFunc [$ ext ], $ outputImage , $ outFile );
102103 printf ('Output image written to %s ' . PHP_EOL , $ outFile );
104+ # [END vision_face_detection_tutorial_run_application]
103105 }
106+ // [START vision_face_detection]
104107}
105108// [END vision_face_detection]
0 commit comments