Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vision face tutorial #880

Merged
merged 4 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions vision/api/face_detection/requirements.txt

This file was deleted.

1 change: 1 addition & 0 deletions vision/cloud-client/face_detection/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out.jpg
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,9 @@
"""Draws squares around faces in the given image."""

import argparse
import base64

import googleapiclient.discovery
from PIL import Image
from PIL import ImageDraw


# [START get_vision_service]
def get_vision_service():
return googleapiclient.discovery.build('vision', 'v1')
# [END get_vision_service]
from google.cloud import vision
from PIL import Image, ImageDraw


def detect_face(face_file, max_results=4):
Expand All @@ -37,26 +29,14 @@ def detect_face(face_file, max_results=4):
face_file: A file-like object containing an image with faces.

Returns:
An array of dicts with information about the faces in the picture.
An array of Face objects with information about the picture.
"""
image_content = face_file.read()
batch_request = [{
'image': {
'content': base64.b64encode(image_content).decode('utf-8')
},
'features': [{
'type': 'FACE_DETECTION',
'maxResults': max_results,
}]
}]

service = get_vision_service()
request = service.images().annotate(body={
'requests': batch_request,
})
response = request.execute()

return response['responses'][0]['faceAnnotations']
content = face_file.read()
# [START get_vision_service]
image = vision.Client().image(content=content)
# [END get_vision_service]

return image.detect_faces()


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

for face in faces:
box = [(v.get('x', 0.0), v.get('y', 0.0))
for v in face['fdBoundingPoly']['vertices']]
box = [(bound.x_coordinate, bound.y_coordinate)
for bound in face.bounds.vertices]
draw.line(box + [box[0]], width=5, fill='#00ff00')

im.save(output_filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

import os

from PIL import Image

from faces import main
from PIL import Image


def test_main(resource, tmpdir):
Expand Down
2 changes: 2 additions & 0 deletions vision/cloud-client/face_detection/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google-cloud-vision==0.23.3
Pillow==4.0.0