14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
-
18
17
// [START vision_face_detection]
19
18
namespace Google \Cloud \Samples \Vision ;
20
19
21
- // [START import_client_library ]
20
+ // [START vision_face_detection_tutorial_imports ]
22
21
use Google \Cloud \Vision \V1 \ImageAnnotatorClient ;
23
22
24
- // [END import_client_library]
25
-
26
- // $path = 'path/to/your/image.jpg'
23
+ // [END vision_face_detection_tutorial_imports]
27
24
28
25
function detect_face ($ path , $ outFile = null )
29
26
{
30
- // [START get_vision_service ]
27
+ // [START vision_face_detection_tutorial_client ]
31
28
$ 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 ]
35
32
# annotate the image
33
+ // $path = 'path/to/your/image.jpg'
36
34
$ image = file_get_contents ($ path );
37
35
$ response = $ imageAnnotator ->faceDetection ($ image );
38
36
$ faces = $ response ->getFaceAnnotations ();
39
- // [END detect_face ]
37
+ // [END vision_face_detection_tutorial_send_request ]
40
38
41
39
# names of likelihood from google.cloud.vision.enums
42
40
$ likelihoodName = ['UNKNOWN ' , 'VERY_UNLIKELY ' , 'UNLIKELY ' ,
@@ -62,7 +60,9 @@ function detect_face($path, $outFile = null)
62
60
print ('Bounds: ' . join (', ' ,$ bounds ) . PHP_EOL );
63
61
print (PHP_EOL );
64
62
}
63
+ // [END vision_face_detection]
65
64
65
+ # [START vision_face_detection_tutorial_process_response]
66
66
# draw box around faces
67
67
if ($ faces && $ outFile ) {
68
68
$ imageCreateFunc = [
@@ -79,14 +79,14 @@ function detect_face($path, $outFile = null)
79
79
'jpg ' => 'imagejpeg ' ,
80
80
'jpeg ' => 'imagejpeg ' ,
81
81
];
82
-
82
+
83
83
copy ($ path , $ outFile );
84
84
$ ext = strtolower (pathinfo ($ path , PATHINFO_EXTENSION ));
85
85
if (!array_key_exists ($ ext , $ imageCreateFunc )) {
86
86
throw new \Exception ('Unsupported image extension ' );
87
87
}
88
88
$ outputImage = call_user_func ($ imageCreateFunc [$ ext ], $ outFile );
89
- # [START highlight_image]
89
+
90
90
foreach ($ faces as $ face ) {
91
91
$ vertices = $ face ->getBoundingPoly ()->getVertices ();
92
92
if ($ vertices ) {
@@ -97,9 +97,12 @@ function detect_face($path, $outFile = null)
97
97
imagerectangle ($ outputImage , $ x1 , $ y1 , $ x2 , $ y2 , 0x00ff00 );
98
98
}
99
99
}
100
- # [END highlight_image]
100
+ # [END vision_face_detection_tutorial_process_response]
101
+ # [START vision_face_detection_tutorial_run_application]
101
102
call_user_func ($ imageWriteFunc [$ ext ], $ outputImage , $ outFile );
102
103
printf ('Output image written to %s ' . PHP_EOL , $ outFile );
104
+ # [END vision_face_detection_tutorial_run_application]
103
105
}
106
+ // [START vision_face_detection]
104
107
}
105
108
// [END vision_face_detection]
0 commit comments