From 6301eae663cdf1e551b5ab2f7027077a96d29ab3 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Tue, 28 Mar 2017 12:07:47 -0700 Subject: [PATCH 1/4] Updates sample to use the Cloud client library --- vision/api/face_detection/requirements.txt | 2 - vision/cloud-client/face_detection/.gitignore | 1 + .../face_detection/README.rst | 0 .../face_detection/README.rst.in | 0 .../face_detection/faces.py | 42 +++++------------- .../face_detection/faces_test.py | 0 .../face_detection/requirements.txt | 2 + .../face_detection/resources/face-input.jpg | Bin 8 files changed, 15 insertions(+), 32 deletions(-) delete mode 100644 vision/api/face_detection/requirements.txt create mode 100644 vision/cloud-client/face_detection/.gitignore rename vision/{api => cloud-client}/face_detection/README.rst (100%) rename vision/{api => cloud-client}/face_detection/README.rst.in (100%) rename vision/{api => cloud-client}/face_detection/faces.py (73%) rename vision/{api => cloud-client}/face_detection/faces_test.py (100%) create mode 100644 vision/cloud-client/face_detection/requirements.txt rename vision/{api => cloud-client}/face_detection/resources/face-input.jpg (100%) diff --git a/vision/api/face_detection/requirements.txt b/vision/api/face_detection/requirements.txt deleted file mode 100644 index 965872d93b78..000000000000 --- a/vision/api/face_detection/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -google-api-python-client==1.6.2 -Pillow==4.0.0 diff --git a/vision/cloud-client/face_detection/.gitignore b/vision/cloud-client/face_detection/.gitignore new file mode 100644 index 000000000000..01f02dff9a77 --- /dev/null +++ b/vision/cloud-client/face_detection/.gitignore @@ -0,0 +1 @@ +out.jpg diff --git a/vision/api/face_detection/README.rst b/vision/cloud-client/face_detection/README.rst similarity index 100% rename from vision/api/face_detection/README.rst rename to vision/cloud-client/face_detection/README.rst diff --git a/vision/api/face_detection/README.rst.in b/vision/cloud-client/face_detection/README.rst.in similarity index 100% rename from vision/api/face_detection/README.rst.in rename to vision/cloud-client/face_detection/README.rst.in diff --git a/vision/api/face_detection/faces.py b/vision/cloud-client/face_detection/faces.py similarity index 73% rename from vision/api/face_detection/faces.py rename to vision/cloud-client/face_detection/faces.py index 43d1e0939889..066d611c783a 100755 --- a/vision/api/face_detection/faces.py +++ b/vision/cloud-client/face_detection/faces.py @@ -19,15 +19,8 @@ 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): @@ -37,26 +30,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): @@ -73,8 +54,9 @@ 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']] + print(dir(face.bounds.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) diff --git a/vision/api/face_detection/faces_test.py b/vision/cloud-client/face_detection/faces_test.py similarity index 100% rename from vision/api/face_detection/faces_test.py rename to vision/cloud-client/face_detection/faces_test.py diff --git a/vision/cloud-client/face_detection/requirements.txt b/vision/cloud-client/face_detection/requirements.txt new file mode 100644 index 000000000000..7a5de2e03c81 --- /dev/null +++ b/vision/cloud-client/face_detection/requirements.txt @@ -0,0 +1,2 @@ +google-cloud-vision==0.23.3 +Pillow==4.0.0 diff --git a/vision/api/face_detection/resources/face-input.jpg b/vision/cloud-client/face_detection/resources/face-input.jpg similarity index 100% rename from vision/api/face_detection/resources/face-input.jpg rename to vision/cloud-client/face_detection/resources/face-input.jpg From 2dcdb5841df80a8a3c2f30f444eb468f47e91c7c Mon Sep 17 00:00:00 2001 From: Gus Class Date: Tue, 28 Mar 2017 12:10:26 -0700 Subject: [PATCH 2/4] Nits found after commit --- vision/cloud-client/face_detection/faces.py | 2 -- vision/cloud-client/face_detection/faces_test.py | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/vision/cloud-client/face_detection/faces.py b/vision/cloud-client/face_detection/faces.py index 066d611c783a..e1821cfcecb7 100755 --- a/vision/cloud-client/face_detection/faces.py +++ b/vision/cloud-client/face_detection/faces.py @@ -17,7 +17,6 @@ """Draws squares around faces in the given image.""" import argparse -import base64 from google.cloud import vision from PIL import Image, ImageDraw @@ -54,7 +53,6 @@ def highlight_faces(image, faces, output_filename): draw = ImageDraw.Draw(im) for face in faces: - print(dir(face.bounds.vertices)) box = [(bound.x_coordinate, bound.y_coordinate) for bound in face.bounds.vertices] draw.line(box + [box[0]], width=5, fill='#00ff00') diff --git a/vision/cloud-client/face_detection/faces_test.py b/vision/cloud-client/face_detection/faces_test.py index 9611c82ed499..43a0075856d9 100644 --- a/vision/cloud-client/face_detection/faces_test.py +++ b/vision/cloud-client/face_detection/faces_test.py @@ -13,9 +13,8 @@ import os -from PIL import Image - from faces import main +from PIL import Image def test_main(resource, tmpdir): From 005b710f3fb506c28ef5cb9beb260602b3b35cf1 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Tue, 28 Mar 2017 12:46:51 -0700 Subject: [PATCH 3/4] Nudge for travis --- vision/cloud-client/face_detection/faces.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vision/cloud-client/face_detection/faces.py b/vision/cloud-client/face_detection/faces.py index e1821cfcecb7..6baef63c76b1 100755 --- a/vision/cloud-client/face_detection/faces.py +++ b/vision/cloud-client/face_detection/faces.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Draws squares around faces in the given image.""" +"""Draws squares around detected faces in the given image.""" import argparse From cf182f31ca95d484a6697f8d11b4f704dd536057 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Tue, 28 Mar 2017 14:59:12 -0700 Subject: [PATCH 4/4] flake8 hates my face --- vision/cloud-client/face_detection/faces_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vision/cloud-client/face_detection/faces_test.py b/vision/cloud-client/face_detection/faces_test.py index 43a0075856d9..9611c82ed499 100644 --- a/vision/cloud-client/face_detection/faces_test.py +++ b/vision/cloud-client/face_detection/faces_test.py @@ -13,9 +13,10 @@ import os -from faces import main from PIL import Image +from faces import main + def test_main(resource, tmpdir): out_file = os.path.join(tmpdir.dirname, 'face-output.jpg')