14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
- """Draws squares around faces in the given image."""
17
+ """Draws squares around detected faces in the given image."""
18
18
19
19
import argparse
20
- import base64
21
20
22
- import googleapiclient .discovery
23
- from PIL import Image
24
- from PIL import ImageDraw
25
-
26
-
27
- # [START get_vision_service]
28
- def get_vision_service ():
29
- return googleapiclient .discovery .build ('vision' , 'v1' )
30
- # [END get_vision_service]
21
+ from google .cloud import vision
22
+ from PIL import Image , ImageDraw
31
23
32
24
33
25
def detect_face (face_file , max_results = 4 ):
@@ -37,26 +29,14 @@ def detect_face(face_file, max_results=4):
37
29
face_file: A file-like object containing an image with faces.
38
30
39
31
Returns:
40
- An array of dicts with information about the faces in the picture.
32
+ An array of Face objects with information about the picture.
41
33
"""
42
- image_content = face_file .read ()
43
- batch_request = [{
44
- 'image' : {
45
- 'content' : base64 .b64encode (image_content ).decode ('utf-8' )
46
- },
47
- 'features' : [{
48
- 'type' : 'FACE_DETECTION' ,
49
- 'maxResults' : max_results ,
50
- }]
51
- }]
52
-
53
- service = get_vision_service ()
54
- request = service .images ().annotate (body = {
55
- 'requests' : batch_request ,
56
- })
57
- response = request .execute ()
58
-
59
- return response ['responses' ][0 ]['faceAnnotations' ]
34
+ content = face_file .read ()
35
+ # [START get_vision_service]
36
+ image = vision .Client ().image (content = content )
37
+ # [END get_vision_service]
38
+
39
+ return image .detect_faces ()
60
40
61
41
62
42
def highlight_faces (image , faces , output_filename ):
@@ -73,8 +53,8 @@ def highlight_faces(image, faces, output_filename):
73
53
draw = ImageDraw .Draw (im )
74
54
75
55
for face in faces :
76
- box = [(v . get ( 'x' , 0.0 ), v . get ( 'y' , 0.0 ) )
77
- for v in face [ 'fdBoundingPoly' ][ ' vertices' ] ]
56
+ box = [(bound . x_coordinate , bound . y_coordinate )
57
+ for bound in face . bounds . vertices ]
78
58
draw .line (box + [box [0 ]], width = 5 , fill = '#00ff00' )
79
59
80
60
im .save (output_filename )
0 commit comments