Skip to content

Commit 96ae58c

Browse files
gguussdpebot
authored andcommitted
Vision face tutorial (#880)
* Updates sample to use the Cloud client library * Nits found after commit * Nudge for travis * flake8 hates my face
1 parent 6245c9d commit 96ae58c

File tree

8 files changed

+15
-34
lines changed

8 files changed

+15
-34
lines changed

vision/api/face_detection/requirements.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out.jpg

vision/api/face_detection/faces.py renamed to vision/cloud-client/face_detection/faces.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
"""Draws squares around faces in the given image."""
17+
"""Draws squares around detected faces in the given image."""
1818

1919
import argparse
20-
import base64
2120

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
3123

3224

3325
def detect_face(face_file, max_results=4):
@@ -37,26 +29,14 @@ def detect_face(face_file, max_results=4):
3729
face_file: A file-like object containing an image with faces.
3830
3931
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.
4133
"""
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()
6040

6141

6242
def highlight_faces(image, faces, output_filename):
@@ -73,8 +53,8 @@ def highlight_faces(image, faces, output_filename):
7353
draw = ImageDraw.Draw(im)
7454

7555
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]
7858
draw.line(box + [box[0]], width=5, fill='#00ff00')
7959

8060
im.save(output_filename)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-vision==0.23.3
2+
Pillow==4.0.0

0 commit comments

Comments
 (0)